github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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 testAccRancherEnvironmentDisappears(env *rancherClient.Project) resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 client, err := testAccProvider.Meta().(*Config).GlobalClient() 66 if err != nil { 67 return err 68 } 69 if err := client.Project.Delete(env); err != nil { 70 return fmt.Errorf("Error deleting Environment: %s", err) 71 } 72 stateConf := &resource.StateChangeConf{ 73 Pending: []string{"active", "removed", "removing"}, 74 Target: []string{"removed"}, 75 Refresh: EnvironmentStateRefreshFunc(client, env.Id), 76 Timeout: 10 * time.Minute, 77 Delay: 1 * time.Second, 78 MinTimeout: 3 * time.Second, 79 } 80 81 _, waitErr := stateConf.WaitForState() 82 if waitErr != nil { 83 return fmt.Errorf( 84 "Error waiting for environment (%s) to be removed: %s", env.Id, waitErr) 85 } 86 return nil 87 } 88 } 89 90 func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 rs, ok := s.RootModule().Resources[n] 93 94 if !ok { 95 return fmt.Errorf("Not found: %s", n) 96 } 97 98 if rs.Primary.ID == "" { 99 return fmt.Errorf("No App Name is set") 100 } 101 102 client, err := testAccProvider.Meta().(*Config).GlobalClient() 103 if err != nil { 104 return err 105 } 106 107 foundEnv, err := client.Project.ById(rs.Primary.ID) 108 if err != nil { 109 return err 110 } 111 112 if foundEnv.Resource.Id != rs.Primary.ID { 113 return fmt.Errorf("Environment not found") 114 } 115 116 *env = *foundEnv 117 118 return nil 119 } 120 } 121 122 func testAccCheckRancherEnvironmentDestroy(s *terraform.State) error { 123 client, err := testAccProvider.Meta().(*Config).GlobalClient() 124 if err != nil { 125 return err 126 } 127 128 for _, rs := range s.RootModule().Resources { 129 if rs.Type != "rancher_environment" { 130 continue 131 } 132 env, err := client.Project.ById(rs.Primary.ID) 133 134 if err == nil { 135 if env != nil && 136 env.Resource.Id == rs.Primary.ID && 137 env.State != "removed" { 138 return fmt.Errorf("Environment still exists") 139 } 140 } 141 142 return nil 143 } 144 return nil 145 } 146 147 const testAccRancherEnvironmentConfig = ` 148 resource "rancher_environment" "foo" { 149 name = "foo" 150 description = "Terraform acc test group" 151 orchestration = "cattle" 152 } 153 ` 154 155 const testAccRancherEnvironmentUpdateConfig = ` 156 resource "rancher_environment" "foo" { 157 name = "foo2" 158 description = "Terraform acc test group - updated" 159 orchestration = "swarm" 160 } 161 `