github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/rancher/resource_rancher_registry_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 TestAccRancherRegistry_basic(t *testing.T) {
    14  	var registry rancherClient.Registry
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckRancherRegistryDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccRancherRegistryConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    25  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
    26  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
    27  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccRancherRegistryUpdateConfig,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    34  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo2"),
    35  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test - updated"),
    36  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.updated.com:8080"),
    37  				),
    38  			},
    39  			resource.TestStep{
    40  				Config: testAccRancherRegistryRecreateConfig,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    43  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
    44  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
    45  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccRancherRegistry_disappears(t *testing.T) {
    53  	var registry rancherClient.Registry
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckRancherRegistryDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: testAccRancherRegistryConfig,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    64  					testAccRancherRegistryDisappears(&registry),
    65  				),
    66  				ExpectNonEmptyPlan: true,
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func testAccRancherRegistryDisappears(reg *rancherClient.Registry) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		client, err := testAccProvider.Meta().(*Config).EnvironmentClient(reg.AccountId)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		// Step 1: Deactivate
    80  		if _, e := client.Registry.ActionDeactivate(reg); e != nil {
    81  			return fmt.Errorf("Error deactivating Registry: %s", err)
    82  		}
    83  
    84  		stateConf := &resource.StateChangeConf{
    85  			Pending:    []string{"active", "inactive", "deactivating"},
    86  			Target:     []string{"inactive"},
    87  			Refresh:    RegistryStateRefreshFunc(client, reg.Id),
    88  			Timeout:    10 * time.Minute,
    89  			Delay:      1 * time.Second,
    90  			MinTimeout: 3 * time.Second,
    91  		}
    92  
    93  		_, waitErr := stateConf.WaitForState()
    94  		if waitErr != nil {
    95  			return fmt.Errorf(
    96  				"Error waiting for registry (%s) to be deactivated: %s", reg.Id, waitErr)
    97  		}
    98  
    99  		// Update resource to reflect its state
   100  		reg, err = client.Registry.ById(reg.Id)
   101  		if err != nil {
   102  			return fmt.Errorf("Failed to refresh state of deactivated registry (%s): %s", reg.Id, err)
   103  		}
   104  
   105  		// Step 2: Remove
   106  		if _, err := client.Registry.ActionRemove(reg); err != nil {
   107  			return fmt.Errorf("Error removing Registry: %s", err)
   108  		}
   109  
   110  		stateConf = &resource.StateChangeConf{
   111  			Pending:    []string{"inactive", "removed", "removing"},
   112  			Target:     []string{"removed"},
   113  			Refresh:    RegistryStateRefreshFunc(client, reg.Id),
   114  			Timeout:    10 * time.Minute,
   115  			Delay:      1 * time.Second,
   116  			MinTimeout: 3 * time.Second,
   117  		}
   118  
   119  		_, waitErr = stateConf.WaitForState()
   120  		if waitErr != nil {
   121  			return fmt.Errorf(
   122  				"Error waiting for registry (%s) to be removed: %s", reg.Id, waitErr)
   123  		}
   124  
   125  		return nil
   126  	}
   127  }
   128  
   129  func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[n]
   132  
   133  		if !ok {
   134  			return fmt.Errorf("Not found: %s", n)
   135  		}
   136  
   137  		if rs.Primary.ID == "" {
   138  			return fmt.Errorf("No App Name is set")
   139  		}
   140  
   141  		client, err := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		foundReg, err := client.Registry.ById(rs.Primary.ID)
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  		if foundReg.Resource.Id != rs.Primary.ID {
   152  			return fmt.Errorf("Registry not found")
   153  		}
   154  
   155  		*reg = *foundReg
   156  
   157  		return nil
   158  	}
   159  }
   160  
   161  func testAccCheckRancherRegistryDestroy(s *terraform.State) error {
   162  	for _, rs := range s.RootModule().Resources {
   163  		if rs.Type != "rancher_registry" {
   164  			continue
   165  		}
   166  		client, err := testAccProvider.Meta().(*Config).GlobalClient()
   167  		if err != nil {
   168  			return err
   169  		}
   170  
   171  		reg, err := client.Registry.ById(rs.Primary.ID)
   172  
   173  		if err == nil {
   174  			if reg != nil &&
   175  				reg.Resource.Id == rs.Primary.ID &&
   176  				reg.State != "removed" {
   177  				return fmt.Errorf("Registry still exists")
   178  			}
   179  		}
   180  
   181  		return nil
   182  	}
   183  	return nil
   184  }
   185  
   186  const testAccRancherRegistryConfig = `
   187  resource "rancher_environment" "foo_registry" {
   188  	name = "registry test"
   189  	description = "environment to test registries"
   190  }
   191  
   192  resource "rancher_registry" "foo" {
   193    name = "foo"
   194    description = "registry test"
   195    server_address = "http://foo.com:8080"
   196    environment_id = "${rancher_environment.foo_registry.id}"
   197  }
   198  `
   199  
   200  const testAccRancherRegistryUpdateConfig = `
   201   resource "rancher_environment" "foo_registry" {
   202     name = "registry test"
   203     description = "environment to test registries"
   204   }
   205  
   206   resource "rancher_registry" "foo" {
   207     name = "foo2"
   208     description = "registry test - updated"
   209     server_address = "http://foo.updated.com:8080"
   210     environment_id = "${rancher_environment.foo_registry.id}"
   211   }
   212   `
   213  
   214  const testAccRancherRegistryRecreateConfig = `
   215   resource "rancher_environment" "foo_registry" {
   216     name = "registry test"
   217     description = "environment to test registries"
   218   }
   219  
   220   resource "rancher_environment" "foo_registry2" {
   221     name = "alternative registry test"
   222     description = "other environment to test registries"
   223   }
   224  
   225   resource "rancher_registry" "foo" {
   226     name = "foo"
   227     description = "registry test"
   228     server_address = "http://foo.com:8080"
   229     environment_id = "${rancher_environment.foo_registry2.id}"
   230   }
   231   `