Skip to content

Commit

Permalink
Merge pull request #1293 from justaugustus/stage-checks
Browse files Browse the repository at this point in the history
[krel] gcbmgr: Add validation method for GcbmgrOptions
  • Loading branch information
k8s-ci-robot authored May 15, 2020
2 parents 8c5612e + fe27c93 commit 9292ae3
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cmd/krel/cmd/gcbmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func init() {
// RunGcbmgr is the function invoked by 'krel gcbmgr', responsible for
// submitting release jobs to GCB
func RunGcbmgr(opts *GcbmgrOptions) error {
if err := opts.Validate(); err != nil {
return errors.Wrap(err, "validating gcbmgr options")
}

toolOrg := release.GetToolOrg()
toolRepo := release.GetToolRepo()
toolBranch := release.GetToolBranch()
Expand All @@ -189,10 +193,6 @@ func RunGcbmgr(opts *GcbmgrOptions) error {
logrus.Infof("Running gcbmgr with the following options: %+v", opts)
logrus.Infof("Build options: %v", *buildOpts)

if opts.Stage && opts.Release {
return errors.New("cannot specify both the 'stage' and 'release' flag; resubmit with only one build type selected")
}

buildOpts.NoSource = true
buildOpts.DiskSize = release.DefaultDiskSize

Expand Down Expand Up @@ -362,3 +362,21 @@ func listJobs(project string, lastJobs int64) error {
logrus.Infof("Listing last %d GCB jobs:", lastJobs)
return BuildListJobs(project, lastJobs)
}

func (o *GcbmgrOptions) Validate() error {
if o.Stage && o.Release {
return errors.New("cannot specify both the 'stage' and 'release' flag; resubmit with only one build type selected")
}

if o.Branch == git.Master {
if o.ReleaseType == ReleaseTypeRC || o.ReleaseType == ReleaseTypeOfficial {
return errors.New("cannot cut a release candidate or an official release from master")
}
} else {
if o.ReleaseType == ReleaseTypeAlpha || o.ReleaseType == ReleaseTypeBeta {
return errors.New("cannot cut an alpha or beta release from a release branch")
}
}

return nil
}
100 changes: 100 additions & 0 deletions cmd/krel/cmd/gcbmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/release/cmd/krel/cmd"
"k8s.io/release/cmd/krel/cmd/cmdfakes"
"k8s.io/release/pkg/gcp/build"
"k8s.io/release/pkg/git"
)

func mockRepo() cmd.Repository {
Expand Down Expand Up @@ -313,6 +314,105 @@ func TestSetGCBSubstitutionsFailure(t *testing.T) {
}
}

func TestValidateSuccess(t *testing.T) {
testcases := []struct {
name string
gcbmgrOpts *cmd.GcbmgrOptions
}{
{
name: "master alpha - stage",
gcbmgrOpts: &cmd.GcbmgrOptions{
Stage: true,
Branch: "master",
ReleaseType: cmd.ReleaseTypeAlpha,
},
},
{
name: "master beta - release",
gcbmgrOpts: &cmd.GcbmgrOptions{
Release: true,
Branch: "master",
ReleaseType: cmd.ReleaseTypeBeta,
BuildVersion: "v1.33.7",
},
},
{
name: "release-1.15 RC",
gcbmgrOpts: &cmd.GcbmgrOptions{
Stage: true,
Branch: "release-1.15",
ReleaseType: cmd.ReleaseTypeRC,
},
},
{
name: "release-1.15 official",
gcbmgrOpts: &cmd.GcbmgrOptions{
Stage: true,
Branch: "release-1.15",
ReleaseType: cmd.ReleaseTypeOfficial,
},
},
{
name: "release-1.16 official with custom tool org, repo, and branch",
gcbmgrOpts: &cmd.GcbmgrOptions{
Stage: true,
Branch: "release-1.16",
ReleaseType: cmd.ReleaseTypeOfficial,
},
},
}

for _, tc := range testcases {
t.Logf("Test case: %s", tc.name)

err := tc.gcbmgrOpts.Validate()
require.Nil(t, err)
}
}

func TestValidateFailure(t *testing.T) {
testcases := []struct {
name string
gcbmgrOpts *cmd.GcbmgrOptions
}{
{
name: "RC on master",
gcbmgrOpts: &cmd.GcbmgrOptions{
Branch: git.Master,
ReleaseType: cmd.ReleaseTypeRC,
},
},
{
name: "official release on master",
gcbmgrOpts: &cmd.GcbmgrOptions{
Branch: git.Master,
ReleaseType: cmd.ReleaseTypeOfficial,
},
},
{
name: "alpha on release branch",
gcbmgrOpts: &cmd.GcbmgrOptions{
Branch: "release-1.16",
ReleaseType: cmd.ReleaseTypeAlpha,
},
},
{
name: "beta on release branch",
gcbmgrOpts: &cmd.GcbmgrOptions{
Branch: "release-1.19",
ReleaseType: cmd.ReleaseTypeBeta,
},
},
}

for _, tc := range testcases {
t.Logf("Test case: %s", tc.name)

err := tc.gcbmgrOpts.Validate()
require.Error(t, err)
}
}

func dropDynamicSubstitutions(orig map[string]string) (result map[string]string) {
result = orig

Expand Down

0 comments on commit 9292ae3

Please sign in to comment.