Skip to content

Commit

Permalink
fix: compare relative paths when excluding, fixes kreuzwerker#280
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelmeulemans committed Jul 7, 2022
1 parent 224c3d1 commit 5dd98ae
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
5 changes: 3 additions & 2 deletions internal/provider/resource_docker_registry_image_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
43 changes: 22 additions & 21 deletions internal/provider/resource_docker_registry_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()

_, err = f.WriteString("modify-me")
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 write to file: %w", err)
panic("failed to create test file")
}
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
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
empty_to_ignore
to_be_ignored
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FROM scratch
COPY empty /empty
COPY * /
Empty file.
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 5dd98ae

Please sign in to comment.