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

integration_test: Create helpers to initialize tests with custom indexes #598

Merged
merged 4 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestKrewIndexAdd(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
if _, err := test.Krew("index", "add").Run(); err == nil {
t.Fatal("expected index add with no args to fail")
}
Expand All @@ -51,7 +51,7 @@ func TestKrewIndexAddUnsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()

cases := []string{"a/b", `a\b`, "../a", `..\a`}
expected := "invalid index name"
Expand All @@ -72,15 +72,14 @@ func TestKrewIndexList(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
out := test.Krew("index", "list").RunOrFailOutput()
if indexes := lines(out); len(indexes) < 2 {
// the first line is the header
t.Fatal("expected at least 1 index in output")
}

localIndex := test.TempDir().Path("index/" + constants.DefaultIndexName)
test.Krew("index", "add", "foo", localIndex).RunOrFail()
test.WithCustomIndexFromDefault("foo")
out = test.Krew("index", "list").RunOrFailOutput()
if indexes := lines(out); len(indexes) < 3 {
// the first line is the header
Expand All @@ -94,7 +93,7 @@ func TestKrewIndexList_NoIndexes(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
index := test.TempDir().Path("index")
if err := os.RemoveAll(index); err != nil {
t.Fatalf("error removing default index: %v", err)
Expand Down Expand Up @@ -122,18 +121,16 @@ func TestKrewIndexRemove_nonExisting(t *testing.T) {
func TestKrewIndexRemove_ok(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
defer cleanup()

localIndex := test.TempDir().Path("index/" + constants.DefaultIndexName)
test.Krew("index", "add", "foo", localIndex).RunOrFail()
test.Krew("index", "remove", "foo").RunOrFail()
}

func TestKrewIndexRemove_unsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
defer cleanup()

expected := "invalid index name"
Expand All @@ -151,7 +148,7 @@ func TestKrewIndexRemove_unsafe(t *testing.T) {
func TestKrewIndexRemoveFailsWhenPluginsInstalled(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
defer cleanup()

test.Krew("install", validPlugin).RunOrFailOutput()
Expand Down
9 changes: 3 additions & 6 deletions integration_test/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestKrewInfo(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

out := string(test.WithIndex().Krew("info", validPlugin).RunOrFailOutput())
out := string(test.WithDefaultIndex().Krew("info", validPlugin).RunOrFailOutput())
expected := `INDEX: default`
if !strings.Contains(out, expected) {
t.Fatalf("info output doesn't have %q. output=%q", expected, out)
Expand All @@ -41,7 +41,7 @@ func TestKrewInfoInvalidPlugin(t *testing.T) {
defer cleanup()

plugin := "invalid-plugin"
_, err := test.WithIndex().Krew("info", plugin).Run()
_, err := test.WithDefaultIndex().Krew("info", plugin).Run()
if err == nil {
t.Errorf("Expected `krew info %s` to fail", plugin)
}
Expand All @@ -53,10 +53,7 @@ func TestKrewInfoCustomIndex(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
defaultIndexRoot := test.TempDir().Path("index/" + constants.DefaultIndexName)

test.Krew("index", "add", "foo", defaultIndexRoot).RunOrFail()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()

out := string(test.Krew("info", "foo/"+validPlugin).RunOrFailOutput())
Expand Down
15 changes: 7 additions & 8 deletions integration_test/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestKrewInstall(t *testing.T) {
t.Fatal("expected to fail without initializing the index")
}

test = test.WithIndex()
test = test.WithDefaultIndex()
if err := test.Krew("install"); err == nil {
t.Fatal("expected failure without any args or stdin")
}
Expand All @@ -52,7 +52,7 @@ func TestKrewInstallReRun(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithIndex()
test = test.WithDefaultIndex()
test.Krew("install", validPlugin).RunOrFail()
test.Krew("install", validPlugin).RunOrFail()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
Expand All @@ -62,7 +62,7 @@ func TestKrewInstallUnsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()

cases := []string{
`../index/` + validPlugin,
Expand All @@ -89,7 +89,7 @@ func TestKrewInstall_MultiplePositionalArgs(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithIndex().Krew("install", validPlugin, validPlugin2).RunOrFailOutput()
test.WithDefaultIndex().Krew("install", validPlugin, validPlugin2).RunOrFailOutput()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
test.AssertExecutableInPATH("kubectl-" + validPlugin2)
}
Expand All @@ -100,7 +100,7 @@ func TestKrewInstall_Stdin(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithIndex().WithStdin(strings.NewReader(validPlugin + "\n" + validPlugin2)).
test.WithDefaultIndex().WithStdin(strings.NewReader(validPlugin + "\n" + validPlugin2)).
Krew("install").RunOrFailOutput()

test.AssertExecutableInPATH("kubectl-" + validPlugin)
Expand All @@ -114,7 +114,7 @@ func TestKrewInstall_StdinAndPositionalArguments(t *testing.T) {
defer cleanup()

// when stdin is detected, it's ignored in favor of positional arguments
test.WithIndex().
test.WithDefaultIndex().
WithStdin(strings.NewReader(validPlugin2)).
Krew("install", validPlugin).RunOrFail()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
Expand All @@ -138,8 +138,7 @@ func TestKrewInstall_CustomIndex(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.Krew("index", "add", "foo", test.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFail()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
test.AssertPluginFromIndex(validPlugin, "foo")
Expand Down
5 changes: 2 additions & 3 deletions integration_test/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestKrewList(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
initialList := test.Krew("list").RunOrFailOutput()
initialOut := []byte{'\n'}

Expand All @@ -52,7 +52,6 @@ func TestKrewList(t *testing.T) {
t.Fatalf("'list' output doesn't match:\n%s", diff)
}

test.Krew("index", "add", "foo", test.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFail()
test.Krew("install", "foo/"+validPlugin2).RunOrFail()

want := []string{validPlugin, "foo/" + validPlugin2}
Expand All @@ -67,7 +66,7 @@ func TestKrewListSorted(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
os.Setenv(constants.EnableMultiIndexSwitch, "1")
defer os.Unsetenv(constants.EnableMultiIndexSwitch)

Expand Down
16 changes: 4 additions & 12 deletions integration_test/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestKrewSearchAll(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

output := test.WithIndex().Krew("search").RunOrFailOutput()
output := test.WithDefaultIndex().Krew("search").RunOrFailOutput()
if plugins := lines(output); len(plugins) < 10 {
// the first line is the header
t.Errorf("Expected at least %d plugins", len(plugins)-1)
Expand All @@ -42,7 +42,7 @@ func TestKrewSearchOne(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

plugins := lines(test.WithIndex().Krew("search", "krew").RunOrFailOutput())
plugins := lines(test.WithDefaultIndex().Krew("search", "krew").RunOrFailOutput())
if len(plugins) < 2 {
t.Errorf("Expected krew to be a valid plugin")
}
Expand All @@ -54,13 +54,9 @@ func TestKrewSearchOne(t *testing.T) {
func TestKrewSearchMultiIndex(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
defer cleanup()

// alias default plugin index to another
localIndex := test.TempDir().Path("index/" + constants.DefaultIndexName)
test.Krew("index", "add", "foo", localIndex).RunOrFailOutput()

test.Krew("install", validPlugin).RunOrFail()
test.Krew("install", "foo/"+validPlugin2).RunOrFail()

Expand All @@ -81,13 +77,9 @@ func TestKrewSearchMultiIndex(t *testing.T) {
func TestKrewSearchMultiIndexSortedByDisplayName(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
defer cleanup()

// alias default plugin index to another
localIndex := test.TempDir().Path("index/" + constants.DefaultIndexName)
test.Krew("index", "add", "foo", localIndex).RunOrFailOutput()

output := string(test.Krew("search").RunOrFailOutput())

// match first column that is not NAME by matching everything up until a space
Expand Down
8 changes: 4 additions & 4 deletions integration_test/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestKrewSystem(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithIndex().Krew("install", validPlugin).RunOrFail()
test.WithDefaultIndex().Krew("install", validPlugin).RunOrFail()

// needs to be after initial installation
prepareOldKrewRoot(test)
Expand All @@ -50,7 +50,7 @@ func TestKrewSystem_ReceiptForKrew(t *testing.T) {
prepareOldKrewRoot(test)
touch(test.tempDir, "store/krew/ensure-folder-exists")

test.WithIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()
test.WithDefaultIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()

assertReceiptExistsFor(test, "krew")
}
Expand All @@ -64,7 +64,7 @@ func TestKrewSystem_IgnoreAdditionalFolders(t *testing.T) {
prepareOldKrewRoot(test)

touch(test.tempDir, "store/not-a-plugin/ensure-folder-exists")
out := test.WithIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()
out := test.WithDefaultIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()

if !bytes.Contains(out, []byte("Skipping plugin not-a-plugin")) {
t.Errorf("Expected a message that 'not-a-plugin' is skipped, but output was:")
Expand All @@ -85,7 +85,7 @@ func TestKrewSystem_IgnoreUnknownPlugins(t *testing.T) {

prepareOldKrewRoot(test)

out := test.WithIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()
out := test.WithDefaultIndex().Krew("system", "receipts-upgrade").RunOrFailOutput()

if !bytes.Contains(out, []byte("Skipping plugin foo")) {
t.Errorf("Expected a message that 'foo' is skipped, but output was:")
Expand Down
31 changes: 25 additions & 6 deletions integration_test/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,24 @@ func (it *ITest) Root() string {
return it.tempDir.Root()
}

// WithIndex initializes the index with the actual krew-index from github/kubernetes-sigs/krew-index.
func (it *ITest) WithIndex() *ITest {
// WithDefaultIndex initializes the index with the actual krew-index from github/kubernetes-sigs/krew-index.
func (it *ITest) WithDefaultIndex() *ITest {
it.initializeIndex()
return it
}

// WithCustomIndexFromDefault initializes a new index by cloning the default index. WithDefaultIndex needs
// to be called before this function. This is a helper function for working with custom indexes in the
// integration tests so that developers don't need to alias the cloned default index each time.
func (it *ITest) WithCustomIndexFromDefault(name string) *ITest {
// TODO(chriskim06) remove this once index migration happens
if !isMultiIndexEnabled(it.env) {
it.t.Fatalf("Cannot add a custom index without %s set", constants.EnableMultiIndexSwitch)
}
it.Krew("index", "add", name, it.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFail()
return it
}

// WithEnv sets an environment variable for the krew run.
func (it *ITest) WithEnv(key string, value interface{}) *ITest {
if key == "KREW_ROOT" {
Expand Down Expand Up @@ -299,13 +311,20 @@ func (it *ITest) initializeIndex() {
}

// TODO(chriskim06) simplify once multi-index is enabled
for _, e := range it.env {
if isMultiIndexEnabled(it.env) {
if err := indexmigration.Migrate(environment.NewPaths(it.Root())); err != nil {
it.t.Fatalf("error migrating index: %s", err)
}
}
}

func isMultiIndexEnabled(env []string) bool {
for _, e := range env {
if strings.Contains(e, constants.EnableMultiIndexSwitch) {
if err := indexmigration.Migrate(environment.NewPaths(it.Root())); err != nil {
it.t.Fatalf("error migrating index: %s", err)
}
return true
}
}
return false
}

func initFromGitClone(t *testing.T) ([]byte, error) {
Expand Down
13 changes: 6 additions & 7 deletions integration_test/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestKrewUninstall(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithIndex()
test = test.WithDefaultIndex()

if _, err := test.Krew("uninstall").Run(); err == nil {
t.Fatal("expected failure without no arguments")
Expand All @@ -53,8 +53,7 @@ func TestKrewUninstallPluginsFromCustomIndex(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.Krew("index", "add", "foo", test.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFail()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()

test.Krew("uninstall", validPlugin).RunOrFail()
Expand All @@ -67,7 +66,7 @@ func TestKrewUninstall_CannotUseIndexSyntax(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
out, err := test.Krew("uninstall", "foo/"+validPlugin).Run()
if err == nil {
t.Error("expected error when uninstalling by canonical name")
Expand All @@ -83,7 +82,7 @@ func TestKrewRemove_AliasSupported(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test.WithIndex().Krew("install", validPlugin).RunOrFailOutput()
test.WithDefaultIndex().Krew("install", validPlugin).RunOrFailOutput()
test.Krew("remove", validPlugin).RunOrFailOutput()
test.AssertExecutableNotInPATH("kubectl-" + validPlugin)
}
Expand All @@ -94,7 +93,7 @@ func TestKrewRemove_ManifestRemovedFromIndex(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

test = test.WithIndex()
test = test.WithDefaultIndex()
manifestDir := environment.NewPaths(test.Root()).IndexPluginsPath(constants.DefaultIndexName)
localManifest := filepath.Join(manifestDir, validPlugin+constants.ManifestExtension)
if _, err := os.Stat(localManifest); err != nil {
Expand All @@ -111,7 +110,7 @@ func TestKrewRemove_Unsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
test.Krew("install", validPlugin).RunOrFailOutput()

cases := []string{
Expand Down
Loading