blob: 46e5c720cfc0d013e77dac825e8dbd0a19e5c506 [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"
23 "log"
Leopold Schabele28e6d72020-06-03 11:39:25 +020024 "net"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020025 "net/http"
26 _ "net/http"
27 _ "net/http/pprof"
Lorenz Brun3ff5af32020-06-24 16:34:11 +020028 "os"
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020029 "strings"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020030 "testing"
31 "time"
32
33 "google.golang.org/grpc"
34 corev1 "k8s.io/api/core/v1"
Lorenz Brun30167f52021-03-17 17:49:01 +010035 "k8s.io/apimachinery/pkg/api/resource"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020036 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37 podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
38
Serge Bazanski31370b02021-01-07 16:31:14 +010039 common "source.monogon.dev/metropolis/node"
40 apb "source.monogon.dev/metropolis/proto/api"
41 "source.monogon.dev/metropolis/test/launch"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020042)
43
Leopold Schabeld603f842020-06-09 17:48:09 +020044const (
45 // Timeout for the global test context.
46 //
47 // Bazel would eventually time out the test after 900s ("large") if, for some reason,
48 // the context cancellation fails to abort it.
49 globalTestTimeout = 600 * time.Second
50
51 // Timeouts for individual end-to-end tests of different sizes.
Serge Bazanski1ebd1e12020-07-13 19:17:16 +020052 smallTestTimeout = 60 * time.Second
Leopold Schabeld603f842020-06-09 17:48:09 +020053 largeTestTimeout = 120 * time.Second
54)
55
Serge Bazanski662b5b32020-12-21 13:49:00 +010056// TestE2E is the main E2E test entrypoint for single-node freshly-bootstrapped E2E tests. It starts a full Metropolis node
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020057// in bootstrap mode and then runs tests against it. The actual tests it performs are located in the RunGroup subtest.
58func TestE2E(t *testing.T) {
Leopold Schabele28e6d72020-06-03 11:39:25 +020059 // Run pprof server for debugging
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020060 go func() {
Leopold Schabele28e6d72020-06-03 11:39:25 +020061 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
62 if err != nil {
63 panic(err)
64 }
65
66 l, err := net.ListenTCP("tcp", addr)
67 if err != nil {
68 log.Fatalf("Failed to listen on pprof port: %s", l.Addr())
69 }
70 defer l.Close()
71
72 log.Printf("pprof server listening on %s", l.Addr())
73 log.Printf("pprof server returned an error: %v", http.Serve(l, nil))
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020074 }()
Leopold Schabele28e6d72020-06-03 11:39:25 +020075
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020076 // Set a global timeout to make sure this terminates
Leopold Schabeld603f842020-06-09 17:48:09 +020077 ctx, cancel := context.WithTimeout(context.Background(), globalTestTimeout)
Lorenz Bruned0503c2020-07-28 17:21:25 +020078 portMap, err := launch.ConflictFreePortMap(launch.NodePorts)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020079 if err != nil {
80 t.Fatalf("Failed to acquire ports for e2e test: %v", err)
81 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020082
83 procExit := make(chan struct{})
84
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020085 go func() {
Serge Bazanski0ed2f962021-03-15 16:39:30 +010086 if err := launch.Launch(ctx, launch.Options{
87 Ports: portMap,
88 SerialPort: os.Stdout,
89 NodeParameters: &apb.NodeParameters{
90 Cluster: &apb.NodeParameters_ClusterBootstrap_{
91 ClusterBootstrap: &apb.NodeParameters_ClusterBootstrap{},
92 },
93 },
94 }); err != nil {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020095 panic(err)
96 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020097 close(procExit)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020098 }()
99 grpcClient, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure())
100 if err != nil {
101 fmt.Printf("Failed to dial debug service (is it running): %v\n", err)
102 }
Serge Bazanskiefdb6e92020-07-13 17:19:27 +0200103 debugClient := apb.NewNodeDebugServiceClient(grpcClient)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200104
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200105 // This exists to keep the parent around while all the children race
106 // It currently tests both a set of OS-level conditions and Kubernetes Deployments and StatefulSets
107 t.Run("RunGroup", func(t *testing.T) {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200108 t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) {
109 t.Parallel()
Leopold Schabeld603f842020-06-09 17:48:09 +0200110 selfCtx, cancel := context.WithTimeout(ctx, largeTestTimeout)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200111 defer cancel()
Lorenz Bruned0503c2020-07-28 17:21:25 +0200112 clientSet, err := GetKubeClientSet(selfCtx, debugClient, portMap[common.KubernetesAPIPort])
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200113 if err != nil {
114 t.Fatal(err)
115 }
Leopold Schabeld603f842020-06-09 17:48:09 +0200116 testEventual(t, "Node is registered and ready", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200117 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
118 if err != nil {
119 return err
120 }
121 if len(nodes.Items) < 1 {
122 return errors.New("node not registered")
123 }
124 if len(nodes.Items) > 1 {
125 return errors.New("more than one node registered (but there is only one)")
126 }
127 node := nodes.Items[0]
128 for _, cond := range node.Status.Conditions {
129 if cond.Type != corev1.NodeReady {
130 continue
131 }
132 if cond.Status != corev1.ConditionTrue {
133 return fmt.Errorf("node not ready: %v", cond.Message)
134 }
135 }
136 return nil
137 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200138 testEventual(t, "Simple deployment", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200139 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
140 return err
141 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200142 testEventual(t, "Simple deployment is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200143 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
144 if err != nil {
145 return err
146 }
147 if len(res.Items) == 0 {
148 return errors.New("pod didn't get created")
149 }
150 pod := res.Items[0]
151 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
152 return nil
153 }
154 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
155 if err != nil || len(events.Items) == 0 {
156 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
157 } else {
158 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
159 }
160 })
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +0200161 testEventual(t, "Simple deployment with runc", ctx, largeTestTimeout, func(ctx context.Context) error {
162 deployment := makeTestDeploymentSpec("test-deploy-2")
163 var runcStr = "runc"
164 deployment.Spec.Template.Spec.RuntimeClassName = &runcStr
165 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, deployment, metav1.CreateOptions{})
166 return err
167 })
168 testEventual(t, "Simple deployment is running on runc", ctx, largeTestTimeout, func(ctx context.Context) error {
169 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-2"})
170 if err != nil {
171 return err
172 }
173 if len(res.Items) == 0 {
174 return errors.New("pod didn't get created")
175 }
176 pod := res.Items[0]
177 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
178 return nil
179 }
180 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
181 if err != nil || len(events.Items) == 0 {
182 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
183 } else {
184 var errorMsg strings.Builder
185 for _, msg := range events.Items {
186 errorMsg.WriteString(" | ")
187 errorMsg.WriteString(msg.Message)
188 }
189 return fmt.Errorf("pod is not ready: %v", errorMsg.String())
190 }
191 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200192 testEventual(t, "Simple StatefulSet with PVC", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200193 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1"), metav1.CreateOptions{})
194 return err
195 })
Leopold Schabeld603f842020-06-09 17:48:09 +0200196 testEventual(t, "Simple StatefulSet with PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200197 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"})
198 if err != nil {
199 return err
200 }
201 if len(res.Items) == 0 {
202 return errors.New("pod didn't get created")
203 }
204 pod := res.Items[0]
205 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
206 return nil
207 }
208 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
209 if err != nil || len(events.Items) == 0 {
210 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
211 } else {
212 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
213 }
214 })
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200215 testEventual(t, "Pod with preseeded image", ctx, smallTestTimeout, func(ctx context.Context) error {
216 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
217 ObjectMeta: metav1.ObjectMeta{
218 Name: "preseed-test-1",
219 },
220 Spec: corev1.PodSpec{
221 Containers: []corev1.Container{{
222 Name: "preseed-test-1",
223 ImagePullPolicy: corev1.PullNever,
Serge Bazanski77cb6c52020-12-19 00:09:22 +0100224 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest",
Lorenz Brun8b0431a2020-07-13 16:56:36 +0200225 }},
226 RestartPolicy: corev1.RestartPolicyNever,
227 },
228 }, metav1.CreateOptions{})
229 return err
230 })
231 testEventual(t, "Pod with preseeded image is completed", ctx, largeTestTimeout, func(ctx context.Context) error {
232 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "preseed-test-1", metav1.GetOptions{})
233 if err != nil {
234 return fmt.Errorf("failed to get pod: %w", err)
235 }
236 if pod.Status.Phase == corev1.PodSucceeded {
237 return nil
238 }
239 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
240 if err != nil || len(events.Items) == 0 {
241 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
242 } else {
243 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
244 }
245 })
Lorenz Brun30167f52021-03-17 17:49:01 +0100246 if os.Getenv("HAVE_NESTED_KVM") != "" {
247 testEventual(t, "Pod for KVM/QEMU smoke test", ctx, smallTestTimeout, func(ctx context.Context) error {
248 runcRuntimeClass := "runc"
249 _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{
250 ObjectMeta: metav1.ObjectMeta{
251 Name: "vm-smoketest",
252 },
253 Spec: corev1.PodSpec{
254 Containers: []corev1.Container{{
255 Name: "vm-smoketest",
256 ImagePullPolicy: corev1.PullNever,
257 Image: "bazel/metropolis/vm/smoketest:smoketest_container",
258 Resources: corev1.ResourceRequirements{
259 Limits: corev1.ResourceList{
260 "devices.monogon.dev/kvm": *resource.NewQuantity(1, ""),
261 },
262 },
263 }},
264 RuntimeClassName: &runcRuntimeClass,
265 RestartPolicy: corev1.RestartPolicyNever,
266 },
267 }, metav1.CreateOptions{})
268 return err
269 })
270 testEventual(t, "KVM/QEMU smoke test completion", ctx, smallTestTimeout, func(ctx context.Context) error {
271 pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "vm-smoketest", metav1.GetOptions{})
272 if err != nil {
273 return fmt.Errorf("failed to get pod: %w", err)
274 }
275 if pod.Status.Phase == corev1.PodSucceeded {
276 return nil
277 }
278 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
279 if err != nil || len(events.Items) == 0 {
280 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
281 } else {
282 return fmt.Errorf("pod is not ready: %v", events.Items[len(events.Items)-1].Message)
283 }
284 })
285 }
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200286 })
287 })
Leopold Schabela013ffa2020-06-03 15:09:32 +0200288
289 // Cancel the main context and wait for our subprocesses to exit
290 // to avoid leaking them and blocking the parent.
291 cancel()
292 <-procExit
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200293}