github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/ignition/resource_ignition_config_test.go (about)

     1  package ignition
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/coreos/ignition/config/types"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestIngnitionFileReplace(t *testing.T) {
    15  	testIgnition(t, `
    16  		data "ignition_config" "test" {
    17  			replace {
    18  				source = "foo"
    19  				verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    20  			}
    21  		}
    22  	`, func(c *types.Config) error {
    23  		r := c.Ignition.Config.Replace
    24  		if r == nil {
    25  			return fmt.Errorf("unable to find replace config")
    26  		}
    27  
    28  		if r.Source.String() != "foo" {
    29  			return fmt.Errorf("config.replace.source, found %q", r.Source)
    30  		}
    31  
    32  		if r.Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
    33  			return fmt.Errorf("config.replace.verification, found %q", r.Verification.Hash)
    34  		}
    35  
    36  		return nil
    37  	})
    38  }
    39  
    40  func TestIngnitionFileAppend(t *testing.T) {
    41  	testIgnition(t, `
    42  		data "ignition_config" "test" {
    43  			append {
    44  				source = "foo"
    45  				verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    46  			}
    47  
    48  		    append {
    49  		    	source = "foo"
    50  		    	verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    51  			}
    52  		}
    53  	`, func(c *types.Config) error {
    54  		a := c.Ignition.Config.Append
    55  		if len(a) != 2 {
    56  			return fmt.Errorf("unable to find append config, expected 2")
    57  		}
    58  
    59  		if a[0].Source.String() != "foo" {
    60  			return fmt.Errorf("config.replace.source, found %q", a[0].Source)
    61  		}
    62  
    63  		if a[0].Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
    64  			return fmt.Errorf("config.replace.verification, found %q", a[0].Verification.Hash)
    65  		}
    66  
    67  		return nil
    68  	})
    69  }
    70  
    71  func testIgnitionError(t *testing.T, input string, expectedErr *regexp.Regexp) {
    72  	resource.Test(t, resource.TestCase{
    73  		Providers: testProviders,
    74  		Steps: []resource.TestStep{
    75  			{
    76  				Config:      fmt.Sprintf(testTemplate, input),
    77  				ExpectError: expectedErr,
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func testIgnition(t *testing.T, input string, assert func(*types.Config) error) {
    84  	check := func(s *terraform.State) error {
    85  		got := s.RootModule().Outputs["rendered"].Value.(string)
    86  
    87  		c := &types.Config{}
    88  		err := json.Unmarshal([]byte(got), c)
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		return assert(c)
    94  	}
    95  
    96  	resource.Test(t, resource.TestCase{
    97  		Providers: testProviders,
    98  		Steps: []resource.TestStep{
    99  			{
   100  				Config: fmt.Sprintf(testTemplate, input),
   101  				Check:  check,
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  var testTemplate = `
   108  %s
   109  
   110  output "rendered" {
   111  	value = "${data.ignition_config.test.rendered}"
   112  }
   113  
   114  `