Skip to content

Commit

Permalink
Allow kubetest to extract bazel builds.
Browse files Browse the repository at this point in the history
This will allow "kubetest --extract bazel/..." to operate as a
pass-through to get-kube.sh with the same version.

x-ref: kubernetes/kubernetes#49884
  • Loading branch information
pipejakob committed Jul 31, 2017
1 parent a5ddb16 commit ad24a52
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
6 changes: 5 additions & 1 deletion kubetest/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
version // v1.5.0, v1.5.0-beta.2
gcs // gs://bucket/prefix/v1.6.0-alpha.0
load // Load a --save cluster
bazel // A pre/postsubmit bazel build version, prefixed with bazel/
)

type extractStrategy struct {
Expand Down Expand Up @@ -76,6 +77,7 @@ func (l *extractStrategies) Set(value string) error {
`^release/(stable.*)$`: stable,
`^(v\d+\.\d+\.\d+[\w.-]*)$`: version,
`^(gs://.*)$`: gcs,
`^(bazel/.*)$`: bazel,
}

if len(*l) == 2 {
Expand Down Expand Up @@ -206,7 +208,7 @@ var (
// Calls KUBERNETES_RELASE_URL=url KUBERNETES_RELEASE=version get-kube.sh.
// This will download version from the specified url subdir and extract
// the tarballs.
func getKube(url, version string) error {
var getKube = func(url, version string) error {
k, err := ensureKube()
if err != nil {
return err
Expand Down Expand Up @@ -394,6 +396,8 @@ func (e extractStrategy) Extract(project, zone string) error {
return getKube(path.Dir(e.option), path.Base(e.option))
case load:
return loadState(e.option)
case bazel:
return getKube("", e.option)
}
return fmt.Errorf("Unrecognized extraction: %v(%v)", e.mode, e.value)
}
Expand Down
55 changes: 55 additions & 0 deletions kubetest/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,58 @@ func TestGetKube(t *testing.T) {
}
}
}

func TestExtractStrategies(t *testing.T) {
cases := []struct {
option string
expectUrl string
expectVersion string
}{
{
"bazel/v1.8.0-alpha.2.899+2c624e590f5670",
"",
"bazel/v1.8.0-alpha.2.899+2c624e590f5670",
},
{
"bazel/49747/master:b341939d6d3666b119028400a4311cc66da9a542,49747:c4656c3d029e47d03b3d7d9915d79cab72a80852",
"",
"bazel/49747/master:b341939d6d3666b119028400a4311cc66da9a542,49747:c4656c3d029e47d03b3d7d9915d79cab72a80852",
},
}

var gotUrl string
var gotVersion string

// getKube is tested independently, so mock it out here so we can test
// that different extraction strategies call getKube with the correct
// arguments.
oldGetKube := getKube
defer func() { getKube = oldGetKube }()
getKube = func(url, version string) error {
gotUrl = url
gotVersion = version
// This is needed or else Extract() will think that getKube failed.
os.Mkdir("kubernetes", 0775)
return nil
}

for _, tc := range cases {
if d, err := ioutil.TempDir("", "extract"); err != nil {
t.Fatal(err)
} else if err := os.Chdir(d); err != nil {
t.Fatal(err)
}

var es extractStrategies
if err := es.Set(tc.option); err != nil {
t.Errorf("extractStrategy.Set(%q) returned err: %q", tc.option, err)
}
if err := es.Extract("", ""); err != nil {
t.Errorf("extractStrategy(%q).Extract() returned err: %q", tc.option, err)
}

if gotUrl != tc.expectUrl || gotVersion != tc.expectVersion {
t.Errorf("extractStrategy(%q).Extract() wanted getKube(%q, %q), got getKube(%q, %q)", tc.option, tc.expectUrl, tc.expectVersion, gotUrl, gotVersion)
}
}
}

0 comments on commit ad24a52

Please sign in to comment.