github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/atlas/resource_artifact_test.go (about)

     1  package atlas
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccArtifact_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Providers: testAccProviders,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccArtifact_basic,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckArtifactState("name", "hashicorp/tf-provider-test"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func TestAccArtifact_metadata(t *testing.T) {
    28  	resource.Test(t, resource.TestCase{
    29  		PreCheck:  func() { testAccPreCheck(t) },
    30  		Providers: testAccProviders,
    31  		Steps: []resource.TestStep{
    32  			resource.TestStep{
    33  				Config: testAccArtifact_metadata,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckArtifactState("name", "hashicorp/tf-provider-test"),
    36  					testAccCheckArtifactState("id", "x86"),
    37  					testAccCheckArtifactState("metadata_full.arch", "x86"),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func TestAccArtifact_metadataSet(t *testing.T) {
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:  func() { testAccPreCheck(t) },
    47  		Providers: testAccProviders,
    48  		Steps: []resource.TestStep{
    49  			resource.TestStep{
    50  				Config: testAccArtifact_metadataSet,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckArtifactState("name", "hashicorp/tf-provider-test"),
    53  					testAccCheckArtifactState("id", "x64"),
    54  					testAccCheckArtifactState("metadata_full.arch", "x64"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccArtifact_buildLatest(t *testing.T) {
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:  func() { testAccPreCheck(t) },
    64  		Providers: testAccProviders,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccArtifact_buildLatest,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckArtifactState("name", "hashicorp/tf-provider-test"),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func TestAccArtifact_versionAny(t *testing.T) {
    77  	resource.Test(t, resource.TestCase{
    78  		PreCheck:  func() { testAccPreCheck(t) },
    79  		Providers: testAccProviders,
    80  		Steps: []resource.TestStep{
    81  			resource.TestStep{
    82  				Config: testAccArtifact_versionAny,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckArtifactState("name", "hashicorp/tf-provider-test"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func testAccCheckArtifactState(key, value string) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources["atlas_artifact.foobar"]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", "atlas_artifact.foobar")
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("No ID is set")
   100  		}
   101  
   102  		p := rs.Primary
   103  		if p.Attributes[key] != value {
   104  			return fmt.Errorf(
   105  				"%s != %s (actual: %s)", key, value, p.Attributes[key])
   106  		}
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  func TestCleanMetadata(t *testing.T) {
   113  	in := map[string]string{
   114  		"region.us-east-1": "in",
   115  		"what is	this?": "out",
   116  	}
   117  	exp := map[string]string{
   118  		"region-us-east-1": "in",
   119  		"what-is-this-":    "out",
   120  	}
   121  	out := cleanMetadata(in)
   122  	if !reflect.DeepEqual(out, exp) {
   123  		t.Fatalf("bad: %#v", out)
   124  	}
   125  }
   126  
   127  const testAccArtifact_basic = `
   128  resource "atlas_artifact" "foobar" {
   129  	name = "hashicorp/tf-provider-test"
   130  	type = "foo"
   131  }`
   132  
   133  const testAccArtifact_metadata = `
   134  resource "atlas_artifact" "foobar" {
   135  	name = "hashicorp/tf-provider-test"
   136  	type = "foo"
   137  	metadata {
   138  		arch = "x86"
   139  	}
   140  	version = "any"
   141  }`
   142  
   143  const testAccArtifact_metadataSet = `
   144  resource "atlas_artifact" "foobar" {
   145  	name = "hashicorp/tf-provider-test"
   146  	type = "foo"
   147  	metadata_keys = ["arch"]
   148  	version = "any"
   149  }`
   150  
   151  const testAccArtifact_buildLatest = `
   152  resource "atlas_artifact" "foobar" {
   153  	name = "hashicorp/tf-provider-test"
   154  	type = "foo"
   155  	build = "latest"
   156  	metadata {
   157  		arch = "x86"
   158  	}
   159  }`
   160  
   161  const testAccArtifact_versionAny = `
   162  resource "atlas_artifact" "foobar" {
   163  	name = "hashicorp/tf-provider-test"
   164  	type = "foo"
   165  	version = "any"
   166  }`