|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + v1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDetectRegion(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + node *v1.Node |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "base", |
| 19 | + node: &v1.Node{}, |
| 20 | + want: "", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "qcloud with no node labels", |
| 24 | + node: &v1.Node{ |
| 25 | + Spec: v1.NodeSpec{ |
| 26 | + ProviderID: "qcloud://01", |
| 27 | + }, |
| 28 | + }, |
| 29 | + want: "", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "qcloud with node labels", |
| 33 | + node: &v1.Node{ |
| 34 | + Spec: v1.NodeSpec{ |
| 35 | + ProviderID: "qcloud://01", |
| 36 | + }, |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Labels: map[string]string{ |
| 39 | + v1.LabelTopologyRegion: "gz", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + want: "ap-guangzhou", |
| 44 | + }, |
| 45 | + } |
| 46 | + for _, tt := range tests { |
| 47 | + t.Run(tt.name, func(t *testing.T) { |
| 48 | + if got := DetectRegion(tt.node); got != tt.want { |
| 49 | + t.Errorf("DetectRegion() = %v, want %v", got, tt.want) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestDetectProvider(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + node *v1.Node |
| 59 | + want ProviderKind |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "base", |
| 63 | + node: &v1.Node{}, |
| 64 | + want: "default", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "qcloud", |
| 68 | + node: &v1.Node{ |
| 69 | + Spec: v1.NodeSpec{ |
| 70 | + ProviderID: "qcloud://01", |
| 71 | + }, |
| 72 | + }, |
| 73 | + want: "qcloud", |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + if got := DetectProvider(tt.node); !reflect.DeepEqual(got, tt.want) { |
| 79 | + t.Errorf("DetectProvider() = %v, want %v", got, tt.want) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestNewProviderConfig(t *testing.T) { |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + customPricing *CustomPricing |
| 89 | + want *PriceConfig |
| 90 | + }{ |
| 91 | + { |
| 92 | + name: "base", |
| 93 | + customPricing: &CustomPricing{}, |
| 94 | + want: &PriceConfig{ |
| 95 | + customPricing: &CustomPricing{}, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + if got := NewProviderConfig(tt.customPricing); !reflect.DeepEqual(got, tt.want) { |
| 102 | + t.Errorf("NewProviderConfig() = %v, want %v", got, tt.want) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestPriceConfig_GetConfig(t *testing.T) { |
| 109 | + tests := []struct { |
| 110 | + name string |
| 111 | + pc *PriceConfig |
| 112 | + want *CustomPricing |
| 113 | + wantErr error |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "base", |
| 117 | + pc: &PriceConfig{ |
| 118 | + customPricing: &CustomPricing{}, |
| 119 | + }, |
| 120 | + want: &CustomPricing{}, |
| 121 | + wantErr: nil, |
| 122 | + }, |
| 123 | + } |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + got, err := tt.pc.GetConfig() |
| 127 | + if err != tt.wantErr { |
| 128 | + t.Errorf("PriceConfig.GetConfig() error = %v, wantErr %v", err, tt.wantErr) |
| 129 | + return |
| 130 | + } |
| 131 | + if !reflect.DeepEqual(got, tt.want) { |
| 132 | + t.Errorf("PriceConfig.GetConfig() = %v, want %v", got, tt.want) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments