blob: 2838af3c61e66cd7033d00647b979d05926bd447 [file] [log] [blame]
Lorenz Brunfc5dbc62020-05-28 12:18:07 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package e2e
18
19import (
Serge Bazanski1f9a03b2021-08-17 13:40:53 +020020 "bytes"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020021 "context"
Serge Bazanski1f9a03b2021-08-17 13:40:53 +020022 "crypto/ed25519"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020023 "errors"
24 "fmt"
25 "log"
Leopold Schabele28e6d72020-06-03 11:39:25 +020026 "net"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020027 "net/http"
28 _ "net/http"
29 _ "net/http/pprof"
Lorenz Brun3ff5af32020-06-24 16:34:11 +020030 "os"
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020031 "strings"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020032 "testing"
33 "time"
34
35 "google.golang.org/grpc"
36 corev1 "k8s.io/api/core/v1"
Lorenz Brun30167f52021-03-17 17:49:01 +010037 "k8s.io/apimachinery/pkg/api/resource"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020038 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39 podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
40
Serge Bazanski31370b02021-01-07 16:31:14 +010041 common "source.monogon.dev/metropolis/node"
Serge Bazanskid7d6e022021-09-01 15:03:06 +020042 "source.monogon.dev/metropolis/node/core/rpc"
Serge Bazanski31370b02021-01-07 16:31:14 +010043 apb "source.monogon.dev/metropolis/proto/api"
44 "source.monogon.dev/metropolis/test/launch"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020045)
46
Leopold Schabeld603f842020-06-09 17:48:09 +020047const (
48 // Timeout for the global test context.
49 //
Serge Bazanski216fe7b2021-05-21 18:36:16 +020050 // Bazel would eventually time out the test after 900s ("large") if, for
51 // some reason, the context cancellation fails to abort it.
Leopold Schabeld603f842020-06-09 17:48:09 +020052 globalTestTimeout = 600 * time.Second
53
54 // Timeouts for individual end-to-end tests of different sizes.
Serge Bazanski1ebd1e12020-07-13 19:17:16 +020055 smallTestTimeout = 60 * time.Second
Leopold Schabeld603f842020-06-09 17:48:09 +020056 largeTestTimeout = 120 * time.Second
57)
58
Serge Bazanski216fe7b2021-05-21 18:36:16 +020059// TestE2E is the main E2E test entrypoint for single-node freshly-bootstrapped
60// E2E tests. It starts a full Metropolis node in bootstrap mode and then runs
61// tests against it. The actual tests it performs are located in the RunGroup
62// subtest.
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020063func TestE2E(t *testing.T) {
Leopold Schabele28e6d72020-06-03 11:39:25 +020064 // Run pprof server for debugging
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020065 go func() {
Leopold Schabele28e6d72020-06-03 11:39:25 +020066 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
67 if err != nil {
68 panic(err)
69 }
70
71 l, err := net.ListenTCP("tcp", addr)
72 if err != nil {
73 log.Fatalf("Failed to listen on pprof port: %s", l.Addr())
74 }
75 defer l.Close()
76
77 log.Printf("pprof server listening on %s", l.Addr())
78 log.Printf("pprof server returned an error: %v", http.Serve(l, nil))
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020079 }()
Leopold Schabele28e6d72020-06-03 11:39:25 +020080
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020081 // Set a global timeout to make sure this terminates
Leopold Schabeld603f842020-06-09 17:48:09 +020082 ctx, cancel := context.WithTimeout(context.Background(), globalTestTimeout)
Serge Bazanski1f9a03b2021-08-17 13:40:53 +020083 defer cancel()
Lorenz Bruned0503c2020-07-28 17:21:25 +020084 portMap, err := launch.ConflictFreePortMap(launch.NodePorts)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020085 if err != nil {
86 t.Fatalf("Failed to acquire ports for e2e test: %v", err)
87 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020088
89 procExit := make(chan struct{})
90
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020091 go func() {
Serge Bazanski0ed2f962021-03-15 16:39:30 +010092 if err := launch.Launch(ctx, launch.Options{
93 Ports: portMap,
94 SerialPort: os.Stdout,
95 NodeParameters: &apb.NodeParameters{
96 Cluster: &apb.NodeParameters_ClusterBootstrap_{
Serge Bazanski1f9a03b2021-08-17 13:40:53 +020097 ClusterBootstrap: launch.InsecureClusterBootstrap,
Serge Bazanski0ed2f962021-03-15 16:39:30 +010098 },
99 },
100 }); err != nil {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200101 panic(err)
102 }
Leopold Schabela013ffa2020-06-03 15:09:32 +0200103 close(procExit)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200104 }()
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200105
106 grpcDebug, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure())
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200107 if err != nil {
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200108 log.Printf("Failed to dial debug service (is it running?): %v", err)
109 return
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200110 }
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200111 debug := apb.NewNodeDebugServiceClient(grpcDebug)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200112
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200113 // This exists to keep the parent around while all the children race.
114 // It currently tests both a set of OS-level conditions and Kubernetes
115 // Deployments and StatefulSets
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200116 t.Run("RunGroup", func(t *testing.T) {
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200117 t.Run("Connect to Curator", func(t *testing.T) {
118 testEventual(t, "Retrieving owner credentials succesful", ctx, 60*time.Second, func(ctx context.Context) error {
Serge Bazanskid7d6e022021-09-01 15:03:06 +0200119 remote := fmt.Sprintf("localhost:%v", portMap[common.CuratorServicePort])
120 initClient, err := rpc.NewEphemeralClient(remote, launch.InsecurePrivateKey, nil)
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200121 if err != nil {
122 return fmt.Errorf("NewInitialClient: %w", err)
123 }
124
Serge Bazanskid7d6e022021-09-01 15:03:06 +0200125 aaa := apb.NewAAAClient(initClient)
126 cert, err := rpc.RetrieveOwnerCertificate(ctx, aaa, launch.InsecurePrivateKey)
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200127 if err != nil {
128 return fmt.Errorf("RetrieveOwnerCertificate: %w", err)
129 }
130
131 if !bytes.Equal(cert.PrivateKey.(ed25519.PrivateKey), launch.InsecurePrivateKey) {
132 t.Fatalf("Received certificate for wrong private key")
133 }
134
Serge Bazanskibf68fa92021-10-05 17:53:58 +0200135 // Connect to management endpoint and retrieve cluster directory.
136 authClient, err := rpc.NewAuthenticatedClient(remote, *cert, nil)
137 if err != nil {
138 return fmt.Errorf("NewAuthenticatedClient: %w", err)
139 }
140 mgmt := apb.NewManagementClient(authClient)
141 res, err := mgmt.GetClusterInfo(ctx, &apb.GetClusterInfoRequest{})
142 if err != nil {
143 return fmt.Errorf("GetClusterInfo: %w", err)
144 }
145
146 // Ensure the node is there with its address.
147 nodes := res.ClusterDirectory.Nodes
148 if want, got := 1, len(nodes); want != got {
149 return fmt.Errorf("wanted %d nodes in cluster directory, got %d", want, got)
150 }
151 node := nodes[0]
152 if want, got := ed25519.PublicKeySize, len(node.PublicKey); want != got {
153 return fmt.Errorf("wanted %d bytes long public key, got %d", want, got)
154 }
155 if want, got := 1, len(node.Addresses); want != got {
156 return fmt.Errorf("wanted %d node address, got %d", want, got)
157 }
158 if want, got := "10.42.0.10", node.Addresses[0].Host; want != got {
159 return fmt.Errorf("wanted status address %q, got %q", want, got)
160 }
161
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200162 return nil
163 })
164 })
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200165 t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) {
166 t.Parallel()
Leopold Schabeld603f842020-06-09 17:48:09 +0200167 selfCtx, cancel := context.WithTimeout(ctx, largeTestTimeout)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200168 defer cancel()
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200169 clientSet, err := GetKubeClientSet(selfCtx, debug, portMap[common.KubernetesAPIPort])
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200170 if err != nil {
171 t.Fatal(err)
172 }
Leopold Schabeld603f842020-06-09 17:48:09 +0200173 testEventual(t, "Node is registered and ready", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200174 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
175 if err != nil {
176 return err
177 }
178 if len(nodes.Items) < 1 {
179 return errors.New("node not registered")
180 }
181 if len(nodes.Items) > 1 {
182 return errors.New("more than one node registered (but there is only one)")
183 }
184 node := nodes.Items[0]
185 for _, cond := range node.Status.Conditions {
186 if cond.Type != corev1.NodeReady {
187 continue
188 }
189 if cond.Status != corev1.ConditionTrue {
190 return fmt.Errorf("node not ready: %v", cond.Message)
191 }
192 }
193 return nil
194 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200195 testEventual(t, "Simple deployment", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200196 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
197 return err
198 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200199 testEventual(t, "Simple deployment is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200200 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
201 if err != nil {
202 return err
203 }
204 if len(res.Items) == 0 {
205 return errors.New("pod didn't get created")
206 }
207 pod := res.Items[0]
208 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
209 return nil
210 }
211 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
212 if err != nil || len(events.Items) == 0 {
213 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
214 } else {
215 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
216 }
217 })
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +0200218 testEventual(t, "Simple deployment with runc", ctx, largeTestTimeout, func(ctx context.Context) error {
219 deployment := makeTestDeploymentSpec("test-deploy-2")
220 var runcStr = "runc"
221 deployment.Spec.Template.Spec.RuntimeClassName = &runcStr
222 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, deployment, metav1.CreateOptions{})
223 return err
224 })
225 testEventual(t, "Simple deployment is running on runc", ctx, largeTestTimeout, func(ctx context.Context) error {
226 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-2"})
227 if err != nil {
228 return err
229 }
230 if len(res.Items) == 0 {
231 return errors.New("pod didn't get created")
232 }
233 pod := res.Items[0]
234 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
235 return nil
236 }
237 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
238 if err != nil || len(events.Items) == 0 {
239 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
240 } else {
241 var errorMsg strings.Builder
242 for _, msg := range events.Items {
243 errorMsg.WriteString(" | ")
244 errorMsg.WriteString(msg.Message)
245 }
246 return fmt.Errorf("pod is not ready: %v", errorMsg.String())
247 }
248 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200249 testEventual(t, "Simple StatefulSet with PVC", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brun37050122021-03-30 14:00:27 +0200250 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1", corev1.PersistentVolumeFilesystem), metav1.CreateOptions{})
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200251 return err
252 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200253 testEventual(t, "Simple StatefulSet with PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200254 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"})
255 if err != nil {
256 return err
257 }
258 if len(res.Items) == 0 {
259 return errors.New("pod didn't get created")
260 }
261 pod := res.Items[0]
262 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
263 return nil
264 }
265 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
266 if err != nil || len(events.Items) == 0 {
267 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
268 } else {
269 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
270 }
271 })
Lorenz Brun37050122021-03-30 14:00:27 +0200272 testEventual(t, "Simple StatefulSet with Block PVC", ctx, largeTestTimeout, func(ctx context.Context) error {
273 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-2", corev1.PersistentVolumeBlock), metav1.CreateOptions{})
274 return err
275 })
276 testEventual(t, "Simple StatefulSet with Block PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error {
277 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-2"})
278 if err != nil {
279 return err
280 }
281 if len(res.Items) == 0 {
282 return errors.New("pod didn't get created")
283 }
284 pod := res.Items[0]
285 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
286 return nil
287 }
288 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
289 if err != nil || len(events.Items) == 0 {
290 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
291 } else {
292 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
293 }
294 })
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200295 testEventual(t, "Pod with preseeded image", ctx, smallTestTimeout, func(ctx context.Context) error {
296 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
297 ObjectMeta: metav1.ObjectMeta{
298 Name: "preseed-test-1",
299 },
300 Spec: corev1.PodSpec{
301 Containers: []corev1.Container{{
302 Name: "preseed-test-1",
303 ImagePullPolicy: corev1.PullNever,
Serge Bazanski77cb6c52020-12-19 00:09:22 +0100304 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest",
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200305 }},
306 RestartPolicy: corev1.RestartPolicyNever,
307 },
308 }, metav1.CreateOptions{})
309 return err
310 })
311 testEventual(t, "Pod with preseeded image is completed", ctx, largeTestTimeout, func(ctx context.Context) error {
312 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "preseed-test-1", metav1.GetOptions{})
313 if err != nil {
314 return fmt.Errorf("failed to get pod: %w", err)
315 }
316 if pod.Status.Phase == corev1.PodSucceeded {
317 return nil
318 }
319 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
320 if err != nil || len(events.Items) == 0 {
321 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
322 } else {
323 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
324 }
325 })
Lorenz Brun30167f52021-03-17 17:49:01 +0100326 if os.Getenv("HAVE_NESTED_KVM") != "" {
327 testEventual(t, "Pod for KVM/QEMU smoke test", ctx, smallTestTimeout, func(ctx context.Context) error {
328 runcRuntimeClass := "runc"
329 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
330 ObjectMeta: metav1.ObjectMeta{
331 Name: "vm-smoketest",
332 },
333 Spec: corev1.PodSpec{
334 Containers: []corev1.Container{{
335 Name: "vm-smoketest",
336 ImagePullPolicy: corev1.PullNever,
337 Image: "bazel/metropolis/vm/smoketest:smoketest_container",
338 Resources: corev1.ResourceRequirements{
339 Limits: corev1.ResourceList{
340 "devices.monogon.dev/kvm": *resource.NewQuantity(1, ""),
341 },
342 },
343 }},
344 RuntimeClassName: &runcRuntimeClass,
345 RestartPolicy: corev1.RestartPolicyNever,
346 },
347 }, metav1.CreateOptions{})
348 return err
349 })
350 testEventual(t, "KVM/QEMU smoke test completion", ctx, smallTestTimeout, func(ctx context.Context) error {
351 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "vm-smoketest", metav1.GetOptions{})
352 if err != nil {
353 return fmt.Errorf("failed to get pod: %w", err)
354 }
355 if pod.Status.Phase == corev1.PodSucceeded {
356 return nil
357 }
358 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
359 if err != nil || len(events.Items) == 0 {
360 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
361 } else {
362 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
363 }
364 })
365 }
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200366 })
367 })
Leopold Schabela013ffa2020-06-03 15:09:32 +0200368
369 // Cancel the main context and wait for our subprocesses to exit
370 // to avoid leaking them and blocking the parent.
371 cancel()
372 <-procExit
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200373}