github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rancher/resource_rancher_registration_token_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 TestAccRancherRegistrationToken_basic(t *testing.T) { 14 var registrationToken rancherClient.RegistrationToken 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckRancherRegistrationTokenDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccRancherRegistrationTokenConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", ®istrationToken), 25 resource.TestCheckResourceAttr( 26 "rancher_registration_token.foo", "name", "foo"), 27 resource.TestCheckResourceAttr( 28 "rancher_registration_token.foo", "description", "Terraform acc test group"), 29 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "command"), 30 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "registration_url"), 31 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "token"), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccRancherRegistrationTokenUpdateConfig, 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", ®istrationToken), 38 resource.TestCheckResourceAttr( 39 "rancher_registration_token.foo", "name", "foo-u"), 40 resource.TestCheckResourceAttr( 41 "rancher_registration_token.foo", "description", "Terraform acc test group-u"), 42 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "command"), 43 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "registration_url"), 44 resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "token"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func TestAccRancherRegistrationToken_disappears(t *testing.T) { 52 var registrationToken rancherClient.RegistrationToken 53 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 CheckDestroy: testAccCheckRancherRegistrationTokenDestroy, 58 Steps: []resource.TestStep{ 59 resource.TestStep{ 60 Config: testAccRancherRegistrationTokenConfig, 61 Check: resource.ComposeTestCheckFunc( 62 testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", ®istrationToken), 63 testAccRancherRegistrationTokenDisappears(®istrationToken), 64 ), 65 ExpectNonEmptyPlan: true, 66 }, 67 }, 68 }) 69 } 70 71 func testAccRancherRegistrationTokenDisappears(token *rancherClient.RegistrationToken) resource.TestCheckFunc { 72 return func(s *terraform.State) error { 73 client, err := testAccProvider.Meta().(*Config).EnvironmentClient(token.AccountId) 74 if err != nil { 75 return err 76 } 77 78 if _, e := client.RegistrationToken.ActionDeactivate(token); e != nil { 79 return fmt.Errorf("Error deactivating RegistrationToken: %s", err) 80 } 81 82 stateConf := &resource.StateChangeConf{ 83 Pending: []string{"active", "inactive", "deactivating"}, 84 Target: []string{"inactive"}, 85 Refresh: RegistrationTokenStateRefreshFunc(client, token.Id), 86 Timeout: 10 * time.Minute, 87 Delay: 1 * time.Second, 88 MinTimeout: 3 * time.Second, 89 } 90 91 _, waitErr := stateConf.WaitForState() 92 if waitErr != nil { 93 return fmt.Errorf( 94 "Error waiting for registration token (%s) to be deactivated: %s", token.Id, waitErr) 95 } 96 97 // Update resource to reflect its state 98 token, err = client.RegistrationToken.ById(token.Id) 99 if err != nil { 100 return fmt.Errorf("Failed to refresh state of deactivated registration token (%s): %s", token.Id, err) 101 } 102 103 // Step 2: Remove 104 if _, err := client.RegistrationToken.ActionRemove(token); err != nil { 105 return fmt.Errorf("Error removing RegistrationToken: %s", err) 106 } 107 108 stateConf = &resource.StateChangeConf{ 109 Pending: []string{"inactive", "removed", "removing"}, 110 Target: []string{"removed"}, 111 Refresh: RegistrationTokenStateRefreshFunc(client, token.Id), 112 Timeout: 10 * time.Minute, 113 Delay: 1 * time.Second, 114 MinTimeout: 3 * time.Second, 115 } 116 117 _, waitErr = stateConf.WaitForState() 118 if waitErr != nil { 119 return fmt.Errorf( 120 "Error waiting for registration token (%s) to be removed: %s", token.Id, waitErr) 121 } 122 123 return nil 124 } 125 } 126 127 func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.RegistrationToken) resource.TestCheckFunc { 128 return func(s *terraform.State) error { 129 rs, ok := s.RootModule().Resources[n] 130 131 if !ok { 132 return fmt.Errorf("Not found: %s", n) 133 } 134 135 if rs.Primary.ID == "" { 136 return fmt.Errorf("No App Name is set") 137 } 138 139 client, err := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"]) 140 if err != nil { 141 return err 142 } 143 144 foundRegT, err := client.RegistrationToken.ById(rs.Primary.ID) 145 if err != nil { 146 return err 147 } 148 149 if foundRegT.Resource.Id != rs.Primary.ID { 150 return fmt.Errorf("RegistrationToken not found") 151 } 152 153 *regT = *foundRegT 154 155 return nil 156 } 157 } 158 159 func testAccCheckRancherRegistrationTokenDestroy(s *terraform.State) error { 160 161 for _, rs := range s.RootModule().Resources { 162 if rs.Type != "rancher_registration_token" { 163 continue 164 } 165 client, err := testAccProvider.Meta().(*Config).GlobalClient() 166 if err != nil { 167 return err 168 } 169 170 regT, err := client.RegistrationToken.ById(rs.Primary.ID) 171 172 if err == nil { 173 if regT != nil && 174 regT.Resource.Id == rs.Primary.ID && 175 regT.State != "removed" { 176 return fmt.Errorf("RegistrationToken still exists") 177 } 178 } 179 180 return nil 181 } 182 return nil 183 } 184 185 const testAccRancherRegistrationTokenConfig = ` 186 resource "rancher_environment" "foo" { 187 name = "foo" 188 } 189 190 resource "rancher_registration_token" "foo" { 191 name = "foo" 192 description = "Terraform acc test group" 193 environment_id = "${rancher_environment.foo.id}" 194 } 195 ` 196 197 const testAccRancherRegistrationTokenUpdateConfig = ` 198 resource "rancher_environment" "foo" { 199 name = "foo" 200 } 201 202 resource "rancher_registration_token" "foo" { 203 name = "foo-u" 204 description = "Terraform acc test group-u" 205 environment_id = "${rancher_environment.foo.id}" 206 } 207 `