Skip to content

Commit

Permalink
add top level buildrun cancel unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Jul 26, 2021
1 parent 794b4e2 commit 8af5ffb
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
6 changes: 1 addition & 5 deletions pkg/shp/cmd/buildrun/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -61,13 +60,10 @@ func (c *CancelCommand) Run(params *params.Params, ioStreams *genericclioptions.
if br, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Get(c.cmd.Context(), c.name, metav1.GetOptions{}); err != nil {
return fmt.Errorf("failed to retrieve BuildRun %s: %s", c.name, err.Error())
}
//TODO replace with br.IsDone() when that is available and vendored in
cond := br.Status.GetCondition(buildv1alpha1.Succeeded)
if cond != nil && cond.GetStatus() != corev1.ConditionUnknown {
if br.IsDone() {
return fmt.Errorf("failed to cancel BuildRun %s: execution has already finished", c.name)
}

//TODO use constant when vendor in api changes
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Expand Down
125 changes: 125 additions & 0 deletions pkg/shp/cmd/buildrun/cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package buildrun

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/spf13/cobra"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/client/clientset/versioned/fake"
"github.com/shipwright-io/cli/pkg/shp/params"
)

func TestCancelBuildRun(t *testing.T) {
tests := map[string]struct {
br *v1alpha1.BuildRun
expectCancelSet bool
expectErr bool
}{
"does-not-exist": {
br: nil,
expectErr: true,
},
"completed": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "completed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionTrue,
},
},
}},
expectErr: true,
},
"failed": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionFalse,
},
},
}},
expectErr: true,
},
"condition-missing": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
},
expectCancelSet: true,
},
"in-progress": {
br: &v1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: "failed",
Namespace: metav1.NamespaceDefault,
},
Status: v1alpha1.BuildRunStatus{
Conditions: v1alpha1.Conditions{
{
Type: v1alpha1.Succeeded,
Status: corev1.ConditionUnknown,
},
},
}},
expectCancelSet: true,
},
}
for testName, test := range tests {
t.Logf("running %s with args %#v", testName, test)

cmd := CancelCommand{cmd: &cobra.Command{}}
var clientset *fake.Clientset
if test.br != nil {
cmd.name = test.br.Name
clientset = fake.NewSimpleClientset(test.br)
} else {
clientset = fake.NewSimpleClientset()
}

// set up context
cmd.Cmd().ExecuteC()
param := params.NewParamsForTest(nil, clientset, nil, metav1.NamespaceDefault)

ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
err := cmd.Run(param, &ioStreams)

if err != nil && !test.expectErr {
t.Errorf("%s: did not expect err: %s", testName, err.Error())
}
if err == nil && test.expectErr {
t.Errorf("%s: did not get err when expected", testName)
}
if err != nil && test.expectErr {
continue
}

buildRun, _ := clientset.ShipwrightV1alpha1().BuildRuns(param.Namespace()).Get(context.Background(), test.br.Name, metav1.GetOptions{})

if test.expectCancelSet && !buildRun.IsCanceled() {
t.Errorf("%s: cancel not set", testName)
}

if !test.expectCancelSet && buildRun.IsCanceled() {
t.Errorf("%s: cancel set", testName)
}
}
}
12 changes: 12 additions & 0 deletions pkg/shp/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ func NewParams() *Params {

return p
}

func NewParamsForTest(clientset kubernetes.Interface,
shpClientset buildclientset.Interface,
configFlags *genericclioptions.ConfigFlags,
namespace string) *Params {
return &Params{
clientset: clientset,
shpClientset: shpClientset,
configFlags: configFlags,
namespace: namespace,
}
}

0 comments on commit 8af5ffb

Please sign in to comment.