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