mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-27 01:59:36 -05:00
fix: compare relative paths when excluding, fixes kreuzwerker#280 (#397)
This commit is contained in:
parent
224c3d170f
commit
af072b22aa
6 changed files with 45 additions and 25 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
empty_to_ignore
|
||||
to_be_ignored
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
FROM scratch
|
||||
COPY empty /empty
|
||||
COPY * /
|
||||
|
|
|
|||
18
testdata/resources/docker_registry_image/testBuildDockerRegistryImageNoKeepJustCache.tf
vendored
Normal file
18
testdata/resources/docker_registry_image/testBuildDockerRegistryImageNoKeepJustCache.tf
vendored
Normal file
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue