~egtann/up

ref: e0094c2d396c40d9b217e16605545b83ed0985b4 up/cmd/up/up_test.go -rw-r--r-- 3.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main

import (
	"fmt"
	"log"
	"testing"

	"egt.run/up"
)

func TestMakeBatches(t *testing.T) {
	t.Parallel()
	tcs := []struct {
		serial int
		have   map[up.InvName][]string
		want   batch
	}{
		{
			serial: 1,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
			},
			want: batch{
				"srv1": [][]string{{"a"}, {"b"}, {"c"}},
			},
		},
		{
			serial: 3,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b", "c"}},
				"srv2": [][]string{{"d", "e"}},
			},
		},
		{
			serial: 0,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b", "c"}},
				"srv2": [][]string{{"d", "e"}},
			},
		},
		{
			serial: 2,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e", "f", "g"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b"}, {"c"}},
				"srv2": [][]string{{"d", "e"}, {"f", "g"}},
			},
		},
		{
			serial: 3,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e", "f", "g"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b", "c"}},
				"srv2": [][]string{{"d", "e", "f"}, {"g"}},
			},
		},
		{
			serial: 10,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e", "f", "g"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b", "c"}},
				"srv2": [][]string{{"d", "e", "f", "g"}},
			},
		},
		{
			serial: 2,
			have: map[up.InvName][]string{
				"srv1": []string{"a", "b", "c"},
				"srv2": []string{"d", "e", "f", "g"},
				"srv3": []string{"d", "e"},
				"srv4": []string{"h", "j"},
				"srv5": []string{"k", "i"},
				"srv6": []string{"l", "m", "n"},
				"srv7": []string{"o"},
				"srv8": []string{"p", "q", "r", "s", "t", "u", "v"},
			},
			want: batch{
				"srv1": [][]string{{"a", "b"}, {"c"}},
				"srv2": [][]string{{"d", "e"}, {"f", "g"}},
				"srv3": [][]string{{"d", "e"}},
				"srv4": [][]string{{"h", "j"}},
				"srv5": [][]string{{"k", "i"}},
				"srv6": [][]string{{"l", "m"}, {"n"}},
				"srv7": [][]string{{"o"}},
				"srv8": [][]string{{"p", "q"}, {"r", "s"}, {"t", "u"}, {"v"}},
			},
		},
	}
	for i, tc := range tcs {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			conf := &up.Config{Inventory: tc.have}
			batches, err := makeBatches(conf, tc.serial)
			if err != nil {
				t.Fatal(err)
			}
			for typ, ipgroups := range batches {
				wantgroups := tc.want[typ]
				if !sliceDeepEq(wantgroups, ipgroups) {
					log.Printf("%+v\n", batches)
					t.Fatalf("expected %+v, got %+v",
						tc.want[typ], ipgroups)
				}
			}
		})
	}
}

// sliceDeepEq compares nested slice equality without caring about order.
func sliceDeepEq(a, b [][]string) bool {
	if len(a) != len(b) {
		return false
	}
	count := 0
	seen := map[string]struct{}{}
	for i, vs := range a {
		if len(vs) != len(b[i]) {
			return false
		}
		for _, v := range vs {
			count++
			seen[v] = struct{}{}
		}
	}
	for _, vs := range b {
		for _, v := range vs {
			seen[v] = struct{}{}
		}
	}
	return len(seen) == count
}