-
Notifications
You must be signed in to change notification settings - Fork 98
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
Fix reporting duration error and add tests #84
Merged
openshift-merge-robot
merged 4 commits into
openshift:master
from
martinkunc:invalid-interval-not-reported2
Mar 5, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package configobserver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/openshift/insights-operator/pkg/config" | ||
"github.com/openshift/insights-operator/pkg/utils" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
clsetfake "k8s.io/client-go/kubernetes/fake" | ||
corefake "k8s.io/client-go/kubernetes/typed/core/v1/fake" | ||
"k8s.io/klog" | ||
|
||
clienttesting "k8s.io/client-go/testing" | ||
) | ||
|
||
func TestChangeSupportConfig(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
config map[string]*corev1.Secret | ||
expConfig *config.Controller | ||
expErr error | ||
}{ | ||
{name: "interval too short", | ||
config: map[string]*corev1.Secret{ | ||
pullSecretKey: &corev1.Secret{Data: map[string][]byte{ | ||
".dockerconfigjson": nil, | ||
}}, | ||
supportKey: &corev1.Secret{Data: map[string][]byte{ | ||
"username": []byte("someone"), | ||
"password": []byte("secret"), | ||
"endpoint": []byte("http://po.rt"), | ||
"interval": []byte("1s"), | ||
}}, | ||
}, | ||
expErr: fmt.Errorf("insights secret interval must be a duration (1h, 10m) greater than or equal to one minute: too short"), | ||
}, | ||
{name: "correct interval", | ||
config: map[string]*corev1.Secret{ | ||
pullSecretKey: &corev1.Secret{Data: map[string][]byte{ | ||
".dockerconfigjson": nil, | ||
}}, | ||
supportKey: &corev1.Secret{Data: map[string][]byte{ | ||
"interval": []byte("1m"), | ||
}}, | ||
}, | ||
expConfig: &config.Controller{ | ||
Interval: 1 * time.Minute, | ||
}, | ||
expErr: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
// log klog to test | ||
klog.SetOutput(utils.NewTestLog(t).Writer()) | ||
|
||
ctrl := config.Controller{} | ||
kube := kubeClientResponder{} | ||
secs := tt.config | ||
// setup mock responses for secretes by secret name | ||
kube.CoreV1().(*corefake.FakeCoreV1).Fake.AddReactor("get", "secrets", | ||
func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) { | ||
actionName := "" | ||
if getAction, ok := action.(clienttesting.GetAction); ok { | ||
actionName = getAction.GetName() | ||
} | ||
|
||
key := fmt.Sprintf("(%s) %s.%s", action.GetResource(), action.GetNamespace(), actionName) | ||
sv, ok := secs[key] | ||
if !ok { | ||
return false, nil, nil | ||
} | ||
return true, sv, nil | ||
}) | ||
// imitates New function | ||
c := &Controller{ | ||
kubeClient: &kube, | ||
defaultConfig: ctrl, | ||
} | ||
c.mergeConfigLocked() | ||
err := c.retrieveToken() | ||
if err == nil { | ||
err = c.retrieveConfig() | ||
} | ||
expErrS := "" | ||
if tt.expErr != nil { | ||
expErrS = tt.expErr.Error() | ||
} | ||
errS := "" | ||
if err != nil { | ||
errS = err.Error() | ||
} | ||
if expErrS != errS { | ||
t.Fatalf("The test expected error doesn't match actual error.\nExpected: %s Actual: %s", tt.expErr, err) | ||
} | ||
if tt.expConfig != nil && !reflect.DeepEqual(tt.expConfig, c.config) { | ||
t.Fatalf("The test expected config doesn't match actual config.\nExpected: %v Actual: %v", tt.expConfig, c.config) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
const ( | ||
pullSecretKey = "(/v1, Resource=secrets) openshift-config.pull-secret" | ||
supportKey = "(/v1, Resource=secrets) openshift-config.support" | ||
intervalKey = "interval" | ||
) | ||
|
||
func mustMarshal(v interface{}) []byte { | ||
bt, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bt | ||
} | ||
|
||
func mustLoad(filename string) []byte { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
panic(fmt.Errorf("test failed to load data: %v", err)) | ||
} | ||
defer f.Close() | ||
bts, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
panic(fmt.Errorf("test failed to read data: %v", err)) | ||
} | ||
return bts | ||
} | ||
|
||
type kubeClientResponder struct { | ||
clsetfake.Clientset | ||
} | ||
|
||
var _ kubernetes.Interface = (*kubeClientResponder)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
) | ||
|
||
func NewTestLog(t testing.TB) *log.Logger { | ||
t.Helper() | ||
return log.New(testWriter{TB: t}, t.Name()+" ", log.LstdFlags|log.Lshortfile|log.LUTC) | ||
} | ||
|
||
type testWriter struct { | ||
testing.TB | ||
} | ||
|
||
func (tw testWriter) Write(p []byte) (int, error) { | ||
tw.Helper() | ||
tw.Logf("%s", p) | ||
return len(p), nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather create 2 tests, each test with only one purpose. It's more clear to read and understand. But up to you.