-
Notifications
You must be signed in to change notification settings - Fork 137
/
tc000010_no_outputs_test.go
217 lines (198 loc) · 7.5 KB
/
tc000010_no_outputs_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
package controllers
import (
"context"
"fmt"
"os"
"testing"
"time"
. "github.com/onsi/gomega"
infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// +kubebuilder:docs-gen:collapse=Imports
func Test_000010_no_outputs_test(t *testing.T) {
Spec("This spec describes the behaviour of a Terraform resource with no backend, and `auto` approve.")
It("should be reconciled to have available outputs.")
const (
sourceName = "test-tf-controller-no-output"
terraformName = "helloworld-no-outputs"
)
g := NewWithT(t)
ctx := context.Background()
Given("a GitRepository")
By("defining a new GitRepository resource.")
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},
},
}
By("creating the GitRepository resource in the cluster.")
It("should be created successfully.")
g.Expect(k8sClient.Create(ctx, &testRepo)).Should(Succeed())
defer waitResourceToBeDelete(g, &testRepo)
Given("the GitRepository's reconciled status.")
By("setting the GitRepository's status, with the downloadable BLOB's URL, and the correct checksum.")
updatedTime := time.Now()
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",
},
},
Artifact: &sourcev1.Artifact{
Path: "gitrepository/flux-system/test-tf-controller/b8e362c206e3d0cbb7ed22ced771a0056455a2fb.tar.gz",
URL: server.URL() + "/file.tar.gz",
Revision: "master/b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
Digest: "sha256:80ddfd18eb96f7d31cadc1a8a5171c6e2d95df3f6c23b0ed9cd8dddf6dba1406", // 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())
Given("a Terraform resource with auto approve, attached to the given GitRepository resource.")
By("creating a new TF resource and attaching to the repo via `sourceRef`.")
var helloWorldTF infrav1.Terraform
err := helloWorldTF.FromBytes([]byte(fmt.Sprintf(`
apiVersion: infra.contrib.fluxcd.io/v1alpha2
kind: Terraform
metadata:
name: %s
namespace: flux-system
spec:
approvePlan: auto
path: ./terraform-hello-world-example
sourceRef:
kind: GitRepository
name: %s
namespace: flux-system
interval: 10s
`, terraformName, sourceName)), runnerServer.Scheme)
g.Expect(err).ToNot(HaveOccurred())
It("should be created and attached successfully.")
g.Expect(k8sClient.Create(ctx, &helloWorldTF)).Should(Succeed())
defer waitResourceToBeDelete(g, &helloWorldTF)
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())
It("should be reconciled and contain some status conditions.")
By("checking that the TF resource's status conditions has some elements.")
g.Eventually(func() int {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return -1
}
return len(createdHelloWorldTF.Status.Conditions)
}, timeout, interval).ShouldNot(BeZero())
It("should be planned.")
By("checking that the Plan's reason of the TF resource become `TerraformPlannedWithChanges`.")
g.Eventually(func() 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,
"Message": c.Message,
}
}
}
return createdHelloWorldTF.Status
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": infrav1.ConditionTypePlan,
"Reason": infrav1.PlannedWithChangesReason,
"Message": "Plan generated",
}))
It("should generate the Secret containing the plan named with branch and commit id.")
By("checking that the Secret contains plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb in its labels.")
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"],
"Is TFPlan empty ?": string(tfplanSecret.Data["tfplan"]) == "",
"HasEncodingAnnotation": tfplanSecret.Annotations["encoding"] == "gzip",
}
}, timeout, interval).Should(Equal(map[string]interface{}{
"SavedPlan": "plan-master-b8e362c206",
"Is TFPlan empty ?": false,
"HasEncodingAnnotation": true,
}))
It("should contain an Apply condition saying that the plan were apply successfully.")
By("checking that the reason of the Apply condition is TerraformAppliedSucceed, and the LastAppliedPlan is the 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,
"Message": c.Message,
"LastAppliedPlan": createdHelloWorldTF.Status.Plan.LastApplied,
}
}
}
return nil
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": infrav1.ConditionTypeApply,
"Reason": infrav1.TFExecApplySucceedReason,
"Message": "Applied successfully",
"LastAppliedPlan": "plan-master-b8e362c206",
}))
It("should have an available output.")
By("checking that the Terraform resource's .status.availableOutputs contains hello_world as an output name.")
g.Eventually(func() []string {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if err != nil {
return nil
}
return createdHelloWorldTF.Status.AvailableOutputs
}, timeout, interval).Should(Equal([]string{"hello_world"}))
if os.Getenv("DISABLE_TF_K8S_BACKEND") == "1" {
It("should not produce a Secret because the controller runs locally, outside Kubernetes.")
By("checking there are no secret generated by default.")
tfStateKey := types.NamespacedName{Namespace: "flux-system", Name: "tfstate-default-" + terraformName}
tfStateSecret := corev1.Secret{}
g.Eventually(func() string {
err := k8sClient.Get(ctx, tfStateKey, &tfStateSecret)
if err != nil {
return err.Error()
}
return tfStateSecret.Name
}, timeout, interval).Should(Equal(fmt.Sprintf("secrets \"tfstate-default-%s\" not found", terraformName)))
} else {
// TODO there's must be the default tfstate secret
}
}