-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_e2e_test.go
209 lines (178 loc) · 7.63 KB
/
setup_e2e_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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tests
import (
"context"
"flag"
"fmt"
"math/rand"
"strings"
"testing"
"time"
cloudkms "cloud.google.com/go/kms/apiv1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
computealpha "google.golang.org/api/compute/v0.alpha"
computebeta "google.golang.org/api/compute/v0.beta"
compute "google.golang.org/api/compute/v1"
"k8s.io/klog/v2"
"k8s.io/utils/strings/slices"
testutils "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/remote"
)
var (
project = flag.String("project", "", "Project to run tests in")
serviceAccount = flag.String("service-account", "", "Service account to bring up instance with")
vmNamePrefix = flag.String("vm-name-prefix", "gce-pd-csi-e2e", "VM name prefix")
architecture = flag.String("arch", "amd64", "Architecture pd csi driver build on")
minCpuPlatform = flag.String("min-cpu-platform", "rome", "Minimum CPU architecture")
mwMinCpuPlatform = flag.String("min-cpu-platform-mw", "sapphirerapids", "Minimum CPU architecture for multiwriter tests")
zones = flag.String("zones", "us-east4-a,us-east4-c", "Zones to run tests in. If there are multiple zones, separate each by comma")
machineType = flag.String("machine-type", "n2d-standard-4", "Type of machine to provision instance on")
// Multi-writer is only supported on M3, C3, and N4
// https://cloud.google.com/compute/docs/disks/sharing-disks-between-vms#hd-multi-writer
mwMachineType = flag.String("mw-machine-type", "c3-standard-4", "Type of machine to provision instance for multiwriter tests")
imageURL = flag.String("image-url", "projects/ubuntu-os-cloud/global/images/family/ubuntu-minimal-2404-lts-amd64", "OS image url to get image from")
runInProw = flag.Bool("run-in-prow", false, "If true, use a Boskos loaned project and special CI service accounts and ssh keys")
deleteInstances = flag.Bool("delete-instances", false, "Delete the instances after tests run")
cloudtopHost = flag.Bool("cloudtop-host", false, "The local host is cloudtop, a kind of googler machine with special requirements to access GCP")
extraDriverFlags = flag.String("extra-driver-flags", "", "Extra flags to pass to the driver")
enableConfidentialCompute = flag.Bool("enable-confidential-compute", false, "Create VMs with confidential compute mode. This uses NVMe devices")
testContexts = []*remote.TestContext{}
multiWriterTestContexts = []*remote.TestContext{}
computeService *compute.Service
computeAlphaService *computealpha.Service
computeBetaService *computebeta.Service
kmsClient *cloudkms.KeyManagementClient
)
func init() {
klog.InitFlags(flag.CommandLine)
}
func TestE2E(t *testing.T) {
flag.Parse()
RegisterFailHandler(Fail)
RunSpecs(t, "Google Compute Engine Persistent Disk Container Storage Interface Driver Tests")
}
var _ = BeforeSuite(func() {
var err error
tcc := make(chan *remote.TestContext)
mwTcc := make(chan *remote.TestContext)
defer close(tcc)
defer close(mwTcc)
zones := strings.Split(*zones, ",")
rand.Seed(time.Now().UnixNano())
computeService, err = remote.GetComputeClient()
Expect(err).To(BeNil())
computeAlphaService, err = remote.GetComputeAlphaClient()
Expect(err).To(BeNil())
computeBetaService, err = remote.GetComputeBetaClient()
Expect(err).To(BeNil())
// Create the KMS client.
kmsClient, err = cloudkms.NewKeyManagementClient(context.Background())
Expect(err).To(BeNil())
if *runInProw {
*project, *serviceAccount = testutils.SetupProwConfig("gce-project")
}
Expect(*project).ToNot(BeEmpty(), "Project should not be empty")
Expect(*serviceAccount).ToNot(BeEmpty(), "Service account should not be empty")
klog.Infof("Running in project %v with service account %v", *project, *serviceAccount)
for _, zone := range zones {
go func(curZone string) {
defer GinkgoRecover()
tcc <- NewTestContext(curZone, *machineType, *minCpuPlatform)
mwTcc <- NewTestContext(curZone, *mwMachineType, *mwMinCpuPlatform)
}(zone)
}
for i := 0; i < len(zones); i++ {
tc := <-tcc
testContexts = append(testContexts, tc)
mwTc := <-mwTcc
multiWriterTestContexts = append(multiWriterTestContexts, mwTc)
klog.Infof("Added TestContext for node %s", tc.Instance.GetName())
}
})
var _ = AfterSuite(func() {
for _, tc := range testContexts {
err := remote.TeardownDriverAndClient(tc)
Expect(err).To(BeNil(), "Teardown Driver and Client failed with error")
if *deleteInstances {
tc.Instance.DeleteInstance()
}
}
for _, mwTc := range multiWriterTestContexts {
err := remote.TeardownDriverAndClient(mwTc)
Expect(err).To(BeNil(), "Multiwriter Teardown Driver and Client failed with error")
if *deleteInstances {
mwTc.Instance.DeleteInstance()
}
}
})
func notEmpty(v string) bool {
return v != ""
}
func getDriverConfig() testutils.DriverConfig {
return testutils.DriverConfig{
ExtraFlags: slices.Filter(nil, strings.Split(*extraDriverFlags, ","), notEmpty),
Zones: strings.Split(*zones, ","),
}
}
func NewTestContext(zone string, machineType string, minCpuPlatform string) *remote.TestContext {
nodeID := fmt.Sprintf("%s-%s-%s", *vmNamePrefix, zone, machineType)
klog.Infof("Setting up node %s", nodeID)
instanceConfig := remote.InstanceConfig{
Project: *project,
Architecture: *architecture,
MinCpuPlatform: minCpuPlatform,
Zone: zone,
Name: nodeID,
MachineType: machineType,
ServiceAccount: *serviceAccount,
ImageURL: *imageURL,
CloudtopHost: *cloudtopHost,
EnableConfidentialCompute: *enableConfidentialCompute,
ComputeService: computeService,
}
i, err := remote.SetupInstance(instanceConfig)
if err != nil {
klog.Fatalf("Failed to setup instance %v: %v", nodeID, err)
}
err = testutils.MkdirAll(i, "/lib/udev_containerized")
if err != nil {
klog.Fatalf("Failed to make scsi_id containerized directory: %v", err)
}
err = testutils.CopyFile(i, "/lib/udev/scsi_id", "/lib/udev_containerized/scsi_id")
if err != nil {
klog.Fatalf("Failed to copy scsi_id to containerized directory: %v", err)
}
err = testutils.CopyFile(i, "/lib/udev/google_nvme_id", "/lib/udev_containerized/google_nvme_id")
if err != nil {
klog.Fatalf("Failed to copy google_nvme_id to containerized directory: %v", err)
}
klog.Infof("Creating new driver and client for node %s", i.GetName())
tc, err := testutils.GCEClientAndDriverSetup(i, getDriverConfig())
if err != nil {
klog.Fatalf("Failed to set up TestContext for instance %v: %v", i.GetName(), err)
}
klog.Infof("Finished creating TestContext for node %s", tc.Instance.GetName())
return tc
}
func getRandomTestContext() *remote.TestContext {
Expect(testContexts).ToNot(BeEmpty())
rn := rand.Intn(len(testContexts))
return testContexts[rn]
}
func getRandomMwTestContext() *remote.TestContext {
Expect(multiWriterTestContexts).ToNot(BeEmpty())
rn := rand.Intn(len(multiWriterTestContexts))
return multiWriterTestContexts[rn]
}