-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.go
172 lines (157 loc) · 5.15 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package kommons
import (
"context"
goerrors "errors"
"fmt"
"strings"
"github.com/flanksource/commons/console"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
typedv1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
func TestDeploy(client kubernetes.Interface, ns string, deploymentName string, t *console.TestResults) {
if client == nil {
t.Failf(deploymentName, "failed to get kubernetes client")
return
}
deployment, err := client.AppsV1().Deployments(ns).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if errors.IsNotFound(err) {
t.Failf(deploymentName, "deployment not found")
return
}
labelMap, _ := metav1.LabelSelectorAsMap(deployment.Spec.Selector)
TestPodsByLabels(client, deploymentName, ns, labelMap, t)
}
func TestStatefulSet(client kubernetes.Interface, ns string, name string, t *console.TestResults) {
if client == nil {
t.Failf(name, "failed to get kubernetes client")
return
}
statefulset, err := client.AppsV1().StatefulSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
t.Failf(name, "statefulset not found")
return
}
labelMap, _ := metav1.LabelSelectorAsMap(statefulset.Spec.Selector)
TestPodsByLabels(client, name, ns, labelMap, t)
}
func TestDaemonSet(client kubernetes.Interface, ns string, name string, t *console.TestResults) {
testName := name
if client == nil {
t.Failf(testName, "failed to get kubernetes client")
return
}
daemonset, err := client.AppsV1().DaemonSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
t.Failf(testName, "daemonset not found")
return
}
labelMap, _ := metav1.LabelSelectorAsMap(daemonset.Spec.Selector)
TestPodsByLabels(client, testName, ns, labelMap, t)
}
func TestPodsByLabels(client kubernetes.Interface, testName string, ns string, labelMap map[string]string, t *console.TestResults) {
if client == nil {
t.Failf(testName, "failed to get kubernetes client")
return
}
events := client.CoreV1().Events(ns)
pods, _ := client.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labelMap).String(),
})
if len(pods.Items) == 0 {
t.Failf(testName, "No pods found for %s", testName)
return
}
fails := make([]error, 0)
for _, pod := range pods.Items {
if err := TestPod(testName, client, events, pod); err != nil {
fails = append(fails, err)
}
}
if len(fails) > 0 {
message := fmt.Sprintf("[%s] %d of %d pods failed: ", testName, len(fails), len(pods.Items))
for _, err := range fails {
message += err.Error() + ". "
}
message = strings.TrimSuffix(message, " ")
t.Failf(testName, message)
}
t.Passf(testName, "[%s] %d of %d pods passed", testName, len(pods.Items), len(pods.Items))
}
func TestPod(testName string, client kubernetes.Interface, events typedv1.EventInterface, pod v1.Pod) error {
if client == nil {
return fmt.Errorf("%s: failed to get kubernetes client", testName)
}
conditions := true
// for _, condition := range pod.Status.Conditions {
// if condition.Status == v1.ConditionFalse {
// t.Failf(ns, "%s => %s: %s", pod.Name, condition.Type, condition.Message)
// conditions = false
// }
// }
if conditions && pod.Status.Phase == v1.PodRunning || pod.Status.Phase == v1.PodSucceeded {
return nil
} else {
events, err := events.List(context.TODO(), metav1.ListOptions{
FieldSelector: "involvedObject.name=" + pod.Name,
})
if err != nil {
return goerrors.New(fmt.Sprintf("%s => %s, failed to get events %+v",
pod.Name, pod.Status.Phase, err))
}
msg := ""
for _, event := range events.Items {
if event.Type == "Normal" {
continue
}
msg += fmt.Sprintf("%s: %s ", event.Reason, event.Message)
}
if pod.Spec.NodeName == "" {
msg += fmt.Sprintf("nodeName=<blank>: pod not scheduled yet ")
}
return goerrors.New(fmt.Sprintf("%s/%s=%s %s ", pod.Namespace, pod.Name, pod.Status.Phase, msg))
}
// check all pods running or completed with < 3 restarts
// check unbound pvcs
// check all pod liveness / readiness
}
func GetOwner(pod v1.Pod) string {
return pod.GetName()
}
func TestNamespace(client kubernetes.Interface, ns string, t *console.TestResults) {
if client == nil {
t.Failf(ns, "failed to get kubernetes client")
return
}
pods := client.CoreV1().Pods(ns)
events := client.CoreV1().Events(ns)
list, err := pods.List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Failf(ns, "Failed to get pods for %s: %v", ns, err)
return
}
if len(list.Items) == 0 {
_, err := client.CoreV1().Namespaces().Get(context.TODO(), ns, metav1.GetOptions{})
if errors.IsNotFound(err) {
t.Failf(ns, "[%s] namespace not found, skipping", ns)
} else {
t.Failf(ns, "[%s] Expected pods but none running", ns)
}
return
}
failed := 0
for _, pod := range list.Items {
if err := TestPod(ns, client, events, pod); err != nil {
t.Failf(ns, "%s %s", GetOwner(pod), err)
failed++
}
}
if failed > 0 {
t.Failf(ns, "[%s] %d of %d pods failed: ", ns, failed, len(list.Items))
} else {
t.Passf(ns, "[%s] %d of %d pods passed", ns, len(list.Items), len(list.Items))
}
}