-
Notifications
You must be signed in to change notification settings - Fork 372
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
Override default index #666
Override default index #666
Conversation
/assign @ahmetb |
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 looking into this, @chriskim06!
Given that this is a really simple logic change, I don't think we should go crazy with testing all config variations. So I think it would be better to keep just one of the integration tests, and maybe add unit tests? However, the logic is simple enough so that it does not require a unit test IMO.
pkg/constants/constants.go
Outdated
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" { | ||
return uri | ||
} | ||
return "https://github.com/kubernetes-sigs/krew-index.git" |
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.
Maybe keep this as an unexported constant?
pkg/constants/constants.go
Outdated
var DefaultIndexURI = getDefaultIndexURI() | ||
|
||
func getDefaultIndexURI() string { | ||
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" { | ||
return uri | ||
} | ||
return "https://github.com/kubernetes-sigs/krew-index.git" | ||
} |
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'd suggest to use this pattern:
var DefaultIndexURI = getDefaultIndexURI() | |
func getDefaultIndexURI() string { | |
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" { | |
return uri | |
} | |
return "https://github.com/kubernetes-sigs/krew-index.git" | |
} | |
const defaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git" | |
var DefaultIndexURI string = defaultIndexURI | |
func init() { | |
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" { | |
DefaultIndexURI = uri | |
} | |
} |
Then you can unit-test this logic by explicitly calling init()
in your test. However, I think this function is so simple that it does necessarily not need one.
integration_test/version_test.go
Outdated
@@ -36,7 +48,7 @@ func TestKrewVersion(t *testing.T) { | |||
} | |||
optionValue := lineSplit.Split(line, 2) | |||
if len(optionValue) < 2 { | |||
t.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue) | |||
test.t.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue) |
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.
Better return an error from this function and log the result in the caller, i.e.
test.t.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue) | |
return fmt.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue) |
checkRequiredSubstrings(test, "https://github.com/kubernetes-sigs/krew-index.git", stdOut) | ||
} | ||
|
||
func TestKrewVersion_CustomDefaultIndexURI(t *testing.T) { |
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 think this test is the best to keep, because it is simple and does not do any extra installation. 👍
integration_test/install_test.go
Outdated
@@ -141,6 +141,18 @@ func TestKrewInstall_CustomIndex(t *testing.T) { | |||
test.AssertExecutableNotInPATH("kubectl-" + validPlugin2) | |||
} | |||
|
|||
func TestKrewInstall_CustomDefaultIndexURI(t *testing.T) { |
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 don't think this test is necessary. We don't need to check all our machinery with & without setting all env variables. You can't do that exhaustively anyway, because you get a factorial explosion of test cases.
I'd recommend to have a single simple test that checks if the env var gets picked up.
integration_test/testutil_test.go
Outdated
// check for KREW_DEFAULT_INDEX_URI | ||
for _, e := range it.env { | ||
if strings.Contains(e, "KREW_DEFAULT_INDEX_URI") { | ||
val := strings.Split(e, "=")[1] | ||
it.extractKrewIndexTar(val) | ||
it.Krew("index", "add", "default", val).RunOrFail() | ||
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.
Let's not complicate the test setup further. This adds considerable complexity for a one-line config change.
I'd prefer not to have a special case for KREW_DEFAULT_INDEX_URI
here.
Btw, look at the issue number 😈 |
Thanks for the feedback! You're right that the integration tests were definitely the most complex piece of this. I'll make the changes you suggested. |
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.
Just one last nit, then let's merge this!
Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
@corneliusweig @ahmetb could I get another round of reviews 🙏 |
@chriskim06 Sure, looks good! Thanks for taking care of this! |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: chriskim06, corneliusweig 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 |
) | ||
|
||
// DefaultIndexURI points to the upstream index. | ||
var DefaultIndexURI = defaultIndexURI |
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.
sorry for the after-the-merge review.
I think we should not have code in pkg/constants that are actually not constants and change in runtime.
I recommend keeping this package as-is and implementing a DefaultIndex() method in another package, like pkg/index.
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.
no worries, that makes sense. I think I can get a PR for that in later today
Let me know what you guys think about the integration tests. I decided to reuse the krew-index tarball when
KREW_DEFAULT_INDEX_URI
is passed in a test by unarchiving it in the directory specified. Even though it's the same contents as krew-index I felt it still tests this feature since the default index is changing to some temp dir instead of https://github.com/kubernetes-sigs/krew-index.I figured I could do the docs change in a follow up PR, but let me know if you want me to add it here.
Related issue: #637