github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_registry_credential_test.go (about)

     1  package rancher
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	rancherClient "github.com/rancher/go-rancher/client"
    10  )
    11  
    12  func TestAccRancherRegistryCredential(t *testing.T) {
    13  	var registry rancherClient.RegistryCredential
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckRancherRegistryCredentialDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRancherRegistryCredentialConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", &registry),
    24  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo"),
    25  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test"),
    26  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccRancherRegistryCredentialUpdateConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", &registry),
    33  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo2"),
    34  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test - updated"),
    35  					resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user2"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.RegistryCredential) resource.TestCheckFunc {
    43  	return func(s *terraform.State) error {
    44  		rs, ok := s.RootModule().Resources[n]
    45  
    46  		if !ok {
    47  			return fmt.Errorf("Not found: %s", n)
    48  		}
    49  
    50  		if rs.Primary.ID == "" {
    51  			return fmt.Errorf("No App Name is set")
    52  		}
    53  
    54  		client := testAccProvider.Meta().(*Config)
    55  
    56  		foundReg, err := client.RegistryCredential.ById(rs.Primary.ID)
    57  		if err != nil {
    58  			return err
    59  		}
    60  
    61  		if foundReg.Resource.Id != rs.Primary.ID {
    62  			return fmt.Errorf("Environment not found")
    63  		}
    64  
    65  		*reg = *foundReg
    66  
    67  		return nil
    68  	}
    69  }
    70  
    71  func testAccCheckRancherRegistryCredentialDestroy(s *terraform.State) error {
    72  	client := testAccProvider.Meta().(*Config)
    73  
    74  	for _, rs := range s.RootModule().Resources {
    75  		if rs.Type != "rancher_registry_credential" {
    76  			continue
    77  		}
    78  		reg, err := client.RegistryCredential.ById(rs.Primary.ID)
    79  
    80  		if err == nil {
    81  			if reg != nil &&
    82  				reg.Resource.Id == rs.Primary.ID &&
    83  				reg.State != "removed" {
    84  				return fmt.Errorf("RegistryCredential still exists")
    85  			}
    86  		}
    87  
    88  		return nil
    89  	}
    90  	return nil
    91  }
    92  
    93  const testAccRancherRegistryCredentialConfig = `
    94  resource "rancher_environment" "foo" {
    95  	name = "foo"
    96  }
    97  
    98  resource "rancher_registry" "foo" {
    99    name = "foo"
   100    description = "registry test"
   101    server_address = "http://bar.com:8080"
   102    environment_id = "${rancher_environment.foo.id}"
   103  }
   104  
   105  resource "rancher_registry_credential" "foo" {
   106  	name = "foo"
   107  	description = "registry credential test"
   108  	registry_id = "${rancher_registry.foo.id}"
   109  	email = "registry@credential.com"
   110  	public_value = "user"
   111  	secret_value = "pass"
   112  }
   113  `
   114  
   115  const testAccRancherRegistryCredentialUpdateConfig = `
   116  resource "rancher_environment" "foo" {
   117  	name = "foo"
   118  }
   119  
   120  resource "rancher_registry" "foo" {
   121    name = "foo"
   122    description = "registry test"
   123    server_address = "http://bar.com:8080"
   124    environment_id = "${rancher_environment.foo.id}"
   125  }
   126  
   127  resource "rancher_registry_credential" "foo" {
   128  	name = "foo2"
   129  	description = "registry credential test - updated"
   130  	registry_id = "${rancher_registry.foo.id}"
   131  	email = "registry@credential.com"
   132  	public_value = "user2"
   133  	secret_value = "pass"
   134  }
   135   `