Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package e2e |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "log" |
Leopold Schabel | e28e6d7 | 2020-06-03 11:39:25 +0200 | [diff] [blame] | 24 | "net" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 25 | "net/http" |
| 26 | _ "net/http" |
| 27 | _ "net/http/pprof" |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 28 | "os" |
Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 29 | "strings" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 30 | "testing" |
| 31 | "time" |
| 32 | |
| 33 | "google.golang.org/grpc" |
| 34 | corev1 "k8s.io/api/core/v1" |
| 35 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | podv1 "k8s.io/kubernetes/pkg/api/v1/pod" |
| 37 | |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 38 | "git.monogon.dev/source/nexantic.git/core/internal/common" |
| 39 | "git.monogon.dev/source/nexantic.git/core/internal/launch" |
Serge Bazanski | efdb6e9 | 2020-07-13 17:19:27 +0200 | [diff] [blame] | 40 | apb "git.monogon.dev/source/nexantic.git/core/proto/api" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 41 | ) |
| 42 | |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 43 | const ( |
| 44 | // Timeout for the global test context. |
| 45 | // |
| 46 | // Bazel would eventually time out the test after 900s ("large") if, for some reason, |
| 47 | // the context cancellation fails to abort it. |
| 48 | globalTestTimeout = 600 * time.Second |
| 49 | |
| 50 | // Timeouts for individual end-to-end tests of different sizes. |
Serge Bazanski | 1ebd1e1 | 2020-07-13 19:17:16 +0200 | [diff] [blame] | 51 | smallTestTimeout = 60 * time.Second |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 52 | largeTestTimeout = 120 * time.Second |
| 53 | ) |
| 54 | |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 55 | // TestE2E is the main E2E test entrypoint for single-node freshly-bootstrapped E2E tests. It starts a full Smalltown node |
| 56 | // in bootstrap mode and then runs tests against it. The actual tests it performs are located in the RunGroup subtest. |
| 57 | func TestE2E(t *testing.T) { |
Leopold Schabel | e28e6d7 | 2020-06-03 11:39:25 +0200 | [diff] [blame] | 58 | // Run pprof server for debugging |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 59 | go func() { |
Leopold Schabel | e28e6d7 | 2020-06-03 11:39:25 +0200 | [diff] [blame] | 60 | addr, err := net.ResolveTCPAddr("tcp", "localhost:0") |
| 61 | if err != nil { |
| 62 | panic(err) |
| 63 | } |
| 64 | |
| 65 | l, err := net.ListenTCP("tcp", addr) |
| 66 | if err != nil { |
| 67 | log.Fatalf("Failed to listen on pprof port: %s", l.Addr()) |
| 68 | } |
| 69 | defer l.Close() |
| 70 | |
| 71 | log.Printf("pprof server listening on %s", l.Addr()) |
| 72 | log.Printf("pprof server returned an error: %v", http.Serve(l, nil)) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 73 | }() |
Leopold Schabel | e28e6d7 | 2020-06-03 11:39:25 +0200 | [diff] [blame] | 74 | |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 75 | // Set a global timeout to make sure this terminates |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 76 | ctx, cancel := context.WithTimeout(context.Background(), globalTestTimeout) |
Lorenz Brun | ed0503c | 2020-07-28 17:21:25 +0200 | [diff] [blame] | 77 | portMap, err := launch.ConflictFreePortMap(launch.NodePorts) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 78 | if err != nil { |
| 79 | t.Fatalf("Failed to acquire ports for e2e test: %v", err) |
| 80 | } |
Leopold Schabel | a013ffa | 2020-06-03 15:09:32 +0200 | [diff] [blame] | 81 | |
| 82 | procExit := make(chan struct{}) |
| 83 | |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 84 | go func() { |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 85 | if err := launch.Launch(ctx, launch.Options{Ports: portMap, SerialPort: os.Stdout}); err != nil { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 86 | panic(err) |
| 87 | } |
Leopold Schabel | a013ffa | 2020-06-03 15:09:32 +0200 | [diff] [blame] | 88 | close(procExit) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 89 | }() |
| 90 | grpcClient, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure()) |
| 91 | if err != nil { |
| 92 | fmt.Printf("Failed to dial debug service (is it running): %v\n", err) |
| 93 | } |
Serge Bazanski | efdb6e9 | 2020-07-13 17:19:27 +0200 | [diff] [blame] | 94 | debugClient := apb.NewNodeDebugServiceClient(grpcClient) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 95 | |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 96 | // This exists to keep the parent around while all the children race |
| 97 | // It currently tests both a set of OS-level conditions and Kubernetes Deployments and StatefulSets |
| 98 | t.Run("RunGroup", func(t *testing.T) { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 99 | t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) { |
| 100 | t.Parallel() |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 101 | selfCtx, cancel := context.WithTimeout(ctx, largeTestTimeout) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 102 | defer cancel() |
Lorenz Brun | ed0503c | 2020-07-28 17:21:25 +0200 | [diff] [blame] | 103 | clientSet, err := GetKubeClientSet(selfCtx, debugClient, portMap[common.KubernetesAPIPort]) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 107 | testEventual(t, "Node is registered and ready", ctx, largeTestTimeout, func(ctx context.Context) error { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 108 | nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | if len(nodes.Items) < 1 { |
| 113 | return errors.New("node not registered") |
| 114 | } |
| 115 | if len(nodes.Items) > 1 { |
| 116 | return errors.New("more than one node registered (but there is only one)") |
| 117 | } |
| 118 | node := nodes.Items[0] |
| 119 | for _, cond := range node.Status.Conditions { |
| 120 | if cond.Type != corev1.NodeReady { |
| 121 | continue |
| 122 | } |
| 123 | if cond.Status != corev1.ConditionTrue { |
| 124 | return fmt.Errorf("node not ready: %v", cond.Message) |
| 125 | } |
| 126 | } |
| 127 | return nil |
| 128 | }) |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 129 | testEventual(t, "Simple deployment", ctx, largeTestTimeout, func(ctx context.Context) error { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 130 | _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{}) |
| 131 | return err |
| 132 | }) |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 133 | testEventual(t, "Simple deployment is running", ctx, largeTestTimeout, func(ctx context.Context) error { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 134 | res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"}) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | if len(res.Items) == 0 { |
| 139 | return errors.New("pod didn't get created") |
| 140 | } |
| 141 | pod := res.Items[0] |
| 142 | if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) { |
| 143 | return nil |
| 144 | } |
| 145 | events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)}) |
| 146 | if err != nil || len(events.Items) == 0 { |
| 147 | return fmt.Errorf("pod is not ready: %v", pod.Status.Phase) |
| 148 | } else { |
| 149 | return fmt.Errorf("pod is not ready: %v", events.Items[0].Message) |
| 150 | } |
| 151 | }) |
Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 152 | testEventual(t, "Simple deployment with runc", ctx, largeTestTimeout, func(ctx context.Context) error { |
| 153 | deployment := makeTestDeploymentSpec("test-deploy-2") |
| 154 | var runcStr = "runc" |
| 155 | deployment.Spec.Template.Spec.RuntimeClassName = &runcStr |
| 156 | _, err := clientSet.AppsV1().Deployments("default").Create(ctx, deployment, metav1.CreateOptions{}) |
| 157 | return err |
| 158 | }) |
| 159 | testEventual(t, "Simple deployment is running on runc", ctx, largeTestTimeout, func(ctx context.Context) error { |
| 160 | res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-2"}) |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | if len(res.Items) == 0 { |
| 165 | return errors.New("pod didn't get created") |
| 166 | } |
| 167 | pod := res.Items[0] |
| 168 | if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) { |
| 169 | return nil |
| 170 | } |
| 171 | events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)}) |
| 172 | if err != nil || len(events.Items) == 0 { |
| 173 | return fmt.Errorf("pod is not ready: %v", pod.Status.Phase) |
| 174 | } else { |
| 175 | var errorMsg strings.Builder |
| 176 | for _, msg := range events.Items { |
| 177 | errorMsg.WriteString(" | ") |
| 178 | errorMsg.WriteString(msg.Message) |
| 179 | } |
| 180 | return fmt.Errorf("pod is not ready: %v", errorMsg.String()) |
| 181 | } |
| 182 | }) |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 183 | testEventual(t, "Simple StatefulSet with PVC", ctx, largeTestTimeout, func(ctx context.Context) error { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 184 | _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1"), metav1.CreateOptions{}) |
| 185 | return err |
| 186 | }) |
Leopold Schabel | d603f84 | 2020-06-09 17:48:09 +0200 | [diff] [blame] | 187 | testEventual(t, "Simple StatefulSet with PVC is running", ctx, largeTestTimeout, func(ctx context.Context) error { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 188 | res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"}) |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | if len(res.Items) == 0 { |
| 193 | return errors.New("pod didn't get created") |
| 194 | } |
| 195 | pod := res.Items[0] |
| 196 | if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) { |
| 197 | return nil |
| 198 | } |
| 199 | events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)}) |
| 200 | if err != nil || len(events.Items) == 0 { |
| 201 | return fmt.Errorf("pod is not ready: %v", pod.Status.Phase) |
| 202 | } else { |
| 203 | return fmt.Errorf("pod is not ready: %v", events.Items[0].Message) |
| 204 | } |
| 205 | }) |
Lorenz Brun | 8b0431a | 2020-07-13 16:56:36 +0200 | [diff] [blame] | 206 | testEventual(t, "Pod with preseeded image", ctx, smallTestTimeout, func(ctx context.Context) error { |
| 207 | _, err := clientSet.CoreV1().Pods("default").Create(ctx, &corev1.Pod{ |
| 208 | ObjectMeta: metav1.ObjectMeta{ |
| 209 | Name: "preseed-test-1", |
| 210 | }, |
| 211 | Spec: corev1.PodSpec{ |
| 212 | Containers: []corev1.Container{{ |
| 213 | Name: "preseed-test-1", |
| 214 | ImagePullPolicy: corev1.PullNever, |
| 215 | Image: "bazel/core/tests/e2e/preseedtest:preseedtest", |
| 216 | }}, |
| 217 | RestartPolicy: corev1.RestartPolicyNever, |
| 218 | }, |
| 219 | }, metav1.CreateOptions{}) |
| 220 | return err |
| 221 | }) |
| 222 | testEventual(t, "Pod with preseeded image is completed", ctx, largeTestTimeout, func(ctx context.Context) error { |
| 223 | pod, err := clientSet.CoreV1().Pods("default").Get(ctx, "preseed-test-1", metav1.GetOptions{}) |
| 224 | if err != nil { |
| 225 | return fmt.Errorf("failed to get pod: %w", err) |
| 226 | } |
| 227 | if pod.Status.Phase == corev1.PodSucceeded { |
| 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[len(events.Items)-1].Message) |
| 235 | } |
| 236 | }) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 237 | }) |
| 238 | }) |
Leopold Schabel | a013ffa | 2020-06-03 15:09:32 +0200 | [diff] [blame] | 239 | |
| 240 | // Cancel the main context and wait for our subprocesses to exit |
| 241 | // to avoid leaking them and blocking the parent. |
| 242 | cancel() |
| 243 | <-procExit |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 244 | } |