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

Override default index #666

Merged
merged 10 commits into from
Nov 22, 2020

Conversation

chriskim06
Copy link
Member

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

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 14, 2020
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 14, 2020
@chriskim06
Copy link
Member Author

/assign @ahmetb
/assign @corneliusweig

Copy link
Contributor

@corneliusweig corneliusweig left a 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.

if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
return uri
}
return "https://github.com/kubernetes-sigs/krew-index.git"
Copy link
Contributor

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?

Comment on lines 30 to 37
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"
}
Copy link
Contributor

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:

Suggested change
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.

@@ -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)
Copy link
Contributor

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.

Suggested change
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) {
Copy link
Contributor

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. 👍

@@ -141,6 +141,18 @@ func TestKrewInstall_CustomIndex(t *testing.T) {
test.AssertExecutableNotInPATH("kubectl-" + validPlugin2)
}

func TestKrewInstall_CustomDefaultIndexURI(t *testing.T) {
Copy link
Contributor

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.

Comment on lines 296 to 304
// 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
}
}
Copy link
Contributor

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.

@corneliusweig
Copy link
Contributor

Btw, look at the issue number 😈

@chriskim06
Copy link
Member Author

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.

Copy link
Contributor

@corneliusweig corneliusweig left a 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!

integration_test/version_test.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 16, 2020
Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
@chriskim06
Copy link
Member Author

@corneliusweig @ahmetb could I get another round of reviews 🙏

@corneliusweig
Copy link
Contributor

@chriskim06 Sure, looks good! Thanks for taking care of this!
/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 22, 2020
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit bb0fe8b into kubernetes-sigs:master Nov 22, 2020
)

// DefaultIndexURI points to the upstream index.
var DefaultIndexURI = defaultIndexURI
Copy link
Member

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.

Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants