-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtenant_test.go
123 lines (110 loc) · 2.67 KB
/
tenant_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
package main
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestTenantsConfig(t *testing.T) {
testCases := map[string]struct {
fileContent TenantsConfig
expectErr bool
expectTenants []string
expectPanics bool
}{
"empty file": {
fileContent: TenantsConfig{},
expectErr: true,
},
"valid tenants": {
fileContent: TenantsConfig{
Tenants: []TenantConfig{
{
ID: "tenant1",
},
{
ID: "tenant2",
},
},
},
expectTenants: []string{"tenant1", "tenant2"},
},
"multiple tenants with duplicates": {
fileContent: TenantsConfig{
Tenants: []TenantConfig{
{
ID: "tenant1",
},
{
ID: "tenant1",
},
},
},
expectErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
tenantsCfg, err := yaml.Marshal(tc.fileContent)
assert.NoError(t, err)
tenants, err := readTenantsConfig(tenantsCfg)
if tc.expectErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Len(t, tenants, len(tc.expectTenants))
})
}
}
type testTenantsSetterFunc func(tenants []string) error
func (f testTenantsSetterFunc) SetTenants(tenants []string) {
f(tenants)
}
func TestTenantsFileReloader(t *testing.T) {
testCases := map[string]struct {
tenantsReader func() ([]string, error)
interval time.Duration
contextDuration time.Duration
expectTenantsUpdateCalls int
expectErr bool
}{
"reloads tenants until context cancel": {
tenantsReader: func() ([]string, error) {
return []string{"tenant1"}, nil
},
interval: 100 * time.Millisecond,
contextDuration: 250 * time.Millisecond,
expectTenantsUpdateCalls: 2,
},
"3 errors in a row exits with error": {
tenantsReader: func() ([]string, error) {
return nil, errors.New("test error")
},
interval: 100 * time.Millisecond,
contextDuration: 1000 * time.Millisecond,
expectTenantsUpdateCalls: 0,
expectErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
tenantsUpdateCalls := 0
tenantsUpdate := func(tenants []string) error {
tenantsUpdateCalls++
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), tc.contextDuration)
defer cancel()
err := newTenantsFileReloader(ctx, tc.tenantsReader, tc.interval, testTenantsSetterFunc(tenantsUpdate))
if tc.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.expectTenantsUpdateCalls, tenantsUpdateCalls)
})
}
}