Add unit test to verify app re-initialization on certain cluster events
@@ -24,12 +24,18 @@ import (
"net/http"
"net/http/httptest"
"net/url"
+ "sync"
+ "time"
restful "github.com/emicklei/go-restful"
. "github.com/onsi/ginkgo"
+ "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
+ kubev1 "k8s.io/api/core/v1"
+
"kubevirt.io/kubevirt/pkg/rest"
+ testutils "kubevirt.io/kubevirt/pkg/testutils"
)
func newValidGetRequest() *http.Request {
@@ -40,6 +46,38 @@ func newValidGetRequest() *http.Request {
var _ = Describe("Application", func() {
var app VirtControllerApp = VirtControllerApp{}
+ Describe("Reinitialization conditions", func() {
+ table.DescribeTable("Re-trigger initialization", func(hasCdiAtInit bool, addCrd bool, removeCrd bool, expectReInit bool) {
+ app := VirtControllerApp{}
+
+ clusterConfig, _, crdInformer := testutils.NewFakeClusterConfig(&kubev1.ConfigMap{})
+ app.clusterConfig = clusterConfig
+ app.reInitChan = make(chan struct{})
+ app.reInitTriggered = false
+ app.reInitLock = &sync.Mutex{}
+ app.hasCdi = hasCdiAtInit
+
+ app.clusterConfig.SetConfigModifiedCallback(app.configModificationCallback)
+
+ if addCrd {
+ testutils.AddDataVolumeAPI(crdInformer)
+ } else if removeCrd {
+ testutils.RemoveDataVolumeAPI(crdInformer)
+ }
+
+ time.Sleep(3)
+
+ app.reInitLock.Lock()
+ defer app.reInitLock.Unlock()
+ Expect(app.reInitTriggered).To(Equal(expectReInit))
+ },
+ table.Entry("when CDI is introduced", false, true, false, true),
+ table.Entry("when CDI is removed", true, false, true, true),
+ table.Entry("not when nothing changed and cdi exists", true, true, false, false),
+ table.Entry("not when nothing changed and does not exists", false, false, true, false),
+ )
+ })
+
Describe("Readiness probe", func() {
var recorder *httptest.ResponseRecorder
var request *http.Request
