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