github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_registration_token_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 TestAccRancherRegistrationToken(t *testing.T) {
    13  	var registrationToken rancherClient.RegistrationToken
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckRancherRegistrationTokenDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRancherRegistrationTokenConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", &registrationToken),
    24  					resource.TestCheckResourceAttr(
    25  						"rancher_registration_token.foo", "name", "foo"),
    26  					resource.TestCheckResourceAttr(
    27  						"rancher_registration_token.foo", "description", "Terraform acc test group"),
    28  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "command"),
    29  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "registration_url"),
    30  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "token"),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccRancherRegistrationTokenUpdateConfig,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", &registrationToken),
    37  					resource.TestCheckResourceAttr(
    38  						"rancher_registration_token.foo", "name", "foo-u"),
    39  					resource.TestCheckResourceAttr(
    40  						"rancher_registration_token.foo", "description", "Terraform acc test group-u"),
    41  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "command"),
    42  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "registration_url"),
    43  					resource.TestCheckResourceAttrSet("rancher_registration_token.foo", "token"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.RegistrationToken) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  
    54  		if !ok {
    55  			return fmt.Errorf("Not found: %s", n)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No App Name is set")
    60  		}
    61  
    62  		client := testAccProvider.Meta().(*Config)
    63  
    64  		foundRegT, err := client.RegistrationToken.ById(rs.Primary.ID)
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		if foundRegT.Resource.Id != rs.Primary.ID {
    70  			return fmt.Errorf("RegistrationToken not found")
    71  		}
    72  
    73  		*regT = *foundRegT
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  func testAccCheckRancherRegistrationTokenDestroy(s *terraform.State) error {
    80  	client := testAccProvider.Meta().(*Config)
    81  
    82  	for _, rs := range s.RootModule().Resources {
    83  		if rs.Type != "rancher_registration_token" {
    84  			continue
    85  		}
    86  		regT, err := client.RegistrationToken.ById(rs.Primary.ID)
    87  
    88  		if err == nil {
    89  			if regT != nil &&
    90  				regT.Resource.Id == rs.Primary.ID &&
    91  				regT.State != "removed" {
    92  				return fmt.Errorf("RegistrationToken still exists")
    93  			}
    94  		}
    95  
    96  		return nil
    97  	}
    98  	return nil
    99  }
   100  
   101  const testAccRancherRegistrationTokenConfig = `
   102  resource "rancher_environment" "foo" {
   103  	name = "foo"
   104  }
   105  
   106  resource "rancher_registration_token" "foo" {
   107  	name = "foo"
   108  	description = "Terraform acc test group"
   109  	environment_id = "${rancher_environment.foo.id}"
   110  }
   111  `
   112  
   113  const testAccRancherRegistrationTokenUpdateConfig = `
   114  resource "rancher_environment" "foo" {
   115  	name = "foo"
   116  }
   117  
   118  resource "rancher_registration_token" "foo" {
   119  	name = "foo-u"
   120  	description = "Terraform acc test group-u"
   121  	environment_id = "${rancher_environment.foo.id}"
   122  }
   123  `