From 4307f70a957705c7f8fc44edfa4d339d78214441 Mon Sep 17 00:00:00 2001 From: John Collier Date: Wed, 26 Aug 2020 11:28:13 -0400 Subject: [PATCH 1/5] Add devfile registry to preferences on init Signed-off-by: John Collier --- pkg/preference/preference.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/preference/preference.go b/pkg/preference/preference.go index 928f23ba6af..d0a69394470 100644 --- a/pkg/preference/preference.go +++ b/pkg/preference/preference.go @@ -218,6 +218,15 @@ func NewPreferenceInfo() (*PreferenceInfo, error) { // If the preference file doesn't exist then we return with default preference if _, err = os.Stat(preferenceFile); os.IsNotExist(err) { + // If initializing a new preferences file, make sure we add the default devfile registry to it + defaultRegistryList := []Registry{ + { + Name: DefaultDevfileRegistryName, + URL: DefaultDevfileRegistryURL, + Secure: false, + }, + } + c.OdoSettings.RegistryList = &defaultRegistryList return &c, nil } From a37a4c49cb9aa403528ad3f13c7b1139e6cc07f2 Mon Sep 17 00:00:00 2001 From: John Collier Date: Wed, 26 Aug 2020 11:52:15 -0400 Subject: [PATCH 2/5] Add integration test Signed-off-by: John Collier --- tests/integration/devfile/cmd_devfile_registry_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/integration/devfile/cmd_devfile_registry_test.go b/tests/integration/devfile/cmd_devfile_registry_test.go index 05c9b7c39fa..a5812cf3053 100644 --- a/tests/integration/devfile/cmd_devfile_registry_test.go +++ b/tests/integration/devfile/cmd_devfile_registry_test.go @@ -19,7 +19,7 @@ var _ = Describe("odo devfile registry command tests", func() { // Using program commmand according to cliRunner in devfile cliRunner := helper.GetCliRunner() - // This is run after every Spec (It) + // This is run before every Spec (It) var _ = BeforeEach(func() { SetDefaultEventuallyTimeout(10 * time.Minute) context = helper.CreateNewContext() @@ -59,6 +59,14 @@ var _ = Describe("odo devfile registry command tests", func() { helper.MatchAllInOutput(output, []string{"No devfile registries added to the configuration. Refer `odo registry add -h` to add one"}) }) + + It("Should list all default registries when experimental mode is set via env", func() { + helper.CmdShouldPass("odo", "preference", "unset", "Experimental") + os.Setenv("ODO_EXPERIMENTAL", "true") + output := helper.CmdShouldPass("odo", "registry", "list") + helper.MatchAllInOutput(output, []string{"DefaultDevfileRegistry"}) + os.Unsetenv("ODO_EXPERIMENTAL") + }) }) Context("When executing registry commands with the registry is not present", func() { From 0b290e5338df4e28fd0ab7c9430b7f7b7d984b7a Mon Sep 17 00:00:00 2001 From: John Collier Date: Wed, 26 Aug 2020 13:13:16 -0400 Subject: [PATCH 3/5] Add integration test Signed-off-by: John Collier --- tests/helper/helper_run.go | 18 +++++++++++++++++- .../devfile/cmd_devfile_registry_test.go | 9 +++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/helper/helper_run.go b/tests/helper/helper_run.go index 3ff30ceac14..d5b0aac7dda 100644 --- a/tests/helper/helper_run.go +++ b/tests/helper/helper_run.go @@ -17,17 +17,26 @@ func runningCmd(cmd *exec.Cmd) string { return fmt.Sprintf("Running %s with args %v", prog, cmd.Args) } -func CmdRunner(program string, args ...string) *gexec.Session { +// CmdRunnerWithEnv runs the specified program and args, and sets the given env variables +func CmdRunnerWithEnv(env []string, program string, args ...string) *gexec.Session { //prefix ginkgo verbose output with program name prefix := fmt.Sprintf("[%s] ", filepath.Base(program)) prefixWriter := gexec.NewPrefixedWriter(prefix, GinkgoWriter) command := exec.Command(program, args...) + command.Env = env fmt.Fprintln(GinkgoWriter, runningCmd(command)) session, err := gexec.Start(command, prefixWriter, prefixWriter) Expect(err).NotTo(HaveOccurred()) return session } +// CmdRunner runs the specified program and args +func CmdRunner(program string, args ...string) *gexec.Session { + // Re-use the CmdRunnerWithEnv function since the logic is the same + // By passing in nil as the env, we default to the system env + return CmdRunnerWithEnv(nil, program, args...) +} + // CmdShouldPass returns stdout if command succeeds func CmdShouldPass(program string, args ...string) string { session := CmdRunner(program, args...) @@ -35,6 +44,13 @@ func CmdShouldPass(program string, args ...string) string { return string(session.Wait().Out.Contents()) } +// CmdShouldPassWithEnv returns stdout if command succeeds +func CmdShouldPassWithEnv(env []string, program string, args ...string) string { + session := CmdRunnerWithEnv(env, program, args...) + Eventually(session).Should(gexec.Exit(0), runningCmd(session.Command)) + return string(session.Wait().Out.Contents()) +} + // CmdShouldRunWithTimeout waits for a certain duration and then returns stdout func CmdShouldRunWithTimeout(timeout time.Duration, program string, args ...string) string { session := CmdRunner(program, args...) diff --git a/tests/integration/devfile/cmd_devfile_registry_test.go b/tests/integration/devfile/cmd_devfile_registry_test.go index a5812cf3053..49644158744 100644 --- a/tests/integration/devfile/cmd_devfile_registry_test.go +++ b/tests/integration/devfile/cmd_devfile_registry_test.go @@ -61,11 +61,12 @@ var _ = Describe("odo devfile registry command tests", func() { }) It("Should list all default registries when experimental mode is set via env", func() { - helper.CmdShouldPass("odo", "preference", "unset", "Experimental") - os.Setenv("ODO_EXPERIMENTAL", "true") - output := helper.CmdShouldPass("odo", "registry", "list") + // Point to an alternative global odo config for this test case (want to test without any preferences file) + // And set experimental mode via env + fakeConfig := filepath.Join(context, "fakeconfig.yaml") + env := []string{"GLOBALODOCONFIG=" + fakeConfig, "ODO_EXPERIMENTAL=true"} + output := helper.CmdShouldPassWithEnv(env, "odo", "registry", "list") helper.MatchAllInOutput(output, []string{"DefaultDevfileRegistry"}) - os.Unsetenv("ODO_EXPERIMENTAL") }) }) From 9728862dbd753baf4ffb0bab9cdb2489b1ac72a4 Mon Sep 17 00:00:00 2001 From: John Collier Date: Wed, 26 Aug 2020 14:34:14 -0400 Subject: [PATCH 4/5] Add experimental mode gate Signed-off-by: John Collier --- pkg/preference/preference.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/preference/preference.go b/pkg/preference/preference.go index d0a69394470..8683ba2a39a 100644 --- a/pkg/preference/preference.go +++ b/pkg/preference/preference.go @@ -218,15 +218,20 @@ func NewPreferenceInfo() (*PreferenceInfo, error) { // If the preference file doesn't exist then we return with default preference if _, err = os.Stat(preferenceFile); os.IsNotExist(err) { - // If initializing a new preferences file, make sure we add the default devfile registry to it - defaultRegistryList := []Registry{ - { - Name: DefaultDevfileRegistryName, - URL: DefaultDevfileRegistryURL, - Secure: false, - }, + // Add the default devfile registry if "ODO_EXPERIMENTAL" env variable is true + // We specifically need to check for the experimental mode env, as `odo preferences set experimental true` already properly + // initializes the default devfile registries. + experimentalEnvStr, _ := os.LookupEnv("ODO_EXPERIMENTAL") + if experimentalEnvStr == "true" { + defaultRegistryList := []Registry{ + { + Name: DefaultDevfileRegistryName, + URL: DefaultDevfileRegistryURL, + Secure: false, + }, + } + c.OdoSettings.RegistryList = &defaultRegistryList } - c.OdoSettings.RegistryList = &defaultRegistryList return &c, nil } From c4df0a367a7b9dca0ffa7b19f769bdd2d9e5407a Mon Sep 17 00:00:00 2001 From: John Collier Date: Mon, 31 Aug 2020 12:16:36 -0400 Subject: [PATCH 5/5] Add nil check for registryList pointer Signed-off-by: John Collier --- pkg/odo/cli/registry/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/odo/cli/registry/list.go b/pkg/odo/cli/registry/list.go index ea99feca521..2f6393352ef 100644 --- a/pkg/odo/cli/registry/list.go +++ b/pkg/odo/cli/registry/list.go @@ -57,7 +57,7 @@ func (o *ListOptions) Run() (err error) { } registryList := cfg.OdoSettings.RegistryList - if len(*registryList) == 0 { + if registryList == nil || len(*registryList) == 0 { return fmt.Errorf("No devfile registries added to the configuration. Refer `odo registry add -h` to add one") }