Skip to content

Commit

Permalink
Using go matchers
Browse files Browse the repository at this point in the history
Signed-off-by: willdavsmith <[email protected]>
  • Loading branch information
willdavsmith committed Feb 23, 2024
1 parent 43012da commit 470afa3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
27 changes: 24 additions & 3 deletions pkg/cli/cmd/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/test/radcli"
"github.com/radius-project/radius/test/testcontext"
"k8s.io/apimachinery/pkg/labels"
)

func Test_CommandValidation(t *testing.T) {
Expand Down Expand Up @@ -158,8 +159,10 @@ func Test_Run(t *testing.T) {
portforwardMock := portforward.NewMockInterface(ctrl)

appPortforwardOptionsChan := make(chan portforward.Options, 1)
appLabelSelector, err := portforward.CreateLabelSelectorForApplication("test-application")
require.NoError(t, err)
portforwardMock.EXPECT().
Run(gomock.Any(), gomock.Any()).
Run(gomock.Any(), PortForwardOptionsMatcher{LabelSelector: appLabelSelector}).
DoAndReturn(func(ctx context.Context, o portforward.Options) error {
// Capture options for verification
appPortforwardOptionsChan <- o
Expand All @@ -175,8 +178,10 @@ func Test_Run(t *testing.T) {
Times(1)

dashboardPortforwardOptionsChan := make(chan portforward.Options, 1)
dashboardLabelSelector, err := portforward.CreateLabelSelectorForDashboard()
require.NoError(t, err)
portforwardMock.EXPECT().
Run(gomock.Any(), gomock.Any()).
Run(gomock.Any(), PortForwardOptionsMatcher{LabelSelector: dashboardLabelSelector}).
DoAndReturn(func(ctx context.Context, o portforward.Options) error {
// Capture options for verification
dashboardPortforwardOptionsChan <- o
Expand Down Expand Up @@ -300,7 +305,7 @@ func Test_Run(t *testing.T) {

// Shut down the log stream and verify result
cancel()
err := <-resultErrChan
err = <-resultErrChan
require.NoError(t, err)

// All of the output in this command is being done by functions that we mock for testing, so this
Expand All @@ -318,3 +323,19 @@ func Test_Run(t *testing.T) {
}
require.Equal(t, expected, outputSink.Writes)
}

type PortForwardOptionsMatcher struct {
LabelSelector labels.Selector
}

func (p PortForwardOptionsMatcher) Matches(x interface{}) bool {
if s, ok := x.(portforward.Options); ok {
return p.LabelSelector.String() == s.LabelSelector.String()
}

return false
}

func (p PortForwardOptionsMatcher) String() string {
return fmt.Sprintf("expected label selector %s", p.LabelSelector.String())
}
6 changes: 6 additions & 0 deletions pkg/cli/kubernetes/portforward/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ func CreateLabelsForDashboard() labels.Labels {
kubernetes.LabelPartOf: "radius",
}
}

func CreateLabelsForApplication(applicationName string) labels.Labels {
return labels.Set{
kubernetes.LabelRadiusApplication: applicationName,
}
}
14 changes: 14 additions & 0 deletions pkg/cli/kubernetes/portforward/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ func Test_CreateLabelsForDashboard(t *testing.T) {
require.Equal(t, "dashboard", labels.Get("app.kubernetes.io/name"))
require.Equal(t, "radius", labels.Get("app.kubernetes.io/part-of"))
}

func Test_CreateLabelsForApplication(t *testing.T) {
// Create labels for the application "test-app"
labels := CreateLabelsForApplication("test-app")
require.NotNil(t, labels)
require.True(t, labels.Has("radapp.io/application"))
require.Equal(t, "test-app", labels.Get("radapp.io/application"))

// Create labels for the application "another-test-app"
labels = CreateLabelsForApplication("another-test-app")
require.NotNil(t, labels)
require.True(t, labels.Has("radapp.io/application"))
require.Equal(t, "another-test-app", labels.Get("radapp.io/application"))
}

0 comments on commit 470afa3

Please sign in to comment.