diff --git a/cmd/cli-doc/cli-doc.go b/cmd/cli-doc/cli-doc.go index 5423f77cffc..b356296c6df 100644 --- a/cmd/cli-doc/cli-doc.go +++ b/cmd/cli-doc/cli-doc.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "fmt" "os" @@ -171,11 +172,12 @@ func main() { if len(args) == 0 { fmt.Print(command.Usage()) } else { + ctx := context.Background() switch args[0] { case "reference": - fmt.Print(referencePrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0)) + fmt.Print(referencePrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0)) case "structure": - fmt.Print(commandPrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0)) + fmt.Print(commandPrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0)) default: fmt.Print(command.Usage()) } diff --git a/cmd/odo/odo.go b/cmd/odo/odo.go index 4aebcb70a0a..ed54bf5b584 100644 --- a/cmd/odo/odo.go +++ b/cmd/odo/odo.go @@ -6,6 +6,11 @@ import ( "os" "github.com/posener/complete" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + + "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/log" "github.com/redhat-developer/odo/pkg/odo/cli" "github.com/redhat-developer/odo/pkg/odo/cli/version" @@ -13,16 +18,24 @@ import ( "github.com/redhat-developer/odo/pkg/odo/util/completion" "github.com/redhat-developer/odo/pkg/preference" segment "github.com/redhat-developer/odo/pkg/segment/context" - "github.com/spf13/cobra" - "github.com/spf13/pflag" + "k8s.io/klog" ) func main() { + // Create a context ready for receiving telemetry data + // and save into it configuration based on environment variables + ctx := segment.NewContext(context.Background()) + envConfig, err := config.GetConfiguration() + if err != nil { + util.LogErrorAndExit(err, "") + } + ctx = envcontext.WithEnvConfig(ctx, *envConfig) + // create the complete command klog.InitFlags(nil) - root := cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName) + root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName) rootCmp := createCompletion(root) cmp := complete.New("odo", rootCmp) @@ -41,7 +54,7 @@ func main() { // parse the flags but hack around to avoid exiting with error code 2 on help flag.CommandLine.Init(os.Args[0], flag.ContinueOnError) args := os.Args[1:] - if err := flag.CommandLine.Parse(args); err != nil { + if err = flag.CommandLine.Parse(args); err != nil { if err == flag.ErrHelp { os.Exit(0) } @@ -55,7 +68,7 @@ func main() { return } - cfg, err := preference.NewClient() + cfg, err := preference.NewClient(ctx) if err != nil { util.LogErrorAndExit(err, "") } @@ -67,7 +80,7 @@ func main() { updateInfo := make(chan string) go version.GetLatestReleaseInfo(updateInfo) - util.LogErrorAndExit(root.ExecuteContext(segment.NewContext(context.Background())), "") + util.LogErrorAndExit(root.ExecuteContext(ctx), "") select { case message := <-updateInfo: log.Info(message) @@ -75,7 +88,7 @@ func main() { klog.V(4).Info("Could not get the latest release information in time. Never mind, exiting gracefully :)") } } else { - util.LogErrorAndExit(root.ExecuteContext(segment.NewContext(context.Background())), "") + util.LogErrorAndExit(root.ExecuteContext(ctx), "") } } diff --git a/pkg/odo/cli/cli.go b/pkg/odo/cli/cli.go index fd1f8d12c67..c01ac09e9b6 100644 --- a/pkg/odo/cli/cli.go +++ b/pkg/odo/cli/cli.go @@ -1,6 +1,7 @@ package cli import ( + "context" "errors" "flag" "fmt" @@ -101,8 +102,8 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e const pluginPrefix = "odo" // NewCmdOdo creates a new root command for odo -func NewCmdOdo(name, fullName string) *cobra.Command { - rootCmd := odoRootCmd(name, fullName) +func NewCmdOdo(ctx context.Context, name, fullName string) *cobra.Command { + rootCmd := odoRootCmd(ctx, name, fullName) if len(os.Args) > 1 { cmdPathPieces := os.Args[1:] @@ -120,7 +121,7 @@ func NewCmdOdo(name, fullName string) *cobra.Command { return rootCmd } -func odoRootCmd(name, fullName string) *cobra.Command { +func odoRootCmd(ctx context.Context, name, fullName string) *cobra.Command { // rootCmd represents the base command when called without any subcommands rootCmd := &cobra.Command{ Use: name, @@ -177,7 +178,7 @@ func odoRootCmd(name, fullName string) *cobra.Command { login.NewCmdLogin(login.RecommendedCommandName, util.GetFullName(fullName, login.RecommendedCommandName)), logout.NewCmdLogout(logout.RecommendedCommandName, util.GetFullName(fullName, logout.RecommendedCommandName)), version.NewCmdVersion(version.RecommendedCommandName, util.GetFullName(fullName, version.RecommendedCommandName)), - preference.NewCmdPreference(preference.RecommendedCommandName, util.GetFullName(fullName, preference.RecommendedCommandName)), + preference.NewCmdPreference(ctx, preference.RecommendedCommandName, util.GetFullName(fullName, preference.RecommendedCommandName)), telemetry.NewCmdTelemetry(telemetry.RecommendedCommandName), list.NewCmdList(list.RecommendedCommandName, util.GetFullName(fullName, list.RecommendedCommandName)), build_images.NewCmdBuildImages(build_images.RecommendedCommandName, util.GetFullName(fullName, build_images.RecommendedCommandName)), diff --git a/pkg/odo/cli/preference/preference.go b/pkg/odo/cli/preference/preference.go index 0889522ef3f..0c1f9a9d39c 100644 --- a/pkg/odo/cli/preference/preference.go +++ b/pkg/odo/cli/preference/preference.go @@ -1,6 +1,7 @@ package preference import ( + "context" "fmt" "github.com/redhat-developer/odo/pkg/odo/cli/preference/add" @@ -20,11 +21,11 @@ var preferenceLongDesc = ktemplates.LongDesc(`Modifies odo specific configuratio %[1]s`) // NewCmdPreference implements the utils config odo command -func NewCmdPreference(name, fullName string) *cobra.Command { +func NewCmdPreference(ctx context.Context, name, fullName string) *cobra.Command { // Main Commands preferenceViewCmd := NewCmdView(viewCommandName, util.GetFullName(fullName, viewCommandName)) - preferenceSetCmd := NewCmdSet(setCommandName, util.GetFullName(fullName, setCommandName)) + preferenceSetCmd := NewCmdSet(ctx, setCommandName, util.GetFullName(fullName, setCommandName)) preferenceUnsetCmd := NewCmdUnset(unsetCommandName, util.GetFullName(fullName, unsetCommandName)) preferenceAddCmd := add.NewCmdAdd(add.RecommendedCommandName, util.GetFullName(fullName, add.RecommendedCommandName)) preferenceRemoveCmd := remove.NewCmdRemove(remove.RecommendedCommandName, util.GetFullName(fullName, remove.RecommendedCommandName)) diff --git a/pkg/odo/cli/preference/set.go b/pkg/odo/cli/preference/set.go index 31b3683d95e..6de18a22364 100644 --- a/pkg/odo/cli/preference/set.go +++ b/pkg/odo/cli/preference/set.go @@ -87,14 +87,14 @@ func (o *SetOptions) Run(ctx context.Context) (err error) { } // NewCmdSet implements the config set odo command -func NewCmdSet(name, fullName string) *cobra.Command { +func NewCmdSet(ctx context.Context, name, fullName string) *cobra.Command { o := NewSetOptions() preferenceSetCmd := &cobra.Command{ Use: name, Short: "Set a value in the odo preference file", Long: fmt.Sprintf(setLongDesc, preference.FormatSupportedParameters()), Example: func(exampleString, fullName string) string { - prefClient, err := preference.NewClient() + prefClient, err := preference.NewClient(ctx) if err != nil { util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md") } diff --git a/pkg/odo/genericclioptions/clientset/clientset.go b/pkg/odo/genericclioptions/clientset/clientset.go index e8fbcd0145f..3abbad7e16a 100644 --- a/pkg/odo/genericclioptions/clientset/clientset.go +++ b/pkg/odo/genericclioptions/clientset/clientset.go @@ -153,7 +153,7 @@ func Fetch(command *cobra.Command, platform string) (*Clientset, error) { } } if isDefined(command, PREFERENCE) { - dep.PreferenceClient, err = preference.NewClient() + dep.PreferenceClient, err = preference.NewClient(command.Context()) if err != nil { return nil, err } diff --git a/pkg/odo/genericclioptions/runnable.go b/pkg/odo/genericclioptions/runnable.go index 22306df67b2..9a9fe480a4a 100644 --- a/pkg/odo/genericclioptions/runnable.go +++ b/pkg/odo/genericclioptions/runnable.go @@ -15,7 +15,6 @@ import ( "github.com/devfile/library/pkg/devfile/parser" - "github.com/redhat-developer/odo/pkg/config" "github.com/redhat-developer/odo/pkg/machineoutput" "github.com/redhat-developer/odo/pkg/odo/cmdline" @@ -81,14 +80,14 @@ const ( ) func GenericRun(o Runnable, cmd *cobra.Command, args []string) { - var err error - startTime := time.Now() - userConfig, _ := preference.NewClient() + var ( + err error + startTime = time.Now() + ctx = cmd.Context() + ) - envConfig, err := config.GetConfiguration() - if err != nil { - util.LogErrorAndExit(err, "") - } + userConfig, _ := preference.NewClient(ctx) + envConfig := envcontext.GetEnvConfig(ctx) //lint:ignore SA1019 We deprecated this env var, but until it is removed, we still need to support it disableTelemetryEnvSet := envConfig.OdoDisableTelemetry != nil @@ -97,7 +96,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { disableTelemetry = *envConfig.OdoDisableTelemetry } debugTelemetry := pointer.StringDeref(envConfig.OdoDebugTelemetryFile, "") - trackingConsentValue, isTrackingConsentEnabled, trackingConsentEnvSet, trackingConsentErr := segment.IsTrackingConsentEnabled(envConfig) + trackingConsentValue, isTrackingConsentEnabled, trackingConsentEnvSet, trackingConsentErr := segment.IsTrackingConsentEnabled(&envConfig) // check for conflicting settings if trackingConsentErr == nil && disableTelemetryEnvSet && trackingConsentEnvSet && disableTelemetry == isTrackingConsentEnabled { @@ -158,7 +157,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { scontext.SetFlags(cmd.Context(), cmd.Flags()) // set value for telemetry status in context so that we do not need to call IsTelemetryEnabled every time to check its status - scontext.SetTelemetryStatus(cmd.Context(), segment.IsTelemetryEnabled(userConfig, *envConfig)) + scontext.SetTelemetryStatus(cmd.Context(), segment.IsTelemetryEnabled(userConfig, envConfig)) // Send data to telemetry in case of user interrupt captureSignals := []os.Signal{syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt} @@ -174,7 +173,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { startTelemetry(cmd, err, startTime) }) - util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(envConfig, cmd), "") + util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(&envConfig, cmd), "") util.LogErrorAndExit(commonflags.CheckRunOnCommand(cmd), "") util.LogErrorAndExit(commonflags.CheckVariablesCommand(cmd), "") @@ -186,8 +185,6 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { } o.SetClientset(deps) - ctx := cmdLineObj.Context() - ctx = envcontext.WithEnvConfig(ctx, *envConfig) ctx = fcontext.WithJsonOutput(ctx, commonflags.GetJsonOutputValue(cmdLineObj)) ctx = fcontext.WithRunOn(ctx, platform) ctx = odocontext.WithApplication(ctx, defaultAppName) diff --git a/pkg/preference/implem.go b/pkg/preference/implem.go index e9dde55dc34..5c32b316ffc 100644 --- a/pkg/preference/implem.go +++ b/pkg/preference/implem.go @@ -1,6 +1,7 @@ package preference import ( + "context" "fmt" "os" "os/user" @@ -9,6 +10,7 @@ import ( "strings" "time" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/log" "github.com/redhat-developer/odo/pkg/odo/cli/ui" "github.com/redhat-developer/odo/pkg/util" @@ -69,9 +71,10 @@ type preferenceInfo struct { var _ Client = (*preferenceInfo)(nil) -func getPreferenceFile() (string, error) { - if env, ok := os.LookupEnv(GlobalConfigEnvName); ok { - return env, nil +func getPreferenceFile(ctx context.Context) (string, error) { + envConfig := envcontext.GetEnvConfig(ctx) + if envConfig.Globalodoconfig != nil { + return *envConfig.Globalodoconfig, nil } if len(customHomeDir) != 0 { @@ -85,8 +88,8 @@ func getPreferenceFile() (string, error) { return filepath.Join(currentUser.HomeDir, ".odo", configFileName), nil } -func NewClient() (Client, error) { - return newPreferenceInfo() +func NewClient(ctx context.Context) (Client, error) { + return newPreferenceInfo(ctx) } // newPreference creates an empty Preference struct with type meta information @@ -101,8 +104,8 @@ func newPreference() Preference { // newPreferenceInfo gets the PreferenceInfo from preference file // or returns default PreferenceInfo if preference file does not exist -func newPreferenceInfo() (*preferenceInfo, error) { - preferenceFile, err := getPreferenceFile() +func newPreferenceInfo(ctx context.Context) (*preferenceInfo, error) { + preferenceFile, err := getPreferenceFile(ctx) klog.V(4).Infof("The path for preference file is %+v", preferenceFile) if err != nil { return nil, err diff --git a/pkg/preference/implem_test.go b/pkg/preference/implem_test.go index e79586cab0b..1467f99f957 100644 --- a/pkg/preference/implem_test.go +++ b/pkg/preference/implem_test.go @@ -1,6 +1,7 @@ package preference import ( + "context" "fmt" "io/ioutil" "os" @@ -9,6 +10,9 @@ import ( "testing" "time" + "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -19,7 +23,7 @@ func TestNew(t *testing.T) { t.Fatal(err) } defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) + tempConfigFileName := tempConfigFile.Name() tests := []struct { name string @@ -52,7 +56,11 @@ func TestNew(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - cfi, err := newPreferenceInfo() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{ + Globalodoconfig: &tempConfigFileName, + }) + cfi, err := newPreferenceInfo(ctx) switch test.success { case true: if err != nil { @@ -72,12 +80,6 @@ func TestNew(t *testing.T) { } func TestGetPushTimeout(t *testing.T) { - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) nonzeroValue := 5 * time.Second @@ -103,7 +105,9 @@ func TestGetPushTimeout(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg, err := newPreferenceInfo() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{}) + cfg, err := newPreferenceInfo(ctx) if err != nil { t.Error(err) } @@ -118,12 +122,6 @@ func TestGetPushTimeout(t *testing.T) { } func TestGetTimeout(t *testing.T) { - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) zeroValue := 0 * time.Second nonzeroValue := 5 * time.Second tests := []struct { @@ -159,7 +157,9 @@ func TestGetTimeout(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg, err := newPreferenceInfo() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{}) + cfg, err := newPreferenceInfo(ctx) if err != nil { t.Error(err) } @@ -174,12 +174,6 @@ func TestGetTimeout(t *testing.T) { } func TestSetConfiguration(t *testing.T) { - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) trueValue := true falseValue := false minValue := minimumDurationValue @@ -357,7 +351,9 @@ func TestSetConfiguration(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg, err := newPreferenceInfo() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{}) + cfg, err := newPreferenceInfo(ctx) if err != nil { t.Error(err) } @@ -402,12 +398,6 @@ func TestSetConfiguration(t *testing.T) { } func TestConsentTelemetry(t *testing.T) { - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) trueValue := true falseValue := false @@ -457,13 +447,6 @@ func TestConsentTelemetry(t *testing.T) { } func TestGetupdateNotification(t *testing.T) { - - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) trueValue := true falseValue := false @@ -562,13 +545,15 @@ func TestIsSupportedParameter(t *testing.T) { func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) { // cleaning up old odo files if any - filename, err := getPreferenceFile() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{}) + filename, err := getPreferenceFile(ctx) if err != nil { t.Error(err) } os.RemoveAll(filename) - conf, err := newPreferenceInfo() + conf, err := newPreferenceInfo(ctx) if err != nil { t.Errorf("error while creating global preference %v", err) } @@ -578,7 +563,9 @@ func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) { } func TestMetaTypePopulatedInPreference(t *testing.T) { - pi, err := newPreferenceInfo() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{}) + pi, err := newPreferenceInfo(ctx) if err != nil { t.Error(err) @@ -687,12 +674,6 @@ func TestHandleWithRegistryExist(t *testing.T) { } func TestGetConsentTelemetry(t *testing.T) { - tempConfigFile, err := ioutil.TempFile("", "odoconfig") - if err != nil { - t.Fatal(err) - } - defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) trueValue := true falseValue := false diff --git a/pkg/registry/registry_test.go b/pkg/registry/registry_test.go index 22f493e6fa0..08fc5a83f67 100644 --- a/pkg/registry/registry_test.go +++ b/pkg/registry/registry_test.go @@ -39,8 +39,7 @@ OdoSettings: if err != nil { t.Error(err) } - - t.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name()) + tempConfigFileName := tempConfigFile.Name() tests := []struct { name string @@ -78,7 +77,11 @@ OdoSettings: for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prefClient, _ := preference.NewClient() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{ + Globalodoconfig: &tempConfigFileName, + }) + prefClient, _ := preference.NewClient(ctx) catClient := NewRegistryClient(filesystem.NewFakeFs(), prefClient) got, err := catClient.GetDevfileRegistries(tt.registryName) if err != nil { diff --git a/pkg/registry/utils_test.go b/pkg/registry/utils_test.go index 0ea1cf02cac..b3b85185190 100644 --- a/pkg/registry/utils_test.go +++ b/pkg/registry/utils_test.go @@ -1,25 +1,23 @@ package registry import ( + "context" "io/ioutil" "reflect" "testing" + "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/preference" ) -const ( - // GlobalConfigEnvName is the environment variable GLOBALODOCONFIG - GlobalConfigEnvName = "GLOBALODOCONFIG" -) - func TestIsSecure(t *testing.T) { tempConfigFile, err := ioutil.TempFile("", "odoconfig") if err != nil { t.Fatal(err) } defer tempConfigFile.Close() - t.Setenv(GlobalConfigEnvName, tempConfigFile.Name()) + tempConfigFileName := tempConfigFile.Name() tests := []struct { name string @@ -52,7 +50,11 @@ func TestIsSecure(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cfg, err := preference.NewClient() + ctx := context.Background() + ctx = envcontext.WithEnvConfig(ctx, config.Configuration{ + Globalodoconfig: &tempConfigFileName, + }) + cfg, err := preference.NewClient(ctx) if err != nil { t.Errorf("Unable to get preference file with error: %v", err) } diff --git a/tests/helper/helper_generic.go b/tests/helper/helper_generic.go index 2c642828fe2..be7bb56e4a1 100644 --- a/tests/helper/helper_generic.go +++ b/tests/helper/helper_generic.go @@ -2,6 +2,7 @@ package helper import ( "bufio" + "context" "encoding/json" "fmt" "os" @@ -15,6 +16,8 @@ import ( "github.com/tidwall/gjson" + "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/preference" "github.com/redhat-developer/odo/pkg/segment" @@ -200,11 +203,21 @@ func CommonBeforeEach(setupCluster bool) CommonVar { commonVar.Project = commonVar.CliRunner.CreateAndSetRandNamespaceProject() } commonVar.OriginalWorkingDirectory = Getwd() - os.Setenv("GLOBALODOCONFIG", filepath.Join(commonVar.ConfigDir, "preference.yaml")) + + configPath := filepath.Join(commonVar.ConfigDir, "preference.yaml") + os.Setenv("GLOBALODOCONFIG", configPath) + + // Create context with env var configuration + ctx := context.Background() + envConfig, err := config.GetConfiguration() + Expect(err).To(BeNil()) + ctx = envcontext.WithEnvConfig(ctx, *envConfig) + // Set ConsentTelemetry to false so that it does not prompt to set a preference value - cfg, _ := preference.NewClient() - err := cfg.SetConfiguration(preference.ConsentTelemetrySetting, "false") + cfg, _ := preference.NewClient(ctx) + err = cfg.SetConfiguration(preference.ConsentTelemetrySetting, "false") Expect(err).To(BeNil()) + // Use ephemeral volumes (emptyDir) in tests to make test faster err = cfg.SetConfiguration(preference.EphemeralSetting, "true") Expect(err).To(BeNil()) diff --git a/tests/helper/helper_telemetry.go b/tests/helper/helper_telemetry.go index 9d80f4eac4d..e32c7843189 100644 --- a/tests/helper/helper_telemetry.go +++ b/tests/helper/helper_telemetry.go @@ -1,6 +1,7 @@ package helper import ( + "context" "encoding/json" "io/ioutil" "os" @@ -9,6 +10,7 @@ import ( . "github.com/onsi/gomega" "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/preference" "github.com/redhat-developer/odo/pkg/segment" ) @@ -25,8 +27,14 @@ func setDebugTelemetryFile(value string) error { //it also sets up envs and cfg for the same func EnableTelemetryDebug() { Expect(os.Setenv(segment.TrackingConsentEnv, "yes")).NotTo(HaveOccurred()) - cfg, _ := preference.NewClient() - err := cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true") + + ctx := context.Background() + envConfig, err := config.GetConfiguration() + Expect(err).To(BeNil()) + ctx = envcontext.WithEnvConfig(ctx, *envConfig) + + cfg, _ := preference.NewClient(ctx) + err = cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true") Expect(err).To(BeNil()) tempFile, err := ioutil.TempFile("", "telemetry") Expect(err).NotTo(HaveOccurred()) @@ -58,10 +66,14 @@ func GetTelemetryDebugData() segment.TelemetryData { func ResetTelemetry() { Expect(os.Setenv(segment.TrackingConsentEnv, "no")).NotTo(HaveOccurred()) Expect(os.Unsetenv(DebugTelemetryFileEnv)) - cfg, _ := preference.NewClient() - err := cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true") - Expect(err).NotTo(HaveOccurred()) + + ctx := context.Background() envConfig, err := config.GetConfiguration() + Expect(err).To(BeNil()) + ctx = envcontext.WithEnvConfig(ctx, *envConfig) + + cfg, _ := preference.NewClient(ctx) + err = cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true") Expect(err).NotTo(HaveOccurred()) Expect(segment.IsTelemetryEnabled(cfg, *envConfig)).To(BeFalse()) } diff --git a/tests/integration/cmd_devfile_init_test.go b/tests/integration/cmd_devfile_init_test.go index eb932485166..a0a8f2cd86a 100644 --- a/tests/integration/cmd_devfile_init_test.go +++ b/tests/integration/cmd_devfile_init_test.go @@ -1,11 +1,14 @@ package integration import ( + "context" "fmt" "io/ioutil" "os" "path/filepath" + "github.com/redhat-developer/odo/pkg/config" + envcontext "github.com/redhat-developer/odo/pkg/config/context" "github.com/redhat-developer/odo/pkg/odo/cli/messages" "github.com/redhat-developer/odo/pkg/preference" "github.com/redhat-developer/odo/pkg/segment" @@ -469,7 +472,12 @@ var _ = Describe("odo devfile init command tests", Label(helper.LabelNoCluster), BeforeEach(func() { helper.EnableTelemetryDebug() - cfg, err := preference.NewClient() + ctx := context.Background() + envConfig, err := config.GetConfiguration() + Expect(err).To(BeNil()) + ctx = envcontext.WithEnvConfig(ctx, *envConfig) + + cfg, err := preference.NewClient(ctx) Expect(err).ShouldNot(HaveOccurred()) if tt.setupFunc != nil { tt.setupFunc(cfg)