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

     1  package template
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	r "github.com/hashicorp/terraform/helper/resource"
     8  )
     9  
    10  func TestRender(t *testing.T) {
    11  	testCases := []struct {
    12  		ResourceBlock string
    13  		Expected      string
    14  	}{
    15  		{
    16  			`data "template_cloudinit_config" "foo" {
    17  				gzip = false
    18  				base64_encode = false
    19  
    20  				part {
    21  					content_type = "text/x-shellscript"
    22  					content = "baz"
    23  				}
    24  			}`,
    25  			"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nbaz\r\n--MIMEBOUNDARY--\r\n",
    26  		},
    27  		{
    28  			`data "template_cloudinit_config" "foo" {
    29  				gzip = false
    30  				base64_encode = false
    31  
    32  				part {
    33  					content_type = "text/x-shellscript"
    34  					content = "baz"
    35  					filename = "foobar.sh"
    36  				}
    37  			}`,
    38  			"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Disposition: attachment; filename=\"foobar.sh\"\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nbaz\r\n--MIMEBOUNDARY--\r\n",
    39  		},
    40  		{
    41  			`data "template_cloudinit_config" "foo" {
    42  				gzip = false
    43  				base64_encode = false
    44  
    45  				part {
    46  					content_type = "text/x-shellscript"
    47  					content = "baz"
    48  				}
    49  				part {
    50  					content_type = "text/x-shellscript"
    51  					content = "ffbaz"
    52  				}
    53  			}`,
    54  			"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nbaz\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nffbaz\r\n--MIMEBOUNDARY--\r\n",
    55  		},
    56  	}
    57  
    58  	for _, tt := range testCases {
    59  		r.UnitTest(t, r.TestCase{
    60  			Providers: testProviders,
    61  			Steps: []r.TestStep{
    62  				{
    63  					Config: tt.ResourceBlock,
    64  					Check: r.ComposeTestCheckFunc(
    65  						r.TestCheckResourceAttr("data.template_cloudinit_config.foo", "rendered", tt.Expected),
    66  					),
    67  				},
    68  			},
    69  		})
    70  	}
    71  }
    72  
    73  // From GH-13572, Correctly handle panic on a misconfigured cloudinit part
    74  func TestRender_handlePanic(t *testing.T) {
    75  	r.UnitTest(t, r.TestCase{
    76  		Providers: testProviders,
    77  		Steps: []r.TestStep{
    78  			{
    79  				Config:      testCloudInitConfig_misconfiguredParts,
    80  				ExpectError: regexp.MustCompile("Unable to parse parts in cloudinit resource declaration"),
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  var testCloudInitConfig_misconfiguredParts = `
    87  data "template_cloudinit_config" "foo" {
    88    part {
    89      content = ""
    90    }
    91  }
    92  `