github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_registry_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 TestAccRancherRegistry(t *testing.T) {
    13  	var registry rancherClient.Registry
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckRancherRegistryDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRancherRegistryConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    24  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
    25  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
    26  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccRancherRegistryUpdateConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    33  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo2"),
    34  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test - updated"),
    35  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.updated.com:8080"),
    36  				),
    37  			},
    38  			resource.TestStep{
    39  				Config: testAccRancherRegistryRecreateConfig,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckRancherRegistryExists("rancher_registry.foo", &registry),
    42  					resource.TestCheckResourceAttr("rancher_registry.foo", "name", "foo"),
    43  					resource.TestCheckResourceAttr("rancher_registry.foo", "description", "registry test"),
    44  					resource.TestCheckResourceAttr("rancher_registry.foo", "server_address", "http://foo.com:8080"),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[n]
    54  
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No App Name is set")
    61  		}
    62  
    63  		client := testAccProvider.Meta().(*Config)
    64  
    65  		foundReg, err := client.Registry.ById(rs.Primary.ID)
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		if foundReg.Resource.Id != rs.Primary.ID {
    71  			return fmt.Errorf("Environment not found")
    72  		}
    73  
    74  		*reg = *foundReg
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccCheckRancherRegistryDestroy(s *terraform.State) error {
    81  	client := testAccProvider.Meta().(*Config)
    82  
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "rancher_registry" {
    85  			continue
    86  		}
    87  		reg, err := client.Registry.ById(rs.Primary.ID)
    88  
    89  		if err == nil {
    90  			if reg != nil &&
    91  				reg.Resource.Id == rs.Primary.ID &&
    92  				reg.State != "removed" {
    93  				return fmt.Errorf("Registry still exists")
    94  			}
    95  		}
    96  
    97  		return nil
    98  	}
    99  	return nil
   100  }
   101  
   102  const testAccRancherRegistryConfig = `
   103  resource "rancher_environment" "foo_registry" {
   104  	name = "registry test"
   105  	description = "environment to test registries"
   106  }
   107  
   108  resource "rancher_registry" "foo" {
   109    name = "foo"
   110    description = "registry test"
   111    server_address = "http://foo.com:8080"
   112    environment_id = "${rancher_environment.foo_registry.id}"
   113  }
   114  `
   115  
   116  const testAccRancherRegistryUpdateConfig = `
   117   resource "rancher_environment" "foo_registry" {
   118     name = "registry test"
   119     description = "environment to test registries"
   120   }
   121  
   122   resource "rancher_registry" "foo" {
   123     name = "foo2"
   124     description = "registry test - updated"
   125     server_address = "http://foo.updated.com:8080"
   126     environment_id = "${rancher_environment.foo_registry.id}"
   127   }
   128   `
   129  
   130  const testAccRancherRegistryRecreateConfig = `
   131   resource "rancher_environment" "foo_registry" {
   132     name = "registry test"
   133     description = "environment to test registries"
   134   }
   135  
   136   resource "rancher_environment" "foo_registry2" {
   137     name = "alternative registry test"
   138     description = "other environment to test registries"
   139   }
   140  
   141   resource "rancher_registry" "foo" {
   142     name = "foo"
   143     description = "registry test"
   144     server_address = "http://foo.com:8080"
   145     environment_id = "${rancher_environment.foo_registry2.id}"
   146   }
   147   `