Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1952906: Add a few tests to configobserver_test.go #409

Merged
merged 3 commits into from
May 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
},
0sewa0 marked this conversation as resolved.
Show resolved Hide resolved
{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))
}
}
0sewa0 marked this conversation as resolved.
Show resolved Hide resolved

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)