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

     1  package alicloud
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  )
     9  
    10  func TestAccAlicloudImagesDataSource_images(t *testing.T) {
    11  	resource.Test(t, resource.TestCase{
    12  		PreCheck:  func() { testAccPreCheck(t) },
    13  		Providers: testAccProviders,
    14  		Steps: []resource.TestStep{
    15  			{
    16  				Config: testAccCheckAlicloudImagesDataSourceImagesConfig,
    17  				Check: resource.ComposeTestCheckFunc(
    18  					testAccCheckAlicloudDataSourceID("data.alicloud_images.multi_image"),
    19  
    20  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.#", "2"),
    21  
    22  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.architecture", "x86_64"),
    23  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.disk_device_mappings.#", "0"),
    24  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.creation_time", regexp.MustCompile("^20[0-9]{2}-")),
    25  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.image_id", regexp.MustCompile("^centos_6\\w{1,5}[64]{1}.")),
    26  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.image_owner_alias", "system"),
    27  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.os_type", "linux"),
    28  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.name", regexp.MustCompile("^centos_6[a-zA-Z0-9_]{1,5}[64]{1}.")),
    29  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.progress", "100%"),
    30  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.state", "Available"),
    31  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.status", "Available"),
    32  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.usage", "instance"),
    33  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.tags.%", "0"),
    34  
    35  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.architecture", "i386"),
    36  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.disk_device_mappings.#", "0"),
    37  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.creation_time", regexp.MustCompile("^20[0-9]{2}-")),
    38  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.image_id", regexp.MustCompile("^centos_6[a-zA-Z0-9_]{1,5}[32]{1}.")),
    39  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.image_owner_alias", "system"),
    40  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.os_type", "linux"),
    41  					resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.name", regexp.MustCompile("^centos_6\\w{1,5}[32]{1}.")),
    42  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.progress", "100%"),
    43  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.state", "Available"),
    44  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.status", "Available"),
    45  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.usage", "instance"),
    46  					resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.tags.%", "0"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAlicloudImagesDataSource_owners(t *testing.T) {
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:  func() { testAccPreCheck(t) },
    56  		Providers: testAccProviders,
    57  		Steps: []resource.TestStep{
    58  			{
    59  				Config: testAccCheckAlicloudImagesDataSourceOwnersConfig,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckAlicloudDataSourceID("data.alicloud_images.owners_filtered_image"),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAlicloudImagesDataSource_ownersEmpty(t *testing.T) {
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:  func() { testAccPreCheck(t) },
    71  		Providers: testAccProviders,
    72  		Steps: []resource.TestStep{
    73  			{
    74  				Config: testAccCheckAlicloudImagesDataSourceEmptyOwnersConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckAlicloudDataSourceID("data.alicloud_images.empty_owners_filtered_image"),
    77  					resource.TestCheckResourceAttr("data.alicloud_images.empty_owners_filtered_image", "most_recent", "true"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAlicloudImagesDataSource_nameRegexFilter(t *testing.T) {
    85  	resource.Test(t, resource.TestCase{
    86  		PreCheck:  func() { testAccPreCheck(t) },
    87  		Providers: testAccProviders,
    88  		Steps: []resource.TestStep{
    89  			{
    90  				Config: testAccCheckAlicloudImagesDataSourceNameRegexConfig,
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testAccCheckAlicloudDataSourceID("data.alicloud_images.name_regex_filtered_image"),
    93  					resource.TestMatchResourceAttr("data.alicloud_images.name_regex_filtered_image", "images.0.image_id", regexp.MustCompile("^centos_")),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  // Instance store test - using centos images
   101  const testAccCheckAlicloudImagesDataSourceImagesConfig = `
   102  data "alicloud_images" "multi_image" {
   103  	owners = "system"
   104  	name_regex = "^centos_6"
   105  }
   106  `
   107  
   108  // Testing owner parameter
   109  const testAccCheckAlicloudImagesDataSourceOwnersConfig = `
   110  data "alicloud_images" "owners_filtered_image" {
   111  	most_recent = true
   112  	owners = "system"
   113  }
   114  `
   115  
   116  const testAccCheckAlicloudImagesDataSourceEmptyOwnersConfig = `
   117  data "alicloud_images" "empty_owners_filtered_image" {
   118  	most_recent = true
   119  	owners = ""
   120  }
   121  `
   122  
   123  // Testing name_regex parameter
   124  const testAccCheckAlicloudImagesDataSourceNameRegexConfig = `
   125  data "alicloud_images" "name_regex_filtered_image" {
   126  	most_recent = true
   127  	owners = "system"
   128  	name_regex = "^centos_6\\w{1,5}[64]{1}.*"
   129  }
   130  `