github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/template/template_test.go (about)

     1  package template
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  const FixturesDir = "./test-fixtures"
    10  
    11  // fixtureDir returns the path to a test fixtures directory
    12  func fixtureDir(n string) string {
    13  	return filepath.Join(FixturesDir, n)
    14  }
    15  
    16  func TestTemplateValidate(t *testing.T) {
    17  	cases := []struct {
    18  		File string
    19  		Err  bool
    20  	}{
    21  		{
    22  			"validate-no-builders.json",
    23  			true,
    24  		},
    25  
    26  		{
    27  			"validate-bad-override.json",
    28  			true,
    29  		},
    30  
    31  		{
    32  			"validate-good-override.json",
    33  			false,
    34  		},
    35  
    36  		{
    37  			"validate-bad-prov-only.json",
    38  			true,
    39  		},
    40  
    41  		{
    42  			"validate-good-prov-only.json",
    43  			false,
    44  		},
    45  
    46  		{
    47  			"validate-bad-prov-except.json",
    48  			true,
    49  		},
    50  
    51  		{
    52  			"validate-good-prov-except.json",
    53  			false,
    54  		},
    55  
    56  		{
    57  			"validate-bad-pp-only.json",
    58  			true,
    59  		},
    60  
    61  		{
    62  			"validate-good-pp-only.json",
    63  			false,
    64  		},
    65  
    66  		{
    67  			"validate-bad-pp-except.json",
    68  			true,
    69  		},
    70  
    71  		{
    72  			"validate-good-pp-except.json",
    73  			false,
    74  		},
    75  	}
    76  
    77  	for _, tc := range cases {
    78  		f, err := os.Open(fixtureDir(tc.File))
    79  		if err != nil {
    80  			t.Fatalf("err: %s", err)
    81  		}
    82  
    83  		tpl, err := Parse(f)
    84  		f.Close()
    85  		if err != nil {
    86  			t.Fatalf("err: %s\n\n%s", tc.File, err)
    87  		}
    88  
    89  		err = tpl.Validate()
    90  		if (err != nil) != tc.Err {
    91  			t.Fatalf("err: %s\n\n%s", tc.File, err)
    92  		}
    93  	}
    94  }
    95  
    96  func TestOnlyExceptSkip(t *testing.T) {
    97  	cases := []struct {
    98  		Only, Except []string
    99  		Input        string
   100  		Result       bool
   101  	}{
   102  		{
   103  			[]string{"foo"},
   104  			nil,
   105  			"foo",
   106  			false,
   107  		},
   108  
   109  		{
   110  			nil,
   111  			[]string{"foo"},
   112  			"foo",
   113  			true,
   114  		},
   115  
   116  		{
   117  			nil,
   118  			nil,
   119  			"foo",
   120  			false,
   121  		},
   122  	}
   123  
   124  	for _, tc := range cases {
   125  		oe := &OnlyExcept{
   126  			Only:   tc.Only,
   127  			Except: tc.Except,
   128  		}
   129  
   130  		actual := oe.Skip(tc.Input)
   131  		if actual != tc.Result {
   132  			t.Fatalf(
   133  				"bad: %#v\n\n%#v\n\n%#v\n\n%#v",
   134  				actual, tc.Only, tc.Except, tc.Input)
   135  		}
   136  	}
   137  }