terraform-provider-docker/internal/provider/provider_test.go
Manuel Vogel ad3e56da2b
feat: migrate to terraform-sdk v2 (#102)
* 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>
2021-03-18 08:30:54 +01:00

93 lines
2.4 KiB
Go

package provider
import (
"context"
"os/exec"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
var (
// testAccProvider is the "main" provider instance
//
// This Provider can be used in testing code for API calls without requiring
// the use of saving and referencing specific ProviderFactories instances.
//
// testAccPreCheck(t) must be called before using this provider instance.
testAccProvider *schema.Provider
// providerFactories are used to instantiate a provider during acceptance testing.
// The factory function will be invoked for every Terraform CLI command executed
// to create a provider server to which the CLI can reattach.
providerFactories map[string]func() (*schema.Provider, error)
)
func init() {
testAccProvider = New("dev")()
providerFactories = map[string]func() (*schema.Provider, error){
"docker": func() (*schema.Provider, error) {
return New("dev")(), nil
},
}
}
func TestProvider_impl(t *testing.T) {
var _ *schema.Provider = New("dev")()
}
func TestProvider(t *testing.T) {
if err := New("dev")().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestAccDockerProvider_WithIncompleteRegistryAuth(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDockerProviderWithIncompleteAuthConfig,
ExpectError: regexp.MustCompile(`401 Unauthorized`),
},
},
})
}
func testAccPreCheck(t *testing.T) {
cmd := exec.Command("docker", "version")
if err := cmd.Run(); err != nil {
t.Fatalf("Docker must be available: %s", err)
}
cmd = exec.Command("docker", "node", "ls")
if err := cmd.Run(); err != nil {
cmd = exec.Command("docker", "swarm", "init")
if err := cmd.Run(); err != nil {
t.Fatalf("Docker swarm could not be initialized: %s", err)
}
}
err := testAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
if err != nil {
t.Fatal(err)
}
}
const testAccDockerProviderWithIncompleteAuthConfig = `
provider "docker" {
alias = "private"
registry_auth {
address = ""
username = ""
password = ""
}
}
data "docker_registry_image" "foobar" {
provider = "docker.private"
name = "localhost:15000/helloworld:1.0"
}
`