add ut for revisereplica

Signed-off-by: AllenZMC <zhongming.chang@daocloud.io>
This commit is contained in:
AllenZMC 2022-04-19 22:20:09 +08:00
parent 2d1bb23b9d
commit f67684acf7
1 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,156 @@
package defaultinterpreter
import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func TestReviseDeploymentReplica(t *testing.T) {
tests := []struct {
name string
object *unstructured.Unstructured
replica int64
expected *unstructured.Unstructured
expectError bool
}{
{
name: "Deployment .spec.replicas accessor error, expected int64",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "fake-deployment",
},
"spec": map[string]interface{}{
"replicas": 1,
},
},
},
replica: 3,
expectError: true,
},
{
name: "revise deployment replica",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "fake-deployment",
},
"spec": map[string]interface{}{
"replicas": int64(1),
},
},
},
replica: 3,
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "fake-deployment",
},
"spec": map[string]interface{}{
"replicas": int64(3),
},
},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := reviseDeploymentReplica(tt.object, tt.replica)
if err == nil && tt.expectError == true {
t.Fatal("expect an error but got none")
}
if err != nil && tt.expectError != true {
t.Fatalf("expect no error but got: %v", err)
}
if err == nil && tt.expectError == false {
if !reflect.DeepEqual(res, tt.expected) {
t.Errorf("reviseDeploymentReplica() = %v, want %v", res, tt.expected)
}
}
})
}
}
func TestReviseJobReplica(t *testing.T) {
tests := []struct {
name string
object *unstructured.Unstructured
replica int64
expected *unstructured.Unstructured
expectError bool
}{
{
name: "Job .spec.parallelism accessor error, expected int64",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]interface{}{
"name": "fake-job",
},
"spec": map[string]interface{}{
"parallelism": 1,
},
},
},
replica: 3,
expectError: true,
},
{
name: "revise job parallelism",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]interface{}{
"name": "fake-job",
},
"spec": map[string]interface{}{
"parallelism": int64(1),
},
},
},
replica: 3,
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]interface{}{
"name": "fake-job",
},
"spec": map[string]interface{}{
"parallelism": int64(3),
},
},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := reviseJobReplica(tt.object, tt.replica)
if err == nil && tt.expectError == true {
t.Fatal("expect an error but got none")
}
if err != nil && tt.expectError != true {
t.Fatalf("expect no error but got: %v", err)
}
if err == nil && tt.expectError == false {
if !reflect.DeepEqual(res, tt.expected) {
t.Errorf("reviseJobReplica() = %v, want %v", res, tt.expected)
}
}
})
}
}