mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-18 23:06:10 -05:00
Some checks failed
Acc Tests / acc-test (TestAccDockerConfig, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerConfig, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 1.8.x) (push) Has been cancelled
Compile Binaries / compile-fast (push) Has been cancelled
Compile Binaries / compile (push) Has been cancelled
golangci-lint / lint (push) Has been cancelled
Unit Tests / unit-test (push) Has been cancelled
Website Checks / markdown-link-check (push) Has been cancelled
Docs and Website Lint / website-generation (push) Has been cancelled
Docs and Website Lint / website-lint-spellcheck-tffmt (push) Has been cancelled
Docs and Website Lint / markdown-lint (push) Has been cancelled
190 lines
4.8 KiB
Go
190 lines
4.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"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()
|
|
var s swarm.Secret
|
|
|
|
testCheckSecretInspect := func(*terraform.State) error {
|
|
if s.Spec.Name == "" {
|
|
return fmt.Errorf("Secret Spec.Name is wrong: %v", s.Spec.Name)
|
|
}
|
|
|
|
if len(s.Spec.Labels) != 1 || !mapEquals("foo", "bar", s.Spec.Labels) {
|
|
return fmt.Errorf("Secret Spec.Labels is wrong: %v", s.Spec.Labels)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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 = "foo"
|
|
value = "bar"
|
|
}
|
|
}
|
|
`,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "name", "foo-secret"),
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
|
|
testAccServiceSecretCreated("docker_secret.foo", &s),
|
|
testCheckSecretInspect,
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
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, err := testAccProvider.Meta().(*ProviderConfig).MakeClient(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create Docker client: %w", err)
|
|
}
|
|
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
|
|
}
|
|
|
|
func testAccServiceSecretCreated(resourceName string, secret *swarm.Secret) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
ctx := context.Background()
|
|
rs, ok := s.RootModule().Resources[resourceName]
|
|
if !ok {
|
|
return fmt.Errorf("Resource with name '%s' not found in state", resourceName)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No ID is set")
|
|
}
|
|
|
|
client, err := testAccProvider.Meta().(*ProviderConfig).MakeClient(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create Docker client: %w", err)
|
|
}
|
|
inspectedSecret, _, err := client.SecretInspectWithRaw(ctx, rs.Primary.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("Secret with ID '%s': %w", rs.Primary.ID, err)
|
|
}
|
|
|
|
// we set the value to the pointer to be able to use the value
|
|
// outside of the function
|
|
*secret = inspectedSecret
|
|
return nil
|
|
|
|
}
|
|
}
|