This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (129 loc) · 3.93 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/alex-leonhardt/kube-smoketest/pkg/smoketests"
"github.com/golang/glog"
"github.com/hashicorp/go-multierror"
)
func main() {
debug := flag.Bool("debug", false, "do not delete namespace at the end of the test, you must manually delete the NS and wait for it to be gone before re-running kube-smoketest")
flag.Set("logtostderr", "true")
flag.Parse()
kubeconfigpath := os.Getenv("KUBECONFIG")
if kubeconfigpath == "" {
homeDir, _ := os.UserHomeDir()
kubeconfigpath = homeDir + "/.kube/config"
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigpath)
if err != nil {
glog.Fatalln(err.Error())
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Fatalln(err.Error())
}
// ------------------------
errors := multierror.Error{} // collect all errors here...
// ------------------------
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// -------------------------------------------------
err = smoketests.ComponentStatus(ctx, client)
if err != nil {
glog.Errorf("\t🔴 Component statuses: %v", err)
errors.Errors = append(errors.Errors, err)
LogAndExit(errors) // exit early as if components are failed
}
if err == nil {
glog.Infoln("\t✅ Component statuses")
}
// -------------------------------------------------
err = smoketests.CreateNamespace(ctx, client)
if err != nil {
glog.Errorf("\t🔴 Create namespace: %v", err)
errors.Errors = append(errors.Errors, err)
LogAndExit(errors) // exit early as if there's no namespace, then we cannot run
}
if err == nil {
glog.Infoln("\t✅ Create namespace")
}
// -------------------------------------------------
err = smoketests.PodLogs(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 Pod + Logs: %v", err)
}
if err == nil {
glog.Infoln("\t✅ Pod + Logs")
}
// -------------------------------------------------
err = smoketests.CreateDeployment(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 Deployment: %v", err)
}
if err == nil {
glog.Infoln("\t✅ Deployment")
}
// -------------------------------------------------
err = smoketests.CreateService(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 Service: %v", err)
}
if err == nil {
glog.Infoln("\t✅ Service")
}
// -------------------------------------------------
err = smoketests.CreateNodePortService(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 NodePort Service: %v", err)
}
if err == nil {
glog.Infoln("\t✅ NodePort Service")
}
// -------------------------------------------------
err = smoketests.CreateSecret(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 Secret: %v", err)
}
if err == nil {
glog.Infoln("\t✅ Secret")
}
// -------------------------------------------------
// don't delete the namespace when debug is set to true
if *debug != false {
glog.Infoln("\t⚠️ Namespace remains for debugging")
LogAndExit(errors)
}
err = smoketests.DeleteNamespace(ctx, client)
if err != nil {
errors.Errors = append(errors.Errors, err)
glog.Errorf("\t🔴 Delete namespace: %v", err)
}
if err == nil {
glog.Infoln("\t✅ Delete namespace")
}
LogAndExit(errors)
}
// LogAndExit does just that...
func LogAndExit(errors multierror.Error) {
fmt.Println("")
fmt.Println(strings.Repeat("-", 20), "RESULT", strings.Repeat("-", 20))
fmt.Println("")
if errors.ErrorOrNil() != nil {
glog.Errorln("\t🔴 FAILED: too many errors found, expected: 0, actual:", len(errors.Errors))
} else {
glog.Infoln("\t✅ SUCCESS: all tests passed")
}
os.Exit(len(errors.Errors)) // Exits > 0 if any errors occured :)
}