Skip to content

Commit bb0fe8b

Browse files
Override default index (kubernetes-sigs#666)
* Add initial changes * Use krew-index tar for alternative default index * Add tests for index list and install * Use consistent naming for new tests * Don't initialize index in test and let update clone it * Add code change suggestions * Remove extra tests * Remove outdated comment * Fix code patterns error * Update integration_test/version_test.go Co-authored-by: Cornelius Weig <[email protected]> Co-authored-by: Cornelius Weig <[email protected]>
1 parent 7f53514 commit bb0fe8b

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

integration_test/version_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"regexp"
2020
"strings"
2121
"testing"
22+
23+
"github.com/pkg/errors"
2224
)
2325

2426
func TestKrewVersion(t *testing.T) {
@@ -27,7 +29,25 @@ func TestKrewVersion(t *testing.T) {
2729
test := NewTest(t)
2830

2931
stdOut := string(test.Krew("version").RunOrFailOutput())
32+
err := checkRequiredSubstrings(test, "https://github.com/kubernetes-sigs/krew-index.git", stdOut)
33+
if err != nil {
34+
t.Error(err)
35+
}
36+
}
3037

38+
func TestKrewVersion_CustomDefaultIndexURI(t *testing.T) {
39+
skipShort(t)
40+
41+
test := NewTest(t)
42+
43+
stdOut := string(test.WithEnv("KREW_DEFAULT_INDEX_URI", "foo").Krew("version").RunOrFailOutput())
44+
err := checkRequiredSubstrings(test, "foo", stdOut)
45+
if err != nil {
46+
t.Error(err)
47+
}
48+
}
49+
50+
func checkRequiredSubstrings(test *ITest, index, stdOut string) error {
3151
lineSplit := regexp.MustCompile(`\s+`)
3252
actual := make(map[string]string)
3353
for _, line := range strings.Split(stdOut, "\n") {
@@ -36,7 +56,7 @@ func TestKrewVersion(t *testing.T) {
3656
}
3757
optionValue := lineSplit.Split(line, 2)
3858
if len(optionValue) < 2 {
39-
t.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue)
59+
return errors.Errorf("`%v` is not an `OPTION VALUE` pair separated by spaces", optionValue)
4060
}
4161
actual[optionValue[0]] = optionValue[1]
4262
}
@@ -46,7 +66,7 @@ func TestKrewVersion(t *testing.T) {
4666
"BasePath": test.Root(),
4767
"GitTag": "",
4868
"GitCommit": "",
49-
"IndexURI": "https://github.com/kubernetes-sigs/krew-index.git",
69+
"IndexURI": index,
5070
"IndexPath": path.Join(test.Root(), "index"),
5171
"InstallPath": path.Join(test.Root(), "store"),
5272
"BinPath": path.Join(test.Root(), "bin"),
@@ -56,9 +76,11 @@ func TestKrewVersion(t *testing.T) {
5676
for k, v := range requiredSubstrings {
5777
got, ok := actual[k]
5878
if !ok {
59-
t.Errorf("`krew version` output doesn't contain field %q", k)
79+
return errors.Errorf("`krew version` output doesn't contain field %q", k)
6080
} else if !strings.Contains(got, v) {
61-
t.Errorf("`krew version` %q field doesn't contain string %q (got: %q)", k, v, got)
81+
return errors.Errorf("`krew version` %q field doesn't contain string %q (got: %q)", k, v, got)
6282
}
6383
}
84+
85+
return nil
6486
}

pkg/constants/constants.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414

1515
package constants
1616

17+
import "os"
18+
1719
const (
1820
CurrentAPIVersion = "krew.googlecontainertools.github.com/v1alpha2"
1921
PluginKind = "Plugin"
2022
ManifestExtension = ".yaml"
2123
KrewPluginName = "krew" // plugin name of krew itself
2224

23-
// DefaultIndexURI points to the upstream index.
24-
DefaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
2525
// DefaultIndexName is a magic string that's used for a plugin name specified without an index.
2626
DefaultIndexName = "default"
27+
28+
// defaultIndexURI is the default krew-index URI that gets used unless it's overridden
29+
defaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
2730
)
31+
32+
// DefaultIndexURI points to the upstream index.
33+
var DefaultIndexURI = defaultIndexURI
34+
35+
func init() {
36+
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
37+
DefaultIndexURI = uri
38+
}
39+
}

0 commit comments

Comments
 (0)