mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-20 22:59:42 -05:00
* chore: runs v2 upgrade cmd * chore: moves all files into the internal provider dir * feat: migrates main and provider * fix: migrates tests to provider factories * fix: replace import state passthrough ctx func * chore: bump tf-sdk to v2.4.4 * fix: acc test by adding stop grace period * fix: move to validate diag functions * test: switch from ctx TODO to Background * feat: add state upgrade for restart_policy and auth Co-authored-by: Shunsuke Suzuki <suzuki-shunsuke@users.noreply.github.com>
136 lines
3.3 KiB
Go
136 lines
3.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
|
|
)
|
|
|
|
func TestAccDockerSecret_basic(t *testing.T) {
|
|
ctx := context.Background()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
resource "docker_secret" "foo" {
|
|
name = "foo-secret"
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
|
}
|
|
`,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "name", "foo-secret"),
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDockerSecret_basicUpdatable(t *testing.T) {
|
|
ctx := context.Background()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
resource "docker_secret" "foo" {
|
|
name = "tftest-mysecret-${replace(timestamp(),":", ".")}"
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
|
|
|
lifecycle {
|
|
ignore_changes = ["name"]
|
|
create_before_destroy = true
|
|
}
|
|
}
|
|
`,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
|
|
),
|
|
},
|
|
{
|
|
Config: `
|
|
resource "docker_secret" "foo" {
|
|
name = "tftest-mysecret2-${replace(timestamp(),":", ".")}"
|
|
data = "U3VuIDI1IE1hciAyMDE4IDE0OjUzOjIxIENFU1QK"
|
|
|
|
lifecycle {
|
|
ignore_changes = ["name"]
|
|
create_before_destroy = true
|
|
}
|
|
}
|
|
`,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "U3VuIDI1IE1hciAyMDE4IDE0OjUzOjIxIENFU1QK"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDockerSecret_labels(t *testing.T) {
|
|
ctx := context.Background()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
resource "docker_secret" "foo" {
|
|
name = "foo-secret"
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
|
labels {
|
|
label = "test1"
|
|
value = "foo"
|
|
}
|
|
labels {
|
|
label = "test2"
|
|
value = "bar"
|
|
}
|
|
}
|
|
`,
|
|
Check: testCheckLabelMap("docker_secret.foo", "labels",
|
|
map[string]string{
|
|
"test1": "foo",
|
|
"test2": "bar",
|
|
},
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
/////////////
|
|
// Helpers
|
|
/////////////
|
|
func testCheckDockerSecretDestroy(ctx context.Context, s *terraform.State) error {
|
|
client := testAccProvider.Meta().(*ProviderConfig).DockerClient
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "secrets" {
|
|
continue
|
|
}
|
|
|
|
id := rs.Primary.Attributes["id"]
|
|
_, _, err := client.SecretInspectWithRaw(ctx, id)
|
|
|
|
if err == nil {
|
|
return fmt.Errorf("Secret with id '%s' still exists", id)
|
|
}
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|