github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_environment_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 TestAccRancherEnvironment(t *testing.T) {
    13  	var environment rancherClient.Project
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckRancherEnvironmentDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRancherEnvironmentConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment),
    24  					resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo"),
    25  					resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group"),
    26  					resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "cattle"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccRancherEnvironmentUpdateConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment),
    33  					resource.TestCheckResourceAttr("rancher_environment.foo", "name", "foo2"),
    34  					resource.TestCheckResourceAttr("rancher_environment.foo", "description", "Terraform acc test group - updated"),
    35  					resource.TestCheckResourceAttr("rancher_environment.foo", "orchestration", "swarm"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project) 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  		foundEnv, err := client.Project.ById(rs.Primary.ID)
    57  		if err != nil {
    58  			return err
    59  		}
    60  
    61  		if foundEnv.Resource.Id != rs.Primary.ID {
    62  			return fmt.Errorf("Environment not found")
    63  		}
    64  
    65  		*env = *foundEnv
    66  
    67  		return nil
    68  	}
    69  }
    70  
    71  func testAccCheckRancherEnvironmentDestroy(s *terraform.State) error {
    72  	client := testAccProvider.Meta().(*Config)
    73  
    74  	for _, rs := range s.RootModule().Resources {
    75  		if rs.Type != "rancher_environment" {
    76  			continue
    77  		}
    78  		env, err := client.Project.ById(rs.Primary.ID)
    79  
    80  		if err == nil {
    81  			if env != nil &&
    82  				env.Resource.Id == rs.Primary.ID &&
    83  				env.State != "removed" {
    84  				return fmt.Errorf("Environment still exists")
    85  			}
    86  		}
    87  
    88  		return nil
    89  	}
    90  	return nil
    91  }
    92  
    93  const testAccRancherEnvironmentConfig = `
    94  resource "rancher_environment" "foo" {
    95  	name = "foo"
    96  	description = "Terraform acc test group"
    97  	orchestration = "cattle"
    98  }
    99  `
   100  
   101  const testAccRancherEnvironmentUpdateConfig = `
   102  resource "rancher_environment" "foo" {
   103  	name = "foo2"
   104  	description = "Terraform acc test group - updated"
   105  	orchestration = "swarm"
   106  }
   107  `