-
Notifications
You must be signed in to change notification settings - Fork 139
/
tc000120_delete_test.go
171 lines (152 loc) · 5.52 KB
/
tc000120_delete_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
package controllers
import (
"context"
"testing"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
. "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_000120_delete_test(t *testing.T) {
const (
sourceName = "test-tf-controller-delete"
terraformName = "helloworld-delete"
)
g := NewWithT(t)
ctx := context.Background()
By("creating a new Git repository object")
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},
},
}
g.Expect(k8sClient.Create(ctx, &testRepo)).Should(Succeed())
defer waitResourceToBeDelete(g, &testRepo)
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",
},
},
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",
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")
helloWorldTF := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName,
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
ApprovePlan: "auto",
Path: "./terraform-hello-world-example",
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
Interval: metav1.Duration{Duration: time.Second * 10},
WriteOutputsToSecret: &infrav1.WriteOutputsToSecretSpec{
Name: "tf-output-" + terraformName,
Outputs: []string{
"hello_world",
},
},
},
}
g.Expect(k8sClient.Create(ctx, &helloWorldTF)).Should(Succeed())
defer waitResourceToBeDelete(g, &helloWorldTF)
By("checking that the hello world TF got created")
helloWorldTFKey := types.NamespacedName{Namespace: "flux-system", Name: terraformName}
createdHelloWorldTF := infrav1.Terraform{}
// We'll need to retry getting this newly created Terraform, Given that creation may not immediately happen.
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 output secret contains a binary data")
outputKey := types.NamespacedName{Namespace: "flux-system", Name: "tf-output-" + terraformName}
outputSecret := corev1.Secret{}
g.Eventually(func() (int, error) {
err := k8sClient.Get(ctx, outputKey, &outputSecret)
if err != nil {
return -1, err
}
return len(outputSecret.Data), nil
}, timeout, interval).Should(Equal(1))
By("checking that the TF output secrets contains the correct output provisioned By the TF hello world")
// Value is a JSON representation of TF's OutputMeta
expectedOutputValue := map[string]string{
"Name": "tf-output-" + terraformName,
"Namespace": "flux-system",
"Value": "Hello, World!",
"OwnerRef[0]": string(createdHelloWorldTF.UID),
}
g.Eventually(func() (map[string]string, error) {
err := k8sClient.Get(ctx, outputKey, &outputSecret)
value := string(outputSecret.Data["hello_world"])
return map[string]string{
"Name": outputSecret.Name,
"Namespace": outputSecret.Namespace,
"Value": value,
"OwnerRef[0]": string(outputSecret.OwnerReferences[0].UID),
}, err
}, timeout, interval).Should(Equal(expectedOutputValue), "expected output %v", expectedOutputValue)
g.Expect(k8sClient.Delete(ctx, &createdHelloWorldTF)).Should(Succeed())
tfplanKey := types.NamespacedName{Namespace: "flux-system", Name: "tfplan-default-" + terraformName}
tfplanSecret := corev1.Secret{}
g.Eventually(func() bool {
err := k8sClient.Get(ctx, tfplanKey, &tfplanSecret)
if apierrors.IsNotFound(err) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
g.Eventually(func() bool {
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
if apierrors.IsNotFound(err) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
g.Eventually(func() bool {
err := k8sClient.Get(ctx, outputKey, &outputSecret)
if apierrors.IsNotFound(err) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
}