Skip to content

Commit e166947

Browse files
committed
Get controller.go test coverage to 100%
1 parent 0a34d4f commit e166947

File tree

11 files changed

+415
-17
lines changed

11 files changed

+415
-17
lines changed

pkg/controller/controller.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type controller struct {
9393
// the queue for processing
9494
queue workqueue.RateLimitingInterface
9595

96+
jitterPeriod time.Duration
97+
9698
// once ensures unspecified fields get default values
9799
once sync.Once
98100

@@ -102,6 +104,8 @@ type controller struct {
102104
// mu is used to synchronize controller setup
103105
mu sync.Mutex
104106

107+
waitForCache func(stopCh <-chan struct{}, cacheSyncs ...cache.InformerSynced) bool
108+
105109
// TODO(pwittrock): Consider initializing a logger with the controller name as the tag
106110
}
107111

@@ -145,21 +149,33 @@ func (c *controller) Start(stop <-chan struct{}) error {
145149
for _, informer := range allInformers {
146150
syncedFuncs = append(syncedFuncs, informer.HasSynced)
147151
}
148-
if ok := cache.WaitForCacheSync(stop, syncedFuncs...); !ok {
152+
153+
// Allow waitForCache to be mocked out for tests
154+
if c.waitForCache == nil {
155+
c.waitForCache = cache.WaitForCacheSync
156+
}
157+
if ok := c.waitForCache(stop, syncedFuncs...); !ok {
158+
// This code is unreachable right now since WaitForCacheSync will never return an error
159+
// Leaving it here because that could happen in the future
149160
err := fmt.Errorf("failed to wait for %s caches to sync", c.name)
150161
log.Error(err, "Could not wait for Cache to sync", "controller", c.name)
151162
return err
152163
}
153164

165+
if c.jitterPeriod == 0 {
166+
c.jitterPeriod = time.Second
167+
}
168+
154169
// Launch two workers to process resources
155170
log.Info("Starting workers", "controller", c.name, "WorkerCount", c.maxConcurrentReconciles)
156171
for i := 0; i < c.maxConcurrentReconciles; i++ {
157-
// Continually process work items
172+
// Process work items
158173
go wait.Until(func() {
159-
// TODO(pwittrock): Should we really use wait.Until to continuously restart this if it exits?
174+
fmt.Printf("Running\n")
160175
for c.processNextWorkItem() {
161176
}
162-
}, time.Second, stop)
177+
fmt.Printf("Stopping\n")
178+
}, c.jitterPeriod, stop)
163179
}
164180

165181
<-stop
@@ -174,13 +190,12 @@ func (c *controller) processNextWorkItem() bool {
174190

175191
obj, shutdown := c.queue.Get()
176192
if obj == nil {
177-
log.Error(nil, "Encountered nil Request", "Object", obj)
193+
// Sometimes the queue gives us nil items when it starts up
178194
c.queue.Forget(obj)
179195
}
180196

181197
if shutdown {
182-
// Return false, this will cause the controller to back off for a second before trying again.
183-
// TODO(community): This was copied from the sample-controller. Figure out why / if we need this.
198+
// Stop working
184199
return false
185200
}
186201

@@ -210,8 +225,6 @@ func (c *controller) processNextWorkItem() bool {
210225
c.queue.AddRateLimited(req)
211226
log.Error(nil, "reconcile error", "controller", c.name, "Request", req)
212227

213-
// Return false, this will cause the controller to back off for a second before trying again.
214-
// TODO(community): This was copied from the sample-controller. Figure out why / if we need this.
215228
return false
216229
} else if result.Requeue {
217230
c.queue.AddRateLimited(req)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
)
22+
23+
var _ = Describe("controller", func() {
24+
BeforeEach(func() {
25+
})
26+
27+
AfterEach(func() {
28+
})
29+
30+
Describe("", func() {
31+
32+
It("should do something", func() {
33+
34+
})
35+
})
36+
})

pkg/controller/controller_suite_test.go renamed to pkg/controller/controller_suite_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
func TestSource(t *testing.T) {
3333
RegisterFailHandler(Fail)
34-
RunSpecsWithDefaultAndCustomReporters(t, "controller Suite", []Reporter{test.NewlineReporter{}})
34+
RunSpecsWithDefaultAndCustomReporters(t, "Controller Integration Suite", []Reporter{test.NewlineReporter{}})
3535
}
3636

3737
var testenv *test.Environment
@@ -40,7 +40,7 @@ var clientset *kubernetes.Clientset
4040
var icache informer.Informers
4141

4242
var _ = BeforeSuite(func() {
43-
logf.SetLogger(logf.ZapLogger(true))
43+
logf.SetLogger(logf.ZapLogger(false))
4444

4545
testenv = &test.Environment{}
4646

0 commit comments

Comments
 (0)