diff --git a/internal/provider/resource_docker_registry_image_funcs.go b/internal/provider/resource_docker_registry_image_funcs.go index 6378de38..7f4d0047 100644 --- a/internal/provider/resource_docker_registry_image_funcs.go +++ b/internal/provider/resource_docker_registry_image_funcs.go @@ -292,7 +292,7 @@ func buildDockerImageContextTar(buildContext string) (string, error) { pm, err := fileutils.NewPatternMatcher(excludes) if err != nil { - return "", fmt.Errorf("unable to create pattern matcher from .dockerignore exlcudes - %v", err.Error()) + return "", fmt.Errorf("unable to create pattern matcher from .dockerignore excludes - %v", err.Error()) } tw := tar.NewWriter(tmpFile) @@ -305,7 +305,8 @@ func buildDockerImageContextTar(buildContext string) (string, error) { } // if .dockerignore is present, ignore files from there - skip, err := pm.Matches(file) + rel, _ := filepath.Rel(buildContext, file) + skip, err := pm.Matches(rel) if err != nil { return err } diff --git a/internal/provider/resource_docker_registry_image_test.go b/internal/provider/resource_docker_registry_image_test.go index b1b6a263..1e79b133 100644 --- a/internal/provider/resource_docker_registry_image_test.go +++ b/internal/provider/resource_docker_registry_image_test.go @@ -158,48 +158,49 @@ func TestAccDockerRegistryImageResource_buildWithDockerignore(t *testing.T) { pushOptions := createPushImageOptions("127.0.0.1:15000/tftest-dockerregistryimage-ignore:1.0") wd, _ := os.Getwd() context := strings.ReplaceAll((filepath.Join(wd, "..", "..", "scripts", "testing", "docker_registry_image_context_dockerignore")), "\\", "\\\\") + ignoredFile := context + "/to_be_ignored" + expectedSha := "" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_registry_image", "testBuildDockerRegistryImageNoKeepConfig"), pushOptions.Registry, pushOptions.Name, context), + Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_registry_image", "testBuildDockerRegistryImageNoKeepJustCache"), pushOptions.Registry, "one", pushOptions.Name, context), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("docker_registry_image.foo", "sha256_digest"), + resource.TestCheckResourceAttrSet("docker_registry_image.one", "sha256_digest"), + resource.TestCheckResourceAttrWith("docker_registry_image.one", "sha256_digest", func(value string) error { + expectedSha = value + return nil + }), ), }, { - Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_registry_image", "testBuildDockerRegistryImageNoKeepConfig"), pushOptions.Registry, pushOptions.Name, context), - Check: func(*terraform.State) error { - // we will modify the ignored file - f, err := os.OpenFile(context+"/empty_to_ignore", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + PreConfig: func() { + // create a file that should be ignored + f, err := os.OpenFile(ignoredFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { - return fmt.Errorf("failed to open file: %w", err) + panic("failed to create test file") } - defer f.Close() - - _, err = f.WriteString("modify-me") - if err != nil { - return fmt.Errorf("failed to write to file: %w", err) - } - return nil + f.Close() }, - }, - { - Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_registry_image", "testBuildDockerRegistryImageNoKeepConfig"), pushOptions.Registry, pushOptions.Name, context), + Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_registry_image", "testBuildDockerRegistryImageNoKeepJustCache"), pushOptions.Registry, "two", pushOptions.Name, context), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("docker_registry_image.foo", "sha256_digest"), + resource.TestCheckResourceAttrWith("docker_registry_image.two", "sha256_digest", func(value string) error { + if value != expectedSha { + return fmt.Errorf("Image sha256_digest changed, expected %#v, got %#v", expectedSha, value) + } + return nil + }), ), }, }, CheckDestroy: resource.ComposeTestCheckFunc( testDockerRegistryImageNotInRegistry(pushOptions), func(*terraform.State) error { - // the 0 specifies the file will be empty afterwards - err := os.Truncate(context+"/empty_to_ignore", 0) + err := os.Remove(ignoredFile) if err != nil { - return fmt.Errorf("failed to truncate the ignored file: %w", err) + return fmt.Errorf("failed to remove ignored file: %w", err) } return nil }, diff --git a/scripts/testing/docker_registry_image_context_dockerignore/.dockerignore b/scripts/testing/docker_registry_image_context_dockerignore/.dockerignore index 90fd1815..c7715a3a 100644 --- a/scripts/testing/docker_registry_image_context_dockerignore/.dockerignore +++ b/scripts/testing/docker_registry_image_context_dockerignore/.dockerignore @@ -1 +1 @@ -empty_to_ignore \ No newline at end of file +to_be_ignored diff --git a/scripts/testing/docker_registry_image_context_dockerignore/Dockerfile b/scripts/testing/docker_registry_image_context_dockerignore/Dockerfile index ef5380d1..75c1dbc7 100644 --- a/scripts/testing/docker_registry_image_context_dockerignore/Dockerfile +++ b/scripts/testing/docker_registry_image_context_dockerignore/Dockerfile @@ -1,2 +1,2 @@ FROM scratch -COPY empty /empty \ No newline at end of file +COPY * / diff --git a/scripts/testing/docker_registry_image_context_dockerignore/empty_to_ignore b/scripts/testing/docker_registry_image_context_dockerignore/empty_to_ignore deleted file mode 100644 index e69de29b..00000000 diff --git a/testdata/resources/docker_registry_image/testBuildDockerRegistryImageNoKeepJustCache.tf b/testdata/resources/docker_registry_image/testBuildDockerRegistryImageNoKeepJustCache.tf new file mode 100644 index 00000000..4268b81e --- /dev/null +++ b/testdata/resources/docker_registry_image/testBuildDockerRegistryImageNoKeepJustCache.tf @@ -0,0 +1,18 @@ +provider "docker" { + alias = "private" + registry_auth { + address = "%s" + } +} +resource "docker_registry_image" "%s" { + provider = "docker.private" + name = "%s" + insecure_skip_verify = true + + build { + context = "%s" + remove = false + force_remove = false + no_cache = false + } +}