-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdatabaseclaim_test.go
139 lines (120 loc) · 3.83 KB
/
databaseclaim_test.go
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
package databaseclaim
import (
"context"
"errors"
v1 "github.com/infobloxopen/db-controller/api/v1"
"github.com/infobloxopen/db-controller/pkg/hostparams"
"github.com/spf13/viper"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"
)
type MockClient struct {
client.Client
GetFunc func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
ListFunc func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
UpdateFunc func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
CreateFunc func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
DeleteFunc func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error
}
func (m *MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if m.GetFunc != nil {
return m.GetFunc(ctx, key, obj, opts...)
}
return nil
}
func (m *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if m.ListFunc != nil {
return m.ListFunc(ctx, list, opts...)
}
return nil
}
func (m *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
if m.UpdateFunc != nil {
return m.UpdateFunc(ctx, obj, opts...)
}
return nil
}
func (m *MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
if m.CreateFunc != nil {
return m.CreateFunc(ctx, obj, opts...)
}
return nil
}
func (m *MockClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
if m.DeleteFunc != nil {
return m.DeleteFunc(ctx, obj, opts...)
}
return nil
}
func Test_providerCRAlreadyExists(t *testing.T) {
ctx := context.TODO()
t.Run("CR does exist on the cluster", func(t *testing.T) {
mockClient := &MockClient{
GetFunc: func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return nil
},
}
reqInfo := &requestInfo{
HostParams: hostparams.HostParams{},
}
dbClaim := &v1.DatabaseClaim{}
reconciler := &DatabaseClaimReconciler{
Client: mockClient,
Config: &DatabaseClaimConfig{
Viper: viper.New(),
},
}
reconciler.Config.Viper.Set("cloud", "aws")
exists, err := reconciler.providerCRAlreadyExists(ctx, reqInfo, dbClaim)
if err != nil {
t.Fatalf("failed to check if CR exists: %v", err)
}
if !exists {
t.Errorf("expected CR exists")
}
})
t.Run("CR does not exist on the cluster", func(t *testing.T) {
mockClient := &MockClient{
GetFunc: func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return errors.New("not found")
},
}
reqInfo := &requestInfo{
HostParams: hostparams.HostParams{},
}
dbClaim := &v1.DatabaseClaim{}
reconciler := &DatabaseClaimReconciler{
Client: mockClient,
Config: &DatabaseClaimConfig{
Viper: viper.New(),
},
}
reconciler.Config.Viper.Set("cloud", "aws")
exists, err := reconciler.providerCRAlreadyExists(ctx, reqInfo, dbClaim)
if err != nil {
t.Fatalf("failed to check if CR exists: %v", err)
}
if exists {
t.Errorf("expected CR does not exists")
}
})
t.Run("Bad viper cloud configuration", func(t *testing.T) {
reqInfo := &requestInfo{
HostParams: hostparams.HostParams{},
}
dbClaim := &v1.DatabaseClaim{}
r := &DatabaseClaimReconciler{
Client: &MockClient{},
Config: &DatabaseClaimConfig{
Viper: viper.New(),
},
}
r.Config.Viper.Set("cloud", "anything")
_, err := r.providerCRAlreadyExists(ctx, reqInfo, dbClaim)
if err == nil {
t.Errorf("expected an error but got nil")
} else if err.Error() != "unsupported cloud cloudProvider: anything" {
t.Errorf("expected error 'unsupported cloud cloudProvider: anything', got '%s'", err.Error())
}
})
}