Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
fix: Adds a message when new logs aren't shown to the user immediately (
Browse files Browse the repository at this point in the history
#131)

* Adds a message when new logs aren't shown to the user immediately
  • Loading branch information
elliot-smith authored Nov 2, 2021
1 parent 1b24bd0 commit 54349d2
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/cli/internal/pkg/aws/cwl/stream_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (c Client) StreamLogs(ctx context.Context, logGroupName string, streams ...
if aws.ToString(lastToken) != aws.ToString(output.NextToken) {
stream <- StreamEvent{Logs: parseEventLogs(output.Events, startTime)}
lastToken = output.NextToken
} else {
stream <- StreamEvent{}
}

select {
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/internal/pkg/aws/cwl/stream_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ func TestClient_StreamLogs(t *testing.T) {
_, isOpen := <-stream
assert.False(t, isOpen)
}
func TestClient_StreamLogs_EmptyLog(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
client := NewMockClient()
client.cwl.(*CwlMock).On("FilterLogEvents", ctx, mock.Anything).
Return(&cloudwatchlogs.FilterLogEventsOutput{
NextToken: aws.String("Token"),
Events: []types.FilteredLogEvent{}}, nil)
cancel()
stream := client.StreamLogs(ctx, testLogGroupName)
event := <-stream
assert.Equal(t, []string{}, event.Logs)
assert.NoError(t, event.Err)
cancel()
_, isOpen := <-stream
assert.False(t, isOpen)
}

func TestClient_StreamLogs_Error(t *testing.T) {
ctx := context.Background()
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/internal/pkg/cli/logs_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Use a question mark for OR, such as "?ERROR ?WARN". Filter out terms with a minu
tailFlagDescription = "Follow the log output."
)

var logInfo = log.Info

var printLn = func(args ...interface{}) {
_, _ = fmt.Println(args...)
}
Expand Down Expand Up @@ -124,6 +126,8 @@ func (o *logsSharedOpts) followLogGroup(logGroupName string) error {
}

func (o *logsSharedOpts) displayEventFromChannel(channel <-chan cwl.StreamEvent) error {
firstEvent := true

for event := range channel {
if event.Err != nil {
return event.Err
Expand All @@ -132,10 +136,15 @@ func (o *logsSharedOpts) displayEventFromChannel(channel <-chan cwl.StreamEvent)
for _, line := range event.Logs {
printLn(line)
}
} else if firstEvent {
logInfo().Msg("There are no new logs. Please wait for the first logs to appear...")
} else {
log.Debug().Msg("No new logs")
}

firstEvent = false
}

return nil
}

Expand Down
47 changes: 47 additions & 0 deletions packages/cli/internal/pkg/cli/logs_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"time"

"github.com/aws/amazon-genomics-cli/internal/pkg/aws/cwl"
awsmocks "github.com/aws/amazon-genomics-cli/internal/pkg/mocks/aws"
iomocks "github.com/aws/amazon-genomics-cli/internal/pkg/mocks/io"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -95,3 +98,47 @@ func Test_fanInChannels_nominal(t *testing.T) {
expected := []string{"foo1", "bar1", "foo2", "bar2", "something else", "singleton3"}
assert.ElementsMatch(t, actual, expected)
}

func Test_displayEventFromChannel_noLogs_showsWaitingMessage(t *testing.T) {
ctrl := gomock.NewController(t)
cwlMock := awsmocks.NewMockCwlClient(ctrl)
mockLogs := iomocks.NewMockLog(ctrl)
orig := logInfo
logInfo = mockLogs.Info

mockLogs.EXPECT().Info().Times(1)

opts := logsAccessOpts{
logsSharedOpts: logsSharedOpts{cwlClient: cwlMock},
logsAccessVars: logsAccessVars{logsSharedVars{contextName: testContextName1}},
}
channel := make(chan cwl.StreamEvent)
go func() {
channel <- cwl.StreamEvent{Logs: []string{}}
close(channel)
}()
_ = opts.displayEventFromChannel(channel)
logInfo = orig
}

func Test_displayEventFromChannel_oneLog_OnlyShowsMessageFromChannel(t *testing.T) {
ctrl := gomock.NewController(t)
cwlMock := awsmocks.NewMockCwlClient(ctrl)
mockFmt := iomocks.NewMockFormat(ctrl)
orig := printLn
printLn = mockFmt.LogsPrintLn

mockFmt.EXPECT().LogsPrintLn("hi").Return().Times(1)

opts := logsAccessOpts{
logsSharedOpts: logsSharedOpts{cwlClient: cwlMock},
logsAccessVars: logsAccessVars{logsSharedVars{contextName: testContextName1}},
}
channel := make(chan cwl.StreamEvent)
go func() {
channel <- cwl.StreamEvent{Logs: []string{"hi"}}
defer close(channel)
}()
_ = opts.displayEventFromChannel(channel)
printLn = orig
}
14 changes: 13 additions & 1 deletion packages/cli/internal/pkg/mocks/io/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package iomocks

import "io/fs"
import (
"io/fs"

"github.com/rs/zerolog"
)

type OS interface {
Remove(name string) error
Expand All @@ -25,3 +29,11 @@ type FileReader interface {
type FileWriter interface {
WriteFile(filename string, data []byte, perm fs.FileMode) error
}

type Format interface {
LogsPrintLn(args ...interface{})
}

type Log interface {
Info() *zerolog.Event
}
77 changes: 77 additions & 0 deletions packages/cli/internal/pkg/mocks/io/mock_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54349d2

Please sign in to comment.