Skip to content

Commit

Permalink
disable sending events from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Dec 4, 2021
1 parent 130cf2a commit d02a43a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
41 changes: 24 additions & 17 deletions cli/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
}
Expand Down
3 changes: 3 additions & 0 deletions domain/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions test/activation_test.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
7 changes: 7 additions & 0 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d02a43a

Please sign in to comment.