Skip to content

Commit

Permalink
Bug 1952906: Add a few tests to configobserver_test.go (#409)
Browse files Browse the repository at this point in the history
* Add a few tests to configobserver_test.go

* Fixes according to the reviews

* remove unnecessary code
  • Loading branch information
0sewa0 authored May 7, 2021
1 parent 4df6a76 commit f3ff4b5
Showing 1 changed file with 147 additions and 10 deletions.
157 changes: 147 additions & 10 deletions pkg/config/configobserver/configobserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import (
clienttesting "k8s.io/client-go/testing"
)

type kubeClientResponder struct {
clsetfake.Clientset
}

const (
pullSecretKey = "(/v1, Resource=secrets) openshift-config.pull-secret" //nolint: gosec
supportKey = "(/v1, Resource=secrets) openshift-config.support"
)

//nolint: lll, funlen
func Test_ConfigObserver_ChangeSupportConfig(t *testing.T) {
var cases = []struct {
name string
Expand All @@ -40,6 +50,50 @@ func Test_ConfigObserver_ChangeSupportConfig(t *testing.T) {
},
expErr: fmt.Errorf("insights secret interval must be a duration (1h, 10m) greater than or equal to ten seconds: too short"),
},
{name: "interval incorrect format",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"interval": []byte("every second"),
}},
},
expErr: fmt.Errorf("insights secret interval must be a duration (1h, 10m) greater than or equal to ten seconds: time: invalid duration \"every second\""),
},
{name: "reportPullingDelay incorrect format",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"reportPullingDelay": []byte("every second"),
}},
},
expConfig: &config.Controller{}, // it only produces a warning in the log
},
{name: "reportMinRetryTime incorrect format",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"reportMinRetryTime": []byte("every second"),
}},
},
expConfig: &config.Controller{}, // it only produces a warning in the log
},
{name: "reportPullingTimeout incorrect format",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"reportPullingTimeout": []byte("every second"),
}},
},
expConfig: &config.Controller{}, // it only produces a warning in the log
},
{name: "correct interval",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
Expand All @@ -54,6 +108,44 @@ func Test_ConfigObserver_ChangeSupportConfig(t *testing.T) {
},
expErr: nil,
},
{name: "set-all-config",
config: map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"username": []byte("Dude"),
"password": []byte("pswd123"),
"endpoint": []byte("http://po.rt"),
"httpProxy": []byte("http://pro.xy"),
"httpsProxy": []byte("https://pro.xy"),
"noProxy": []byte("http://no.xy"),
"enableGlobalObfuscation": []byte("true"),
"reportEndpoint": []byte("http://rep.rt"),
"reportPullingDelay": []byte("10m"),
"reportMinRetryTime": []byte("10m"),
"reportPullingTimeout": []byte("10m"),
"interval": []byte("10m"),
}},
},
expConfig: &config.Controller{
Report: true,
Username: "Dude",
Password: "pswd123",
Endpoint: "http://po.rt",
HTTPConfig: config.HTTPConfig{
HTTPProxy: "http://pro.xy",
HTTPSProxy: "https://pro.xy",
NoProxy: "http://no.xy",
},
EnableGlobalObfuscation: true,
ReportEndpoint: "http://rep.rt",
ReportPullingDelay: 10 * time.Minute,
ReportMinRetryTime: 10 * time.Minute,
ReportPullingTimeout: 10 * time.Minute,
Interval: 10 * time.Minute,
},
},
}

for _, tt := range cases {
Expand Down Expand Up @@ -97,10 +189,61 @@ func Test_ConfigObserver_ChangeSupportConfig(t *testing.T) {
}
}

const (
pullSecretKey = "(/v1, Resource=secrets) openshift-config.pull-secret" //nolint: gosec
supportKey = "(/v1, Resource=secrets) openshift-config.support"
)
func Test_ConfigObserver_ConfigChanged(t *testing.T) {
ctrl := config.Controller{}
kube := kubeClientResponder{}
// Imitates New function
c := &Controller{
kubeClient: &kube,
defaultConfig: ctrl,
}
c.mergeConfigLocked()

// Subscribe to config change event
configCh, closeFn := c.ConfigChanged()
if len(configCh) > 0 {
t.Fatalf("Config channel is not empty on start.")
}
// Setup mock for 1. config update
provideSecretMock(&kube, map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"username": []byte("test2"),
}},
})
// 1. config update
err := c.retrieveConfig(context.TODO())
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
// Check if the event arrived on the channel
if len(configCh) != 1 {
t.Fatalf("Config channel has more/less then 1 event on a singal config change. len(configCh)==%d", len(configCh))
}

// Unsubscribe from config change
closeFn()
// Setup mock for 2. config update
provideSecretMock(&kube, map[string]*corev1.Secret{
pullSecretKey: {Data: map[string][]byte{
".dockerconfigjson": nil,
}},
supportKey: {Data: map[string][]byte{
"username": []byte("test3"),
}},
})
// 2. config update
err = c.retrieveConfig(context.TODO())
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
// Check if unsubscribe worked. ie: no new event on the channel
if len(configCh) != 1 {
t.Fatalf("The closing function failed to unsubscribe from the config change event. len(configCh)==%d", len(configCh))
}
}

func provideSecretMock(kube kubernetes.Interface, secs map[string]*corev1.Secret) {
kube.CoreV1().(*corefake.FakeCoreV1).Fake.AddReactor("get", "secrets",
Expand All @@ -119,9 +262,3 @@ func provideSecretMock(kube kubernetes.Interface, secs map[string]*corev1.Secret
return true, sv, nil
})
}

type kubeClientResponder struct {
clsetfake.Clientset
}

var _ kubernetes.Interface = (*kubeClientResponder)(nil)

0 comments on commit f3ff4b5

Please sign in to comment.