-
Notifications
You must be signed in to change notification settings - Fork 2k
/
options_test.go
111 lines (98 loc) · 3.68 KB
/
options_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
/*
Copyright 2021 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options
import (
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
//"github.com/cert-manager/cert-manager/controller-binary/app/options"
config "github.com/cert-manager/cert-manager/internal/apis/config/controller"
defaults "github.com/cert-manager/cert-manager/internal/apis/config/controller/v1alpha1"
"github.com/cert-manager/cert-manager/internal/apis/config/controller/validation"
)
func TestEnabledControllers(t *testing.T) {
tests := map[string]struct {
controllers []string
expEnabled sets.String
}{
"if no controllers enabled, return empty": {
controllers: []string{},
expEnabled: sets.NewString(),
},
"if some controllers enabled, return list": {
controllers: []string{"foo", "bar"},
expEnabled: sets.NewString("foo", "bar"),
},
"if some controllers enabled, one then disabled, return list without disabled": {
controllers: []string{"foo", "bar", "-foo"},
expEnabled: sets.NewString("bar"),
},
"if all default controllers enabled, return all default controllers": {
controllers: []string{"*"},
expEnabled: sets.NewString(defaults.DefaultEnabledControllers...),
},
"if all controllers enabled, some diabled, return all controllers with disabled": {
controllers: []string{"*", "-clusterissuers", "-issuers"},
expEnabled: sets.NewString(defaults.DefaultEnabledControllers...).Delete("clusterissuers", "issuers"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
o := config.ControllerConfiguration{
Controllers: test.controllers,
}
got := EnabledControllers(&o)
if !got.Equal(test.expEnabled) {
t.Errorf("got unexpected enabled, exp=%s got=%s",
test.expEnabled, got)
}
})
}
}
func TestValidate(t *testing.T) {
tests := map[string]struct {
DNS01RecursiveServers []string
expError string
}{
"if valid dns servers with ip address and port, return no errors": {
DNS01RecursiveServers: []string{"192.168.0.1:53", "10.0.0.1:5353"},
expError: "",
},
"if valid DNS servers with DoH server addresses including https prefix, return no errors": {
DNS01RecursiveServers: []string{"https://dns.example.com", "https://doh.server"},
expError: "",
},
"if invalid DNS server format due to missing https prefix, return 'invalid DNS server' error": {
DNS01RecursiveServers: []string{"dns.example.com"},
expError: "invalid DNS server",
},
"if invalid DNS server format due to invalid IP address length and no port, return 'invalid DNS server' error": {
DNS01RecursiveServers: []string{"192.168.0.1.53"},
expError: "invalid DNS server",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
o, _ := NewControllerConfiguration()
o.ACMEDNS01Config.RecursiveNameservers = test.DNS01RecursiveServers
err := validation.ValidateControllerConfiguration(o)
if test.expError != "" {
if err == nil || !strings.Contains(err.Error(), test.expError) {
t.Errorf("expected error containing '%s', but got: %v", test.expError, err)
}
} else if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}