-
Notifications
You must be signed in to change notification settings - Fork 128
/
tc000052_plan_and_manual_approve_with_files_test.go
266 lines (241 loc) · 9.12 KB
/
tc000052_plan_and_manual_approve_with_files_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package controllers
import (
"testing"
"github.com/weaveworks/tf-controller/utils"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
. "github.com/onsi/gomega"
infrav1 "github.com/weaveworks/tf-controller/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"context"
"time"
)
// +kubebuilder:docs-gen:collapse=Imports
func Test_000052_plan_and_manual_approve_with_files_test(t *testing.T) {
Spec("This spec describes the behaviour of a Terraform resource that is planned and manually approved with a file generated inside the filesystem.")
It("should be reconciled to become planned.")
It("should wait for a manually approval.")
It("then should be reconciled to the applied state.")
const (
sourceName = "gr-plan-and-manual-approve-with-files"
terraformName = "tf-plan-and-manual-approve-with-files"
)
ctx := context.Background()
g := NewWithT(t)
Given("a GitRepository")
By("defining a new GitRepository resource.")
updatedTime := time.Now()
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",
},
}
By("creating the GitRepository resource in the cluster.")
It("should be created successfully.")
g.Expect(k8sClient.Create(ctx, &testRepo)).Should(Succeed())
defer func() { g.Expect(k8sClient.Delete(ctx, &testRepo)).Should(Succeed()) }()
Given("the GitRepository's reconciled status")
By("setting the GitRepository's status, with the downloadable BLOB's 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-data-archive.tar.gz",
Artifact: &sourcev1.Artifact{
Path: "gitrepository/flux-system/test-tf-controller/b8e362c206e3d0cbb7ed22ced771a0056455a2fb.tar.gz",
URL: server.URL() + "/tf-data-archive.tar.gz",
Revision: "master/b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
Checksum: "0ab01c8057debc27f4b355885211228e686f48693c56a2b3af7c2b42b02213a6", // must be the real checksum value
LastUpdateTime: metav1.Time{Time: updatedTime},
},
}
It("should be updated successfully.")
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())
// Obtain testEnvKubeConfigPath
testEnvKubeConfigPath, err := findKubeConfig(testEnv)
g.Expect(err).Should(BeNil())
Given("a Terraform resource with manual approval, attached to the given GitRepository resource")
By("creating a new TF resource without specifying the .spec.approvePlan field.")
By("attaching the TF resource to the repo via `sourceRef`.")
inClusterConfig := false
helloWorldTF := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName,
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
BackendConfig: &infrav1.BackendConfigSpec{
SecretSuffix: terraformName,
InClusterConfig: &inClusterConfig,
ConfigPath: testEnvKubeConfigPath,
},
// Note that we do not specify the `ApprovePlan` field
Path: "./tf-data-archive",
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
RefreshBeforeApply: true,
Interval: metav1.Duration{Duration: time.Second * 10},
Vars: []infrav1.Variable{
{
Name: "kubeconfig",
Value: utils.MustJSONEncodeBytes(t, []byte(testEnvKubeConfigPath)),
},
{
Name: "context",
Value: utils.MustJSONEncodeBytes(t, []byte("envtest")),
},
{
Name: "config_name",
Value: utils.MustJSONEncodeBytes(t, []byte("cm-"+terraformName)),
},
},
},
}
It("should be created and attached successfully.")
g.Expect(k8sClient.Create(ctx, &helloWorldTF)).Should(Succeed())
defer func() { g.Expect(k8sClient.Delete(ctx, &helloWorldTF)).Should(Succeed()) }()
By("checking that the TF resource existed inside the cluster.")
helloWorldTFKey := types.NamespacedName{Namespace: "flux-system", Name: terraformName}
createdHelloWorldTF := infrav1.Terraform{}
g.Eventually(func() bool {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return false
}
return true
}, timeout, interval).Should(BeTrue())
By("checking that the TF's status conditions got reconciled.")
g.Eventually(func() int {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return -1
}
return len(createdHelloWorldTF.Status.Conditions)
}, timeout*3, interval).ShouldNot(BeZero())
Given("the plan id is the `plan` plus the branch name (master) plus the commit id.")
const planId = "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb"
By("checking that the planned status of the TF is created successfully.")
By("checking the reason is `TerraformPlannedWithChanges`.")
By("checking the pending plan is the $planId.")
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return nil
}
for _, c := range createdHelloWorldTF.Status.Conditions {
if c.Type == "Plan" {
return map[string]interface{}{
"Type": c.Type,
"Reason": c.Reason,
"Pending": createdHelloWorldTF.Status.Plan.Pending,
}
}
}
return nil
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": "Plan",
"Reason": "TerraformPlannedWithChanges",
"Pending": planId,
}))
By("checking the message of the ready status contains $planId.")
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return nil
}
for _, c := range createdHelloWorldTF.Status.Conditions {
if c.Type == "Ready" {
return map[string]interface{}{
"Type": c.Type,
"Reason": c.Reason,
"Message": c.Message,
}
}
}
return nil
}, timeout*3, interval).Should(Equal(map[string]interface{}{
"Type": "Ready",
"Reason": "TerraformPlannedWithChanges",
"Message": "Plan generated: set approvePlan: \"plan-master-b8e362c206\" to approve this plan.",
}))
By("checking that the planned secret is created.")
By("checking that the label of the planned secret is the $planId.")
tfplanKey := types.NamespacedName{Namespace: "flux-system", Name: "tfplan-default-" + terraformName}
tfplanSecret := corev1.Secret{}
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, tfplanKey, &tfplanSecret)
if err != nil {
return nil
}
return map[string]interface{}{
"SavedPlan": tfplanSecret.Annotations["savedPlan"],
"TFPlanEmpty": string(tfplanSecret.Data["tfplan"]) == "",
"HasEncodingAnnotation": tfplanSecret.Annotations["encoding"] == "gzip",
}
}, timeout, interval).Should(Equal(map[string]interface{}{
"SavedPlan": planId,
"TFPlanEmpty": false,
"HasEncodingAnnotation": true,
}))
time.Sleep(10 * time.Second)
By("setting the .spec.approvePlan to be plan-main- and a part of commit id (b8e362c206) to approve the plan.")
createdHelloWorldTF.Spec.ApprovePlan = "plan-master-b8e362c206"
g.Expect(k8sClient.Update(ctx, &createdHelloWorldTF)).Should(Succeed())
It("should continue the reconciliation process to the apply state.")
By("checking that the applied status reason is TerraformAppliedSucceed.")
By("checking that the last applied plan is really the pending plan.")
g.Eventually(func() map[string]interface{} {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return nil
}
for _, c := range createdHelloWorldTF.Status.Conditions {
if c.Type == "Apply" {
return map[string]interface{}{
"Type": c.Type,
"Reason": c.Reason,
"LastAppliedPlan": createdHelloWorldTF.Status.Plan.LastApplied,
}
}
}
return nil
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": "Apply",
"Reason": infrav1.TFExecApplySucceedReason,
"LastAppliedPlan": planId,
}))
// TODO check Output condition
It("should contain a list of available outputs in the status.")
By("checking that .status.availableOutput in the TF resource.")
g.Eventually(func() []string {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return nil
}
return createdHelloWorldTF.Status.AvailableOutputs
}, timeout, interval).Should(Equal([]string{"etag"}))
}