github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/rancher/resource_rancher_environment_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 TestAccRancherEnvironment_basic(t *testing.T) { 14 var environment rancherClient.Project 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckRancherEnvironmentDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccRancherEnvironmentConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment), 25 resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo"), 26 resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group"), 27 resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "cattle"), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccRancherEnvironmentUpdateConfig, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment), 34 resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo2"), 35 resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group - updated"), 36 resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "swarm"), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccRancherEnvironment_disappears(t *testing.T) { 44 var environment rancherClient.Project 45 46 resource.Test(t, resource.TestCase{ 47 PreCheck: func() { testAccPreCheck(t) }, 48 Providers: testAccProviders, 49 CheckDestroy: testAccCheckRancherEnvironmentDestroy, 50 Steps: []resource.TestStep{ 51 resource.TestStep{ 52 Config: testAccRancherEnvironmentConfig, 53 Check: resource.ComposeTestCheckFunc( 54 testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment), 55 testAccRancherEnvironmentDisappears(&environment), 56 ), 57 ExpectNonEmptyPlan: true, 58 }, 59 }, 60 }) 61 } 62 63 func TestAccRancherEnvironment_members(t *testing.T) { 64 var environment rancherClient.Project 65 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testAccCheckRancherEnvironmentDestroy, 70 Steps: []resource.TestStep{ 71 resource.TestStep{ 72 Config: testAccRancherEnvironmentMembersConfig, 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment), 75 resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo"), 76 resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group"), 77 resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "cattle"), 78 resource.TestCheckResourceAttr("rancher_environment.foo", "member.#", "2"), 79 ), 80 }, 81 resource.TestStep{ 82 Config: testAccRancherEnvironmentMembersUpdateConfig, 83 Check: resource.ComposeTestCheckFunc( 84 testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment), 85 resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo2"), 86 resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group - updated"), 87 resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "swarm"), 88 resource.TestCheckResourceAttr("rancher_environment.foo", "member.#", "1"), 89 ), 90 }, 91 }, 92 }) 93 } 94 95 func testAccRancherEnvironmentDisappears(env *rancherClient.Project) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 client, err := testAccProvider.Meta().(*Config).GlobalClient() 98 if err != nil { 99 return err 100 } 101 if err := client.Project.Delete(env); err != nil { 102 return fmt.Errorf("Error deleting Environment: %s", err) 103 } 104 stateConf := &resource.StateChangeConf{ 105 Pending: []string{"active", "removed", "removing"}, 106 Target: []string{"removed"}, 107 Refresh: EnvironmentStateRefreshFunc(client, env.Id), 108 Timeout: 10 * time.Minute, 109 Delay: 1 * time.Second, 110 MinTimeout: 3 * time.Second, 111 } 112 113 _, waitErr := stateConf.WaitForState() 114 if waitErr != nil { 115 return fmt.Errorf( 116 "Error waiting for environment (%s) to be removed: %s", env.Id, waitErr) 117 } 118 return nil 119 } 120 } 121 122 func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project) resource.TestCheckFunc { 123 return func(s *terraform.State) error { 124 rs, ok := s.RootModule().Resources[n] 125 126 if !ok { 127 return fmt.Errorf("Not found: %s", n) 128 } 129 130 if rs.Primary.ID == "" { 131 return fmt.Errorf("No App Name is set") 132 } 133 134 client, err := testAccProvider.Meta().(*Config).GlobalClient() 135 if err != nil { 136 return err 137 } 138 139 foundEnv, err := client.Project.ById(rs.Primary.ID) 140 if err != nil { 141 return err 142 } 143 144 if foundEnv.Resource.Id != rs.Primary.ID { 145 return fmt.Errorf("Environment not found") 146 } 147 148 *env = *foundEnv 149 150 return nil 151 } 152 } 153 154 func testAccCheckRancherEnvironmentDestroy(s *terraform.State) error { 155 client, err := testAccProvider.Meta().(*Config).GlobalClient() 156 if err != nil { 157 return err 158 } 159 160 for _, rs := range s.RootModule().Resources { 161 if rs.Type != "rancher_environment" { 162 continue 163 } 164 env, err := client.Project.ById(rs.Primary.ID) 165 166 if err == nil { 167 if env != nil && 168 env.Resource.Id == rs.Primary.ID && 169 env.State != "removed" { 170 return fmt.Errorf("Environment still exists") 171 } 172 } 173 174 return nil 175 } 176 return nil 177 } 178 179 const testAccRancherEnvironmentConfig = ` 180 resource "rancher_environment" "foo" { 181 name = "foo" 182 description = "Terraform acc test group" 183 orchestration = "cattle" 184 } 185 ` 186 187 const testAccRancherEnvironmentUpdateConfig = ` 188 resource "rancher_environment" "foo" { 189 name = "foo2" 190 description = "Terraform acc test group - updated" 191 orchestration = "swarm" 192 } 193 ` 194 195 const testAccRancherEnvironmentMembersConfig = ` 196 resource "rancher_environment" "foo" { 197 name = "foo" 198 description = "Terraform acc test group" 199 orchestration = "cattle" 200 201 member { 202 external_id = "1234" 203 external_id_type = "github_user" 204 role = "owner" 205 } 206 207 member { 208 external_id = "8765" 209 external_id_type = "github_team" 210 role = "member" 211 } 212 } 213 ` 214 215 const testAccRancherEnvironmentMembersUpdateConfig = ` 216 resource "rancher_environment" "foo" { 217 name = "foo" 218 description = "Terraform acc test group" 219 orchestration = "cattle" 220 221 member { 222 external_id = "1235" 223 external_id_type = "github_user" 224 role = "owner" 225 } 226 } 227 `