Skip to content

Commit e16fcf6

Browse files
author
Josef Karasek
committed
unit test for "OLM pod restarts"
Signed-off-by: Josef Karasek <[email protected]>
1 parent 266d34e commit e16fcf6

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

cmd/olm/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func main() {
272272
}
273273

274274
func ensureCSVMetric(logger *logrus.Logger, c *versioned.Clientset) error {
275-
logger.Debug("Emitting CSV metric")
275+
logger.Debug("emitting CSV metric")
276276
listOpts := metav1.ListOptions{}
277277
csvs, err := c.OperatorsV1alpha1().ClusterServiceVersions(metav1.NamespaceAll).List(context.TODO(), listOpts)
278278
if err != nil {

test/e2e/metrics_e2e_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
. "github.com/onsi/gomega"
1616
io_prometheus_client "github.com/prometheus/client_model/go"
1717
"github.com/prometheus/common/expfmt"
18+
appsv1 "k8s.io/api/apps/v1"
1819
corev1 "k8s.io/api/core/v1"
1920
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -112,6 +113,41 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
112113
})
113114
})
114115
})
116+
117+
When("the OLM pod restars", func() {
118+
var (
119+
cleanupCSV cleanupFunc
120+
csv v1alpha1.ClusterServiceVersion
121+
)
122+
BeforeEach(func() {
123+
packageName := genName("csv-test-")
124+
packageStable := fmt.Sprintf("%s-stable", packageName)
125+
csv = newCSV(packageStable, testNamespace, "", semver.MustParse("0.1.0"), nil, nil, nil)
126+
127+
var err error
128+
_, err = createCSV(c, crc, csv, testNamespace, false, false)
129+
Expect(err).ToNot(HaveOccurred())
130+
131+
_, err = fetchCSV(crc, csv.Name, testNamespace, csvSucceededChecker)
132+
Expect(err).ToNot(HaveOccurred())
133+
})
134+
AfterEach(func() {
135+
if cleanupCSV != nil {
136+
cleanupCSV()
137+
}
138+
})
139+
It("csv metric is preserved", func() {
140+
Expect(getMetricsFromPod(c, getPodWithLabel(c, "app=olm-operator"), "8081")).To(
141+
ContainElement(LikeMetric(WithFamily("csv_succeeded"), WithName(csv.Name), WithValue(1))),
142+
)
143+
144+
restartDeploymentWithLabel(c, "app=olm-operator")
145+
146+
Expect(getMetricsFromPod(c, getPodWithLabel(c, "app=olm-operator"), "8081")).To(
147+
ContainElement(LikeMetric(WithFamily("csv_succeeded"), WithName(csv.Name), WithValue(1))),
148+
)
149+
})
150+
})
115151
})
116152

117153
Context("Metrics emitted by objects during operator installation", func() {
@@ -392,6 +428,51 @@ func getPodWithLabel(client operatorclient.ClientInterface, label string) *corev
392428
return &podList.Items[0]
393429
}
394430

431+
func getDeploymentWithLabel(client operatorclient.ClientInterface, label string) *appsv1.Deployment {
432+
listOptions := metav1.ListOptions{LabelSelector: label}
433+
var deploymentList *appsv1.DeploymentList
434+
EventuallyWithOffset(1, func() (numDeps int, err error) {
435+
deploymentList, err = client.KubernetesInterface().AppsV1().Deployments(operatorNamespace).List(context.TODO(), listOptions)
436+
if deploymentList != nil {
437+
numDeps = len(deploymentList.Items)
438+
}
439+
440+
return
441+
}).Should(Equal(1), "expected exactly one Deployment")
442+
443+
return &deploymentList.Items[0]
444+
}
445+
446+
func restartDeploymentWithLabel(client operatorclient.ClientInterface, l string) {
447+
d := getDeploymentWithLabel(client, l)
448+
z := int32(0)
449+
oldZ := *d.Spec.Replicas
450+
d.Spec.Replicas = &z
451+
_, err := client.KubernetesInterface().AppsV1().Deployments(operatorNamespace).Update(context.TODO(), d, metav1.UpdateOptions{})
452+
Expect(err).ToNot(HaveOccurred())
453+
454+
EventuallyWithOffset(1, func() (replicas int32, err error) {
455+
deployment, err := client.KubernetesInterface().AppsV1().Deployments(operatorNamespace).Get(context.TODO(), d.Name, metav1.GetOptions{})
456+
if deployment != nil {
457+
replicas = deployment.Status.Replicas
458+
}
459+
return
460+
}).Should(Equal(int32(0)), "expected exactly 0 Deployments")
461+
462+
updated := getDeploymentWithLabel(client, l)
463+
updated.Spec.Replicas = &oldZ
464+
_, err = client.KubernetesInterface().AppsV1().Deployments(operatorNamespace).Update(context.TODO(), updated, metav1.UpdateOptions{})
465+
Expect(err).ToNot(HaveOccurred())
466+
467+
EventuallyWithOffset(1, func() (replicas int32, err error) {
468+
deployment, err := client.KubernetesInterface().AppsV1().Deployments(operatorNamespace).Get(context.TODO(), d.Name, metav1.GetOptions{})
469+
if deployment != nil {
470+
replicas = deployment.Status.Replicas
471+
}
472+
return
473+
}).Should(Equal(oldZ), "expected exactly 1 Deployment")
474+
}
475+
395476
func getMetricsFromPod(client operatorclient.ClientInterface, pod *corev1.Pod, port string) []Metric {
396477
ctx.Ctx().Logf("querying pod %s/%s\n", pod.GetNamespace(), pod.GetName())
397478

0 commit comments

Comments
 (0)