forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
127 lines (119 loc) · 3.86 KB
/
validators.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
package scenario
import (
"fmt"
"strings"
)
func DirectoryValidator(path string, files []string) *LiveVMValidator {
return &LiveVMValidator{
Description: fmt.Sprintf("assert %s contents", path),
Command: fmt.Sprintf("ls -la %s", path),
Asserter: func(code, stdout, stderr string) error {
if code != "0" {
return fmt.Errorf("validator command terminated with exit code %q but expected code 0", code)
}
for _, file := range files {
if !strings.Contains(stdout, file) {
return fmt.Errorf(fmt.Sprintf("expected to find file %s within directory %s, but did not", file, path))
}
}
return nil
},
}
}
func SysctlConfigValidator(customSysctls map[string]string) *LiveVMValidator {
keysToCheck := make([]string, len(customSysctls))
for k, _ := range customSysctls {
keysToCheck = append(keysToCheck, k)
}
// regex used in sed command to remove extra spaces between two numerical values, used to verify correct values for
// sysctls that have string values, e.g. net.ipv4.ip_local_port_range, which would be printed with extra spaces
return &LiveVMValidator{
Description: "assert sysctl settings",
Command: fmt.Sprintf("sysctl %s | sed -E 's/([0-9])\\s+([0-9])/\\1 \\2/g'", strings.Join(keysToCheck, " ")),
Asserter: func(code, stdout, stderr string) error {
if code != "0" {
return fmt.Errorf("validator command terminated with exit code %q but expected code 0", code)
}
for name, value := range customSysctls {
if !strings.Contains(stdout, fmt.Sprintf("%s = %v", name, value)) {
return fmt.Errorf(fmt.Sprintf("expected to find %s set to %v, but was not", name, value))
}
}
return nil
},
}
}
func NvidiaSMINotInstalledValidator() *LiveVMValidator {
return &LiveVMValidator{
Description: "assert nvidia-smi is not installed",
Command: "nvidia-smi",
Asserter: func(code, stdout, stderr string) error {
if code != "1" {
return fmt.Errorf(
"nvidia-smi not installed should trigger exit 1, actual was: %q, stdout: %q, stderr: %q",
code,
stdout,
stderr,
)
}
if !strings.Contains(stderr, "nvidia-smi: command not found") {
return fmt.Errorf(
"expected stderr to contain 'nvidia-smi: command not found', actual: %q, stdout: %q",
stderr,
stdout,
)
}
return nil
},
}
}
func NvidiaSMIInstalledValidator() *LiveVMValidator {
return &LiveVMValidator{
Description: "assert nvidia-smi is installed",
Command: "nvidia-smi",
Asserter: func(code, stdout, stderr string) error {
if code != "0" {
return fmt.Errorf(
"nvidia-smi installed should trigger exit 0 actual was: %q, stdout: %q, stderr: %q",
code,
stdout,
stderr,
)
}
return nil
},
}
}
func NonEmptyDirectoryValidator(dirName string) *LiveVMValidator {
return &LiveVMValidator{
Description: "assert that there are files in directory",
Command: fmt.Sprintf("ls -1q %s | grep -q '^.*$' && true || false", dirName),
Asserter: func(code, stdout, stderr string) error {
if code != "0" {
return fmt.Errorf("expected to find a file in directory %s, but did not", dirName)
}
return nil
},
}
}
func UlimitValidator(ulimits map[string]string) *LiveVMValidator {
ulimitKeys := make([]string, 0, len(ulimits))
for k := range ulimits {
ulimitKeys = append(ulimitKeys, k)
}
return &LiveVMValidator{
Description: "assert ulimit settings",
Command: fmt.Sprintf("systemctl cat containerd.service | grep -E -i '%s'", strings.Join(ulimitKeys, "|")),
Asserter: func(code, stdout, stderr string) error {
if code != "0" {
return fmt.Errorf("validator command terminated with exit code %q but expected code 0", code)
}
for name, value := range ulimits {
if !strings.Contains(stdout, fmt.Sprintf("%s=%v", name, value)) {
return fmt.Errorf(fmt.Sprintf("expected to find %s set to %v, but was not", name, value))
}
}
return nil
},
}
}