Skip to content

Commit

Permalink
test-pkg-cloud
Browse files Browse the repository at this point in the history
Signed-off-by: jxs1211 <327411586@qq.com>
  • Loading branch information
jxs1211 committed Sep 17, 2022
1 parent ea59b5d commit 0f5341b
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cloud

import (
"reflect"
"testing"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestDetectRegion(t *testing.T) {
tests := []struct {
name string
node *v1.Node
want string
}{
{
name: "base",
node: &v1.Node{},
want: "",
},
{
name: "qcloud with no node labels",
node: &v1.Node{
Spec: v1.NodeSpec{
ProviderID: "qcloud://01",
},
},
want: "",
},
{
name: "qcloud with node labels",
node: &v1.Node{
Spec: v1.NodeSpec{
ProviderID: "qcloud://01",
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
v1.LabelTopologyRegion: "gz",
},
},
},
want: "ap-guangzhou",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DetectRegion(tt.node); got != tt.want {
t.Errorf("DetectRegion() = %v, want %v", got, tt.want)
}
})
}
}

func TestDetectProvider(t *testing.T) {
tests := []struct {
name string
node *v1.Node
want ProviderKind
}{
{
name: "base",
node: &v1.Node{},
want: "default",
},
{
name: "qcloud",
node: &v1.Node{
Spec: v1.NodeSpec{
ProviderID: "qcloud://01",
},
},
want: "qcloud",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DetectProvider(tt.node); !reflect.DeepEqual(got, tt.want) {
t.Errorf("DetectProvider() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewProviderConfig(t *testing.T) {
tests := []struct {
name string
customPricing *CustomPricing
want *PriceConfig
}{
{
name: "base",
customPricing: &CustomPricing{},
want: &PriceConfig{
customPricing: &CustomPricing{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewProviderConfig(tt.customPricing); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewProviderConfig() = %v, want %v", got, tt.want)
}
})
}
}

func TestPriceConfig_GetConfig(t *testing.T) {
tests := []struct {
name string
pc *PriceConfig
want *CustomPricing
wantErr error
}{
{
name: "base",
pc: &PriceConfig{
customPricing: &CustomPricing{},
},
want: &CustomPricing{},
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.pc.GetConfig()
if err != tt.wantErr {
t.Errorf("PriceConfig.GetConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("PriceConfig.GetConfig() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0f5341b

Please sign in to comment.