github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_search_service_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/jen20/riviera/search"
    11  )
    12  
    13  func TestAccAzureRMSearchService_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMSearchServiceDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
    26  					resource.TestCheckResourceAttr(
    27  						"azurerm_search_service.test", "tags.%", "2"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMSearchService_updateReplicaCountAndTags(t *testing.T) {
    35  	ri := acctest.RandInt()
    36  	preConfig := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, ri)
    37  	postConfig := fmt.Sprintf(testAccAzureRMSearchService_updated, ri, ri)
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testCheckAzureRMSearchServiceDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: preConfig,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
    48  					resource.TestCheckResourceAttr(
    49  						"azurerm_search_service.test", "tags.%", "2"),
    50  					resource.TestCheckResourceAttr(
    51  						"azurerm_search_service.test", "replica_count", "1"),
    52  				),
    53  			},
    54  
    55  			resource.TestStep{
    56  				Config: postConfig,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
    59  					resource.TestCheckResourceAttr(
    60  						"azurerm_search_service.test", "tags.%", "1"),
    61  					resource.TestCheckResourceAttr(
    62  						"azurerm_search_service.test", "replica_count", "2"),
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func testCheckAzureRMSearchServiceExists(name string) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  
    72  		rs, ok := s.RootModule().Resources[name]
    73  		if !ok {
    74  			return fmt.Errorf("Not found: %s", name)
    75  		}
    76  
    77  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    78  
    79  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
    80  		readRequest.Command = &search.GetSearchService{}
    81  
    82  		readResponse, err := readRequest.Execute()
    83  		if err != nil {
    84  			return fmt.Errorf("Bad: GetSearchService: %s", err)
    85  		}
    86  		if !readResponse.IsSuccessful() {
    87  			return fmt.Errorf("Bad: GetSearchService: %s", readResponse.Error)
    88  		}
    89  
    90  		return nil
    91  	}
    92  }
    93  
    94  func testCheckAzureRMSearchServiceDestroy(s *terraform.State) error {
    95  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "azurerm_search_service" {
    99  			continue
   100  		}
   101  
   102  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   103  		readRequest.Command = &search.GetSearchService{}
   104  
   105  		readResponse, err := readRequest.Execute()
   106  		if err != nil {
   107  			return fmt.Errorf("Bad: GetSearchService: %s", err)
   108  		}
   109  
   110  		if readResponse.IsSuccessful() {
   111  			return fmt.Errorf("Bad: Search Service still exists: %s", readResponse.Error)
   112  		}
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  var testAccAzureRMSearchService_basic = `
   119  resource "azurerm_resource_group" "test" {
   120      name = "acctest_rg_%d"
   121      location = "West US"
   122  }
   123  resource "azurerm_search_service" "test" {
   124      name = "acctestsearchservice%d"
   125      resource_group_name = "${azurerm_resource_group.test.name}"
   126      location = "West US"
   127      sku = "standard"
   128  
   129      tags {
   130      	environment = "staging"
   131      	database = "test"
   132      }
   133  }
   134  `
   135  
   136  var testAccAzureRMSearchService_updated = `
   137  resource "azurerm_resource_group" "test" {
   138      name = "acctest_rg_%d"
   139      location = "West US"
   140  }
   141  resource "azurerm_search_service" "test" {
   142      name = "acctestsearchservice%d"
   143      resource_group_name = "${azurerm_resource_group.test.name}"
   144      location = "West US"
   145      sku = "standard"
   146      replica_count = 2
   147  
   148      tags {
   149      	environment = "production"
   150      }
   151  }
   152  `