Skip to content

Commit

Permalink
add more tests to pass the test threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Jun 12, 2024
1 parent ed791f1 commit 3225fe6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/command/command_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
package command

import (
"bytes"
"context"
"strings"
"testing"
"time"

"github.com/nginx/agent/v3/internal/bus"
"github.com/nginx/agent/v3/internal/command/commandfakes"
"github.com/nginx/agent/v3/internal/grpc/grpcfakes"
"github.com/nginx/agent/v3/test/protos"
"github.com/nginx/agent/v3/test/stub"
"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -76,3 +80,54 @@ func TestCommandPlugin_Process(t *testing.T) {
commandPlugin.Process(ctx, &bus.Message{Topic: bus.InstanceHealthTopic, Data: protos.GetInstanceHealths()})
require.Equal(t, 1, fakeCommandService.UpdateDataPlaneHealthCallCount())
}

func TestMonitorSubscribeChannel(t *testing.T) {
ctx, cncl := context.WithCancel(context.Background())
defer cncl()

logBuf := &bytes.Buffer{}
stub.StubLoggerWith(logBuf)

cp := NewCommandPlugin(types.GetAgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{})

message := protos.CreateManagementPlaneRequest()

// Run in a separate goroutine
go cp.monitorSubscribeChannel(ctx)

// Give some time to exit the goroutine
time.Sleep(100 * time.Millisecond)

cp.subscribeChannel <- message

// Give some time to process the message
time.Sleep(100 * time.Millisecond)

// Verify the logger was called
if s := logBuf.String(); !strings.Contains(s, "Received management plane request") {
t.Errorf("Unexpected log %s", s)
}
}

func TestMonitorSubscribeChannel_ContextCancel(t *testing.T) {
ctx, cncl := context.WithCancel(context.Background())

logBuf := &bytes.Buffer{}
stub.StubLoggerWith(logBuf)

cp := NewCommandPlugin(types.GetAgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{})

// Run in a separate goroutine
go cp.monitorSubscribeChannel(ctx)

// Give some time to process the goroutine
time.Sleep(100 * time.Millisecond)

cncl()

// Give some time to cancel the goroutine
time.Sleep(100 * time.Millisecond)

// Verify the logger was called
assert.Equal(t, "", logBuf.String())
}
6 changes: 6 additions & 0 deletions test/protos/management_plane_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ func CreateManagementPlaneRequestConfigApplyRequest() *v1.ManagementPlaneRequest
},
}
}

func CreateManagementPlaneRequest() *v1.ManagementPlaneRequest {
return &v1.ManagementPlaneRequest{
MessageMeta: CreateMessageMeta(),
}
}
26 changes: 26 additions & 0 deletions test/stub/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.

package stub

import (
"bytes"
"log/slog"
)

// StubLoggerWith follows the pattern for replacing slog
// with a handler that can take a buffer. The buffer gets filled
// by adding log statements to as the code is executed.
// You can see more information in the following video for context
// https://www.youtube.com/watch?v=i1bDIyIaxbE

func StubLoggerWith(buffer *bytes.Buffer) {
mockLoggerHandler := slog.NewTextHandler(buffer, &slog.HandlerOptions{
Level: slog.LevelDebug,
})

logger := slog.New(mockLoggerHandler)
slog.SetDefault(logger)
}

0 comments on commit 3225fe6

Please sign in to comment.