Skip to content

Commit 40dc197

Browse files
authored
replace ginkgo time based test with trigger based std test (#415)
* replace ginkgo time based test with trigger based std test * reduce logging noise
1 parent eed6c30 commit 40dc197

8 files changed

+1271
-1318
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package integration_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/golang/mock/gomock"
9+
tfaplv1beta1 "github.com/utilitywarehouse/terraform-applier/api/v1beta1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/types"
12+
)
13+
14+
func TestModuleController_Filter(t *testing.T) {
15+
const (
16+
moduleNamespace = "default"
17+
commitHash = "a1b2c3d4"
18+
commitMsg = "test commit"
19+
labelSelectorKey = "terraform-applier.uw.systems/test"
20+
labelSelectorKeyInvalid = "terraform-applier.uw.systems/test-invalid"
21+
)
22+
23+
setup := func(t *testing.T, runCh chan *tfaplv1beta1.Run) *gomock.Controller {
24+
ctrl := setupTest(t)
25+
26+
// reset Time
27+
fakeClock.T = time.Date(2022, 02, 01, 01, 00, 00, 0000, time.UTC)
28+
testReconciler.Runner = testMockRunner1
29+
30+
// add label selector
31+
testFilter.LabelSelectorKey = labelSelectorKey
32+
testFilter.LabelSelectorValue = "true"
33+
34+
// Trigger Job run as soon as module is created
35+
testRepos.EXPECT().Hash(gomock.Any(), "https://host.xy/dummy/repo.git", "HEAD", "hello-filter-test").
36+
Return(commitHash, nil).AnyTimes()
37+
testRepos.EXPECT().Subject(gomock.Any(), "https://host.xy/dummy/repo.git", commitHash).
38+
Return(commitMsg, nil).AnyTimes()
39+
40+
testMetrics.EXPECT().SetRunPending(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
41+
42+
// Configure Mock to send to channel
43+
testMockRunner1.EXPECT().Start(gomock.Any(), gomock.Any()).
44+
DoAndReturn(func(run *tfaplv1beta1.Run, _ chan struct{}) bool {
45+
runCh <- run
46+
return true
47+
}).AnyTimes()
48+
49+
return ctrl
50+
}
51+
52+
t.Run("Should send module with valid selector label selector to job queue", func(t *testing.T) {
53+
runCh := make(chan *tfaplv1beta1.Run, 1)
54+
ctrl := setup(t, runCh)
55+
defer ctrl.Finish()
56+
57+
const (
58+
moduleName = "filter-test-module1"
59+
repoURL = "https://host.xy/dummy/repo.git"
60+
path = "hello-filter-test"
61+
)
62+
63+
ctx := context.Background()
64+
module := &tfaplv1beta1.Module{
65+
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: moduleName,
68+
Namespace: moduleNamespace,
69+
Labels: map[string]string{labelSelectorKey: "true"},
70+
},
71+
Spec: tfaplv1beta1.ModuleSpec{Schedule: "1 * * * *", RepoURL: repoURL, Path: path},
72+
}
73+
74+
if err := k8sClient.Create(ctx, module); err != nil {
75+
t.Fatalf("Failed to create module: %v", err)
76+
}
77+
// delete module to stopping requeue
78+
defer func() {
79+
if err := k8sClient.Delete(ctx, module); err != nil {
80+
t.Errorf("Failed to delete module: %v", err)
81+
}
82+
}()
83+
84+
moduleLookupKey := types.NamespacedName{Name: moduleName, Namespace: moduleNamespace}
85+
86+
// Wait for run on channel
87+
select {
88+
case run := <-runCh:
89+
if run.Module != moduleLookupKey {
90+
t.Errorf("Expected run for %v, got %v", moduleLookupKey, run.Module)
91+
}
92+
case <-time.After(5 * time.Second):
93+
t.Fatalf("Timeout waiting for run of module %s", moduleName)
94+
}
95+
})
96+
97+
t.Run("Should not send module with valid selector label key but invalid value to job queue", func(t *testing.T) {
98+
runCh := make(chan *tfaplv1beta1.Run, 1)
99+
ctrl := setup(t, runCh)
100+
defer ctrl.Finish()
101+
102+
const (
103+
moduleName = "filter-test-module2"
104+
repoURL = "https://host.xy/dummy/repo.git"
105+
path = "hello-filter-test"
106+
)
107+
108+
ctx := context.Background()
109+
module := &tfaplv1beta1.Module{
110+
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
111+
ObjectMeta: metav1.ObjectMeta{
112+
Name: moduleName,
113+
Namespace: moduleNamespace,
114+
Labels: map[string]string{labelSelectorKey: "false"},
115+
},
116+
Spec: tfaplv1beta1.ModuleSpec{Schedule: "1 * * * *", RepoURL: repoURL, Path: path},
117+
}
118+
119+
if err := k8sClient.Create(ctx, module); err != nil {
120+
t.Fatalf("Failed to create module: %v", err)
121+
}
122+
// delete module to stopping requeue
123+
defer func() {
124+
if err := k8sClient.Delete(ctx, module); err != nil {
125+
t.Errorf("Failed to delete module: %v", err)
126+
}
127+
}()
128+
129+
// Verify no run is triggered
130+
select {
131+
case <-runCh:
132+
t.Fatal("Run should NOT have been triggered")
133+
case <-time.After(2 * time.Second):
134+
// Success
135+
}
136+
})
137+
138+
t.Run("Should not send module with missing selector label selector to job queue", func(t *testing.T) {
139+
runCh := make(chan *tfaplv1beta1.Run, 1)
140+
ctrl := setup(t, runCh)
141+
defer ctrl.Finish()
142+
143+
const (
144+
moduleName = "filter-test-module3"
145+
repoURL = "https://host.xy/dummy/repo.git"
146+
path = "hello-filter-test"
147+
)
148+
149+
ctx := context.Background()
150+
module := &tfaplv1beta1.Module{
151+
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
152+
ObjectMeta: metav1.ObjectMeta{
153+
Name: moduleName,
154+
Namespace: moduleNamespace,
155+
Labels: map[string]string{labelSelectorKeyInvalid: "true"},
156+
},
157+
Spec: tfaplv1beta1.ModuleSpec{Schedule: "1 * * * *", RepoURL: repoURL, Path: path},
158+
}
159+
160+
if err := k8sClient.Create(ctx, module); err != nil {
161+
t.Fatalf("Failed to create module: %v", err)
162+
}
163+
// delete module to stopping requeue
164+
defer func() {
165+
if err := k8sClient.Delete(ctx, module); err != nil {
166+
t.Errorf("Failed to delete module: %v", err)
167+
}
168+
}()
169+
170+
select {
171+
case <-runCh:
172+
t.Fatal("Run should NOT have been triggered")
173+
case <-time.After(2 * time.Second):
174+
// Success
175+
}
176+
})
177+
178+
t.Run("Should not send module with no labels to job queue", func(t *testing.T) {
179+
runCh := make(chan *tfaplv1beta1.Run, 1)
180+
ctrl := setup(t, runCh)
181+
defer ctrl.Finish()
182+
183+
const (
184+
moduleName = "filter-test-module4"
185+
repoURL = "https://host.xy/dummy/repo.git"
186+
path = "hello-filter-test"
187+
)
188+
189+
ctx := context.Background()
190+
module := &tfaplv1beta1.Module{
191+
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
192+
ObjectMeta: metav1.ObjectMeta{
193+
Name: moduleName,
194+
Namespace: moduleNamespace,
195+
},
196+
Spec: tfaplv1beta1.ModuleSpec{Schedule: "1 * * * *", RepoURL: repoURL, Path: path},
197+
}
198+
199+
if err := k8sClient.Create(ctx, module); err != nil {
200+
t.Fatalf("Failed to create module: %v", err)
201+
}
202+
// delete module to stopping requeue
203+
defer func() {
204+
if err := k8sClient.Delete(ctx, module); err != nil {
205+
t.Errorf("Failed to delete module: %v", err)
206+
}
207+
}()
208+
209+
select {
210+
case <-runCh:
211+
t.Fatal("Run should NOT have been triggered")
212+
case <-time.After(2 * time.Second):
213+
// Success
214+
}
215+
})
216+
}

0 commit comments

Comments
 (0)