-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathscenario_helpers_test.go
308 lines (275 loc) · 11.1 KB
/
scenario_helpers_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package e2e
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"testing"
"time"
aksnodeconfigv1 "github.com/Azure/agentbaker/aks-node-controller/pkg/gen/aksnodeconfig/v1"
"github.com/Azure/agentbaker/e2e/config"
"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
// it's important to share context between tests to allow graceful shutdown
// cancellation signal can be sent before a test starts, without shared context such test will miss the signal
var testCtx = setupSignalHandler()
// setupSignalHandler handles OS signals to gracefully shutdown the test suite
func setupSignalHandler() context.Context {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
red := func(text string) string {
return "\033[31m" + text + "\033[0m"
}
go func() {
// block until signal is received
<-ch
fmt.Println(red("Received cancellation signal, gracefully shutting down the test suite. Cancel again to force exit."))
cancel()
// block until second signal is received
<-ch
fmt.Println(red("Received second cancellation signal, forcing exit."))
os.Exit(1)
}()
return ctx
}
func newTestCtx(t *testing.T) context.Context {
if testCtx.Err() != nil {
t.Skip("test suite is shutting down")
}
ctx, cancel := context.WithTimeout(testCtx, config.Config.TestTimeout)
t.Cleanup(cancel)
return ctx
}
func TestMain(m *testing.M) {
log.Printf("using E2E environment configuration:\n%s\n", config.Config)
// clean up logs from previous run
if _, err := os.Stat("scenario-logs"); err == nil {
_ = os.RemoveAll("scenario-logs")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := ensureResourceGroup(ctx)
mustNoError(err)
_, err = config.Azure.CreateVMManagedIdentity(ctx)
mustNoError(err)
m.Run()
}
func mustNoError(err error) {
if err != nil {
panic(err)
}
}
func RunScenario(t *testing.T, s *Scenario) {
s.T = t
t.Parallel()
ctx := newTestCtx(t)
maybeSkipScenario(ctx, t, s)
cluster, err := s.Config.Cluster(ctx, s.T)
require.NoError(s.T, err)
// in some edge cases cluster cache is broken and nil cluster is returned
// need to find the root cause and fix it, this should help to catch such cases
require.NotNil(t, cluster)
s.Runtime = &ScenarioRuntime{
Cluster: cluster,
}
// use shorter timeout for faster feedback on test failures
ctx, cancel := context.WithTimeout(ctx, config.Config.TestTimeoutVMSS)
defer cancel()
prepareAKSNode(ctx, s)
validateVM(ctx, s)
}
func prepareAKSNode(ctx context.Context, s *Scenario) {
s.Runtime.VMSSName = generateVMSSName(s)
if (s.BootstrapConfigMutator == nil) == (s.AKSNodeConfigMutator == nil) {
s.T.Fatalf("exactly one of BootstrapConfigMutator or AKSNodeConfigMutator must be set")
}
nbc := getBaseNBC(s.T, s.Runtime.Cluster, s.VHD)
if s.VHD.OS == config.OSWindows {
nbc.ContainerService.Properties.WindowsProfile.CseScriptsPackageURL = windowsCSE(ctx, s.T)
}
if s.BootstrapConfigMutator != nil {
s.BootstrapConfigMutator(nbc)
s.Runtime.NBC = nbc
}
if s.AKSNodeConfigMutator != nil {
nodeconfig := nbcToAKSNodeConfigV1(nbc)
s.AKSNodeConfigMutator(nodeconfig)
s.Runtime.AKSNodeConfig = nodeconfig
}
var err error
s.Runtime.SSHKeyPrivate, s.Runtime.SSHKeyPublic, err = getNewRSAKeyPair()
publicKeyData := datamodel.PublicKey{KeyData: string(s.Runtime.SSHKeyPublic)}
// check it all.
if s.Runtime.NBC != nil && s.Runtime.NBC.ContainerService != nil && s.Runtime.NBC.ContainerService.Properties != nil && s.Runtime.NBC.ContainerService.Properties.LinuxProfile != nil {
if s.Runtime.NBC.ContainerService.Properties.LinuxProfile.SSH.PublicKeys == nil {
s.Runtime.NBC.ContainerService.Properties.LinuxProfile.SSH.PublicKeys = []datamodel.PublicKey{}
}
// Windows fetches SSH keys from the linux profile and replaces any existing SSH keys with these. So we have to set
// the Linux SSH keys for Windows SSH to work. Yeah. I find it odd too.
s.Runtime.NBC.ContainerService.Properties.LinuxProfile.SSH.PublicKeys = append(s.Runtime.NBC.ContainerService.Properties.LinuxProfile.SSH.PublicKeys, publicKeyData)
}
require.NoError(s.T, err)
createVMSS(ctx, s)
err = getCustomScriptExtensionStatus(ctx, s)
require.NoError(s.T, err)
s.T.Logf("vmss %s creation succeeded", s.Runtime.VMSSName)
s.Runtime.KubeNodeName = s.Runtime.Cluster.Kube.WaitUntilNodeReady(ctx, s.T, s.Runtime.VMSSName)
s.T.Logf("node %s is ready", s.Runtime.VMSSName)
s.Runtime.VMPrivateIP, err = getVMPrivateIPAddress(ctx, s)
require.NoError(s.T, err, "failed to get VM private IP address")
hostPod, err := s.Runtime.Cluster.Kube.GetHostNetworkDebugPod(ctx, s.T)
require.NoError(s.T, err, "failed to get host network debug pod name")
s.Runtime.DebugHostPod = hostPod.Name
}
func maybeSkipScenario(ctx context.Context, t *testing.T, s *Scenario) {
s.Tags.Name = t.Name()
s.Tags.OS = string(s.VHD.OS)
s.Tags.Arch = s.VHD.Arch
s.Tags.ImageName = s.VHD.Name
if config.Config.TagsToRun != "" {
matches, err := s.Tags.MatchesFilters(config.Config.TagsToRun)
if err != nil {
t.Fatalf("could not match tags for %q: %s", t.Name(), err)
}
if !matches {
t.Skipf("skipping scenario %q: scenario tags %+v does not match filter %q", t.Name(), s.Tags, config.Config.TagsToRun)
}
}
if config.Config.TagsToSkip != "" {
matches, err := s.Tags.MatchesAnyFilter(config.Config.TagsToSkip)
if err != nil {
t.Fatalf("could not match tags for %q: %s", t.Name(), err)
}
if matches {
t.Skipf("skipping scenario %q: scenario tags %+v matches filter %q", t.Name(), s.Tags, config.Config.TagsToSkip)
}
}
vhd, err := s.VHD.VHDResourceID(ctx, t)
if err != nil {
if config.Config.IgnoreScenariosWithMissingVHD && errors.Is(err, config.ErrNotFound) {
t.Skipf("skipping scenario %q: could not find image", t.Name())
} else {
t.Fatalf("could not find image for %q: %s", t.Name(), err)
}
}
t.Logf("VHD: %q, TAGS %+v", vhd, s.Tags)
}
func validateVM(ctx context.Context, s *Scenario) {
ValidatePodRunning(ctx, s)
// skip when outbound type is block as the wasm will create pod from gcr, however, network isolated cluster scenario will block egress traffic of gcr.
// TODO(xinhl): add another way to validate
if s.Runtime.NBC != nil && s.Runtime.NBC.AgentPoolProfile != nil && s.Runtime.NBC.AgentPoolProfile.WorkloadRuntime == datamodel.WasmWasi && s.Runtime.NBC.OutboundType != datamodel.OutboundTypeBlock && s.Runtime.NBC.OutboundType != datamodel.OutboundTypeNone {
ValidateWASM(ctx, s, s.Runtime.KubeNodeName)
}
if s.Runtime.AKSNodeConfig != nil && s.Runtime.AKSNodeConfig.WorkloadRuntime == aksnodeconfigv1.WorkloadRuntime_WORKLOAD_RUNTIME_WASM_WASI {
ValidateWASM(ctx, s, s.Runtime.KubeNodeName)
}
switch s.VHD.OS {
case config.OSWindows:
// TODO: validate something
default:
ValidateCommonLinux(ctx, s)
}
// test-specific validation
if s.Config.Validator != nil {
s.Config.Validator(ctx, s)
}
s.T.Log("validation succeeded")
}
func getExpectedPackageVersions(packageName, distro, release string) []string {
var expectedVersions []string
// since we control this json, we assume its going to be properly formatted here
jsonBytes, _ := os.ReadFile("../parts/linux/cloud-init/artifacts/components.json")
packages := gjson.GetBytes(jsonBytes, fmt.Sprintf("Packages.#(name=%s).downloadURIs", packageName))
for _, packageItem := range packages.Array() {
// check if versionsV2 exists
if packageItem.Get(fmt.Sprintf("%s.%s.versionsV2", distro, release)).Exists() {
versions := packageItem.Get(fmt.Sprintf("%s.%s.versionsV2", distro, release))
for _, version := range versions.Array() {
// get versions.latestVersion and append to expectedVersions
expectedVersions = append(expectedVersions, version.Get("latestVersion").String())
// get versions.previousLatestVersion (if exists) and append to expectedVersions
if version.Get("previousLatestVersion").Exists() {
expectedVersions = append(expectedVersions, version.Get("previousLatestVersion").String())
}
}
}
}
return expectedVersions
}
func getCustomScriptExtensionStatus(ctx context.Context, s *Scenario) error {
pager := config.Azure.VMSSVM.NewListPager(*s.Runtime.Cluster.Model.Properties.NodeResourceGroup, s.Runtime.VMSSName, nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return fmt.Errorf("failed to get VMSS instances: %v", err)
}
for _, vmInstance := range page.Value {
instanceViewResp, err := config.Azure.VMSSVM.GetInstanceView(ctx, *s.Runtime.Cluster.Model.Properties.NodeResourceGroup, s.Runtime.VMSSName, *vmInstance.InstanceID, nil)
if err != nil {
return fmt.Errorf("failed to get instance view for VM %s: %v", *vmInstance.InstanceID, err)
}
for _, extension := range instanceViewResp.Extensions {
for _, status := range extension.Statuses {
if s.VHD.OS == config.OSWindows {
if status.Code == nil || !strings.EqualFold(*status.Code, "ProvisioningState/succeeded") {
return fmt.Errorf("failed to get CSE output, error: %s", *status.Message)
}
return nil
} else {
resp, err := parseLinuxCSEMessage(*status)
if err != nil {
return fmt.Errorf("Parse CSE message with error, error %w", err)
}
if resp.ExitCode != "0" {
return fmt.Errorf("vmssCSE %s, output=%s, error=%s, cse output: %s", resp.ExitCode, resp.Output, resp.Error, *status.Message)
}
return nil
}
}
}
}
}
return fmt.Errorf("failed to get CSE output.")
}
func parseLinuxCSEMessage(status armcompute.InstanceViewStatus) (*datamodel.CSEStatus, error) {
if status.Code == nil || status.Message == nil {
return nil, datamodel.NewError(datamodel.InvalidCSEMessage, "No valid Status code or Message provided from cse extension")
}
start := strings.Index(*status.Message, "[stdout]") + len("[stdout]")
end := strings.Index(*status.Message, "[stderr]")
var linuxExtensionExitCodeStrRegex = regexp.MustCompile(linuxExtensionExitCodeStr)
var linuxExtensionErrorCodeRegex = regexp.MustCompile(extensionErrorCodeRegex)
extensionFailed := linuxExtensionErrorCodeRegex.MatchString(*status.Code)
if end <= start {
return nil, fmt.Errorf("Parse CSE failed with error cannot find [stdout] and [stderr], raw CSE Message: %s, delete vm: %t", *status.Message, extensionFailed)
}
rawInstanceViewInfo := (*status.Message)[start:end]
// Parse CSE message
var cseStatus datamodel.CSEStatus
err := json.Unmarshal([]byte(rawInstanceViewInfo), &cseStatus)
if err != nil {
exitCodeMatch := linuxExtensionExitCodeStrRegex.FindStringSubmatch(*status.Message)
if len(exitCodeMatch) > 1 && extensionFailed {
// Failed but the format is not expected.
cseStatus.ExitCode = exitCodeMatch[1]
cseStatus.Error = *status.Message
return &cseStatus, nil
}
return nil, fmt.Errorf("Parse CSE Json failed with error: %s, raw CSE Message: %s, delete vm: %t", err, *status.Message, extensionFailed)
}
if cseStatus.ExitCode == "" {
return nil, fmt.Errorf("CSE Json does not contain exit code, raw CSE Message: %s", *status.Message)
}
return &cseStatus, nil
}