Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ashley Schuett <[email protected]>
  • Loading branch information
ashleyschuett committed May 1, 2021
1 parent 9b5b631 commit 2b0bf53
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 11 deletions.
3 changes: 3 additions & 0 deletions pkg/virt-operator/resource/apply/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ go_test(
"install_strategy_suite_test.go",
"patches_test.go",
"pdb_test.go",
"prometheus_test.go",
"reconcile_test.go",
"scc_test.go",
],
Expand All @@ -85,9 +86,11 @@ go_test(
"//pkg/virt-operator/util:go_default_library",
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/kubevirt/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned/fake:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
"//tools/util:go_default_library",
"//vendor/github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1:go_default_library",
"//vendor/github.com/evanphx/json-patch:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
Expand Down
14 changes: 3 additions & 11 deletions pkg/virt-operator/resource/apply/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ func (r *Reconciler) createOrUpdateServiceMonitor(serviceMonitor *promv1.Service
prometheusClient := r.clientset.PrometheusClient()
version, imageRegistry, id := getTargetVersionRegistryID(r.kv)

var cachedServiceMonitor *promv1.ServiceMonitor

obj, exists, _ := r.stores.ServiceMonitorCache.Get(serviceMonitor)
if exists {
cachedServiceMonitor = obj.(*promv1.ServiceMonitor)
}

injectOperatorMetadata(r.kv, &serviceMonitor.ObjectMeta, version, imageRegistry, id, true)
if !exists {
Expand All @@ -54,6 +49,7 @@ func (r *Reconciler) createOrUpdateServiceMonitor(serviceMonitor *promv1.Service
return nil
}

cachedServiceMonitor := obj.(*promv1.ServiceMonitor)
endpointsModified, err := ensureServiceMonitorSpec(serviceMonitor, cachedServiceMonitor)
if err != nil {
return err
Expand All @@ -73,7 +69,7 @@ func (r *Reconciler) createOrUpdateServiceMonitor(serviceMonitor *promv1.Service
if err != nil {
return err
}

ops, err := getPatchWithObjectMetaAndSpec([]string{}, &serviceMonitor.ObjectMeta, newSpec)
if err != nil {
return err
Expand Down Expand Up @@ -119,12 +115,7 @@ func (r *Reconciler) createOrUpdatePrometheusRule(prometheusRule *promv1.Prometh
prometheusClient := r.clientset.PrometheusClient()
version, imageRegistry, id := getTargetVersionRegistryID(r.kv)

var cachedPrometheusRule *promv1.PrometheusRule

obj, exists, _ := r.stores.PrometheusRuleCache.Get(prometheusRule)
if exists {
cachedPrometheusRule = obj.(*promv1.PrometheusRule)
}

injectOperatorMetadata(r.kv, &prometheusRule.ObjectMeta, version, imageRegistry, id, true)
if !exists {
Expand All @@ -140,6 +131,7 @@ func (r *Reconciler) createOrUpdatePrometheusRule(prometheusRule *promv1.Prometh
return nil
}

cachedPrometheusRule := obj.(*promv1.PrometheusRule)
modified := resourcemerge.BoolPtr(false)
existingCopy := cachedPrometheusRule.DeepCopy()

Expand Down
203 changes: 203 additions & 0 deletions pkg/virt-operator/resource/apply/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package apply

import (
"encoding/json"

promv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
jsonpatch "github.com/evanphx/json-patch"

promclientfake "kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned/fake"
"kubevirt.io/kubevirt/pkg/virt-operator/resource/generate/components"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

extclientfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"

v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/pkg/virt-operator/util"
)

var _ = Describe("Apply Prometheus", func() {
var clientset *kubecli.MockKubevirtClient
var ctrl *gomock.Controller
var extClient *extclientfake.Clientset
var promClient *promclientfake.Clientset
var expectations *util.Expectations
var kv *v1.KubeVirt
var stores util.Stores

config := getConfig("fake-registry", "v9.9.9")

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
kvInterface := kubecli.NewMockKubeVirtInterface(ctrl)

extClient = extclientfake.NewSimpleClientset()

extClient.Fake.PrependReactor("*", "*", func(action testing.Action) (handled bool, obj runtime.Object, err error) {
Expect(action).To(BeNil())
return true, nil, nil
})

stores = util.Stores{}
stores.ServiceMonitorCache = cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
stores.PrometheusRuleCache = cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
stores.InstallStrategyConfigMapCache = cache.NewStore(cache.MetaNamespaceKeyFunc)

expectations = &util.Expectations{}

clientset = kubecli.NewMockKubevirtClient(ctrl)
clientset.EXPECT().KubeVirt(Namespace).Return(kvInterface).AnyTimes()

promClient = promclientfake.NewSimpleClientset()
clientset.EXPECT().PrometheusClient().Return(promClient).AnyTimes()

kv = &v1.KubeVirt{}
})

AfterEach(func() {
ctrl.Finish()
})

It("should not patch ServiceMonitor on sync when they are equal", func() {

sm := components.NewServiceMonitorCR("namespace", "mNamespace", true)

version, imageRegistry, id := getTargetVersionRegistryID(kv)
injectOperatorMetadata(kv, &sm.ObjectMeta, version, imageRegistry, id, true)

stores.ServiceMonitorCache.Add(sm)

r := &Reconciler{
kv: kv,
stores: stores,
clientset: clientset,
expectations: expectations,
}

Expect(r.createOrUpdateServiceMonitor(sm)).To(BeNil())
})

It("should patch ServiceMonitor on sync when they are equal", func() {
sm := components.NewServiceMonitorCR("namespace", "mNamespace", true)

version, imageRegistry, id := getTargetVersionRegistryID(kv)
injectOperatorMetadata(kv, &sm.ObjectMeta, version, imageRegistry, id, true)

stores.ServiceMonitorCache.Add(sm)

r := &Reconciler{
kv: kv,
stores: stores,
clientset: clientset,
expectations: expectations,
}

requiredSM := sm.DeepCopy()
updatedEndpoints := []promv1.Endpoint{
{
Port: "metrics-update",
},
}
requiredSM.Spec.Endpoints = updatedEndpoints

patched := false
promClient.Fake.PrependReactor("patch", "servicemonitors", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
a := action.(testing.PatchActionImpl)
patch, err := jsonpatch.DecodePatch(a.Patch)
Expect(err).ToNot(HaveOccurred())

patched = true

obj, err := json.Marshal(sm)
Expect(err).To(BeNil())

obj, err = patch.Apply(obj)
Expect(err).To(BeNil())

sm := &promv1.ServiceMonitor{}
Expect(json.Unmarshal(obj, sm)).To(Succeed())
Expect(sm.Spec.Endpoints).To(Equal(updatedEndpoints))

return true, sm, nil
})

Expect(r.createOrUpdateServiceMonitor(requiredSM)).To(BeNil())
Expect(patched).To(BeTrue())
})

It("should not patch PrometheusRules on sync when they are equal", func() {

pr := components.NewPrometheusRuleCR("namespace", config.WorkloadUpdatesEnabled())

version, imageRegistry, id := getTargetVersionRegistryID(kv)
injectOperatorMetadata(kv, &pr.ObjectMeta, version, imageRegistry, id, true)

stores.PrometheusRuleCache.Add(pr)

r := &Reconciler{
kv: kv,
stores: stores,
clientset: clientset,
expectations: expectations,
}

Expect(r.createOrUpdatePrometheusRule(pr)).To(BeNil())
})

It("should patch PrometheusRules on sync when they are equal", func() {

pr := components.NewPrometheusRuleCR("namespace", config.WorkloadUpdatesEnabled())

version, imageRegistry, id := getTargetVersionRegistryID(kv)
injectOperatorMetadata(kv, &pr.ObjectMeta, version, imageRegistry, id, true)

stores.PrometheusRuleCache.Add(pr)

r := &Reconciler{
kv: kv,
stores: stores,
clientset: clientset,
expectations: expectations,
}

requiredPR := pr.DeepCopy()
updatedGroups := []promv1.RuleGroup{
{
Name: "Updated",
},
}
requiredPR.Spec.Groups = updatedGroups

patched := false
promClient.Fake.PrependReactor("patch", "prometheusrules", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
a := action.(testing.PatchActionImpl)
patch, err := jsonpatch.DecodePatch(a.Patch)
Expect(err).ToNot(HaveOccurred())

patched = true

obj, err := json.Marshal(pr)
Expect(err).To(BeNil())

obj, err = patch.Apply(obj)
Expect(err).To(BeNil())

pr := &promv1.PrometheusRule{}
Expect(json.Unmarshal(obj, pr)).To(Succeed())
Expect(pr.Spec.Groups).To(Equal(updatedGroups))
Expect(len(pr.Spec.Groups)).To(Equal(1))

return true, pr, nil
})

Expect(r.createOrUpdatePrometheusRule(requiredPR)).To(BeNil())
Expect(patched).To(BeTrue())
})
})

0 comments on commit 2b0bf53

Please sign in to comment.