-
Notifications
You must be signed in to change notification settings - Fork 12
/
config_test.go
85 lines (71 loc) · 1.84 KB
/
config_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
// SPDX-License-Identifier: MIT
//go:build ci
package main
import (
"encoding/json"
"testing"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/stretchr/testify/assert"
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)
var (
testAccessKeyIdRef = cmmeta.SecretKeySelector{
LocalObjectReference: cmmeta.LocalObjectReference{
Name: "alidns-secret",
},
Key: "access-key-id",
}
testAccessKeySecretRef = cmmeta.SecretKeySelector{
LocalObjectReference: cmmeta.LocalObjectReference{
Name: "alidns-secret",
},
Key: "access-key-secret",
}
)
func TestConfig_Validate(t *testing.T) {
t.Run("happy", func(t *testing.T) {
correct := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
AccessKeySecretRef: testAccessKeySecretRef,
}
loaded, err := loadConfig(&extapi.JSON{Raw: mustMarshal(correct)})
if assert.NoError(t, err) {
assert.Equal(t, correct, loaded)
}
})
t.Run("compatible", func(t *testing.T) {
correct := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
SecretAccessKeyRef: testAccessKeySecretRef,
}
loaded, err := loadConfig(&extapi.JSON{Raw: mustMarshal(correct)})
if assert.NoError(t, err) {
assert.NotEmpty(t, loaded)
}
})
t.Run("empty json", func(t *testing.T) {
_, err := loadConfig(&extapi.JSON{Raw: []byte("{}")})
assert.Error(t, err)
})
t.Run("no accessKeyId", func(t *testing.T) {
bad := &Config{
SecretAccessKeyRef: testAccessKeySecretRef,
}
_, err := loadConfig(&extapi.JSON{Raw: mustMarshal(bad)})
assert.Error(t, err)
})
t.Run("no accessKeySecret", func(t *testing.T) {
bad := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
}
_, err := loadConfig(&extapi.JSON{Raw: mustMarshal(bad)})
assert.Error(t, err)
})
}
func mustMarshal(v any) []byte {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
return data
}