-
Notifications
You must be signed in to change notification settings - Fork 128
/
tc000242_bad_health_checks_test.go
208 lines (192 loc) · 6.38 KB
/
tc000242_bad_health_checks_test.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package controllers
import (
"context"
"testing"
"time"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
. "github.com/onsi/gomega"
infrav1 "github.com/weaveworks/tf-controller/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// +kubebuilder:docs-gen:collapse=Imports
func Test_000242_bad_healt_checks_test(t *testing.T) {
Spec("This spec describes behaviour when health checks are specified")
const (
sourceName = "health-check-bad"
terraformName = "tf-health-check-bad"
)
g := NewWithT(t)
ctx := context.Background()
updatedTime := time.Now()
It("should do health checks")
By("creating a new Git repository object")
testRepo := sourcev1.GitRepository{
ObjectMeta: metav1.ObjectMeta{
Name: sourceName,
Namespace: "flux-system",
},
Spec: sourcev1.GitRepositorySpec{
URL: "https://github.com/openshift-fluxv2-poc/podinfo",
Reference: &sourcev1.GitRepositoryRef{
Branch: "master",
},
Interval: metav1.Duration{Duration: time.Second * 30},
GitImplementation: "go-git",
},
}
g.Expect(k8sClient.Create(ctx, &testRepo)).Should(Succeed())
By("setting the git repo status object, the URL, and the correct checksum")
testRepo.Status = sourcev1.GitRepositoryStatus{
ObservedGeneration: int64(1),
Conditions: []metav1.Condition{
{
Type: "Ready",
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: updatedTime},
Reason: "GitOperationSucceed",
Message: "Fetched revision: master/b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
},
},
URL: server.URL() + "/tf-health-check.tar.gz",
Artifact: &sourcev1.Artifact{
Path: "gitrepository/flux-system/test-tf-controller/b8e362c206e3d0cbb7ed22ced771a0056455a2fb.tar.gz",
URL: server.URL() + "/tf-health-check.tar.gz",
Revision: "master/b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
Checksum: "84b1410341b0e87d811bb1b812741e84a74ea00db851f88fd0855589b5093d14", // must be the real checksum value
LastUpdateTime: metav1.Time{Time: updatedTime},
},
}
g.Expect(k8sClient.Status().Update(ctx, &testRepo)).Should(Succeed())
By("checking that the status and its URL gets reconciled")
gitRepoKey := types.NamespacedName{Namespace: "flux-system", Name: sourceName}
createdRepo := sourcev1.GitRepository{}
g.Expect(k8sClient.Get(ctx, gitRepoKey, &createdRepo)).Should(Succeed())
By("creating a new TF and attaching to the repo, with approve plan set to auto and health checks defined")
testEnvKubeConfigPath, err := findKubeConfig(testEnv)
g.Expect(err).Should(BeNil())
inClusterConfig := false
healthCheckTF := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName,
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
Interval: metav1.Duration{Duration: time.Second * 5},
ApprovePlan: "auto",
Path: "./tf-health-check-example",
BackendConfig: &infrav1.BackendConfigSpec{
SecretSuffix: terraformName,
InClusterConfig: &inClusterConfig,
ConfigPath: testEnvKubeConfigPath,
},
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
HealthChecks: []infrav1.HealthCheck{
{
Name: "tcpTestBadPort",
Address: "{{.foo}}:81",
Type: "tcp",
},
},
WriteOutputsToSecret: &infrav1.WriteOutputsToSecretSpec{
Name: "tf-output-" + terraformName,
},
},
}
g.Expect(k8sClient.Create(ctx, &healthCheckTF)).Should(Succeed())
By("checking that the health check example TF gets created")
healthCheckTFKey := types.NamespacedName{Namespace: "flux-system", Name: terraformName}
createdhealthCheckTF := infrav1.Terraform{}
g.Eventually(func() bool {
err := k8sClient.Get(ctx, healthCheckTFKey, &createdhealthCheckTF)
if err != nil {
return false
}
return true
}, timeout, interval).Should(BeTrue())
By("checking that the health checks should fail")
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, healthCheckTFKey, &createdhealthCheckTF)
if err != nil {
return nil
}
for _, c := range createdhealthCheckTF.Status.Conditions {
if c.Type == "HealthCheck" {
return map[string]interface{}{
"Type": c.Type,
"Reason": c.Reason,
}
}
}
return nil
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": "HealthCheck",
"Reason": "HealthChecksFailed",
}))
healthCheckTF2 := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName + "2",
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
Interval: metav1.Duration{Duration: time.Second * 5},
ApprovePlan: "auto",
Path: "./tf-health-check-example",
BackendConfig: &infrav1.BackendConfigSpec{
SecretSuffix: terraformName + "2",
InClusterConfig: &inClusterConfig,
ConfigPath: testEnvKubeConfigPath,
},
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
HealthChecks: []infrav1.HealthCheck{
{
Name: "httpTestBadUrl",
URL: server.URL() + "/something-else",
Type: "http",
Timeout: &metav1.Duration{Duration: time.Second * 5},
},
},
WriteOutputsToSecret: &infrav1.WriteOutputsToSecretSpec{
Name: "tf-output-" + terraformName,
},
},
}
g.Expect(k8sClient.Create(ctx, &healthCheckTF2)).Should(Succeed())
By("checking that the health check example TF 2 gets created")
healthCheckTFKey2 := types.NamespacedName{Namespace: "flux-system", Name: terraformName}
createdhealthCheckTF2 := infrav1.Terraform{}
g.Eventually(func() bool {
err := k8sClient.Get(ctx, healthCheckTFKey2, &createdhealthCheckTF2)
if err != nil {
return false
}
return true
}, timeout, interval).Should(BeTrue())
By("checking that the health checks example 2 should fail")
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, healthCheckTFKey2, &createdhealthCheckTF2)
if err != nil {
return nil
}
for _, c := range createdhealthCheckTF2.Status.Conditions {
if c.Type == "HealthCheck" {
return map[string]interface{}{
"Type": c.Type,
"Reason": c.Reason,
}
}
}
return nil
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": "HealthCheck",
"Reason": "HealthChecksFailed",
}))
}