Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit d5a90c8

Browse files
committed
Add unit tests for controller and fix linting
In this commit we also: - Fix error handler in defer func - Added assertions to unit test inside the controller - - Trying to create assertions to onAdd - - Add right assertions to onAdd - - Add assertions to onDelete - - Add assertions to onUpdate - - tidy up - - clean up - - Add changes suggested by the review - - Refactor to apply reflect.DeepEqual - - Refactor to remove reflect.ValueOf - - Refactor mock and add positive tc onDelete - - Change got the variable scope
1 parent 43ee82b commit d5a90c8

File tree

6 files changed

+401
-11
lines changed

6 files changed

+401
-11
lines changed

telemetry-aware-scheduling/pkg/cache/mocks.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cache
22

33
//This file contains mock methods and objects which are used to test across the TAS packages.
44
import (
5+
"fmt"
56
"time"
67

78
"k8s.io/klog/v2"
@@ -12,6 +13,11 @@ import (
1213
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
)
1415

16+
// MockCache is used in the tests for the core and other packages.
17+
type MockCache struct {
18+
MockCache interface{}
19+
}
20+
1521
//MockEmptySelfUpdatingCache returns auto updating cache
1622
func MockEmptySelfUpdatingCache() ReaderWriter {
1723
n := NewAutoUpdatingCache()
@@ -52,3 +58,49 @@ var mockPolicy = telemetrypolicy.TASPolicy{
5258
var mockPolicy2 = telemetrypolicy.TASPolicy{
5359
ObjectMeta: v1.ObjectMeta{Name: "not-mock-policy", Namespace: "default"},
5460
}
61+
62+
// ReadMetric is a method implemented for Mock cache.
63+
func (n MockCache) ReadMetric(string) (metrics.NodeMetricsInfo, error) {
64+
return metrics.NodeMetricsInfo{}, nil
65+
}
66+
67+
// ReadPolicy is a method implemented for Mock cache.
68+
func (n MockCache) ReadPolicy(string, string) (telemetrypolicy.TASPolicy, error) {
69+
return telemetrypolicy.TASPolicy{}, nil
70+
}
71+
72+
// WriteMetric is a method implemented for Mock cache.
73+
func (n MockCache) WriteMetric(metricName string, _ metrics.NodeMetricsInfo) error {
74+
if metricName != "" {
75+
return nil
76+
}
77+
78+
return fmt.Errorf("failed to write metric")
79+
}
80+
81+
// WritePolicy is a method implemented for Mock cache.
82+
func (n MockCache) WritePolicy(namespace string, _ string, _ telemetrypolicy.TASPolicy) error {
83+
if namespace != "default" {
84+
return fmt.Errorf("failed to write policy")
85+
}
86+
87+
return nil
88+
}
89+
90+
// DeleteMetric is a method implemented for Mock cache.
91+
func (n MockCache) DeleteMetric(metricName string) error {
92+
if metricName != "" {
93+
return nil
94+
}
95+
96+
return fmt.Errorf("no metric to delete")
97+
}
98+
99+
// DeletePolicy is a method implemented for Mock cache.
100+
func (n MockCache) DeletePolicy(namespace string, policyName string) error {
101+
if namespace != "default" || policyName == "" {
102+
return fmt.Errorf("failed to delete policy")
103+
}
104+
105+
return nil
106+
}

telemetry-aware-scheduling/pkg/controller/controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
func (controller *TelemetryPolicyController) Run(context context.Context) {
2525
log.Print("Watching Telemetry Policies", "component", "controller")
2626
defer func() {
27-
if err := recover(); err != nil {
27+
err := recover()
28+
if err != nil {
2829
log.Print("Recovered from runtime error", "component", "controller")
2930
}
3031
}()
@@ -118,8 +119,7 @@ func (controller *TelemetryPolicyController) onUpdate(old, new interface{}) {
118119
polCopy := newPol.DeepCopy()
119120
err := controller.WritePolicy(polCopy.Namespace, polCopy.Name, *polCopy)
120121
if err != nil {
121-
msg := fmt.Sprintf("cached policy not updated %v", err)
122-
klog.V(2).InfoS(msg, "component", "controller")
122+
klog.V(2).InfoS(fmt.Sprintf("cached policy not updated %v", err), "component", "controller")
123123
return
124124
}
125125
klog.V(2).InfoS("Policy: "+polCopy.Name+" updated", "component", "controller")

0 commit comments

Comments
 (0)