-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathrunner.go
126 lines (107 loc) · 2.95 KB
/
runner.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
package test
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/nabsul/k8s-ecr-login-renew/src/k8s"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func runTest(cfg config) {
cfg.t.Cleanup(func() { cleanup(cfg) })
t := cfg.t
c, err := k8s.GetClient()
if err != nil {
printError(t, err)
return
}
err = checkNamespaces(c, allNamespaces())
if err != nil {
printError(t, err)
return
}
t.Log("creating namespaces")
for _, ns := range allNamespaces() {
_, err := createNamespace(c, ns)
if err != nil {
printError(t, err)
return
}
}
t.Log("creating service and permissions")
err = createServiceAccount(c, []string{}, false)
if err != nil {
printError(t, err)
return
}
awsParams, err := getAwsParams(c)
awsId, ok1 := awsParams["ID"]
awsSecret, ok2 := awsParams["SECRET"]
awsRegion, ok3 := awsParams["REGION"]
if !(ok1 && ok2 && ok3) {
values := fmt.Sprintf("[%s], [%s], [%s]", awsId, awsSecret, awsRegion)
err = errors.New(fmt.Sprintf("one or more AWS Param not configured: %s", values))
printError(t, err)
return
}
t.Log("creating cron job")
err = initCronJob(c, cfg.targetNamespace, awsRegion, awsId, awsSecret)
if nil != err {
printError(t, err)
return
}
t.Log("running the job")
logs, err := runCronJob(c)
if nil != err {
printError(t, err)
return
}
t.Log("checking job logs")
if !strings.Contains(logs, "Fetching auth data from AWS... Success.") {
printError(t, errors.New(fmt.Sprintf("no AWS success message found in:\n%s", logs)))
}
expectedUpdates := len(cfg.successNamespaces)
actualUpdates := strings.Count(logs, "Updating secret in namespace")
if actualUpdates != expectedUpdates {
msg := fmt.Sprintf("unexpected number of updates %d != %d", expectedUpdates, actualUpdates)
printError(t, errors.New(msg))
}
for _, ns := range cfg.successNamespaces {
t.Logf("checking for success: %s", ns)
msg := fmt.Sprintf("Updating secret in namespace [%s]... success", ns)
if !strings.Contains(logs, msg) {
msg = fmt.Sprintf("no success message found for namespace [%s]", ns)
printError(t, errors.New(msg))
}
}
/* Not currently implemented
for _, ns := range cfg.failNamespaces {
t.Logf("checking for failure: %s", ns)
msg := fmt.Sprintf("Updating secret in namespace [%s]... failed:", ns)
if !strings.Contains(logs, msg) {
msg = fmt.Sprintf("no fail message found for namespace [%s]", ns)
printError(t, errors.New(msg))
}
}
*/
if t.Failed() {
printError(t, errors.New(logs))
}
}
// as long as we only create stuff in namespaces, deleting the namespaces should
func cleanup(cfg config) {
cfg.t.Log("cleaning up...")
c, err := k8s.GetClient()
if nil != err {
return
}
for _, ns := range allNamespaces() {
err = c.CoreV1().Namespaces().Delete(context.Background(), ns, metaV1.DeleteOptions{})
if err != nil {
fmt.Printf("failed to cleanup namespace [%s]: [%s}\n", ns, err)
}
}
cfg.t.Log("giving namespaces time to go away...")
time.Sleep(10 * time.Second)
}