From e96ba60f73fa6a99653d5d768b90253607b45d89 Mon Sep 17 00:00:00 2001 From: mitchell Date: Mon, 16 Oct 2023 16:46:55 -0400 Subject: [PATCH 1/2] Fixed sessionToken installer analytics. --- cmd/state-installer/cmd.go | 13 +++++++++++++ cmd/state-installer/installer.go | 18 +++--------------- internal/constants/constants.go | 3 +++ test/integration/install_scripts_int_test.go | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/cmd/state-installer/cmd.go b/cmd/state-installer/cmd.go index 462444eff5..8b3c77bf53 100644 --- a/cmd/state-installer/cmd.go +++ b/cmd/state-installer/cmd.go @@ -123,6 +123,19 @@ func main() { logging.Debug("Original Args: %v", os.Args) logging.Debug("Processed Args: %v", processedArgs) + // Store sessionToken to config + for _, envVar := range []string{constants.OverrideSessionTokenEnvVarName, constants.SessionTokenEnvVarName} { + sessionToken, ok := os.LookupEnv(envVar) + if !ok { + continue + } + err := cfg.Set(anaConst.CfgSessionToken, sessionToken) + if err != nil { + logging.Error("Unable to set session token: " + errs.JoinMessage(err)) + } + break + } + an = sync.New(anaConst.SrcStateInstaller, cfg, nil, out) an.Event(anaConst.CatInstallerFunnel, "start") diff --git a/cmd/state-installer/installer.go b/cmd/state-installer/installer.go index eccf65709b..480b64f7a8 100644 --- a/cmd/state-installer/installer.go +++ b/cmd/state-installer/installer.go @@ -9,7 +9,6 @@ import ( svcApp "github.com/ActiveState/cli/cmd/state-svc/app" svcAutostart "github.com/ActiveState/cli/cmd/state-svc/autostart" - anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" @@ -30,10 +29,9 @@ import ( ) type Installer struct { - out output.Outputer - cfg *config.Instance - payloadPath string - sessionToken string + out output.Outputer + cfg *config.Instance + payloadPath string *Params } @@ -49,13 +47,6 @@ func NewInstaller(cfg *config.Instance, out output.Outputer, payloadPath string, } func (i *Installer) Install() (rerr error) { - // Store sessionToken to config - if i.sessionToken != "" && i.cfg.GetString(anaConst.CfgSessionToken) == "" { - if err := i.cfg.Set(anaConst.CfgSessionToken, i.sessionToken); err != nil { - return errs.Wrap(err, "Failed to set session token") - } - } - // Store update tag if i.updateTag != "" { if err := i.cfg.Set(updater.CfgUpdateTag, i.updateTag); err != nil { @@ -145,9 +136,6 @@ func (i *Installer) InstallPath() string { // sanitizeInput cleans up the input and inserts fallback values func (i *Installer) sanitizeInput() error { - if sessionToken, ok := os.LookupEnv(constants.SessionTokenEnvVarName); ok { - i.sessionToken = sessionToken - } if tag, ok := os.LookupEnv(constants.UpdateTagEnvVarName); ok { i.updateTag = tag } diff --git a/internal/constants/constants.go b/internal/constants/constants.go index eafa65e54e..4882229ff2 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -107,6 +107,9 @@ const ProfileEnvVarName = "ACTIVESTATE_PROFILE" // SessionTokenEnvVarName records the session token const SessionTokenEnvVarName = "ACTIVESTATE_SESSION_TOKEN" +// OverrideSessionTokenEnvVarName overrides SessionTokenEnvVarName for integration tests. +const OverrideSessionTokenEnvVarName = "ACTIVESTATE_OVERRIDE_SESSION_TOKEN" + // UpdateTagEnvVarName const UpdateTagEnvVarName = "ACTIVESTATE_UPDATE_TAG" diff --git a/test/integration/install_scripts_int_test.go b/test/integration/install_scripts_int_test.go index 624bbbbcba..083cbe3223 100644 --- a/test/integration/install_scripts_int_test.go +++ b/test/integration/install_scripts_int_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/thoas/go-funk" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/condition" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/environment" @@ -97,12 +98,14 @@ func (suite *InstallScriptsIntegrationTestSuite) TestInstall() { "bash", e2e.OptArgs(argsWithActive...), e2e.OptAppendEnv("ACTIVESTATE_CLI_DISABLE_RUNTIME=false"), e2e.OptAppendEnv(fmt.Sprintf("%s=%s", constants.AppInstallDirOverrideEnvVarName, appInstallDir)), + e2e.OptAppendEnv(fmt.Sprintf("%s=FOO", constants.OverrideSessionTokenEnvVarName)), ) } else { cp = ts.SpawnCmdWithOpts("powershell.exe", e2e.OptArgs(argsWithActive...), e2e.OptAppendEnv("SHELL="), e2e.OptAppendEnv("ACTIVESTATE_CLI_DISABLE_RUNTIME=false"), e2e.OptAppendEnv(fmt.Sprintf("%s=%s", constants.AppInstallDirOverrideEnvVarName, appInstallDir)), + e2e.OptAppendEnv(fmt.Sprintf("%s=FOO", constants.OverrideSessionTokenEnvVarName)), ) } @@ -132,6 +135,7 @@ func (suite *InstallScriptsIntegrationTestSuite) TestInstall() { suite.assertBinDirContents(filepath.Join(installDir, "bin")) suite.assertCorrectVersion(ts, installDir, tt.Version, tt.Channel) + suite.assertAnalytics(ts) suite.DirExists(ts.Dirs.Config) // Verify that can install overtop @@ -260,6 +264,21 @@ func (suite *InstallScriptsIntegrationTestSuite) assertCorrectVersion(ts *e2e.Se } } +func (suite *InstallScriptsIntegrationTestSuite) assertAnalytics(ts *e2e.Session) { + // Verify analytics reported the correct sessionToken. + sessionTokenFound := false + events := parseAnalyticsEvents(suite, ts) + suite.Require().NotEmpty(events) + for _, event := range events { + if event.Category == anaConst.CatInstallerFunnel && event.Dimensions != nil { + suite.Assert().NotEmpty(*event.Dimensions.SessionToken) + sessionTokenFound = true + break + } + } + suite.Assert().True(sessionTokenFound, "sessionToken was not found in analytics") +} + func TestInstallScriptsIntegrationTestSuite(t *testing.T) { suite.Run(t, new(InstallScriptsIntegrationTestSuite)) } From 870c7129b370bc1dbae5ea5c4f7cc4235a4e1757 Mon Sep 17 00:00:00 2001 From: mitchell Date: Tue, 17 Oct 2023 13:44:11 -0400 Subject: [PATCH 2/2] Address PR feedback. --- cmd/state-installer/cmd.go | 2 +- test/integration/install_scripts_int_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/state-installer/cmd.go b/cmd/state-installer/cmd.go index 8b3c77bf53..5dfc5bfc6f 100644 --- a/cmd/state-installer/cmd.go +++ b/cmd/state-installer/cmd.go @@ -131,7 +131,7 @@ func main() { } err := cfg.Set(anaConst.CfgSessionToken, sessionToken) if err != nil { - logging.Error("Unable to set session token: " + errs.JoinMessage(err)) + multilog.Error("Unable to set session token: " + errs.JoinMessage(err)) } break } diff --git a/test/integration/install_scripts_int_test.go b/test/integration/install_scripts_int_test.go index 083cbe3223..1447227e00 100644 --- a/test/integration/install_scripts_int_test.go +++ b/test/integration/install_scripts_int_test.go @@ -265,7 +265,7 @@ func (suite *InstallScriptsIntegrationTestSuite) assertCorrectVersion(ts *e2e.Se } func (suite *InstallScriptsIntegrationTestSuite) assertAnalytics(ts *e2e.Session) { - // Verify analytics reported the correct sessionToken. + // Verify analytics reported a non-empty sessionToken. sessionTokenFound := false events := parseAnalyticsEvents(suite, ts) suite.Require().NotEmpty(events)