Skip to content

Commit

Permalink
Use built-in test funcs where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Oct 11, 2016
1 parent 4892d60 commit 120602a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 89 deletions.
27 changes: 6 additions & 21 deletions builtin/providers/rancher/resource_rancher_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ func TestAccRancherEnvironment(t *testing.T) {
Config: testAccRancherEnvironmentConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment),
testAccCheckRancherEnvironmentAttributes(&environment, "foo", "Terraform acc test group", "cattle"),
resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo"),
resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group"),
resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "cattle"),
),
},
resource.TestStep{
Config: testAccRancherEnvironmentUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment),
testAccCheckRancherEnvironmentAttributes(&environment, "foo2", "Terraform acc test group - updated", "swarm"),
resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo2"),
resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group - updated"),
resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "swarm"),
),
},
},
Expand Down Expand Up @@ -64,25 +68,6 @@ func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project)
}
}

func testAccCheckRancherEnvironmentAttributes(env *rancherClient.Project, envName string, envDesc string, envOrchestration string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if env.Name != envName {
return fmt.Errorf("Bad name: %s shoud be: %s", env.Name, envName)
}

if env.Description != envDesc {
return fmt.Errorf("Bad description: %s shoud be: %s", env.Description, envDesc)
}

orchestration := GetActiveOrchestration(env)
if orchestration != envOrchestration {
return fmt.Errorf("Bad orchestraion: %s shoud be: %s", orchestration, envOrchestration)
}

return nil
}
}

func testAccCheckRancherEnvironmentDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ func TestAccRancherRegistryCredential(t *testing.T) {
Config: testAccRancherRegistryCredentialConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", &registry),
testAccCheckRancherRegistryCredentialAttributes(&registry, "foo", "registry credential test", "user"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user"),
),
},
resource.TestStep{
Config: testAccRancherRegistryCredentialUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", &registry),
testAccCheckRancherRegistryCredentialAttributes(&registry, "foo2", "registry credential test - updated", "user2"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo2"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test - updated"),
resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user2"),
),
},
},
Expand Down Expand Up @@ -64,24 +68,6 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
}
}

func testAccCheckRancherRegistryCredentialAttributes(reg *rancherClient.RegistryCredential, regName string, regDesc string, regPublic string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if reg.Name != regName {
return fmt.Errorf("Bad name: %s shoud be: %s", reg.Name, regName)
}

if reg.Description != regDesc {
return fmt.Errorf("Bad description: %s shoud be: %s", reg.Description, regDesc)
}

if reg.PublicValue != regPublic {
return fmt.Errorf("Bad public_value: %s shoud be: %s", reg.PublicValue, regPublic)
}

return nil
}
}

func testAccCheckRancherRegistryCredentialDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

Expand Down
30 changes: 9 additions & 21 deletions builtin/providers/rancher/resource_rancher_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ func TestAccRancherRegistry(t *testing.T) {
Config: testAccRancherRegistryConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
testAccCheckRancherRegistryAttributes(&registry, "foo", "registry test", "http://foo.com:8080"),
resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
),
},
resource.TestStep{
Config: testAccRancherRegistryUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
testAccCheckRancherRegistryAttributes(&registry, "foo2", "registry test - updated", "http://foo.updated.com:8080"),
resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo2"),
resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test - updated"),
resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.updated.com:8080"),
),
},
resource.TestStep{
Config: testAccRancherRegistryRecreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
testAccCheckRancherRegistryAttributes(&registry, "foo", "registry test", "http://foo.com:8080"),
resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
),
},
},
Expand Down Expand Up @@ -71,24 +77,6 @@ func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) re
}
}

func testAccCheckRancherRegistryAttributes(reg *rancherClient.Registry, regName string, regDesc string, regServerAddress string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if reg.Name != regName {
return fmt.Errorf("Bad name: %s shoud be: %s", reg.Name, regName)
}

if reg.Description != regDesc {
return fmt.Errorf("Bad description: %s shoud be: %s", reg.Description, regDesc)
}

if reg.ServerAddress != regServerAddress {
return fmt.Errorf("Bad server_address: %s shoud be: %s", reg.ServerAddress, regServerAddress)
}

return nil
}
}

func testAccCheckRancherRegistryDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

Expand Down
42 changes: 19 additions & 23 deletions builtin/providers/rancher/resource_rancher_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,36 @@ func TestAccRancherStack(t *testing.T) {
Config: testAccRancherStackConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherStackExists("rancher_stack.foo", &stack),
testAccCheckRancherStackAttributes(&stack, "foo", "Terraform acc test group", "", "", "", emptyEnvironment, false),
resource.TestCheckResourceAttr("rancher_stack.foo", "name", "foo"),
resource.TestCheckResourceAttr("rancher_stack.foo", "description", "Terraform acc test group"),
resource.TestCheckResourceAttr("rancher_stack.foo", "catalog_id", ""),
resource.TestCheckResourceAttr("rancher_stack.foo", "docker_compose", ""),
resource.TestCheckResourceAttr("rancher_stack.foo", "rancher_compose", ""),
testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false),
),
},
resource.TestStep{
Config: testAccRancherStackUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherStackExists("rancher_stack.foo", &stack),
testAccCheckRancherStackAttributes(&stack, "foo2", "Terraform acc test group - updated", "", "", "", emptyEnvironment, false),
resource.TestCheckResourceAttr("rancher_stack.foo", "name", "foo2"),
resource.TestCheckResourceAttr("rancher_stack.foo", "description", "Terraform acc test group - updated"),
resource.TestCheckResourceAttr("rancher_stack.foo", "catalog_id", ""),
resource.TestCheckResourceAttr("rancher_stack.foo", "docker_compose", ""),
resource.TestCheckResourceAttr("rancher_stack.foo", "rancher_compose", ""),
testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false),
),
},
resource.TestStep{
Config: testAccRancherStackComposeConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherStackExists("rancher_stack.compose", &stack),
testAccCheckRancherStackAttributes(&stack, "compose", "Terraform acc test group - compose", "", "web: { image: nginx }", "web: { scale: 1 }", emptyEnvironment, false),
resource.TestCheckResourceAttr("rancher_stack.compose", "name", "compose"),
resource.TestCheckResourceAttr("rancher_stack.compose", "description", "Terraform acc test group - compose"),
resource.TestCheckResourceAttr("rancher_stack.compose", "catalog_id", ""),
resource.TestCheckResourceAttr("rancher_stack.compose", "docker_compose", "web: { image: nginx }"),
resource.TestCheckResourceAttr("rancher_stack.compose", "rancher_compose", "web: { scale: 1 }"),
testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false),
),
},
// resource.TestStep{
Expand Down Expand Up @@ -78,27 +93,8 @@ func testAccCheckRancherStackExists(n string, stack *rancherClient.Environment)
}
}

func testAccCheckRancherStackAttributes(stack *rancherClient.Environment, stackName string, stackDesc string, externalID string, dockerCompose string, rancherCompose string, environment map[string]string, startOnCreate bool) resource.TestCheckFunc {
func testAccCheckRancherStackAttributes(stack *rancherClient.Environment, environment map[string]string, startOnCreate bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if stack.Name != stackName {
return fmt.Errorf("Bad name: %s should be: %s", stack.Name, stackName)
}

if stack.Description != stackDesc {
return fmt.Errorf("Bad description: %s should be: %s", stack.Description, stackDesc)
}

if stack.ExternalId != externalID {
return fmt.Errorf("Bad externalID: %s should be: %s", stack.ExternalId, externalID)
}

if stack.DockerCompose != dockerCompose {
return fmt.Errorf("Bad dockerCompose: %s should be: %s", stack.DockerCompose, dockerCompose)
}

if stack.RancherCompose != rancherCompose {
return fmt.Errorf("Bad rancherCompose: %s should be: %s", stack.RancherCompose, rancherCompose)
}

if len(stack.Environment) != len(environment) {
return fmt.Errorf("Bad environment size: %v should be: %v", len(stack.Environment), environment)
Expand Down
9 changes: 5 additions & 4 deletions website/source/docs/providers/rancher/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ description: |-

The Rancher provider is used to interact with the
resources supported by Rancher. The provider needs to be configured
with the proper credentials before it can be used.
with the URL of the Rancher server at minimum and API credentials if
access control is enabled on the server.

## Example Usage

```hcl
# Configure the Rancher provider
provider "rancher" {
api_url = "http://rancher.my-domain.com:port"
api_url = "http://rancher.my-domain.com:8080"
access_key = "${var.rancher_access_key}"
secret_key = "${var.rancher_secret_key}"
}
Expand All @@ -28,5 +29,5 @@ provider "rancher" {
The following arguments are supported:

* `api_url` - (Required) Rancher API url. It must be provided, but it can also be sourced from the `RANCHER_URL` environment variable.
* `access_key` - (Required) Rancher API access key. It must be provided, but it can also be sourced from the `RANCHER_ACCESS_KEY` environment variable.
* `secret_key` - (Required) Rancher API access key. It must be provided, but it can also be sourced from the `RANCHER_SECRET_KEY` environment variable.
* `access_key` - (Optional) Rancher API access key. It can also be sourced from the `RANCHER_ACCESS_KEY` environment variable.
* `secret_key` - (Optional) Rancher API access key. It can also be sourced from the `RANCHER_SECRET_KEY` environment variable.

0 comments on commit 120602a

Please sign in to comment.