blob: b59ed7d4abb43242fa50eda4499137f830fd32f4 [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"
24 "net/http"
25 _ "net/http"
26 _ "net/http/pprof"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020027 "testing"
28 "time"
29
30 "google.golang.org/grpc"
31 corev1 "k8s.io/api/core/v1"
32 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33 podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
34
35 apipb "git.monogon.dev/source/nexantic.git/core/generated/api"
36 "git.monogon.dev/source/nexantic.git/core/internal/common"
37 "git.monogon.dev/source/nexantic.git/core/internal/launch"
38)
39
40// TestE2E is the main E2E test entrypoint for single-node freshly-bootstrapped E2E tests. It starts a full Smalltown node
41// in bootstrap mode and then runs tests against it. The actual tests it performs are located in the RunGroup subtest.
42func TestE2E(t *testing.T) {
43 go func() {
44 log.Println(http.ListenAndServe("localhost:0", nil))
45 }()
46 // Set a global timeout to make sure this terminates
47 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020048 portMap, err := launch.ConflictFreePortMap()
49 if err != nil {
50 t.Fatalf("Failed to acquire ports for e2e test: %v", err)
51 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020052
53 procExit := make(chan struct{})
54
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020055 go func() {
56 if err := launch.Launch(ctx, launch.Options{Ports: portMap}); err != nil {
57 panic(err)
58 }
Leopold Schabela013ffa2020-06-03 15:09:32 +020059 close(procExit)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020060 }()
61 grpcClient, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure())
62 if err != nil {
63 fmt.Printf("Failed to dial debug service (is it running): %v\n", err)
64 }
65 debugClient := apipb.NewNodeDebugServiceClient(grpcClient)
66
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020067 // This exists to keep the parent around while all the children race
68 // It currently tests both a set of OS-level conditions and Kubernetes Deployments and StatefulSets
69 t.Run("RunGroup", func(t *testing.T) {
70 t.Run("IP available", func(t *testing.T) {
71 t.Parallel()
72 const timeoutSec = 10
73 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
74 defer cancel()
75 if err := waitForCondition(ctx, debugClient, "IPAssigned"); err != nil {
76 t.Errorf("Condition IPAvailable not met in %vs: %v", timeoutSec, err)
77 }
78 })
79 t.Run("Data available", func(t *testing.T) {
80 t.Parallel()
81 const timeoutSec = 30
82 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
83 defer cancel()
84 if err := waitForCondition(ctx, debugClient, "DataAvailable"); err != nil {
85 t.Errorf("Condition DataAvailable not met in %vs: %v", timeoutSec, err)
86 }
87 })
88 t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) {
89 t.Parallel()
90 selfCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
91 defer cancel()
92 clientSet, err := getKubeClientSet(selfCtx, debugClient, portMap[common.KubernetesAPIPort])
93 if err != nil {
94 t.Fatal(err)
95 }
96 testEventual(t, "Node is registered and ready", ctx, 30*time.Second, func(ctx context.Context) error {
97 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
98 if err != nil {
99 return err
100 }
101 if len(nodes.Items) < 1 {
102 return errors.New("node not registered")
103 }
104 if len(nodes.Items) > 1 {
105 return errors.New("more than one node registered (but there is only one)")
106 }
107 node := nodes.Items[0]
108 for _, cond := range node.Status.Conditions {
109 if cond.Type != corev1.NodeReady {
110 continue
111 }
112 if cond.Status != corev1.ConditionTrue {
113 return fmt.Errorf("node not ready: %v", cond.Message)
114 }
115 }
116 return nil
117 })
118 testEventual(t, "Simple deployment", ctx, 30*time.Second, func(ctx context.Context) error {
119 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
120 return err
121 })
122 testEventual(t, "Simple deployment is running", ctx, 40*time.Second, func(ctx context.Context) error {
123 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
124 if err != nil {
125 return err
126 }
127 if len(res.Items) == 0 {
128 return errors.New("pod didn't get created")
129 }
130 pod := res.Items[0]
131 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
132 return nil
133 }
134 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
135 if err != nil || len(events.Items) == 0 {
136 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
137 } else {
138 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
139 }
140 })
141 testEventual(t, "Simple StatefulSet with PVC", ctx, 30*time.Second, func(ctx context.Context) error {
142 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1"), metav1.CreateOptions{})
143 return err
144 })
145 testEventual(t, "Simple StatefulSet with PVC is running", ctx, 40*time.Second, func(ctx context.Context) error {
146 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-1"})
147 if err != nil {
148 return err
149 }
150 if len(res.Items) == 0 {
151 return errors.New("pod didn't get created")
152 }
153 pod := res.Items[0]
154 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
155 return nil
156 }
157 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
158 if err != nil || len(events.Items) == 0 {
159 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
160 } else {
161 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
162 }
163 })
164 })
165 })
Leopold Schabela013ffa2020-06-03 15:09:32 +0200166
167 // Cancel the main context and wait for our subprocesses to exit
168 // to avoid leaking them and blocking the parent.
169 cancel()
170 <-procExit
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200171}