blob: 89df286630f9539861bd0b7580cebdde7bf0ca47 [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 (
20 "context"
21 "errors"
22 "fmt"
Mateusz Zalega32b19292022-05-17 13:26:55 +020023 "io"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020024 "log"
Leopold Schabele28e6d72020-06-03 11:39:25 +020025 "net"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020026 "net/http"
27 _ "net/http"
28 _ "net/http/pprof"
Lorenz Brun3ff5af32020-06-24 16:34:11 +020029 "os"
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020030 "strings"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020031 "testing"
32 "time"
33
Serge Bazanskibe742842022-04-04 13:18:50 +020034 "google.golang.org/grpc"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020035 corev1 "k8s.io/api/core/v1"
Lorenz Brun30167f52021-03-17 17:49:01 +010036 "k8s.io/apimachinery/pkg/api/resource"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020037 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38 podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
39
Serge Bazanski31370b02021-01-07 16:31:14 +010040 common "source.monogon.dev/metropolis/node"
Serge Bazanski6dff6d62022-01-28 18:15:14 +010041 "source.monogon.dev/metropolis/node/core/identity"
Serge Bazanskibe742842022-04-04 13:18:50 +020042 "source.monogon.dev/metropolis/node/core/rpc"
Serge Bazanski31370b02021-01-07 16:31:14 +010043 apb "source.monogon.dev/metropolis/proto/api"
Serge Bazanski66e58952021-10-05 17:06:56 +020044 "source.monogon.dev/metropolis/test/launch/cluster"
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
Serge Bazanski66e58952021-10-05 17:06:56 +020065 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
66 if err != nil {
67 panic(err)
68 }
69
70 pprofListen, err := net.ListenTCP("tcp", addr)
71 if err != nil {
72 log.Fatalf("Failed to listen on pprof port: %s", pprofListen.Addr())
73 }
74
75 log.Printf("E2E: pprof server listening on %s", pprofListen.Addr())
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020076 go func() {
Serge Bazanski66e58952021-10-05 17:06:56 +020077 log.Printf("E2E: pprof server returned an error: %v", http.Serve(pprofListen, nil))
78 pprofListen.Close()
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()
Serge Bazanski66e58952021-10-05 17:06:56 +020084
85 // Launch cluster.
Serge Bazanskie78a0892021-10-07 17:03:49 +020086 clusterOptions := cluster.ClusterOptions{
87 NumNodes: 2,
88 }
89 cluster, err := cluster.LaunchCluster(ctx, clusterOptions)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020090 if err != nil {
Serge Bazanski66e58952021-10-05 17:06:56 +020091 t.Fatalf("LaunchCluster failed: %v", err)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020092 }
Serge Bazanski66e58952021-10-05 17:06:56 +020093 defer func() {
94 err := cluster.Close()
95 if err != nil {
96 t.Fatalf("cluster Close failed: %v", err)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020097 }
98 }()
Serge Bazanski1f9a03b2021-08-17 13:40:53 +020099
Serge Bazanski66e58952021-10-05 17:06:56 +0200100 log.Printf("E2E: Cluster running, starting tests...")
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200101
Serge Bazanskibe742842022-04-04 13:18:50 +0200102 // Dial first node's curator.
103 creds := rpc.NewAuthenticatedCredentials(cluster.Owner, nil)
104 remote := net.JoinHostPort(cluster.NodeIDs[0], common.CuratorServicePort.PortString())
105 cl, err := grpc.Dial(remote, grpc.WithContextDialer(cluster.DialNode), grpc.WithTransportCredentials(creds))
106 if err != nil {
107 t.Fatalf("failed to dial first node's curator: %v", err)
108 }
109 defer cl.Close()
110 mgmt := apb.NewManagementClient(cl)
111
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200112 // This exists to keep the parent around while all the children race.
113 // It currently tests both a set of OS-level conditions and Kubernetes
114 // Deployments and StatefulSets
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200115 t.Run("RunGroup", func(t *testing.T) {
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100116 t.Run("Cluster", func(t *testing.T) {
Serge Bazanski66e58952021-10-05 17:06:56 +0200117 testEventual(t, "Retrieving cluster directory sucessful", ctx, 60*time.Second, func(ctx context.Context) error {
Serge Bazanskibe742842022-04-04 13:18:50 +0200118 res, err := mgmt.GetClusterInfo(ctx, &apb.GetClusterInfoRequest{})
Serge Bazanskibf68fa92021-10-05 17:53:58 +0200119 if err != nil {
120 return fmt.Errorf("GetClusterInfo: %w", err)
121 }
122
Serge Bazanskie78a0892021-10-07 17:03:49 +0200123 // Ensure that the expected node count is present.
Serge Bazanskibf68fa92021-10-05 17:53:58 +0200124 nodes := res.ClusterDirectory.Nodes
Serge Bazanskie78a0892021-10-07 17:03:49 +0200125 if want, got := clusterOptions.NumNodes, len(nodes); want != got {
Serge Bazanskibf68fa92021-10-05 17:53:58 +0200126 return fmt.Errorf("wanted %d nodes in cluster directory, got %d", want, got)
127 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100128
129 // Ensure the nodes have the expected addresses.
130 addresses := make(map[string]bool)
131 for _, n := range nodes {
132 if len(n.Addresses) != 1 {
133 return fmt.Errorf("node %s has no addresss", identity.NodeID(n.PublicKey))
134 }
135 address := n.Addresses[0].Host
136 addresses[address] = true
137 }
138
139 for _, address := range []string{"10.1.0.2", "10.1.0.3"} {
140 if !addresses[address] {
141 return fmt.Errorf("address %q not found in directory", address)
142 }
143 }
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200144 return nil
145 })
Mateusz Zalega0246f5e2022-04-22 17:29:04 +0200146 testEventual(t, "Node rejoin successful", ctx, 60*time.Second, func(ctx context.Context) error {
147 // Ensure nodes rejoin the cluster after a reboot by reboting the 1st node.
148 if err := cluster.RebootNode(ctx, 1); err != nil {
149 return fmt.Errorf("while rebooting a node: %w", err)
150 }
151 return nil
152 })
Mateusz Zalega32b19292022-05-17 13:26:55 +0200153 testEventual(t, "Heartbeat test successful", ctx, 60*time.Second, func(ctx context.Context) error {
154 // Ensure all cluster nodes are capable of sending heartbeat updates.
155 // This test assumes the expected count of nodes is already present in
156 // the cluster.
157 for {
158 srvN, err := mgmt.GetNodes(ctx, &apb.GetNodesRequest{})
159 if err != nil {
160 return fmt.Errorf("GetNodes: %w", err)
161 }
162
163 // Count the unhealthy nodes.
164 var unhealthy int
165 for {
166 node, err := srvN.Recv()
167 if err == io.EOF {
168 break
169 }
170 if err != nil {
171 return fmt.Errorf("GetNodes.Recv: %w", err)
172 }
173
174 if node.Health != apb.Node_HEALTHY {
175 unhealthy++
176 }
177 }
178
179 // If all nodes tested in this iteration are healthy, the test has
180 // been passed.
181 if unhealthy == 0 {
182 break
183 }
184 }
185 return nil
186 })
Serge Bazanski1f9a03b2021-08-17 13:40:53 +0200187 })
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100188 t.Run("Kubernetes", func(t *testing.T) {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200189 t.Parallel()
Serge Bazanskibe742842022-04-04 13:18:50 +0200190 // TODO(q3k): use SOCKS proxy.
191 clientSet, err := GetKubeClientSet(cluster, cluster.Ports[uint16(common.KubernetesAPIWrappedPort)])
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200192 if err != nil {
193 t.Fatal(err)
194 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100195 testEventual(t, "Nodes are registered and ready", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200196 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
197 if err != nil {
198 return err
199 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100200 if len(nodes.Items) < 2 {
201 return errors.New("nodes not yet registered")
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200202 }
203 node := nodes.Items[0]
204 for _, cond := range node.Status.Conditions {
205 if cond.Type != corev1.NodeReady {
206 continue
207 }
208 if cond.Status != corev1.ConditionTrue {
209 return fmt.Errorf("node not ready: %v", cond.Message)
210 }
211 }
212 return nil
213 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200214 testEventual(t, "Simple deployment", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200215 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
216 return err
217 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200218 testEventual(t, "Simple deployment is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200219 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
220 if err != nil {
221 return err
222 }
223 if len(res.Items) == 0 {
224 return errors.New("pod didn't get created")
225 }
226 pod := res.Items[0]
227 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
228 return nil
229 }
230 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
231 if err != nil || len(events.Items) == 0 {
232 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
233 } else {
234 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
235 }
236 })
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +0200237 testEventual(t, "Simple deployment with runc", ctx, largeTestTimeout, func(ctx context.Context) error {
238 deployment := makeTestDeploymentSpec("test-deploy-2")
239 var runcStr = "runc"
240 deployment.Spec.Template.Spec.RuntimeClassName = &runcStr
241 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, deployment, metav1.CreateOptions{})
242 return err
243 })
244 testEventual(t, "Simple deployment is running on runc", ctx, largeTestTimeout, func(ctx context.Context) error {
245 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-2"})
246 if err != nil {
247 return err
248 }
249 if len(res.Items) == 0 {
250 return errors.New("pod didn't get created")
251 }
252 pod := res.Items[0]
253 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
254 return nil
255 }
256 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
257 if err != nil || len(events.Items) == 0 {
258 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
259 } else {
260 var errorMsg strings.Builder
261 for _, msg := range events.Items {
262 errorMsg.WriteString(" | ")
263 errorMsg.WriteString(msg.Message)
264 }
265 return fmt.Errorf("pod is not ready: %v", errorMsg.String())
266 }
267 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200268 testEventual(t, "Simple StatefulSet with PVC", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brun37050122021-03-30 14:00:27 +0200269 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1", corev1.PersistentVolumeFilesystem), metav1.CreateOptions{})
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200270 return err
271 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200272 testEventual(t, "Simple StatefulSet with PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200273 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"})
274 if err != nil {
275 return err
276 }
277 if len(res.Items) == 0 {
278 return errors.New("pod didn't get created")
279 }
280 pod := res.Items[0]
281 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
282 return nil
283 }
284 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
285 if err != nil || len(events.Items) == 0 {
286 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
287 } else {
288 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
289 }
290 })
Lorenz Brun37050122021-03-30 14:00:27 +0200291 testEventual(t, "Simple StatefulSet with Block PVC", ctx, largeTestTimeout, func(ctx context.Context) error {
292 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-2", corev1.PersistentVolumeBlock), metav1.CreateOptions{})
293 return err
294 })
295 testEventual(t, "Simple StatefulSet with Block PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error {
296 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-2"})
297 if err != nil {
298 return err
299 }
300 if len(res.Items) == 0 {
301 return errors.New("pod didn't get created")
302 }
303 pod := res.Items[0]
304 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
305 return nil
306 }
307 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
308 if err != nil || len(events.Items) == 0 {
309 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
310 } else {
311 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
312 }
313 })
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200314 testEventual(t, "Pod with preseeded image", ctx, smallTestTimeout, func(ctx context.Context) error {
315 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
316 ObjectMeta: metav1.ObjectMeta{
317 Name: "preseed-test-1",
318 },
319 Spec: corev1.PodSpec{
320 Containers: []corev1.Container{{
321 Name: "preseed-test-1",
322 ImagePullPolicy: corev1.PullNever,
Lorenz Brund13c1c62022-03-30 19:58:58 +0200323 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest_image",
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200324 }},
325 RestartPolicy: corev1.RestartPolicyNever,
326 },
327 }, metav1.CreateOptions{})
328 return err
329 })
330 testEventual(t, "Pod with preseeded image is completed", ctx, largeTestTimeout, func(ctx context.Context) error {
331 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "preseed-test-1", metav1.GetOptions{})
332 if err != nil {
333 return fmt.Errorf("failed to get pod: %w", err)
334 }
335 if pod.Status.Phase == corev1.PodSucceeded {
336 return nil
337 }
338 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
339 if err != nil || len(events.Items) == 0 {
340 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
341 } else {
342 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
343 }
344 })
Lorenz Brun30167f52021-03-17 17:49:01 +0100345 if os.Getenv("HAVE_NESTED_KVM") != "" {
346 testEventual(t, "Pod for KVM/QEMU smoke test", ctx, smallTestTimeout, func(ctx context.Context) error {
347 runcRuntimeClass := "runc"
348 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
349 ObjectMeta: metav1.ObjectMeta{
350 Name: "vm-smoketest",
351 },
352 Spec: corev1.PodSpec{
353 Containers: []corev1.Container{{
354 Name: "vm-smoketest",
355 ImagePullPolicy: corev1.PullNever,
356 Image: "bazel/metropolis/vm/smoketest:smoketest_container",
357 Resources: corev1.ResourceRequirements{
358 Limits: corev1.ResourceList{
359 "devices.monogon.dev/kvm": *resource.NewQuantity(1, ""),
360 },
361 },
362 }},
363 RuntimeClassName: &runcRuntimeClass,
364 RestartPolicy: corev1.RestartPolicyNever,
365 },
366 }, metav1.CreateOptions{})
367 return err
368 })
369 testEventual(t, "KVM/QEMU smoke test completion", ctx, smallTestTimeout, func(ctx context.Context) error {
370 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "vm-smoketest", metav1.GetOptions{})
371 if err != nil {
372 return fmt.Errorf("failed to get pod: %w", err)
373 }
374 if pod.Status.Phase == corev1.PodSucceeded {
375 return nil
376 }
377 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
378 if err != nil || len(events.Items) == 0 {
379 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
380 } else {
381 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
382 }
383 })
384 }
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200385 })
386 })
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200387}