blob: d400b9b47117d23a07cb3c39c41cac941eb9a860 [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"
27 "os"
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
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) {
44 go func() {
45 log.Println(http.ListenAndServe("localhost:0", nil))
46 }()
47 // Set a global timeout to make sure this terminates
48 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
49 defer cancel()
50 portMap, err := launch.ConflictFreePortMap()
51 if err != nil {
52 t.Fatalf("Failed to acquire ports for e2e test: %v", err)
53 }
54 go func() {
55 if err := launch.Launch(ctx, launch.Options{Ports: portMap}); err != nil {
56 panic(err)
57 }
58 }()
59 grpcClient, err := portMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure())
60 if err != nil {
61 fmt.Printf("Failed to dial debug service (is it running): %v\n", err)
62 }
63 debugClient := apipb.NewNodeDebugServiceClient(grpcClient)
64
65 go func() {
66 <-ctx.Done()
67 fmt.Fprintf(os.Stderr, "Main context canceled\n")
68 }()
69
70 // This exists to keep the parent around while all the children race
71 // It currently tests both a set of OS-level conditions and Kubernetes Deployments and StatefulSets
72 t.Run("RunGroup", func(t *testing.T) {
73 t.Run("IP available", func(t *testing.T) {
74 t.Parallel()
75 const timeoutSec = 10
76 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
77 defer cancel()
78 if err := waitForCondition(ctx, debugClient, "IPAssigned"); err != nil {
79 t.Errorf("Condition IPAvailable not met in %vs: %v", timeoutSec, err)
80 }
81 })
82 t.Run("Data available", func(t *testing.T) {
83 t.Parallel()
84 const timeoutSec = 30
85 ctx, cancel := context.WithTimeout(ctx, timeoutSec*time.Second)
86 defer cancel()
87 if err := waitForCondition(ctx, debugClient, "DataAvailable"); err != nil {
88 t.Errorf("Condition DataAvailable not met in %vs: %v", timeoutSec, err)
89 }
90 })
91 t.Run("Get Kubernetes Debug Kubeconfig", func(t *testing.T) {
92 t.Parallel()
93 selfCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
94 defer cancel()
95 clientSet, err := getKubeClientSet(selfCtx, debugClient, portMap[common.KubernetesAPIPort])
96 if err != nil {
97 t.Fatal(err)
98 }
99 testEventual(t, "Node is registered and ready", ctx, 30*time.Second, func(ctx context.Context) error {
100 nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
101 if err != nil {
102 return err
103 }
104 if len(nodes.Items) < 1 {
105 return errors.New("node not registered")
106 }
107 if len(nodes.Items) > 1 {
108 return errors.New("more than one node registered (but there is only one)")
109 }
110 node := nodes.Items[0]
111 for _, cond := range node.Status.Conditions {
112 if cond.Type != corev1.NodeReady {
113 continue
114 }
115 if cond.Status != corev1.ConditionTrue {
116 return fmt.Errorf("node not ready: %v", cond.Message)
117 }
118 }
119 return nil
120 })
121 testEventual(t, "Simple deployment", ctx, 30*time.Second, func(ctx context.Context) error {
122 _, err := clientSet.AppsV1().Deployments("default").Create(ctx, makeTestDeploymentSpec("test-deploy-1"), metav1.CreateOptions{})
123 return err
124 })
125 testEventual(t, "Simple deployment is running", ctx, 40*time.Second, func(ctx context.Context) error {
126 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-deploy-1"})
127 if err != nil {
128 return err
129 }
130 if len(res.Items) == 0 {
131 return errors.New("pod didn't get created")
132 }
133 pod := res.Items[0]
134 if podv1.IsPodAvailable(&pod, 1, metav1.NewTime(time.Now())) {
135 return nil
136 }
137 events, err := clientSet.CoreV1().Events("default").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=default", pod.Name)})
138 if err != nil || len(events.Items) == 0 {
139 return fmt.Errorf("pod is not ready: %v", pod.Status.Phase)
140 } else {
141 return fmt.Errorf("pod is not ready: %v", events.Items[0].Message)
142 }
143 })
144 testEventual(t, "Simple StatefulSet with PVC", ctx, 30*time.Second, func(ctx context.Context) error {
145 _, err := clientSet.AppsV1().StatefulSets("default").Create(ctx, makeTestStatefulSet("test-statefulset-1"), metav1.CreateOptions{})
146 return err
147 })
148 testEventual(t, "Simple StatefulSet with PVC is running", ctx, 40*time.Second, func(ctx context.Context) error {
149 res, err := clientSet.CoreV1().Pods("default").List(ctx, metav1.ListOptions{LabelSelector: "name=test-statefulset-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 })
167 })
168 })
169}