github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_disk_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"log"
    11  )
    12  
    13  func TestAccAlicloudDisk_basic(t *testing.T) {
    14  	var v ecs.DiskItemType
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_disk.foo",
    23  
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckDiskDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccDiskConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckDiskExists(
    31  						"alicloud_disk.foo", &v),
    32  					resource.TestCheckResourceAttr(
    33  						"alicloud_disk.foo",
    34  						"category",
    35  						"cloud_efficiency"),
    36  					resource.TestCheckResourceAttr(
    37  						"alicloud_disk.foo",
    38  						"size",
    39  						"30"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  
    45  }
    46  
    47  func TestAccAlicloudDisk_withTags(t *testing.T) {
    48  	var v ecs.DiskItemType
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck: func() {
    52  			testAccPreCheck(t)
    53  		},
    54  
    55  		//module name
    56  		IDRefreshName: "alicloud_disk.bar",
    57  
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckDiskDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccDiskConfigWithTags,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckDiskExists("alicloud_disk.bar", &v),
    65  					resource.TestCheckResourceAttr(
    66  						"alicloud_disk.bar",
    67  						"tags.Name",
    68  						"TerraformTest"),
    69  				),
    70  			},
    71  		},
    72  	})
    73  }
    74  
    75  func testAccCheckDiskExists(n string, disk *ecs.DiskItemType) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		rs, ok := s.RootModule().Resources[n]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", n)
    80  		}
    81  
    82  		if rs.Primary.ID == "" {
    83  			return fmt.Errorf("No Disk ID is set")
    84  		}
    85  
    86  		client := testAccProvider.Meta().(*AliyunClient)
    87  		conn := client.ecsconn
    88  
    89  		request := &ecs.DescribeDisksArgs{
    90  			RegionId: client.Region,
    91  			DiskIds:  []string{rs.Primary.ID},
    92  		}
    93  
    94  		response, _, err := conn.DescribeDisks(request)
    95  		log.Printf("[WARN] disk ids %#v", rs.Primary.ID)
    96  
    97  		if err == nil {
    98  			if response != nil && len(response) > 0 {
    99  				*disk = response[0]
   100  				return nil
   101  			}
   102  		}
   103  		return fmt.Errorf("Error finding ECS Disk %#v", rs.Primary.ID)
   104  	}
   105  }
   106  
   107  func testAccCheckDiskDestroy(s *terraform.State) error {
   108  
   109  	for _, rs := range s.RootModule().Resources {
   110  		if rs.Type != "alicloud_disk" {
   111  			continue
   112  		}
   113  
   114  		// Try to find the Disk
   115  		client := testAccProvider.Meta().(*AliyunClient)
   116  		conn := client.ecsconn
   117  
   118  		request := &ecs.DescribeDisksArgs{
   119  			RegionId: client.Region,
   120  			DiskIds:  []string{rs.Primary.ID},
   121  		}
   122  
   123  		response, _, err := conn.DescribeDisks(request)
   124  
   125  		if response != nil && len(response) > 0 {
   126  			return fmt.Errorf("Error ECS Disk still exist")
   127  		}
   128  
   129  		if err != nil {
   130  			// Verify the error is what we want
   131  			return err
   132  		}
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  const testAccDiskConfig = `
   139  resource "alicloud_disk" "foo" {
   140  	# cn-beijing
   141  	availability_zone = "cn-beijing-b"
   142  	name = "New-disk"
   143  	description = "Hello ecs disk."
   144  	category = "cloud_efficiency"
   145          size = "30"
   146  }
   147  `
   148  const testAccDiskConfigWithTags = `
   149  resource "alicloud_disk" "bar" {
   150  	# cn-beijing
   151  	availability_zone = "cn-beijing-b"
   152          size = "10"
   153          tags {
   154          	Name = "TerraformTest"
   155          }
   156  }
   157  `