Skip to content

Commit

Permalink
add --follow no pod yet unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Oct 9, 2021
1 parent 65ae1c4 commit d7eebe5
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions pkg/shp/cmd/build/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package build

import (
"bytes"
"github.com/shipwright-io/cli/pkg/shp/reactor"
"strings"
"sync"
"testing"
Expand All @@ -10,7 +11,6 @@ import (
shpfake "github.com/shipwright-io/build/pkg/client/clientset/versioned/fake"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/reactor"
"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
Expand All @@ -27,6 +27,7 @@ func TestStartBuildRunFollowLog(t *testing.T) {
phase corev1.PodPhase
logText string
to string
noPodYet bool
cancelled bool
brDeleted bool
podDeleted bool
Expand Down Expand Up @@ -82,6 +83,11 @@ func TestStartBuildRunFollowLog(t *testing.T) {
to: "1s",
logText: reactor.RequestTimeoutMessage,
},
{
name: "no pod yet",
noPodYet: true,
logText: "has not observed any pod events yet",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -109,6 +115,7 @@ func TestStartBuildRunFollowLog(t *testing.T) {
},
}
shpclientset := shpfake.NewSimpleClientset()

// need this reactor since the Run method uses the ObjectMeta.GenerateName k8s feature to generate the random
// name for the BuildRun. However, for our purposes with unit testing, we want to control the name of the BuildRun
// to facilitate the list/selector via labels that is also employed by the Run method.
Expand All @@ -122,7 +129,10 @@ func TestStartBuildRunFollowLog(t *testing.T) {
return true, br, nil
}
shpclientset.PrependReactor("get", "buildruns", getReactorFunc)
kclientset := fake.NewSimpleClientset(pod)
kclientset := fake.NewSimpleClientset()
if !test.noPodYet {
kclientset = fake.NewSimpleClientset(pod)
}
ccmd := &cobra.Command{}
cmd := &RunCommand{
cmd: ccmd,
Expand Down Expand Up @@ -159,6 +169,7 @@ func TestStartBuildRunFollowLog(t *testing.T) {
checkLog(test.name, test.logText, cmd, out, t)
continue
}

go func() {
err := cmd.Run(param, &ioStreams)
if err != nil {
Expand All @@ -167,9 +178,13 @@ func TestStartBuildRunFollowLog(t *testing.T) {

}()

// mimic watch events, bypassing k8s fake client watch hoopla whose plug points are not always useful;
pod.Status.Phase = test.phase
cmd.onEvent(pod)
if !test.noPodYet {
// mimic watch events, bypassing k8s fake client watch hoopla whose plug points are not always useful;
pod.Status.Phase = test.phase
cmd.onEvent(pod)
} else {
cmd.onNoPodEventsYet()
}
checkLog(test.name, test.logText, cmd, out, t)
}
}
Expand Down

0 comments on commit d7eebe5

Please sign in to comment.