-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathplugins_test.go
More file actions
146 lines (138 loc) · 3.19 KB
/
Copy pathplugins_test.go
File metadata and controls
146 lines (138 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cloud
import (
"io"
"reflect"
"strings"
"testing"
"github.com/gocrane/fadvisor/pkg/cache"
)
func TestRegisterCloudProvider(t *testing.T) {
defer func() {
// clear up
defer providersMutex.Unlock()
providers = make(map[ProviderKind]Factory)
}()
RegisterCloudProvider(TencentCloud, mockFactory)
providersMutex.Lock()
_, ok := providers[TencentCloud]
if !ok {
t.Errorf("RegisterCloudProvider() = not found registered cloud")
}
}
type mockCloud struct {
Cloud
}
type mockCache struct {
cache.Cache
}
func mockFactory(cloudConfig io.Reader, priceConfig *PriceConfig, cache *cache.Cache) (Cloud, error) {
return mockCloud{}, nil
}
func TestGetCloudProvider(t *testing.T) {
tests := []struct {
name string
kindName ProviderKind
cloudConfig io.Reader
priceConfig *PriceConfig
cache cache.Cache
want Cloud
wantErr bool
PreRegister bool
}{
{
name: "base",
kindName: TencentCloud,
cache: mockCache{},
},
{
name: "found provider by name",
kindName: TencentCloud,
cloudConfig: strings.NewReader("test"),
priceConfig: &PriceConfig{},
cache: mockCache{},
want: mockCloud{},
wantErr: false,
PreRegister: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
// clear up
providersMutex.Lock()
defer providersMutex.Unlock()
providers = make(map[ProviderKind]Factory)
}()
if tt.PreRegister {
RegisterCloudProvider(tt.kindName, mockFactory)
}
got, err := GetCloudProvider(tt.kindName, tt.cloudConfig, tt.priceConfig, &tt.cache)
gotErr := (err != nil)
if gotErr != tt.wantErr {
t.Errorf("GetCloudProvider() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCloudProvider() = %v, want %v", got, tt.want)
}
})
}
}
func TestInitCloudProvider(t *testing.T) {
tests := []struct {
name string
kindName ProviderKind
CloudOpts CloudConfig
priceConfig *PriceConfig
cache cache.Cache
want Cloud
wantErr bool
PreRegister bool
}{
{
name: "base",
CloudOpts: CloudConfig{
CloudConfigFile: t.TempDir(),
Provider: string(TencentCloud),
},
wantErr: true,
},
{
name: "not CloudConfigFile and cloud is nil",
CloudOpts: CloudConfig{},
wantErr: true,
},
{
name: "base",
kindName: TencentCloud,
CloudOpts: CloudConfig{
Provider: string(TencentCloud),
},
want: mockCloud{},
wantErr: false,
PreRegister: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
// clear up
providersMutex.Lock()
defer providersMutex.Unlock()
providers = make(map[ProviderKind]Factory)
}()
if tt.PreRegister {
RegisterCloudProvider(tt.kindName, mockFactory)
}
got, err := InitCloudProvider(tt.CloudOpts, tt.priceConfig, &tt.cache)
gotErr := (err != nil)
if gotErr != tt.wantErr {
t.Errorf("InitCloudProvider() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("InitCloudProvider() = %v, want %v", got, tt.want)
}
})
}
}