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

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccScalewayDataSourceImage_Basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccCheckScalewayImageConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckImageID("data.scaleway_image.ubuntu"),
    20  					resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "architecture", "arm"),
    21  					resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "public", "true"),
    22  					resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "organization"),
    23  					resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "creation_date"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func TestAccScalewayDataSourceImage_Filtered(t *testing.T) {
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:  func() { testAccPreCheck(t) },
    33  		Providers: testAccProviders,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccCheckScalewayImageFilterConfig,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckImageID("data.scaleway_image.ubuntu"),
    39  					resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "name", "Ubuntu Precise (12.04)"),
    40  					resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "architecture", "arm"),
    41  					resource.TestCheckResourceAttr("data.scaleway_image.ubuntu", "public", "true"),
    42  					resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "organization"),
    43  					resource.TestCheckResourceAttrSet("data.scaleway_image.ubuntu", "creation_date"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckImageID(n string) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Can't find image data source: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("image data source ID not set")
    59  		}
    60  
    61  		scaleway := testAccProvider.Meta().(*Client).scaleway
    62  		_, err := scaleway.GetImage(rs.Primary.ID)
    63  
    64  		if err != nil {
    65  			return err
    66  		}
    67  
    68  		return nil
    69  	}
    70  }
    71  
    72  const testAccCheckScalewayImageConfig = `
    73  data "scaleway_image" "ubuntu" {
    74    name = "Ubuntu Precise"
    75    architecture = "arm"
    76  }
    77  `
    78  
    79  const testAccCheckScalewayImageFilterConfig = `
    80  data "scaleway_image" "ubuntu" {
    81    name_filter = "Precise"
    82    architecture = "arm"
    83  }
    84  `