Skip to content

Commit

Permalink
gcbmgr: add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Mar 18, 2020
1 parent 389671d commit 6f20c75
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cmd/krel/cmd/gcbmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ func setGCBSubstitutions(o *gcbmgrOptions) (map[string]string, error) {
return gcbSubs, nil
}

var buildListJobs = build.ListJobs

// listJobs lists recent GCB jobs run in the specified project
func listJobs(project string, lastJobs int64) error {
logrus.Infof("Listing last %d GCB jobs:", lastJobs)

return build.ListJobs(project, lastJobs)
return buildListJobs(project, lastJobs)
}
60 changes: 48 additions & 12 deletions cmd/krel/cmd/gcbmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"os"
"testing"
Expand All @@ -26,27 +27,54 @@ import (
"k8s.io/release/pkg/gcp/build"
)

func TestRunGcbmgrSuccess(t *testing.T) {
type fakeListJobs struct {
expectedProject string
expectedLastJobs int64
t *testing.T
err error
}

func (f *fakeListJobs) ListJobs(project string, lastJobs int64) error {
require.Equal(f.t, project, f.expectedProject)
require.Equal(f.t, lastJobs, f.expectedLastJobs)
return f.err
}

func TestRunGcbmgrList(t *testing.T) {
testcases := []struct {
name string
gcbmgrOpts gcbmgrOptions
buildOpts build.Options
expected map[string]string
name string
gcbmgrOpts gcbmgrOptions
buildOpts build.Options
listJobOpts fakeListJobs
expectedErr bool
}{
{
name: "list only",
gcbmgrOpts: gcbmgrOptions{
branch: "master",
gcpUser: "test-user",
branch: "master",
gcpUser: "test-user",
lastJobs: 5,
},
listJobOpts: fakeListJobs{
expectedProject: "kubernetes-release-test",
expectedLastJobs: int64(5),
err: nil,
},
expectedErr: false,
},
{
name: "stream the job",
name: "error on list jobs",
gcbmgrOpts: gcbmgrOptions{
branch: "master",
stream: true,
gcpUser: "test-user",
branch: "master",
gcpUser: "test-user",
lastJobs: 10,
},
listJobOpts: fakeListJobs{
expectedProject: "kubernetes-release-test",
expectedLastJobs: int64(10),
err: errors.New("Generic Error"),
},
expectedErr: true,
},
}

Expand All @@ -55,8 +83,16 @@ func TestRunGcbmgrSuccess(t *testing.T) {

gcbmgrOpts = &tc.gcbmgrOpts

f := &tc.listJobOpts
f.t = t
buildListJobs = f.ListJobs

err := runGcbmgr()
require.Nil(t, err)
if tc.expectedErr {
require.NotNil(t, err)
} else {
require.Nil(t, err)
}
}
}

Expand Down

0 comments on commit 6f20c75

Please sign in to comment.