2021-03-18 03:30:54 -04:00
|
|
|
package provider
|
2018-05-16 12:00:04 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-12-02 06:06:39 -05:00
|
|
|
"context"
|
2018-05-16 12:00:04 -04:00
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-05-31 03:11:49 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2021-03-18 03:30:54 -04:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
|
2018-05-16 12:00:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAccDockerSecret_basic(t *testing.T) {
|
2021-03-18 03:30:54 -04:00
|
|
|
ctx := context.Background()
|
2021-05-31 03:11:49 -04:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 12:00:04 -04:00
|
|
|
resource.Test(t, resource.TestCase{
|
2021-03-18 03:30:54 -04:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
|
ProviderFactories: providerFactories,
|
|
|
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
|
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
|
|
|
},
|
2018-05-16 12:00:04 -04:00
|
|
|
Steps: []resource.TestStep{
|
2019-03-01 16:02:17 -05:00
|
|
|
{
|
2018-05-16 12:00:04 -04:00
|
|
|
Config: `
|
|
|
|
|
resource "docker_secret" "foo" {
|
|
|
|
|
name = "foo-secret"
|
|
|
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
2021-05-31 03:11:49 -04:00
|
|
|
|
|
|
|
|
labels {
|
|
|
|
|
label = "foo"
|
|
|
|
|
value = "bar"
|
|
|
|
|
}
|
2018-05-16 12:00:04 -04:00
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "name", "foo-secret"),
|
|
|
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
|
2021-05-31 03:11:49 -04:00
|
|
|
testAccServiceSecretCreated("docker_secret.foo", &s),
|
|
|
|
|
testCheckSecretInspect,
|
2018-05-16 12:00:04 -04:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-10-18 06:39:58 -04:00
|
|
|
|
2018-07-03 11:30:53 -04:00
|
|
|
func TestAccDockerSecret_basicUpdatable(t *testing.T) {
|
2021-03-18 03:30:54 -04:00
|
|
|
ctx := context.Background()
|
2018-05-16 12:00:04 -04:00
|
|
|
resource.Test(t, resource.TestCase{
|
2021-03-18 03:30:54 -04:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
|
ProviderFactories: providerFactories,
|
|
|
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
|
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
|
|
|
},
|
2018-05-16 12:00:04 -04:00
|
|
|
Steps: []resource.TestStep{
|
2019-03-01 16:02:17 -05:00
|
|
|
{
|
2018-05-16 12:00:04 -04:00
|
|
|
Config: `
|
|
|
|
|
resource "docker_secret" "foo" {
|
|
|
|
|
name = "tftest-mysecret-${replace(timestamp(),":", ".")}"
|
|
|
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
|
|
|
|
|
|
|
|
|
lifecycle {
|
2021-05-31 03:11:49 -04:00
|
|
|
ignore_changes = ["name"]
|
2018-05-16 12:00:04 -04:00
|
|
|
create_before_destroy = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
|
|
|
|
|
),
|
|
|
|
|
},
|
2019-03-01 16:02:17 -05:00
|
|
|
{
|
2018-05-16 12:00:04 -04:00
|
|
|
Config: `
|
|
|
|
|
resource "docker_secret" "foo" {
|
|
|
|
|
name = "tftest-mysecret2-${replace(timestamp(),":", ".")}"
|
|
|
|
|
data = "U3VuIDI1IE1hciAyMDE4IDE0OjUzOjIxIENFU1QK"
|
|
|
|
|
|
|
|
|
|
lifecycle {
|
2021-05-31 03:11:49 -04:00
|
|
|
ignore_changes = ["name"]
|
2018-05-16 12:00:04 -04:00
|
|
|
create_before_destroy = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
|
resource.TestCheckResourceAttr("docker_secret.foo", "data", "U3VuIDI1IE1hciAyMDE4IDE0OjUzOjIxIENFU1QK"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 06:39:58 -04:00
|
|
|
func TestAccDockerSecret_labels(t *testing.T) {
|
2021-03-18 03:30:54 -04:00
|
|
|
ctx := context.Background()
|
2018-10-18 06:39:58 -04:00
|
|
|
resource.Test(t, resource.TestCase{
|
2021-03-18 03:30:54 -04:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
|
ProviderFactories: providerFactories,
|
|
|
|
|
CheckDestroy: func(state *terraform.State) error {
|
|
|
|
|
return testCheckDockerSecretDestroy(ctx, state)
|
|
|
|
|
},
|
2018-10-18 06:39:58 -04:00
|
|
|
Steps: []resource.TestStep{
|
2019-03-01 16:02:17 -05:00
|
|
|
{
|
2018-10-18 06:39:58 -04:00
|
|
|
Config: `
|
|
|
|
|
resource "docker_secret" "foo" {
|
|
|
|
|
name = "foo-secret"
|
|
|
|
|
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
|
2019-11-08 18:24:50 -05:00
|
|
|
labels {
|
|
|
|
|
label = "test1"
|
|
|
|
|
value = "foo"
|
|
|
|
|
}
|
|
|
|
|
labels {
|
|
|
|
|
label = "test2"
|
|
|
|
|
value = "bar"
|
2018-10-18 06:39:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`,
|
2019-11-12 17:41:59 -05:00
|
|
|
Check: testCheckLabelMap("docker_secret.foo", "labels",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"test1": "foo",
|
|
|
|
|
"test2": "bar",
|
2019-11-08 23:15:34 -05:00
|
|
|
},
|
2018-10-18 06:39:58 -04:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 12:00:04 -04:00
|
|
|
/////////////
|
|
|
|
|
// Helpers
|
|
|
|
|
/////////////
|
2021-03-18 03:30:54 -04:00
|
|
|
func testCheckDockerSecretDestroy(ctx context.Context, s *terraform.State) error {
|
2018-05-16 12:00:04 -04:00
|
|
|
client := testAccProvider.Meta().(*ProviderConfig).DockerClient
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
|
if rs.Type != "secrets" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := rs.Primary.Attributes["id"]
|
2021-03-18 03:30:54 -04:00
|
|
|
_, _, err := client.SecretInspectWithRaw(ctx, id)
|
2018-05-16 12:00:04 -04:00
|
|
|
|
2018-07-03 11:30:53 -04:00
|
|
|
if err == nil {
|
2018-05-16 12:00:04 -04:00
|
|
|
return fmt.Errorf("Secret with id '%s' still exists", id)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-05-31 03:11:49 -04:00
|
|
|
|
|
|
|
|
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 := testAccProvider.Meta().(*ProviderConfig).DockerClient
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|