Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Mar 19, 2024
1 parent 8790530 commit 300704d
Showing 1 changed file with 97 additions and 4 deletions.
101 changes: 97 additions & 4 deletions api/turing/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"istio.io/client-go/pkg/apis/networking/v1beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"

//nolint:all
Expand Down Expand Up @@ -201,6 +202,11 @@ func TestDeployKubernetesService(t *testing.T) {
Version: "v1",
Resource: "services",
}
horizontalPodAutoscalerResourceItem := schema.GroupVersionResource{
Group: "autoscaling",
Version: "v2",
Resource: "horizontalpodautoscalers",
}
testK8sSvc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Expand All @@ -213,6 +219,12 @@ func TestDeployKubernetesService(t *testing.T) {
Namespace: testNamespace,
},
}
testK8sHorizontalPodAutoscaler := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
}
svcConf := &KubernetesService{
BaseService: &BaseService{
Name: testName,
Expand Down Expand Up @@ -247,6 +259,37 @@ func TestDeployKubernetesService(t *testing.T) {
getSvcSuccess := func(_ k8stesting.Action) (bool, runtime.Object, error) {
return true, testK8sSvc, nil
}
averageUtilization := int32(80)
getHorizontalPodAutoscalerSuccess := func(_ k8stesting.Action) (bool, runtime.Object, error) {
return true, &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Generation: 1,
},
Spec: autoscalingv2.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "StatefulSet",
Name: "test-svc-fluentd-logger",
},
MinReplicas: &replicas,
MaxReplicas: 3,
Metrics: []autoscalingv2.MetricSpec{
{
Type: autoscalingv2.ResourceMetricSourceType,
Resource: &autoscalingv2.ResourceMetricSource{
Name: corev1.ResourceMemory,
Target: autoscalingv2.MetricTarget{
Type: autoscalingv2.UtilizationMetricType,
AverageUtilization: &averageUtilization,
},
},
},
},
},
}, nil
}

// Define tests
cs := fake.NewSimpleClientset()
Expand Down Expand Up @@ -304,6 +347,30 @@ func TestDeployKubernetesService(t *testing.T) {
return true, testK8sSvc, nil
},
},
{
verb: reactorVerbs.Get,
resource: horizontalPodAutoscalerResourceItem.String(),
rFunc: func(action k8stesting.Action) (bool, runtime.Object, error) {
expAction := k8stesting.NewGetAction(horizontalPodAutoscalerResourceItem, testNamespace, testName)
// Check that the method is called with the expected action
assert.Equal(t, expAction, action)
// Return nil object and error to indicate non existent object
return true, nil, k8serrors.NewNotFound(schema.GroupResource{}, testName)
},
},
{
verb: reactorVerbs.Create,
resource: "horizontalpodautoscalers",
rFunc: func(action k8stesting.Action) (bool, runtime.Object, error) {
expAction := k8stesting.NewCreateAction(horizontalPodAutoscalerResourceItem, testNamespace, testK8sHorizontalPodAutoscaler)

Check failure on line 365 in api/turing/cluster/controller_test.go

View workflow job for this annotation

GitHub Actions / test-api

line is 129 characters (lll)
// Check that the method is called with the expected action
assert.Equal(t, expAction, action)
// Prepend a new get reactor for waitK8sServiceReady to use
cs.PrependReactor(reactorVerbs.Get, "horizontalpodautoscalers", getHorizontalPodAutoscalerSuccess)
// Nil error indicates Create success
return true, testK8sHorizontalPodAutoscaler, nil
},
},
},
},
{
Expand Down Expand Up @@ -355,18 +422,43 @@ func TestDeployKubernetesService(t *testing.T) {
return true, testK8sSvc, nil
},
},
{
verb: reactorVerbs.Get,
resource: horizontalPodAutoscalerResourceItem.String(),
rFunc: func(action k8stesting.Action) (bool, runtime.Object, error) {
expAction := k8stesting.NewGetAction(horizontalPodAutoscalerResourceItem, testNamespace, testName)
// Check that the method is called with the expected action
assert.Equal(t, expAction, action)
// Return nil object and error to indicate non existent object
return true, nil, k8serrors.NewNotFound(schema.GroupResource{}, testName)
},
},
{
verb: reactorVerbs.Update,
resource: "horizontalpodautoscalers",
rFunc: func(action k8stesting.Action) (bool, runtime.Object, error) {
expAction := k8stesting.NewUpdateAction(horizontalPodAutoscalerResourceItem, testNamespace, testK8sHorizontalPodAutoscaler)

Check failure on line 440 in api/turing/cluster/controller_test.go

View workflow job for this annotation

GitHub Actions / test-api

line is 129 characters (lll)
// Check that the method is called with the expected action
assert.Equal(t, expAction, action)
// Prepend a new get reactor for waitK8sServiceReady to use
cs.PrependReactor(reactorVerbs.Get, "horizontalpodautoscalers", getHorizontalPodAutoscalerSuccess)
// Nil error indicates Create success
return true, testK8sHorizontalPodAutoscaler, nil
},
},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
monkey.UnpatchAll()
// Patch the functions not being tested
monkey.PatchInstanceMethod(
reflect.TypeOf(svcConf),
"BuildKubernetesServiceConfig",
func(_ *KubernetesService) (*appsv1.StatefulSet, *corev1.Service) {
return testK8sStatefulSet, testK8sSvc
func(_ *KubernetesService) (*appsv1.StatefulSet, *corev1.Service, *autoscalingv2.HorizontalPodAutoscaler) {
return testK8sStatefulSet, testK8sSvc, testK8sHorizontalPodAutoscaler
},
)
monkey.Patch(k8sServiceSemanticEquals,
Expand Down Expand Up @@ -1947,8 +2039,9 @@ func createTestK8sController(cs *fake.Clientset, reactors []reactor) *controller
}
// Return test controller with a fake knative serving client
return &controller{
k8sCoreClient: cs.CoreV1(),
k8sAppsClient: cs.AppsV1(),
k8sCoreClient: cs.CoreV1(),
k8sAppsClient: cs.AppsV1(),
k8sAutoscalingClient: cs.AutoscalingV2(),
}
}

Expand Down

0 comments on commit 300704d

Please sign in to comment.