github.com/dacamp/packer@v0.10.2/template/parse_test.go (about)

     1  package template
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestParse(t *testing.T) {
    12  	cases := []struct {
    13  		File   string
    14  		Result *Template
    15  		Err    bool
    16  	}{
    17  		/*
    18  		 * Builders
    19  		 */
    20  		{
    21  			"parse-basic.json",
    22  			&Template{
    23  				Builders: map[string]*Builder{
    24  					"something": &Builder{
    25  						Name: "something",
    26  						Type: "something",
    27  					},
    28  				},
    29  			},
    30  			false,
    31  		},
    32  		{
    33  			"parse-builder-no-type.json",
    34  			nil,
    35  			true,
    36  		},
    37  		{
    38  			"parse-builder-repeat.json",
    39  			nil,
    40  			true,
    41  		},
    42  
    43  		/*
    44  		 * Provisioners
    45  		 */
    46  		{
    47  			"parse-provisioner-basic.json",
    48  			&Template{
    49  				Provisioners: []*Provisioner{
    50  					&Provisioner{
    51  						Type: "something",
    52  					},
    53  				},
    54  			},
    55  			false,
    56  		},
    57  
    58  		{
    59  			"parse-provisioner-pause-before.json",
    60  			&Template{
    61  				Provisioners: []*Provisioner{
    62  					&Provisioner{
    63  						Type:        "something",
    64  						PauseBefore: 1 * time.Second,
    65  					},
    66  				},
    67  			},
    68  			false,
    69  		},
    70  
    71  		{
    72  			"parse-provisioner-only.json",
    73  			&Template{
    74  				Provisioners: []*Provisioner{
    75  					&Provisioner{
    76  						Type: "something",
    77  						OnlyExcept: OnlyExcept{
    78  							Only: []string{"foo"},
    79  						},
    80  					},
    81  				},
    82  			},
    83  			false,
    84  		},
    85  
    86  		{
    87  			"parse-provisioner-except.json",
    88  			&Template{
    89  				Provisioners: []*Provisioner{
    90  					&Provisioner{
    91  						Type: "something",
    92  						OnlyExcept: OnlyExcept{
    93  							Except: []string{"foo"},
    94  						},
    95  					},
    96  				},
    97  			},
    98  			false,
    99  		},
   100  
   101  		{
   102  			"parse-provisioner-override.json",
   103  			&Template{
   104  				Provisioners: []*Provisioner{
   105  					&Provisioner{
   106  						Type: "something",
   107  						Override: map[string]interface{}{
   108  							"foo": map[string]interface{}{},
   109  						},
   110  					},
   111  				},
   112  			},
   113  			false,
   114  		},
   115  
   116  		{
   117  			"parse-provisioner-no-type.json",
   118  			nil,
   119  			true,
   120  		},
   121  
   122  		{
   123  			"parse-variable-default.json",
   124  			&Template{
   125  				Variables: map[string]*Variable{
   126  					"foo": &Variable{
   127  						Default: "foo",
   128  					},
   129  				},
   130  			},
   131  			false,
   132  		},
   133  
   134  		{
   135  			"parse-variable-required.json",
   136  			&Template{
   137  				Variables: map[string]*Variable{
   138  					"foo": &Variable{
   139  						Required: true,
   140  					},
   141  				},
   142  			},
   143  			false,
   144  		},
   145  
   146  		{
   147  			"parse-pp-basic.json",
   148  			&Template{
   149  				PostProcessors: [][]*PostProcessor{
   150  					[]*PostProcessor{
   151  						&PostProcessor{
   152  							Type: "foo",
   153  							Config: map[string]interface{}{
   154  								"foo": "bar",
   155  							},
   156  						},
   157  					},
   158  				},
   159  			},
   160  			false,
   161  		},
   162  
   163  		{
   164  			"parse-pp-keep.json",
   165  			&Template{
   166  				PostProcessors: [][]*PostProcessor{
   167  					[]*PostProcessor{
   168  						&PostProcessor{
   169  							Type:              "foo",
   170  							KeepInputArtifact: true,
   171  						},
   172  					},
   173  				},
   174  			},
   175  			false,
   176  		},
   177  
   178  		{
   179  			"parse-pp-only.json",
   180  			&Template{
   181  				PostProcessors: [][]*PostProcessor{
   182  					[]*PostProcessor{
   183  						&PostProcessor{
   184  							Type: "foo",
   185  							OnlyExcept: OnlyExcept{
   186  								Only: []string{"bar"},
   187  							},
   188  						},
   189  					},
   190  				},
   191  			},
   192  			false,
   193  		},
   194  
   195  		{
   196  			"parse-pp-except.json",
   197  			&Template{
   198  				PostProcessors: [][]*PostProcessor{
   199  					[]*PostProcessor{
   200  						&PostProcessor{
   201  							Type: "foo",
   202  							OnlyExcept: OnlyExcept{
   203  								Except: []string{"bar"},
   204  							},
   205  						},
   206  					},
   207  				},
   208  			},
   209  			false,
   210  		},
   211  
   212  		{
   213  			"parse-pp-string.json",
   214  			&Template{
   215  				PostProcessors: [][]*PostProcessor{
   216  					[]*PostProcessor{
   217  						&PostProcessor{
   218  							Type: "foo",
   219  						},
   220  					},
   221  				},
   222  			},
   223  			false,
   224  		},
   225  
   226  		{
   227  			"parse-pp-map.json",
   228  			&Template{
   229  				PostProcessors: [][]*PostProcessor{
   230  					[]*PostProcessor{
   231  						&PostProcessor{
   232  							Type: "foo",
   233  						},
   234  					},
   235  				},
   236  			},
   237  			false,
   238  		},
   239  
   240  		{
   241  			"parse-pp-slice.json",
   242  			&Template{
   243  				PostProcessors: [][]*PostProcessor{
   244  					[]*PostProcessor{
   245  						&PostProcessor{
   246  							Type: "foo",
   247  						},
   248  					},
   249  					[]*PostProcessor{
   250  						&PostProcessor{
   251  							Type: "bar",
   252  						},
   253  					},
   254  				},
   255  			},
   256  			false,
   257  		},
   258  
   259  		{
   260  			"parse-pp-multi.json",
   261  			&Template{
   262  				PostProcessors: [][]*PostProcessor{
   263  					[]*PostProcessor{
   264  						&PostProcessor{
   265  							Type: "foo",
   266  						},
   267  						&PostProcessor{
   268  							Type: "bar",
   269  						},
   270  					},
   271  				},
   272  			},
   273  			false,
   274  		},
   275  
   276  		{
   277  			"parse-pp-no-type.json",
   278  			nil,
   279  			true,
   280  		},
   281  
   282  		{
   283  			"parse-description.json",
   284  			&Template{
   285  				Description: "foo",
   286  			},
   287  			false,
   288  		},
   289  
   290  		{
   291  			"parse-min-version.json",
   292  			&Template{
   293  				MinVersion: "1.2",
   294  			},
   295  			false,
   296  		},
   297  
   298  		{
   299  			"parse-push.json",
   300  			&Template{
   301  				Push: Push{
   302  					Name: "foo",
   303  				},
   304  			},
   305  			false,
   306  		},
   307  
   308  		{
   309  			"parse-comment.json",
   310  			&Template{
   311  				Builders: map[string]*Builder{
   312  					"something": &Builder{
   313  						Name: "something",
   314  						Type: "something",
   315  					},
   316  				},
   317  			},
   318  			false,
   319  		},
   320  	}
   321  
   322  	for _, tc := range cases {
   323  		path, _ := filepath.Abs(fixtureDir(tc.File))
   324  		tpl, err := ParseFile(fixtureDir(tc.File))
   325  		if (err != nil) != tc.Err {
   326  			t.Fatalf("err: %s", err)
   327  		}
   328  
   329  		if tc.Result != nil {
   330  			tc.Result.Path = path
   331  		}
   332  		if tpl != nil {
   333  			tpl.RawContents = nil
   334  		}
   335  		if !reflect.DeepEqual(tpl, tc.Result) {
   336  			t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result)
   337  		}
   338  	}
   339  }
   340  
   341  func TestParse_contents(t *testing.T) {
   342  	tpl, err := ParseFile(fixtureDir("parse-contents.json"))
   343  	if err != nil {
   344  		t.Fatalf("err: %s", err)
   345  	}
   346  
   347  	actual := strings.TrimSpace(string(tpl.RawContents))
   348  	expected := `{"builders":[{"type":"test"}]}`
   349  	if actual != expected {
   350  		t.Fatalf("bad: %s\n\n%s", actual, expected)
   351  	}
   352  }
   353  
   354  func TestParse_bad(t *testing.T) {
   355  	cases := []struct {
   356  		File     string
   357  		Expected string
   358  	}{
   359  		{"error-beginning.json", "line 1, column 1 (offset 1)"},
   360  		{"error-middle.json", "line 5, column 6 (offset 50)"},
   361  		{"error-end.json", "line 1, column 30 (offset 30)"},
   362  	}
   363  	for _, tc := range cases {
   364  		_, err := ParseFile(fixtureDir(tc.File))
   365  		if err == nil {
   366  			t.Fatalf("expected error")
   367  		}
   368  		if !strings.Contains(err.Error(), tc.Expected) {
   369  			t.Fatalf("file: %s\nExpected: %s\n%s\n", tc.File, tc.Expected, err.Error())
   370  		}
   371  	}
   372  }