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
Added support for multiple --from-env flags #104232
Conversation
t.Errorf("test %s was expecting an error but no error occurred", name) | ||
if test.expectErr != "" && err == nil { | ||
t.Errorf("was expecting an error but no error occurred") | ||
return |
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.
Added return
because without it if the test fails it will throw a panic for the next line when we want to dereference the err err.Error()
but the err will be nil
. Logically, it makes sense to have return
there because we want to move on to the next test cases if the current fail.
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 agree that only one of the four conditionals here is intended to apply to any given case. The same would be true for the configmap test changes too, right?
FWIW I like using testify
's require
for this, e.g.:
if test.expectErr != "" {
require.EqualError(t, test.expectErr, err.Error())
}
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}
It looks like testify is already used in other create
tests, so that could be an option, if you want (up to you).
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.
Thanks, didn't know that exists 👍🏼
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.
Actually I think I didn't explain it well enough,
For example if I have a test case
"foo": {
secretName: "foo",
fromLiteral: []string{"key1=value1"},
expectErr: `foo`,
},
when it will first reach this line, which is fine because we are expecting an error and the err is nil
if test.expectErr != "" && err == nil
But when it reaches the next line, err.Error()
will cause a panic because err
is nil
.
if test.expectErr != "" && test.expectErr != err.Error() {
If I do,
if test.expectErr != "" {
require.EqualError(t, test.expectErr, err.Error())
}
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}
woudln't require.EqualError(t, test.expectErr, err.Error())
cause panic too if err is nil
?
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.
Ah yes, you're right. You'd need an additional require.Error(t, err)
as the second line.
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.
If that's the case, do you think require
is better or return
? Personally, I think return
would make the code cleaner.
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 find the require
version more succinct/readable, but it's a personal preference. I'll LGTM either way once the error wording fix is pushed.
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.
sure! let's go with require
then, I will play around with it and make an update. Thank you
/triage accepted |
/cc |
t.Errorf("test %s was expecting an error but no error occurred", name) | ||
if test.expectErr != "" && err == nil { | ||
t.Errorf("was expecting an error but no error occurred") | ||
return |
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 agree that only one of the four conditionals here is intended to apply to any given case. The same would be true for the configmap test changes too, right?
FWIW I like using testify
's require
for this, e.g.:
if test.expectErr != "" {
require.EqualError(t, test.expectErr, err.Error())
}
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}
It looks like testify is already used in other create
tests, so that could be an option, if you want (up to you).
} | ||
} | ||
if info.IsDir() { | ||
return fmt.Errorf("env config file cannot be a directory") |
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.
This error message changed slightly. Was it intentional or ported over from the configmap code?
return fmt.Errorf("env config file cannot be a directory") | |
return fmt.Errorf("env secret file cannot be a directory") |
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.
Thanks for the quick review! I ported the code from configmap and made the careless mistake 😅 , will make changes now
/assign @eddiezane |
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.
/lgtm
if !test.expectErr && err != nil { | ||
t.Errorf("test %s, unexpected error: %v", name, err) | ||
if test.expectErr == "" && err != nil { | ||
t.Errorf("unexpected error: %v", err) |
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.
If you want you can simplify this one slightly with require as well, and have it return instead of attempt to run the DeepEqual if the assertion fails. My full suggestion is below but this is also fine as-is.
if test.expectErr == "" {
require.NoError(err)
if !apiequality.Semantic.DeepEqual(secret, test.expected) {
t.Errorf("\nexpected:\n%#v\ngot:\n%#v", test.expected, secret)
}
} else {
require.Error(t, err)
require.EqualError(t, err, test.expectErr)
}
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.
Let's simplify it then, I think this change is better ! will make a change now
Thank you @KnVerey for your thoughtful reviews! |
/lgtm |
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.
That seems to only be changing secrets, does that already work for ConfigMaps?
err = cmdutil.AddFromEnvFile(envFileSource, func(key, value string) error { | ||
return addKeyFromLiteralToSecret(secret, key, []byte(value)) | ||
}) |
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.
What do you do if you have duplicate keys? Is this properly handled by this function? Specifically if they are coming from different files with the same name in different directories?
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.
@eddiezane actually asked the same questions 😄 and @KnVerey answered it over here (#101646 (comment))
Second one is rejected. Regardless of original source, the entries are added with addKeyFromLiteralToConfigMap, which as documented "return[s] an error if the key is not valid or if the key already exists." This behavior didn't need to change from the original single-file implementation, since it was already a possibility there.
Yea there was a PR merge for ConfigMap #101646 |
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.
Thanks for this! One very small nitpick.
/approve
/hold
@@ -21,6 +21,7 @@ import ( | |||
"os" | |||
"testing" | |||
|
|||
"github.com/stretchr/testify/require" |
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.
nit: Add a line here please. Convention is:
built in
3rd party
k8s
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: eddiezane, lauchokyip The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/lgtm |
What type of PR is this?
/kind feature
What this PR does / why we need it:
Added support for multiple --from-env flags
Which issue(s) this PR fixes:
Fixes kubernetes/kubectl#1094
Special notes for your reviewer:
/cc @KnVerey
/cc @eddiezane
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: