This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main_test.go
162 lines (142 loc) · 5.23 KB
/
main_test.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"errors"
"fmt"
"github.com/fatih/color"
"os"
"path/filepath"
"testing"
)
/* First key: filename, second key: step num
data is an array of expected indices, or [-1]*(expected number)*/
var expected map[string]map[int][]int
func visitBad(path string, f os.FileInfo, pathErr error) error {
if pathErr != nil {
return pathErr
}
if f.IsDir() {
return nil /* skip dir, not a test file */
}
test, err := loadTest(path, newTestConfig())
if err != nil {
return err
}
/* This should always error */
subsetPartition, err := partition(test.Config)
if err != nil {
return nil /* Some tests have bad subset partitions specified in configs */
}
if err := validate(test, subsetPartition); err == nil {
return errors.New(fmt.Sprintf("Bad input file %s incorrectly validates", path))
}
return nil
}
func TestBadSelectionFiles(t *testing.T) {
testDir := "test_tests/selection_framework/failtests"
err := filepath.Walk(testDir, visitBad)
if err != nil {
t.Error(err.Error())
}
}
func visit(path string, f os.FileInfo, pathErr error) error {
if pathErr != nil {
return pathErr
}
if f.IsDir() {
return nil /* skip dir, not a test file */
}
test, err := loadTest(path, newTestConfig())
if err != nil {
return err
}
/* This should validate correctly */
subsetPartition, err := partition(test.Config)
if err != nil {
return err
}
err = validate(test, subsetPartition)
if err != nil {
return err
}
/* Run through steps and check that indices are as expected */
color.Cyan("!!! Running test file %s", path)
for i, step := range test.Steps {
expectedIndices := expected[path][i]
actualIndices := selectNodes(step, test.Config, subsetPartition)
color.Blue("### Running step %s on nodes %v", step.Name, actualIndices)
color.Cyan("### Expecting nodes %v", expectedIndices)
if len(actualIndices) == 0 {
return errors.New(fmt.Sprintf("Test %s step %d not running on any nodes", path, i))
}
if expectedIndices[0] < 0 { /* Indicates random selection and value doesn't matter */
if len(actualIndices) != len(expectedIndices) {
return errors.New(fmt.Sprintf("Test %s step %d running on the wrong number of nodes", path, i))
}
} else { /* Equality should be exact */
if !slicesEqual(actualIndices, expectedIndices) {
return errors.New(fmt.Sprintf("Test %s step %d running on the wrong nodes", path, i))
}
}
}
return nil
}
func TestValidSelectionFiles(t *testing.T) {
testDir := "test_tests/selection_framework/succeedtests"
/* Set up the map of expected indices for comparison */
expected = make(map[string]map[int][]int)
expected[testDir+"/old_range.yml"] = make(map[int][]int)
expected[testDir+"/old_range.yml"][0] = []int{2, 3, 4}
expected[testDir+"/percent.yml"] = make(map[int][]int)
expected[testDir+"/percent.yml"][0] = []int{1, 2}
expected[testDir+"/percent.yml"][1] = []int{3, 4}
expected[testDir+"/percent.yml"][2] = []int{-1, -1, -1}
expected[testDir+"/percent.yml"][3] = []int{-1, -1}
expected[testDir+"/rand_even_subset.yml"] = make(map[int][]int)
expected[testDir+"/rand_even_subset.yml"][0] = []int{-1, -1, -1}
expected[testDir+"/rand_even_subset.yml"][1] = []int{-1, -1, -1}
expected[testDir+"/rand_weighted_subset.yml"] = make(map[int][]int)
expected[testDir+"/rand_weighted_subset.yml"][0] = []int{-1, -1}
expected[testDir+"/rand_weighted_subset.yml"][1] = []int{-1, -1}
expected[testDir+"/rand_weighted_subset.yml"][2] = []int{-1}
expected[testDir+"/range.yml"] = make(map[int][]int)
expected[testDir+"/range.yml"][0] = []int{2, 3, 4}
expected[testDir+"/range.yml"][1] = []int{-1, -1, -1}
expected[testDir+"/range.yml"][2] = []int{-1, -1, -1, -1}
expected[testDir+"/seq_even_subset.yml"] = make(map[int][]int)
expected[testDir+"/seq_even_subset.yml"][0] = []int{1, 3, 5}
expected[testDir+"/seq_weighted_subset.yml"] = make(map[int][]int)
expected[testDir+"/seq_weighted_subset.yml"][0] = []int{1, 2}
expected[testDir+"/seq_weighted_subset.yml"][1] = []int{1, 2, 3, 4}
expected[testDir+"/seq_weighted_subset.yml"][2] = []int{5}
expected[testDir+"/seq_weighted_subset_trunc.yml"] = make(map[int][]int)
expected[testDir+"/seq_weighted_subset_trunc.yml"][0] = []int{1, 2, 3}
expected[testDir+"/seq_weighted_subset_trunc.yml"][1] = []int{1, 2, 3, 4}
expected[testDir+"/seq_weighted_subset_trunc.yml"][2] = []int{5}
expected[testDir+"/range_subset.yml"] = make(map[int][]int)
expected[testDir+"/range_subset.yml"][0] = []int{-1, -1, -1}
expected[testDir+"/range_subset.yml"][1] = []int{5}
expected[testDir+"/range_subset.yml"][2] = []int{2, 4}
expected[testDir+"/range_subset.yml"][3] = []int{1, 2, 3, 4}
expected[testDir+"/range_subset.yml"][4] = []int{-1, -1, -1, -1}
expected[testDir+"/range_subset.yml"][5] = []int{1, 3, 5}
expected[testDir+"/percent_subset.yml"] = make(map[int][]int)
expected[testDir+"/percent_subset.yml"][0] = []int{-1, -1, -1, -1, -1}
expected[testDir+"/percent_subset.yml"][1] = []int{3, 4}
expected[testDir+"/percent_subset.yml"][2] = []int{-1, -1}
expected[testDir+"/percent_subset.yml"][3] = []int{-1}
err := filepath.Walk(testDir, visit)
if err != nil {
t.Error(err.Error())
}
}
func slicesEqual(s1, s2 []int) bool {
if len(s1) != len(s2) {
return false
}
for j := 0; j < len(s1); j++ {
if s1[j] != s2[j] {
return false
}
}
return true
}