terraform-provider-docker/internal/provider/resource_docker_config_test.go
Manuel Vogel 0588c2071b
chore/refactor tests (#201)
* chore: format test configs for datasources

* chore: outlines load test config helper and structure

* docs(contributing): add command for resouce tests

to have an example of the regex

* refactor: move container test configs into separate files

* fix: add insecure_skip_verify for image pulls

to fix the local test setup with invalid certs

* chore(ci): remove insecure registry adaption

* chore: regenerate website

* chore: update gitignore for scipts/testing dir

* fix: replace nodejs services with go versions

* fix: move testing program versions in separate files

* test: reactivate flaky test from travis

* chore: fix linter on all go files

* fix(linter): testing go servers

* chore(ci): add env for go version

* chore(ci): name workflow steps

also moves description of available docker versions in to acc dockerfile

* Revert "test: reactivate flaky test from travis"

This reverts commit b02654acc4d6b7d02c8f3ba090e6a3f248741b10.

* docs: fix provider-ssh example

* chore: use alpine als final image for tests

* refactor: move test configs from folder into testname.tf files

* refactor: image delete log is now debug and indented

* refactor: image test config into seprate files

* refactor: move network test config into seperate files

* refactor: move plugin test config into seperate files

* chore: rename registry image test file

* refactor: move registry_image test config into seperate files

* chore: format secret test configs

* refactor: inline volume test configs

* fix: remove unused volume label test function

* refactor: move service test configs into seperate files

* test: reactivate and fix service test

* chore: simplify insecure skip verify add to http client

* chore(ci): debug into service test

* chore(ci): add testacc setup

* chore: format tf config for provider test

* chore(ci): add debug output for config.json

* fix: check service auth for emptyness

* fix: remove re-read of provider auth config

because the bug occured only in CI as the meta object might be GCd

* test: pass auth to service instead of provider

* chore: reactivate all acc tests

* test: outlines service inspect json check for full spec

* test: add service inspect json checks

* test: finish service inspect json checks

* chore(service): move test helper to end to of the file

* chore: move mapEquals to test helpers

* test: add json inspect for config

* chore: add debug inspect log for plugin, secret and volume

* test: add json inspect for secret

* test: add json inspect for image

* test: add json inspect for network

* test: add json inspect for plugin

* test: add json inspect for volume

* test: inline ds plugin test configs

* test: inline network configs

* test: move ds reg image configs into separate files

* test: reactivates container upload checks

* chore: adapt issues ref from old to new xw repo

* fix: reactivate network ingress test

and provide helpers for removing the default ingress network and leaving the swamr

* docs: rerun website gen

* test: fix reg image build and keep test

* chore: add name to todo

* chore: move ds network and plugin specs to file

* chore: format provider test spec

* chore: use simpler error message for empty strings
2021-05-31 16:11:49 +09:00

159 lines
4.1 KiB
Go

package provider
import (
"context"
"errors"
"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 TestAccDockerConfig_basic(t *testing.T) {
ctx := context.Background()
var c swarm.Config
testCheckConfigInspect := func(*terraform.State) error {
if c.Spec.Name == "" {
return errors.New("Config Spec.Name is empty")
}
if len(c.Spec.Data) == 0 {
return errors.New("Config Spec.Data is empty")
}
if len(c.Spec.Labels) != 0 {
return fmt.Errorf("Config Spec.Labels is wrong: %v", c.Spec.Labels)
}
return nil
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: func(state *terraform.State) error {
return testCheckDockerConfigDestroy(ctx, state)
},
Steps: []resource.TestStep{
{
Config: `
resource "docker_config" "foo" {
name = "foo-config"
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("docker_config.foo", "name", "foo-config"),
resource.TestCheckResourceAttr("docker_config.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
testAccServiceConfigCreated("docker_config.foo", &c),
testCheckConfigInspect,
),
},
{
ResourceName: "docker_config.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccDockerConfig_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 testCheckDockerConfigDestroy(ctx, state)
},
Steps: []resource.TestStep{
{
Config: `
resource "docker_config" "foo" {
name = "tftest-myconfig-${replace(timestamp(),":", ".")}"
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
lifecycle {
ignore_changes = ["name"]
create_before_destroy = true
}
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("docker_config.foo", "data", "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="),
),
},
{
Config: `
resource "docker_config" "foo" {
name = "tftest-myconfig2-${replace(timestamp(),":", ".")}"
data = "U3VuIDI1IE1hciAyMDE4IDE0OjQ2OjE5IENFU1QK"
lifecycle {
ignore_changes = ["name"]
create_before_destroy = true
}
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("docker_config.foo", "data", "U3VuIDI1IE1hciAyMDE4IDE0OjQ2OjE5IENFU1QK"),
),
},
{
ResourceName: "docker_config.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
/////////////
// Helpers
/////////////
func testCheckDockerConfigDestroy(ctx context.Context, s *terraform.State) error {
client := testAccProvider.Meta().(*ProviderConfig).DockerClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "configs" {
continue
}
id := rs.Primary.Attributes["id"]
_, _, err := client.ConfigInspectWithRaw(ctx, id)
if err == nil {
return fmt.Errorf("Config with id '%s' still exists", id)
}
return nil
}
return nil
}
func testAccServiceConfigCreated(resourceName string, config *swarm.Config) 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
inspectedConfig, _, err := client.ConfigInspectWithRaw(ctx, rs.Primary.ID)
if err != nil {
return fmt.Errorf("Config 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
*config = inspectedConfig
return nil
}
}