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 42012aa
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 47 deletions.
46 changes: 46 additions & 0 deletions pkg/apis/serving/v1/serviceorchestrator_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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)
}
}
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)
}
})
}
}
38 changes: 38 additions & 0 deletions pkg/apis/serving/v1/stagepodautoscaler_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 TestStagePodAutoscalerGetStatus(t *testing.T) {
stagePodAutoscaler := &StagePodAutoscaler{
Status: StagePodAutoscalerStatus{
Status: duckv1.Status{
ObservedGeneration: 23,
},
},
}

if got, want := *stagePodAutoscaler.GetStatus(), stagePodAutoscaler.Status.Status; !reflect.DeepEqual(got, want) {
t.Errorf("GetStatus() = %v, want: %v", got, want)
}
}
Loading

0 comments on commit 42012aa

Please sign in to comment.