github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/rancher/resource_rancher_registry_credential_test.go (about) 1 package rancher 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 rancherClient "github.com/rancher/go-rancher/client" 11 ) 12 13 func TestAccRancherRegistryCredential_basic(t *testing.T) { 14 var registry rancherClient.RegistryCredential 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckRancherRegistryCredentialDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccRancherRegistryCredentialConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", ®istry), 25 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo"), 26 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test"), 27 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user"), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccRancherRegistryCredentialUpdateConfig, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", ®istry), 34 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "name", "foo2"), 35 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "description", "registry credential test - updated"), 36 resource.TestCheckResourceAttr("rancher_registry_credential.foo", "public_value", "user2"), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccRancherRegistryCredential_disappears(t *testing.T) { 44 var registry rancherClient.RegistryCredential 45 46 resource.Test(t, resource.TestCase{ 47 PreCheck: func() { testAccPreCheck(t) }, 48 Providers: testAccProviders, 49 CheckDestroy: testAccCheckRancherRegistryCredentialDestroy, 50 Steps: []resource.TestStep{ 51 resource.TestStep{ 52 Config: testAccRancherRegistryCredentialConfig, 53 Check: resource.ComposeTestCheckFunc( 54 testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", ®istry), 55 testAccRancherRegistryCredentialDisappears(®istry), 56 ), 57 ExpectNonEmptyPlan: true, 58 }, 59 }, 60 }) 61 } 62 63 func testAccRancherRegistryCredentialDisappears(reg *rancherClient.RegistryCredential) resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 client, err := testAccProvider.Meta().(*Config).EnvironmentClient(reg.AccountId) 66 if err != nil { 67 return err 68 } 69 70 // Step 1: Deactivate 71 if _, e := client.RegistryCredential.ActionDeactivate(reg); e != nil { 72 return fmt.Errorf("Error deactivating RegistryCredential: %s", err) 73 } 74 75 stateConf := &resource.StateChangeConf{ 76 Pending: []string{"active", "inactive", "deactivating"}, 77 Target: []string{"inactive"}, 78 Refresh: RegistryCredentialStateRefreshFunc(client, reg.Id), 79 Timeout: 10 * time.Minute, 80 Delay: 1 * time.Second, 81 MinTimeout: 3 * time.Second, 82 } 83 84 _, waitErr := stateConf.WaitForState() 85 if waitErr != nil { 86 return fmt.Errorf( 87 "Error waiting for registry credential (%s) to be deactivated: %s", reg.Id, waitErr) 88 } 89 90 // Update resource to reflect its state 91 reg, err = client.RegistryCredential.ById(reg.Id) 92 if err != nil { 93 return fmt.Errorf("Failed to refresh state of deactivated registry credential (%s): %s", reg.Id, err) 94 } 95 96 // Step 2: Remove 97 if _, err := client.RegistryCredential.ActionRemove(reg); err != nil { 98 return fmt.Errorf("Error removing RegistryCredential: %s", err) 99 } 100 101 stateConf = &resource.StateChangeConf{ 102 Pending: []string{"inactive", "removed", "removing"}, 103 Target: []string{"removed"}, 104 Refresh: RegistryCredentialStateRefreshFunc(client, reg.Id), 105 Timeout: 10 * time.Minute, 106 Delay: 1 * time.Second, 107 MinTimeout: 3 * time.Second, 108 } 109 110 _, waitErr = stateConf.WaitForState() 111 if waitErr != nil { 112 return fmt.Errorf( 113 "Error waiting for registry (%s) to be removed: %s", reg.Id, waitErr) 114 } 115 116 return nil 117 } 118 } 119 120 func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.RegistryCredential) resource.TestCheckFunc { 121 return func(s *terraform.State) error { 122 rs, ok := s.RootModule().Resources[n] 123 124 if !ok { 125 return fmt.Errorf("Not found: %s", n) 126 } 127 128 if rs.Primary.ID == "" { 129 return fmt.Errorf("No App Name is set") 130 } 131 132 client, err := testAccProvider.Meta().(*Config).RegistryClient(rs.Primary.Attributes["registry_id"]) 133 if err != nil { 134 return err 135 } 136 137 foundReg, err := client.RegistryCredential.ById(rs.Primary.ID) 138 if err != nil { 139 return err 140 } 141 142 if foundReg.Resource.Id != rs.Primary.ID { 143 return fmt.Errorf("RegistryCredential not found") 144 } 145 146 *reg = *foundReg 147 148 return nil 149 } 150 } 151 152 func testAccCheckRancherRegistryCredentialDestroy(s *terraform.State) error { 153 for _, rs := range s.RootModule().Resources { 154 if rs.Type != "rancher_registry_credential" { 155 continue 156 } 157 client, err := testAccProvider.Meta().(*Config).GlobalClient() 158 if err != nil { 159 return err 160 } 161 162 reg, err := client.RegistryCredential.ById(rs.Primary.ID) 163 164 if err == nil { 165 if reg != nil && 166 reg.Resource.Id == rs.Primary.ID && 167 reg.State != "removed" { 168 return fmt.Errorf("RegistryCredential still exists") 169 } 170 } 171 172 return nil 173 } 174 return nil 175 } 176 177 const testAccRancherRegistryCredentialConfig = ` 178 resource "rancher_environment" "foo" { 179 name = "foo" 180 } 181 182 resource "rancher_registry" "foo" { 183 name = "foo" 184 description = "registry test" 185 server_address = "http://bar.com:8080" 186 environment_id = "${rancher_environment.foo.id}" 187 } 188 189 resource "rancher_registry_credential" "foo" { 190 name = "foo" 191 description = "registry credential test" 192 registry_id = "${rancher_registry.foo.id}" 193 email = "registry@credential.com" 194 public_value = "user" 195 secret_value = "pass" 196 } 197 ` 198 199 const testAccRancherRegistryCredentialUpdateConfig = ` 200 resource "rancher_environment" "foo" { 201 name = "foo" 202 } 203 204 resource "rancher_registry" "foo" { 205 name = "foo" 206 description = "registry test" 207 server_address = "http://bar.com:8080" 208 environment_id = "${rancher_environment.foo.id}" 209 } 210 211 resource "rancher_registry_credential" "foo" { 212 name = "foo2" 213 description = "registry credential test - updated" 214 registry_id = "${rancher_registry.foo.id}" 215 email = "registry@credential.com" 216 public_value = "user2" 217 secret_value = "pass" 218 } 219 `