Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve import tests #53

Merged
merged 4 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/provider/resource_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ func (r *initResource) Read(ctx context.Context, req resource.ReadRequest, resp

directory := state.Directory.Value

var newState initResourceModel
newState.Directory = state.Directory
newState.Id = state.Id

repository := openRepository(ctx, directory, &resp.Diagnostics)
if repository == nil {
resp.State.RemoveResource(ctx)
return
}

var newState initResourceModel
newState.Directory = state.Directory

worktree, err := getWorktree(repository, &resp.Diagnostics)
if err != nil {
return
Expand Down Expand Up @@ -209,14 +209,14 @@ func (r *initResource) ImportState(ctx context.Context, req resource.ImportState
return
}

var state initResourceModel
state.Directory = types.String{Value: req.ID}
state.Id = types.String{Value: req.ID}

repository := openRepository(ctx, req.ID, &resp.Diagnostics)
if repository == nil {
return
}

var state initResourceModel
state.Directory = types.String{Value: req.ID}
state.Id = types.String{Value: req.ID}
worktree, err := getWorktree(repository, &resp.Diagnostics)
if err != nil {
return
Expand Down
49 changes: 42 additions & 7 deletions internal/provider/resource_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/metio/terraform-provider-git/internal/testutils"
"os"
"regexp"
"testing"
)

Expand Down Expand Up @@ -87,14 +88,54 @@ func TestResourceGitInit_NonBare(t *testing.T) {
})
}

func TestResourceGitInit_Import(t *testing.T) {
func TestResourceGitInit_Import_NonExistingRepo(t *testing.T) {
t.Parallel()
directory := testutils.TemporaryDirectory(t)
defer os.RemoveAll(directory)

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_init" "test" {
directory = "%s"
}
`, directory),
ResourceName: "git_init.test",
ImportState: true,
ImportStateId: directory,
ImportStatePersist: false,
ExpectError: regexp.MustCompile(`Cannot open repository`),
},
},
})
}

func TestResourceGitInit_Import_ExistingRepo(t *testing.T) {
t.Parallel()
directory, _ := testutils.CreateRepository(t)
defer os.RemoveAll(directory)

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_init" "test" {
directory = "%s"
}
`, directory),
ResourceName: "git_init.test",
ImportState: true,
ImportStateId: directory,
ImportStatePersist: true,
ImportStateCheck: testutils.ComposeImportStateCheck(
testutils.CheckResourceAttrInstanceState("directory", directory),
testutils.CheckResourceAttrInstanceState("id", directory),
testutils.CheckResourceAttrInstanceState("bare", "false"),
),
},
{
Config: fmt.Sprintf(`
resource "git_init" "test" {
Expand All @@ -107,12 +148,6 @@ func TestResourceGitInit_Import(t *testing.T) {
resource.TestCheckResourceAttr("git_init.test", "bare", "false"),
),
},
{
ResourceName: "git_init.test",
ImportState: true,
ImportStateId: directory,
ImportStateVerify: true,
},
},
})
}
Expand Down
163 changes: 149 additions & 14 deletions internal/provider/resource_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,92 @@ func TestResourceGitRemote_MissingUrls(t *testing.T) {
})
}

func TestResourceGitRemote_Import(t *testing.T) {
func TestResourceGitRemote_Import_NonExistingRepo(t *testing.T) {
t.Parallel()
directory, _ := testutils.CreateRepository(t)
directory := testutils.TemporaryDirectory(t)
defer os.RemoveAll(directory)
name := "some-name"
url := "https://github.com/some-org/some-repo.git"

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s"]
}
`, directory, name, url),
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStatePersist: false,
ExpectError: regexp.MustCompile(`Cannot open repository`),
},
},
})
}

func TestResourceGitRemote_Import_NonExistingRemote(t *testing.T) {
t.Parallel()
directory, _ := testutils.CreateRepository(t)
defer os.RemoveAll(directory)
name := "some-name"
url := "https://example.com/metio/terraform-provider-git.git"

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s"]
}
`, directory, name, url),
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStatePersist: false,
ExpectError: regexp.MustCompile(`Cannot read remote`),
},
},
})
}

func TestResourceGitRemote_Import_SingleUrl(t *testing.T) {
t.Parallel()
directory, repository := testutils.CreateRepository(t)
defer os.RemoveAll(directory)
name := "some-name"
url := "https://example.com/metio/terraform-provider-git.git"
testutils.CreateRemoteWithUrls(t, repository, name, []string{url})

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s"]
}
`, directory, name, url),
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStatePersist: true,
ImportStateCheck: testutils.ComposeImportStateCheck(
testutils.CheckResourceAttrInstanceState("directory", directory),
testutils.CheckResourceAttrInstanceState("id", fmt.Sprintf("%s|%s", directory, name)),
testutils.CheckResourceAttrInstanceState("name", name),
testutils.CheckResourceAttrInstanceState("urls.0", url),
),
},
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
Expand All @@ -186,27 +262,92 @@ func TestResourceGitRemote_Import(t *testing.T) {
resource.TestCheckResourceAttr("git_remote.test", "urls.0", url),
),
},
},
})
}

func TestResourceGitRemote_Import_SingleUrlWithDrift(t *testing.T) {
t.Parallel()
directory, repository := testutils.CreateRepository(t)
defer os.RemoveAll(directory)
name := "some-name"
url := "https://example.com/metio/terraform-provider-git.git"
testutils.CreateRemoteWithUrls(t, repository, name, []string{"https://example.com/another/random.git"})

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStateVerify: true,
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s"]
}
`, directory, name, url),
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStatePersist: true,
ImportStateCheck: testutils.ComposeImportStateCheck(
testutils.CheckResourceAttrInstanceState("directory", directory),
testutils.CheckResourceAttrInstanceState("id", fmt.Sprintf("%s|%s", directory, name)),
testutils.CheckResourceAttrInstanceState("name", name),
testutils.CheckResourceAttrInstanceState("urls.0", "https://example.com/another/random.git"),
),
},
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s"]
}
`, directory, name, url),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("git_remote.test", "directory", directory),
resource.TestCheckResourceAttr("git_remote.test", "id", fmt.Sprintf("%s|%s", directory, name)),
resource.TestCheckResourceAttr("git_remote.test", "name", name),
resource.TestCheckResourceAttr("git_remote.test", "urls.#", "1"),
resource.TestCheckResourceAttr("git_remote.test", "urls.0", url),
),
},
},
})
}

func TestResourceGitRemote_ImportMultipleUrls(t *testing.T) {
func TestResourceGitRemote_Import_MultipleUrls(t *testing.T) {
t.Parallel()
directory, _ := testutils.CreateRepository(t)
directory, repository := testutils.CreateRepository(t)
defer os.RemoveAll(directory)
name := "some-name"
url1 := "https://github.com/some-org/some-repo.git"
url2 := "https://codeberg.org/some-org/some-repo.git"
testutils.CreateRemoteWithUrls(t, repository, name, []string{url1, url2})

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
directory = "%s"
name = "%s"
urls = ["%s", "%s"]
}
`, directory, name, url1, url2),
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStatePersist: true,
ImportStateCheck: testutils.ComposeImportStateCheck(
testutils.CheckResourceAttrInstanceState("directory", directory),
testutils.CheckResourceAttrInstanceState("id", fmt.Sprintf("%s|%s", directory, name)),
testutils.CheckResourceAttrInstanceState("name", name),
testutils.CheckResourceAttrInstanceState("urls.0", url1),
testutils.CheckResourceAttrInstanceState("urls.1", url2),
),
},
{
Config: fmt.Sprintf(`
resource "git_remote" "test" {
Expand All @@ -224,12 +365,6 @@ func TestResourceGitRemote_ImportMultipleUrls(t *testing.T) {
resource.TestCheckResourceAttr("git_remote.test", "urls.1", url2),
),
},
{
ResourceName: "git_remote.test",
ImportState: true,
ImportStateId: fmt.Sprintf("%s|%s", directory, name),
ImportStateVerify: true,
},
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (r *tagResource) ImportState(ctx context.Context, req resource.ImportStateR

var state tagResourceModel
state.Directory = types.String{Value: directory}
state.Id = types.String{Value: id}
state.Id = types.String{Value: fmt.Sprintf("%s|%s", directory, tagName)}
state.Name = types.String{Value: tagName}
state.Revision = types.String{Value: revision}
state.SHA1 = types.String{Value: tagReference.Hash().String()}
Expand Down
Loading