Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
houshengbo committed Oct 26, 2023
1 parent d033ea4 commit ff06df9
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 47 deletions.
206 changes: 206 additions & 0 deletions pkg/apis/serving/v1/serviceorchestrator_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
Copyright 2023 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"reflect"
"testing"

duckv1 "knative.dev/pkg/apis/duck/v1"
)

func TestServiceOrchestratorGetConditionSet(t *testing.T) {
serviceOrchestrator := &ServiceOrchestrator{
Status: ServiceOrchestratorStatus{
Status: duckv1.Status{
ObservedGeneration: 23,
},
},
}

if got, want := serviceOrchestrator.GetConditionSet(), serviceOrchestratorCondSet; !reflect.DeepEqual(got, want) {
t.Errorf("GetStatus() = %v, want: %v", got, want)
}
}

func TestServiceOrchestratorGetGroupVersionKind(t *testing.T) {
serviceOrchestrator := &ServiceOrchestrator{}

if got, want := serviceOrchestrator.GetGroupVersionKind(), SchemeGroupVersion.WithKind("ServiceOrchestrator"); !reflect.DeepEqual(got, want) {
t.Errorf("GetStatus() = %v, want: %v", got, want)
}
}

func TestServiceOrchestratorStatus(t *testing.T) {
tests := []struct {
name string
so *ServiceOrchestrator
expectedStageScaleUpReady bool
expectedStageScaleUpInProgress bool
expectedStageReady bool
expectedReady bool
}{{
name: "ServiceOrchestrator with empty spec and status",
so: emptyServiceOrchestrator(),
expectedStageScaleUpReady: false,
expectedStageScaleUpInProgress: true,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with scale up in progress",
so: serviceOrchestratorWithStageRevisionScaleUpInProgress(),
expectedStageScaleUpReady: false,
expectedStageScaleUpInProgress: true,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with scale up ready",
so: serviceOrchestratorWithStageRevisionScaleUpReady(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with scale down in progress",
so: serviceOrchestratorWithStageRevisionScaleDownInProgress(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with scale down ready",
so: serviceOrchestratorWithStageRevisionScaleDownReady(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with stage ready",
so: serviceOrchestratorWithStageReady(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: true,
expectedReady: false,
}, {
name: "ServiceOrchestrator with stage in progress",
so: serviceOrchestratorWithStageReadyInProgress(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: false,
expectedReady: false,
}, {
name: "ServiceOrchestrator with last stage ready",
so: serviceOrchestratorWithLastStageComplete(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: true,
expectedReady: true,
}, {
name: "ServiceOrchestrator with last stage in progress",
so: serviceOrchestratorWithLastStageIncomplete(),
expectedStageScaleUpReady: true,
expectedStageScaleUpInProgress: false,
expectedStageReady: true,
expectedReady: false,
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.so.IsStageScaleUpReady()
if !reflect.DeepEqual(result, tt.expectedStageScaleUpReady) {
t.Errorf("IsStageScaleUpReady() = %v, want: %v", result, tt.expectedStageScaleUpReady)
}
result = tt.so.IsStageScaleUpInProgress()
if !reflect.DeepEqual(result, tt.expectedStageScaleUpInProgress) {
t.Errorf("IsStageScaleUpInProgress() = %v, want: %v", result, tt.expectedStageScaleUpInProgress)
}
result = tt.so.IsStageReady()
if !reflect.DeepEqual(result, tt.expectedStageReady) {
t.Errorf("IsStageReady() = %v, want: %v", result, tt.expectedStageReady)
}
result = tt.so.IsReady()
if !reflect.DeepEqual(result, tt.expectedReady) {
t.Errorf("IsReady() = %v, want: %v", result, tt.expectedReady)
}
})
}

}

func emptyServiceOrchestrator() *ServiceOrchestrator {
return &ServiceOrchestrator{}
}

func serviceOrchestratorWithStageRevisionScaleUpReady() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
return so
}

func serviceOrchestratorWithStageRevisionScaleUpInProgress() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpInProgress("Scaling up in progress", "need to wait for the readiness")
return so
}

func serviceOrchestratorWithStageRevisionScaleDownReady() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownReady()
return so
}

func serviceOrchestratorWithStageRevisionScaleDownInProgress() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownInProgress("Scaling down in progress", "need to wait for the readiness")
return so
}

func serviceOrchestratorWithLastStageComplete() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownReady()
so.Status.MarkStageRevisionReady()
so.Status.MarkLastStageRevisionComplete()
return so
}

func serviceOrchestratorWithLastStageIncomplete() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownReady()
so.Status.MarkStageRevisionReady()
so.Status.MarkLastStageRevisionInComplete("", "")
return so
}

func serviceOrchestratorWithStageReady() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownReady()
so.Status.MarkStageRevisionReady()
return so
}

func serviceOrchestratorWithStageReadyInProgress() *ServiceOrchestrator {
so := &ServiceOrchestrator{}
so.Status.MarkStageRevisionScaleUpReady()
so.Status.MarkStageRevisionScaleDownReady()
so.Status.MarkStageRevisionInProgress("", "")
return so
}
38 changes: 38 additions & 0 deletions pkg/apis/serving/v1/serviceorchestrator_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"reflect"
"testing"

duckv1 "knative.dev/pkg/apis/duck/v1"
)

func TestServiceOrchestratorGetStatus(t *testing.T) {
serviceOrchestrator := &ServiceOrchestrator{
Status: ServiceOrchestratorStatus{
Status: duckv1.Status{
ObservedGeneration: 23,
},
},
}

if got, want := *serviceOrchestrator.GetStatus(), serviceOrchestrator.Status.Status; !reflect.DeepEqual(got, want) {
t.Errorf("GetStatus() = %v, want: %v", got, want)
}
}
152 changes: 152 additions & 0 deletions pkg/apis/serving/v1/stagepodautoscaler_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Copyright 2023 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"reflect"
"testing"

duckv1 "knative.dev/pkg/apis/duck/v1"
)

func TestStagePodAutoscalerGetConditionSet(t *testing.T) {
stagePodAutoscaler := &StagePodAutoscaler{
Status: StagePodAutoscalerStatus{
Status: duckv1.Status{
ObservedGeneration: 23,
},
},
}

if got, want := stagePodAutoscaler.GetConditionSet(), stagePodCondSet; !reflect.DeepEqual(got, want) {
t.Errorf("GetConditionSet() = %v, want: %v", got, want)
}
}

func TestStagePodAutoscalerGetGroupVersionKind(t *testing.T) {
stagePodAutoscaler := &StagePodAutoscaler{}

if got, want := stagePodAutoscaler.GetGroupVersionKind(), SchemeGroupVersion.WithKind("StagePodAutoscaler"); !reflect.DeepEqual(got, want) {
t.Errorf("GetGroupVersionKind() = %v, want: %v", got, want)
}
}

func TestStagePodAutoscalerScaleBounds(t *testing.T) {
var minScale, maxScale *int32
minScale = new(int32)
*minScale = 5

maxScale = new(int32)
*maxScale = 8

stagePodAutoscaler := &StagePodAutoscaler{
Spec: StagePodAutoscalerSpec{
MinScale: minScale,
MaxScale: maxScale,
},
}

min, max := stagePodAutoscaler.ScaleBounds()
if !reflect.DeepEqual(*min, *minScale) {
t.Errorf("min in ScaleBounds() = %v, want: %v", min, *minScale)
}
if !reflect.DeepEqual(*max, *maxScale) {
t.Errorf("max in ScaleBounds() = %v, want: %v", max, *maxScale)
}
}

func TestStagePodAutoscalerIsStageScaleInReady(t *testing.T) {
stageReday := &StagePodAutoscaler{}
stageReday.Status.MarkPodAutoscalerStageReady()

stageNotReday := &StagePodAutoscaler{}
stageNotReday.Status.MarkPodAutoscalerStageNotReady("")

stagePodAutoscalerWithInitial := &StagePodAutoscaler{}
stagePodAutoscalerWithInitial.Status.InitializeConditions()
tests := []struct {
name string
spa *StagePodAutoscaler
expectedResult bool
}{{
name: "StagePodAutoscaler with StageScaleInReady True",
spa: stageReday,
expectedResult: true,
}, {
name: "StagePodAutoscaler with StageScaleInReady False",
spa: stageNotReday,
expectedResult: false,
}, {
name: "StagePodAutoscaler with StageScaleInReady Initial False",
spa: &StagePodAutoscaler{},
expectedResult: false,
}, {
name: "StagePodAutoscaler with StageScaleInReady calling InitializeConditions False",
spa: stagePodAutoscalerWithInitial,
expectedResult: false,
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.spa.IsStageScaleInReady()
if !reflect.DeepEqual(result, tt.expectedResult) {
t.Errorf("IsStageScaleInReady() = %v, want: %v", result, tt.expectedResult)
}
})
}
}

func TestStagePodAutoscalerIsStageScaleInProgress(t *testing.T) {
stageReday := &StagePodAutoscaler{}
stageReday.Status.MarkPodAutoscalerStageReady()

stageNotReday := &StagePodAutoscaler{}
stageNotReday.Status.MarkPodAutoscalerStageNotReady("")

stagePodAutoscalerWithInitial := &StagePodAutoscaler{}
stagePodAutoscalerWithInitial.Status.InitializeConditions()
tests := []struct {
name string
spa *StagePodAutoscaler
expectedResult bool
}{{
name: "StagePodAutoscaler with StageScaleInProgress False",
spa: stageReday,
expectedResult: false,
}, {
name: "StagePodAutoscaler with StageScaleInProgress True",
spa: stageNotReday,
expectedResult: true,
}, {
name: "StagePodAutoscaler with StageScaleInProgress Initial True",
spa: &StagePodAutoscaler{},
expectedResult: true,
}, {
name: "StagePodAutoscaler with StageScaleInProgress call InitializeConditions True",
spa: stagePodAutoscalerWithInitial,
expectedResult: true,
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.spa.IsStageScaleInProgress()
if !reflect.DeepEqual(result, tt.expectedResult) {
t.Errorf("IsStageScaleInProgress() = %v, want: %v", result, tt.expectedResult)
}
})
}
}
Loading

0 comments on commit ff06df9

Please sign in to comment.