~egtann/up

ref: e0094c2d396c40d9b217e16605545b83ed0985b4 up/parser_test.go -rw-r--r-- 1.4 KiB
e0094c2d — Evan Tann update license to openbsd isc 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package up

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"path/filepath"
	"testing"
)

func TestParse(t *testing.T) {
	t.Parallel()
	tests := []struct {
		haveFile string
		want     *Config
		wantErr  bool
	}{
		{haveFile: "empty", wantErr: true},
		{haveFile: "dupe_inventory", wantErr: true},
		{haveFile: "invalid_inventory", wantErr: true},
		{haveFile: "two_inventory_groups", want: &Config{
			Inventory: map[InvName][]string{
				"production": []string{"1.1.1.1"},
				"staging":    []string{"www.example.com", "1.1.1.2"},
			},
			Commands: map[CmdName]*Cmd{
				"deploy": &Cmd{
					ExecIfs: []CmdName{"if1"},
					Execs:   []string{"echo 'hello world'"},
				},
				"if1": &Cmd{Execs: []string{"echo 'if1'"}},
			},
			DefaultCommand:     "deploy",
			DefaultEnvironment: "production",
		}},
	}
	for _, tc := range tests {
		t.Run(tc.haveFile, func(t *testing.T) {
			pth := filepath.Join("testdata", tc.haveFile)
			byt, err := ioutil.ReadFile(pth)
			if err != nil {
				t.Fatal(err)
			}
			rdr := bytes.NewReader(byt)
			conf, err := Parse(rdr)
			if err != nil {
				if tc.wantErr {
					return
				}
				t.Fatal(err)
			}
			byt, err = json.Marshal(conf)
			if err != nil {
				t.Fatal(err)
			}
			got := string(byt)
			byt, err = json.Marshal(tc.want)
			if err != nil {
				t.Fatal(err)
			}
			want := string(byt)
			if got != want {
				t.Fatalf("expected: %s\ngot: %s", want, got)
			}
		})
	}
}