forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuite_test.go
More file actions
209 lines (170 loc) · 5.57 KB
/
suite_test.go
File metadata and controls
209 lines (170 loc) · 5.57 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
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
package e2e_test
import (
"context"
"flag"
"fmt"
mrand "math/rand"
"path/filepath"
"testing"
"time"
"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/agentbakere2e/scenario"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
"github.com/barkimedes/go-deepcopy"
)
var e2eMode string
func init() {
flag.StringVar(&e2eMode, "e2eMode", "", "specify mode for e2e tests - 'coverage' or 'validation' - default: 'validation'")
}
func Test_All(t *testing.T) {
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
ctx := context.Background()
t.Parallel()
suiteConfig, err := newSuiteConfig()
if err != nil {
t.Fatal(err)
}
if err := createE2ELoggingDir(); err != nil {
t.Fatal(err)
}
scenarioTable := scenario.InitScenarioTable(t, suiteConfig.scenariosToRun)
cloud, err := newAzureClient(suiteConfig.subscription)
if err != nil {
t.Fatal(err)
}
if err := ensureResourceGroup(ctx, t, cloud, suiteConfig.resourceGroupName); err != nil {
t.Fatal(err)
}
clusters, err := listClusters(ctx, t, cloud, suiteConfig.resourceGroupName)
if err != nil {
t.Fatal(err)
}
paramCache := paramCache{}
for _, scenario := range scenarioTable {
scenario := scenario
kube, cluster, clusterParams, subnetID := mustChooseCluster(ctx, t, r, cloud, suiteConfig, scenario, &clusters, paramCache)
clusterName := *cluster.Name
t.Logf("chose cluster: %q", clusterName)
baseConfig, err := getBaseNodeBootstrappingConfiguration(ctx, t, cloud, suiteConfig, clusterParams)
if err != nil {
t.Fatal(err)
}
copied, err := deepcopy.Anything(baseConfig)
if err != nil {
t.Error(err)
continue
}
nbc := copied.(*datamodel.NodeBootstrappingConfiguration)
if scenario.Config.BootstrapConfigMutator != nil {
scenario.Config.BootstrapConfigMutator(nbc)
}
t.Run(scenario.Name, func(t *testing.T) {
t.Parallel()
caseLogsDir, err := createVMLogsDir(scenario.Name)
if err != nil {
t.Fatal(err)
}
opts := &scenarioRunOpts{
cloud: cloud,
kube: kube,
suiteConfig: suiteConfig,
scenario: scenario,
chosenCluster: cluster,
nbc: nbc,
subnetID: subnetID,
loggingDir: caseLogsDir,
}
runScenario(ctx, t, r, opts)
})
}
}
func runScenario(ctx context.Context, t *testing.T, r *mrand.Rand, opts *scenarioRunOpts) {
privateKeyBytes, publicKeyBytes, err := getNewRSAKeyPair(r)
if err != nil {
t.Error(err)
return
}
vmssName, vmssModel, cleanupVMSS, err := bootstrapVMSS(ctx, t, r, opts, publicKeyBytes)
if cleanupVMSS != nil {
defer cleanupVMSS()
}
isCSEError := isVMExtensionProvisioningError(err)
vmssSucceeded := true
if err != nil {
vmssSucceeded = false
if !isCSEError {
t.Fatal("Encountered an unknown error while creating VM:", err)
}
t.Log("VM was unable to be provisioned due to a CSE error, will still atempt to extract provisioning logs...")
}
if vmssModel != nil {
if err := writeToFile(filepath.Join(opts.loggingDir, "vmssId.txt"), *vmssModel.ID); err != nil {
t.Fatal("failed to write vmss resource ID to disk", err)
}
}
// Perform posthoc log extraction when the VMSS creation succeeded or failed due to a CSE error
if vmssSucceeded || isCSEError {
debug := func() {
err := pollExtractVMLogs(ctx, t, vmssName, privateKeyBytes, opts)
if err != nil {
t.Fatal(err)
}
}
defer debug()
}
// Only perform node readiness/pod-related checks when VMSS creation succeeded
if vmssSucceeded {
t.Log("vmss creation succeded, proceeding with node readiness and pod checks...")
if err = validateNodeHealth(ctx, t, opts.kube, vmssName); err != nil {
t.Fatal(err)
}
t.Logf("node is ready, proceeding with validation commands...")
err := runLiveVMValidators(ctx, t, *vmssModel.Name, string(privateKeyBytes), opts)
if err != nil {
t.Fatalf("VM validation failed: %s", err)
}
t.Log("node bootstrapping succeeded!")
}
}
func bootstrapVMSS(ctx context.Context, t *testing.T, r *mrand.Rand, opts *scenarioRunOpts, publicKeyBytes []byte) (string, *armcompute.VirtualMachineScaleSet, func(), error) {
nodeBootstrappingFn := getNodeBootstrappingFn(e2eMode)
nodeBootstrapping, err := nodeBootstrappingFn(ctx, opts.nbc)
if err != nil {
return "", nil, nil, fmt.Errorf("unable to get node bootstrapping: %w", err)
}
vmssName := fmt.Sprintf("abtest%s", randomLowercaseString(r, 4))
t.Logf("vmss name: %q", vmssName)
cleanupVMSS := func() {
t.Log("deleting vmss", vmssName)
poller, err := opts.cloud.vmssClient.BeginDelete(ctx, *opts.chosenCluster.Properties.NodeResourceGroup, vmssName, nil)
if err != nil {
t.Error("error deleting vmss", vmssName, err)
return
}
_, err = poller.PollUntilDone(ctx, nil)
if err != nil {
t.Error("error polling deleting vmss", vmssName, err)
}
t.Logf("finished deleting vmss %q", vmssName)
}
vmssModel, err := createVMSSWithPayload(ctx, nodeBootstrapping.CustomData, nodeBootstrapping.CSE, vmssName, publicKeyBytes, opts)
if err != nil {
return "", nil, nil, fmt.Errorf("unable to create VMSS with payload: %w", err)
}
return vmssName, vmssModel, cleanupVMSS, nil
}
func validateNodeHealth(ctx context.Context, t *testing.T, kube *kubeclient, vmssName string) error {
nodeName, err := waitUntilNodeReady(ctx, kube, vmssName)
if err != nil {
return fmt.Errorf("error waiting for node ready: %w", err)
}
err = ensureTestNginxPod(ctx, kube, nodeName)
if err != nil {
return fmt.Errorf("error waiting for pod ready: %w", err)
}
err = waitUntilPodDeleted(ctx, kube, nodeName)
if err != nil {
return fmt.Errorf("error waiting pod deleted: %w", err)
}
return nil
}