1
+ package certrotationcontroller
2
+
3
+ import (
4
+ "fmt"
5
+ "testing"
6
+
7
+ configv1 "github.com/openshift/api/config/v1"
8
+ configv1listers "github.com/openshift/client-go/config/listers/config/v1"
9
+ "github.com/stretchr/testify/require"
10
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
+ "k8s.io/apimachinery/pkg/runtime"
12
+ "k8s.io/client-go/tools/cache"
13
+ )
14
+
15
+ func TestServiceHostNameFunc (t * testing.T ) {
16
+ scenarios := []struct {
17
+ name string
18
+ objects []runtime.Object
19
+ expectedError error
20
+ }{
21
+ {
22
+ "network config status not available" ,
23
+ []runtime.Object {
24
+ fakeNetwork (false ),
25
+ },
26
+ fmt .Errorf ("empty networkConfig ServiceNetwork, can't generate cert" ),
27
+ },
28
+ {
29
+ "happy with network status network ServiceNetwork" ,
30
+ []runtime.Object {
31
+ fakeNetwork (true ),
32
+ },
33
+ nil ,
34
+ },
35
+ }
36
+
37
+ for _ , scenario := range scenarios {
38
+ t .Run (scenario .name , func (t * testing.T ) {
39
+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
40
+ for _ , obj := range scenario .objects {
41
+ if err := indexer .Add (obj ); err != nil {
42
+ require .NoError (t , err )
43
+ }
44
+ }
45
+ controller := CertRotationController {
46
+ networkLister : configv1listers .NewNetworkLister (indexer ),
47
+ serviceNetwork : & DynamicServingRotation {hostnamesChanged : make (chan struct {}, 10 )},
48
+ }
49
+ err := controller .syncServiceHostnames ()
50
+ require .Equal (t , err , scenario .expectedError )
51
+ })
52
+ }
53
+ }
54
+
55
+ func fakeNetwork (hasServiceNetwork bool ) * configv1.Network {
56
+ var serviceNetwork []string
57
+ if hasServiceNetwork {
58
+ serviceNetwork = []string {"10.0.1.0/24" }
59
+ } else {
60
+ serviceNetwork = []string {}
61
+ }
62
+ return & configv1.Network {
63
+ ObjectMeta : metav1.ObjectMeta {Name : "cluster" },
64
+ Status : configv1.NetworkStatus {ServiceNetwork : serviceNetwork },
65
+ }
66
+ }
0 commit comments