From d02a43a58271415ddafbdb1e2edbd40e2fd3ab31 Mon Sep 17 00:00:00 2001 From: Igor Anic Date: Sat, 4 Dec 2021 12:48:35 +0100 Subject: [PATCH] disable sending events from tests --- cli/log/log.go | 41 ++++++++++++++++++++++++----------------- domain/workspace.go | 3 +++ test/activation_test.go | 8 +------- test/main_test.go | 7 +++++++ 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/cli/log/log.go b/cli/log/log.go index 4e4a91b5..2bdc98a2 100644 --- a/cli/log/log.go +++ b/cli/log/log.go @@ -28,38 +28,42 @@ var ( ) func newEventsCollector() *eventsCollector { + var disabled bool + if val, ok := os.LookupEnv(domain.EnvNoEvents); ok && val != "" { + disabled = true + } ec := eventsCollector{ - connectDone: make(chan struct{}), - store: newEventsStore(), + store: newEventsStore(), + disabled: disabled, } - - go func() { - defer close(ec.connectDone) - p, err := net.NewPublisher(secret.EventPublisherCreds) - if err != nil { - Error(fmt.Errorf("failed to start event publisher %w", err)) - return - } - ec.publisher = p - }() return &ec } type eventsCollector struct { - publisher *net.Publisher - connectDone chan struct{} - store *eventsStore + disabled bool + publisher *net.Publisher + store *eventsStore } func (c *eventsCollector) send() error { + if c.disabled { + return nil + } + if c.publisher == nil { + p, err := net.NewPublisher(secret.EventPublisherCreds) + if err != nil { + Error(fmt.Errorf("failed to start event publisher %w", err)) + } else { + c.publisher = p + } + } + cliCommand.End() buf, err := cliCommand.Marshal() if err != nil { c.store.push(buf) return Wrap(err) } - // wait for net connection to finish - <-c.connectDone if c.publisher == nil { c.store.push(buf) return fmt.Errorf("publisher not found") @@ -72,6 +76,9 @@ func (c *eventsCollector) send() error { } func (c *eventsCollector) close() error { + if c.disabled { + return nil + } if err := c.send(); err != nil { return c.store.store() } diff --git a/domain/workspace.go b/domain/workspace.go index 6257151c..27c3d1ab 100644 --- a/domain/workspace.go +++ b/domain/workspace.go @@ -30,6 +30,9 @@ const ( TagKey = EnvKey TagProjectName = EnvProjectName TagStageName = EnvStageName + + // set to non empty to disable sending events from cli + EnvNoEvents = "MANTIL_NO_EVENTS" ) type Workspace struct { diff --git a/test/activation_test.go b/test/activation_test.go index 59a07429..3fb89ed5 100644 --- a/test/activation_test.go +++ b/test/activation_test.go @@ -1,19 +1,13 @@ package test import ( - "io/ioutil" "testing" - "github.com/mantil-io/mantil/domain" "github.com/mantil-io/mantil/kit/clitest" - "github.com/stretchr/testify/require" ) func TestBeforeActivation(t *testing.T) { - workspacePath, err := ioutil.TempDir("/tmp", "mantil-workspace-") - require.NoError(t, err) - t.Setenv(domain.EnvWorkspacePath, workspacePath) - t.Logf("workspace path: %s", workspacePath) + createNewWorkspaceWithoutToken(t) r := clitest.New(t) r.Run("mantil", "--help").Success() diff --git a/test/main_test.go b/test/main_test.go index 4f91467c..e07c8e15 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -34,6 +34,7 @@ func setAwsEnv() { } func TestMain(m *testing.M) { + os.Setenv(domain.EnvNoEvents, "1") // disable sending cli events flag.Parse() for _, f := range []func() int{setup, m.Run, teardown} { @@ -106,6 +107,12 @@ func defaultNodeExists() bool { return false } +func createNewWorkspaceWithoutToken(t *testing.T) { + workspacePath, err := ioutil.TempDir("", "mantil-workspace-") + require.NoError(t, err) + os.Setenv(domain.EnvWorkspacePath, workspacePath) +} + func createNewWorkspace() error { workspacePath, err := ioutil.TempDir("", "mantil-workspace-") if err != nil {