blob: a2eb1e2bc3367d5d5dd25623196b5c1e272fcaa3 [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 Brunfc5dbc62020-05-28 12:18:07 +020028 "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
41// TestE2E is the main E2E test entrypoint for single-node freshly-bootstrapped E2E tests. It starts a full Smalltown node
42// in bootstrap mode and then runs tests against it. The actual tests it performs are located in the RunGroup subtest.
43func TestE2E(t *testing.T) {
Leopold Schabele28e6d72020-06-03 11:39:25 +020044 // Run pprof server for debugging
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020045 go func() {
Leopold Schabele28e6d72020-06-03 11:39:25 +020046 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
47 if err != nil {
48 panic(err)
49 }
50
51 l, err := net.ListenTCP("tcp", addr)
52 if err != nil {
53 log.Fatalf("Failed to listen on pprof port: %s", l.Addr())
54 }
55 defer l.Close()
56
57 log.Printf("pprof server listening on %s", l.Addr())
58 log.Printf("pprof server returned an error: %v", http.Serve(l, nil))
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020059 }()
Leopold Schabele28e6d72020-06-03 11:39:25 +020060
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020061 // Set a global timeout to make sure this terminates
62 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020063 portMap, err := launch.ConflictFreePortMap()
64 if err != nil {
65 t.Fatalf("Failed to acquire ports for e2e test: %v", err)
66 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020067
68 procExit := make(chan struct{})
69
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020070 go func() {
71 if err := launch.Launch(ctx, launch.Options{Ports: portMap}); err != nil {
72 panic(err)
73 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020074 close(procExit)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020075 }()
76 grpcClient, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure())
77 if err != nil {
78 fmt.Printf("Failed to dial debug service (is it running): %v\n", err)
79 }
80 debugClient := apipb.NewNodeDebugServiceClient(grpcClient)
81
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020082 // This exists to keep the parent around while all the children race
83 // It currently tests both a set of OS-level conditions and Kubernetes Deployments and StatefulSets
84 t.Run("RunGroup", func(t *testing.T) {
85 t.Run("IP available", func(t *testing.T) {
86 t.Parallel()
87 const timeoutSec = 10
88 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
89 defer cancel()
90 if err := waitForCondition(ctx, debugClient, "IPAssigned"); err != nil {
91 t.Errorf("Condition IPAvailable not met in %vs: %v", timeoutSec, err)
92 }
93 })
94 t.Run("Data available", func(t *testing.T) {
95 t.Parallel()
96 const timeoutSec = 30
97 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
98 defer cancel()
99 if err := waitForCondition(ctx, debugClient, "DataAvailable"); err != nil {
100 t.Errorf("Condition DataAvailable not met in %vs: %v", timeoutSec, err)
101 }
102 })
103 t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) {
104 t.Parallel()
105 selfCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
106 defer cancel()
107 clientSet, err := getKubeClientSet(selfCtx, debugClient, portMap[common.KubernetesAPIPort])
108 if err != nil {
109 t.Fatal(err)
110 }
111 testEventual(t, "Node is registered and ready", ctx, 30*time.Second, func(ctx context.Context) error {
112 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
113 if err != nil {
114 return err
115 }
116 if len(nodes.Items) < 1 {
117 return errors.New("node not registered")
118 }
119 if len(nodes.Items) > 1 {
120 return errors.New("more than one node registered (but there is only one)")
121 }
122 node := nodes.Items[0]
123 for _, cond := range node.Status.Conditions {
124 if cond.Type != corev1.NodeReady {
125 continue
126 }
127 if cond.Status != corev1.ConditionTrue {
128 return fmt.Errorf("node not ready: %v", cond.Message)
129 }
130 }
131 return nil
132 })
133 testEventual(t, "Simple deployment", ctx, 30*time.Second, func(ctx context.Context) error {
134 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
135 return err
136 })
137 testEventual(t, "Simple deployment is running", ctx, 40*time.Second, func(ctx context.Context) error {
138 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
139 if err != nil {
140 return err
141 }
142 if len(res.Items) == 0 {
143 return errors.New("pod didn't get created")
144 }
145 pod := res.Items[0]
146 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
147 return nil
148 }
149 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
150 if err != nil || len(events.Items) == 0 {
151 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
152 } else {
153 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
154 }
155 })
156 testEventual(t, "Simple StatefulSet with PVC", ctx, 30*time.Second, func(ctx context.Context) error {
157 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1"), metav1.CreateOptions{})
158 return err
159 })
160 testEventual(t, "Simple StatefulSet with PVC is running", ctx, 40*time.Second, func(ctx context.Context) error {
161 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"})
162 if err != nil {
163 return err
164 }
165 if len(res.Items) == 0 {
166 return errors.New("pod didn't get created")
167 }
168 pod := res.Items[0]
169 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
170 return nil
171 }
172 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
173 if err != nil || len(events.Items) == 0 {
174 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
175 } else {
176 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
177 }
178 })
179 })
180 })
Leopold Schabela013ffa2020-06-03 15:09:32 +0200181
182 // Cancel the main context and wait for our subprocesses to exit
183 // to avoid leaking them and blocking the parent.
184 cancel()
185 <-procExit
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200186}