-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlabels.go
More file actions
112 lines (100 loc) · 2.49 KB
/
labels.go
File metadata and controls
112 lines (100 loc) · 2.49 KB
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
package local
import (
"fmt"
"strconv"
"strings"
"github.com/linuxkit/rtf/sysinfo"
)
// ParseLabels constucts a map[string]bool for both positive and negative labels from a comma separated list
func ParseLabels(labels string) (map[string]bool, map[string]bool) {
set := make(map[string]bool)
unSet := make(map[string]bool)
if labels == "" {
return set, unSet
}
l := strings.Split(labels, ",")
for _, s := range l {
if strings.HasPrefix(s, "!") {
unSet[s[1:]] = true
} else {
set[s] = true
}
}
return set, unSet
}
// CheckLabel determines if a group or test should run based on its labels and the RunConfig
func CheckLabel(labels, notLabels map[string]bool, config RunConfig) bool {
// 1. If test has labels
if len(labels) > 0 {
// 2. Check that at least one test label is in the hostLabels
matches := 0
for l := range labels {
if _, ok := config.Labels[l]; ok {
matches++
}
}
if matches == 0 {
return false
}
}
// 3. Check every test notLabel is NOT in the hostLabels
for l := range notLabels {
if _, ok := config.Labels[l]; ok {
return false
}
}
// 4. Check that none of the test labels appear in the hostNotLabels
for l := range config.NotLabels {
if _, ok := labels[l]; ok {
return false
}
}
return true
}
func makeLabelString(labels map[string]bool, notLabels map[string]bool, sep string) string {
var l []string
for s := range notLabels {
l = append(l, fmt.Sprintf("!%s", s))
}
for s := range labels {
l = append(l, s)
}
return strings.Join(l, sep)
}
func getNameAndOrder(path string) (int, string) {
parts := strings.SplitN(path, "_", 2)
if len(parts) < 2 {
return 0, path
}
order, _ := strconv.Atoi(parts[0])
return order, parts[1]
}
func applySystemLabels(labels string) (map[string]bool, map[string]bool) {
systemInfo := sysinfo.GetSystemInfo()
l, nl := ParseLabels(labels)
for _, v := range systemInfo.List() {
if _, ok := l[v]; !ok {
l[v] = true
}
}
return l, nl
}
// NewRunConfig returns a new RunConfig from test labels and a pattern
func NewRunConfig(labels string, pattern string) RunConfig {
matchedLabels, notLabels := applySystemLabels(labels)
return RunConfig{
TestPattern: pattern,
Labels: matchedLabels,
NotLabels: notLabels,
}
}
// ValidatePattern validates that an arg string is a valid test pattern
func ValidatePattern(args []string) (string, error) {
if len(args) > 1 {
return "", fmt.Errorf("expected only one test pattern")
}
if len(args) == 0 {
return "", nil
}
return args[0], nil
}