@@ -2,121 +2,57 @@ package cloud
22
33import (
44 "reflect"
5+ "strings"
56 "testing"
6-
7- v1 "k8s.io/api/core/v1"
8- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
97)
108
11- func TestDetectRegion (t * testing.T ) {
9+ func TestPriceConfig_UpdateConfigFromConfigMap (t * testing.T ) {
1210 tests := []struct {
13- name string
14- node * v1.Node
15- want string
11+ name string
12+ priceConf map [string ]string
13+ pc * PriceConfig
14+ want * CustomPricing
15+ wantErr bool
1616 }{
1717 {
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- },
18+ name : "base" ,
19+ priceConf : map [string ]string {},
20+ pc : & PriceConfig {
21+ customPricing : & CustomPricing {},
2822 },
29- want : "" ,
23+ want : & CustomPricing {},
24+ wantErr : false ,
3025 },
3126 {
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- },
27+ name : "SetCustomPricing raise an error" ,
28+ priceConf : map [string ]string {
29+ "CpuHourlyPrice" : "" ,
4230 },
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- },
31+ pc : & PriceConfig {
32+ customPricing : & CustomPricing {},
7233 },
73- want : "qcloud" ,
34+ want : & CustomPricing {},
35+ wantErr : true ,
7436 },
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- }{
9137 {
92- name : "base" ,
93- customPricing : & CustomPricing {},
94- want : & PriceConfig {
38+ name : "SetCustomPricing ok" ,
39+ priceConf : map [string ]string {
40+ "CpuHourlyPrice" : "1.23" ,
41+ },
42+ pc : & PriceConfig {
9543 customPricing : & CustomPricing {},
9644 },
45+ want : & CustomPricing {
46+ CpuHourlyPrice : 1.23 ,
47+ },
48+ wantErr : false ,
9749 },
9850 }
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_UpdateConfigFromConfigMap (t * testing.T ) {
109- tests := []struct {
110- name string
111- pc * PriceConfig
112- priceConf map [string ]string
113- want * CustomPricing
114- wantErr bool
115- }{}
11651 for _ , tt := range tests {
11752 t .Run (tt .name , func (t * testing.T ) {
11853 got , err := tt .pc .UpdateConfigFromConfigMap (tt .priceConf )
119- if (err != nil ) != tt .wantErr {
54+ gotErr := err != nil
55+ if gotErr != tt .wantErr {
12056 t .Errorf ("PriceConfig.UpdateConfigFromConfigMap() error = %v, wantErr %v" , err , tt .wantErr )
12157 return
12258 }
@@ -127,59 +63,51 @@ func TestPriceConfig_UpdateConfigFromConfigMap(t *testing.T) {
12763 }
12864}
12965
130- func TestPriceConfig_GetConfig (t * testing.T ) {
131- tests := []struct {
132- name string
133- pc * PriceConfig
134- want * CustomPricing
135- wantErr error
136- }{
137- {
138- name : "base" ,
139- pc : & PriceConfig {
140- customPricing : & CustomPricing {},
141- },
142- want : & CustomPricing {},
143- wantErr : nil ,
144- },
145- }
146- for _ , tt := range tests {
147- t .Run (tt .name , func (t * testing.T ) {
148- got , err := tt .pc .GetConfig ()
149- if err != tt .wantErr {
150- t .Errorf ("PriceConfig.GetConfig() error = %v, wantErr %v" , err , tt .wantErr )
151- return
152- }
153- if ! reflect .DeepEqual (got , tt .want ) {
154- t .Errorf ("PriceConfig.GetConfig() = %v, want %v" , got , tt .want )
155- }
156- })
157- }
158- }
159-
16066func TestSetCustomPricing (t * testing.T ) {
16167 tests := []struct {
162- testName string
163- obj * CustomPricing
16468 name string
165- value string
69+ obj * CustomPricing
70+ filedName string
71+ fieldValue string
16672 wantErr bool
16773 wantErrMsg string
16874 }{
16975 {
170- testName : "base" ,
76+ name : "base" ,
17177 obj : & CustomPricing {},
17278 wantErr : true ,
17379 wantErrMsg : "no such field: in obj" ,
17480 },
81+ {
82+ name : "value is not float type" ,
83+ filedName : "CpuHourlyPrice" ,
84+ obj : & CustomPricing {
85+ CpuHourlyPrice : 1.23 ,
86+ },
87+ wantErr : true ,
88+ wantErrMsg : "invalid syntax" ,
89+ },
90+ {
91+ name : "value is float type" ,
92+ filedName : "CpuHourlyPrice" ,
93+ fieldValue : "1.23" ,
94+ obj : & CustomPricing {
95+ CpuHourlyPrice : 1.23 ,
96+ },
97+ wantErr : false ,
98+ wantErrMsg : "" ,
99+ },
175100 }
176101 for _ , tt := range tests {
177102 t .Run (tt .name , func (t * testing.T ) {
178- err := SetCustomPricing (tt .obj , tt .name , tt .value )
103+ err := SetCustomPricing (tt .obj , tt .filedName , tt .fieldValue )
179104 if (err != nil ) != tt .wantErr {
180105 t .Errorf ("SetCustomPricing() error = %v, wantErr %v" , err , tt .wantErr )
181106 }
182- if err .Error () != tt .wantErrMsg {
107+ if err == nil {
108+ return
109+ }
110+ if ! strings .Contains (err .Error (), tt .wantErrMsg ) {
183111 t .Errorf ("SetCustomPricing() error = %v, wantErrMsg %v" , err .Error (), tt .wantErrMsg )
184112 }
185113 })
0 commit comments