2014-10-23 00:49:40 +00:00
/ *
Copyright 2014 Google Inc . All rights reserved .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
// e2e.go runs the e2e test suite. No non-standard package dependencies; call with "go run".
package main
import (
"bytes"
"flag"
"fmt"
2014-12-16 00:03:11 +00:00
"io"
2014-10-23 00:49:40 +00:00
"log"
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
"math/rand"
2014-12-16 00:03:11 +00:00
"net/http"
2014-10-23 00:49:40 +00:00
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
"sort"
2014-10-23 00:49:40 +00:00
"strings"
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
"time"
2014-10-23 00:49:40 +00:00
)
var (
2014-11-11 01:46:11 +00:00
isup = flag . Bool ( "isup" , false , "Check to see if the e2e cluster is up, then exit." )
build = flag . Bool ( "build" , false , "If true, build a new release. Otherwise, use whatever is there." )
2014-12-16 00:03:11 +00:00
version = flag . String ( "version" , "" , "The version to be tested (including the leading 'v'). An empty string defaults to the local build, but it can be set to any release (e.g. v0.4.4, v0.6.0)." )
2014-11-11 01:46:11 +00:00
up = flag . Bool ( "up" , false , "If true, start the the e2e cluster. If cluster is already up, recreate it." )
push = flag . Bool ( "push" , false , "If true, push to e2e cluster. Has no effect if -up is true." )
2014-12-08 22:55:47 +00:00
pushup = flag . Bool ( "pushup" , false , "If true, push to e2e cluster if it's up, otherwise start the e2e cluster." )
2014-11-11 01:46:11 +00:00
down = flag . Bool ( "down" , false , "If true, tear down the cluster before exiting." )
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
orderseed = flag . Int64 ( "orderseed" , 0 , "If non-zero, seed of random test shuffle order. (Otherwise random.)" )
2014-11-11 01:46:11 +00:00
test = flag . Bool ( "test" , false , "Run all tests in hack/e2e-suite." )
tests = flag . String ( "tests" , "" , "Run only tests in hack/e2e-suite matching this glob. Ignored if -test is set." )
2014-12-08 18:13:37 +00:00
times = flag . Int ( "times" , 1 , "Number of times each test is eligible to be run. Individual order is determined by shuffling --times instances of each test using --orderseed (like a multi-deck shoe of cards)." )
2014-11-11 01:46:11 +00:00
root = flag . String ( "root" , absOrDie ( filepath . Clean ( filepath . Join ( path . Base ( os . Args [ 0 ] ) , ".." ) ) ) , "Root directory of kubernetes repository." )
2014-12-11 19:00:29 +00:00
tap = flag . Bool ( "tap" , false , "Enable Test Anything Protocol (TAP) output (disables --verbose, only failure output recorded)" )
2014-11-11 01:46:11 +00:00
verbose = flag . Bool ( "v" , false , "If true, print all command output." )
2014-11-25 18:28:43 +00:00
trace_bash = flag . Bool ( "trace-bash" , false , "If true, pass -x to bash to trace all bash commands" )
2014-11-11 01:46:11 +00:00
checkVersionSkew = flag . Bool ( "check_version_skew" , true , "" +
"By default, verify that client and server have exact version match. " +
"You can explicitly set to false if you're, e.g., testing client changes " +
"for which the server version doesn't make a difference." )
2014-11-08 00:14:29 +00:00
cfgCmd = flag . String ( "cfg" , "" , "If nonempty, pass this as an argument, and call kubecfg. Implies -v." )
ctlCmd = flag . String ( "ctl" , "" , "If nonempty, pass this as an argument, and call kubectl. Implies -v. (-test, -cfg, -ctl are mutually exclusive)" )
2014-10-23 00:49:40 +00:00
)
2014-12-16 00:03:11 +00:00
const (
serverTarName = "kubernetes-server-linux-amd64.tar.gz"
saltTarName = "kubernetes-salt.tar.gz"
downloadDirName = "_output/downloads"
tarDirName = "server"
tempDirName = "upgrade-e2e-temp-dir"
)
var (
signals = make ( chan os . Signal , 100 )
// Root directory of the specified cluster version, rather than of where
// this script is being run from.
versionRoot = * root
)
2014-10-23 00:49:40 +00:00
func absOrDie ( path string ) string {
out , err := filepath . Abs ( path )
if err != nil {
panic ( err )
}
return out
}
2014-12-08 18:13:37 +00:00
type TestResult struct {
Pass int
Fail int
}
type ResultsByTest map [ string ] TestResult
2014-10-23 00:49:40 +00:00
func main ( ) {
flag . Parse ( )
signal . Notify ( signals , os . Interrupt )
2014-12-11 19:00:29 +00:00
if * tap {
fmt . Printf ( "TAP version 13\n" )
log . SetPrefix ( "# " )
// TODO: this limitation is fixable by moving runBash to
// outputing to temp files, which still lets people check on
// stuck things interactively. The current stdout/stderr
// approach isn't really going to work with TAP, though.
* verbose = false
}
2014-10-23 21:47:30 +00:00
if * test {
* tests = "*"
}
if * isup {
status := 1
2014-12-07 23:29:30 +00:00
if IsUp ( ) {
2014-10-23 21:47:30 +00:00
status = 0
log . Printf ( "Cluster is UP" )
} else {
log . Printf ( "Cluster is DOWN" )
}
os . Exit ( status )
}
2014-10-23 00:49:40 +00:00
if * build {
2014-10-28 19:37:03 +00:00
if ! runBash ( "build-release" , ` test-build-release ` ) {
2014-10-23 00:49:40 +00:00
log . Fatal ( "Error building. Aborting." )
}
}
2014-12-16 00:03:11 +00:00
if * version != "" {
// If the desired version isn't available already, do whatever's needed
// to make it available. Once done, update the root directory for client
// tools to be the root of the release directory so that the given
// release's tools will be used. We can't use this new root for
// everything because it likely doesn't have the hack/ directory in it.
if newVersionRoot , err := PrepareVersion ( * version ) ; err != nil {
log . Fatalf ( "Error preparing a binary of version %s: %s. Aborting." , * version , err )
} else {
versionRoot = newVersionRoot
}
}
2014-12-08 22:55:47 +00:00
if * pushup {
if IsUp ( ) {
log . Printf ( "e2e cluster is up, pushing." )
* up = false
* push = true
} else {
log . Printf ( "e2e cluster is down, creating." )
* up = true
* push = false
}
}
2014-10-23 00:49:40 +00:00
if * up {
if ! Up ( ) {
log . Fatal ( "Error starting e2e cluster. Aborting." )
}
} else if * push {
2014-12-16 00:03:11 +00:00
if ! runBash ( "push" , path . Join ( versionRoot , "/cluster/kube-push.sh" ) ) {
2014-10-23 00:49:40 +00:00
log . Fatal ( "Error pushing e2e cluster. Aborting." )
}
}
2014-11-08 00:14:29 +00:00
failure := false
switch {
case * cfgCmd != "" :
failure = ! runBash ( "'kubecfg " + * cfgCmd + "'" , "$KUBECFG " + * cfgCmd )
case * ctlCmd != "" :
2014-11-11 01:46:11 +00:00
failure = ! runBash ( "'kubectl " + * ctlCmd + "'" , "$KUBECTL " + * ctlCmd )
2014-11-08 00:14:29 +00:00
case * tests != "" :
2014-12-08 18:13:37 +00:00
failure = PrintResults ( Test ( ) )
2014-10-23 00:49:40 +00:00
}
if * down {
TearDown ( )
}
2014-11-08 00:14:29 +00:00
if failure {
2014-10-23 00:49:40 +00:00
os . Exit ( 1 )
}
}
func TearDown ( ) {
runBash ( "teardown" , "test-teardown" )
}
func Up ( ) bool {
if ! tryUp ( ) {
log . Printf ( "kube-up failed; will tear down and retry. (Possibly your cluster was in some partially created state?)" )
TearDown ( )
return tryUp ( )
}
return true
}
2014-12-07 23:29:30 +00:00
// Is the e2e cluster up?
func IsUp ( ) bool {
2014-12-15 19:15:24 +00:00
return runBash ( "get status" , ` $KUBECTL version ` )
2014-12-07 23:29:30 +00:00
}
2014-10-23 00:49:40 +00:00
func tryUp ( ) bool {
2014-12-16 00:03:11 +00:00
return runBash ( "up" , path . Join ( versionRoot , "/cluster/kube-up.sh; test-setup;" ) )
}
// PrepareVersion makes sure that the specified release version is locally
// available and ready to be used by kube-up or kube-push. Returns the director
// path of the release.
func PrepareVersion ( version string ) ( string , error ) {
if version == "" {
// Assume that the build flag already handled building a local binary.
return * root , nil
}
// If the version isn't a local build, try fetching the release from Google
// Cloud Storage.
downloadDir := filepath . Join ( * root , downloadDirName )
if err := os . MkdirAll ( downloadDir , 0755 ) ; err != nil {
return "" , err
}
localReleaseDir := filepath . Join ( downloadDir , version )
if err := os . MkdirAll ( localReleaseDir , 0755 ) ; err != nil {
return "" , err
}
remoteReleaseTar := fmt . Sprintf ( "https://storage.googleapis.com/kubernetes-release/release/%s/kubernetes.tar.gz" , version )
localReleaseTar := filepath . Join ( downloadDir , fmt . Sprintf ( "kubernetes-%s.tar.gz" , version ) )
if _ , err := os . Stat ( localReleaseTar ) ; os . IsNotExist ( err ) {
out , err := os . Create ( localReleaseTar )
if err != nil {
return "" , err
}
resp , err := http . Get ( remoteReleaseTar )
if err != nil {
out . Close ( )
return "" , err
}
defer resp . Body . Close ( )
io . Copy ( out , resp . Body )
if err != nil {
out . Close ( )
return "" , err
}
out . Close ( )
}
if ! runRawBash ( "untarRelease" , fmt . Sprintf ( "tar -C %s -zxf %s --strip-components=1" , localReleaseDir , localReleaseTar ) ) {
log . Fatal ( "Failed to untar release. Aborting." )
}
// Now that we have the binaries saved locally, use the path to the untarred
// directory as the "root" path for future operations.
return localReleaseDir , nil
2014-10-23 00:49:40 +00:00
}
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
// Fisher-Yates shuffle using the given RNG r
func shuffleStrings ( strings [ ] string , r * rand . Rand ) {
for i := len ( strings ) - 1 ; i > 0 ; i -- {
j := r . Intn ( i + 1 )
strings [ i ] , strings [ j ] = strings [ j ] , strings [ i ]
}
}
2014-12-08 18:13:37 +00:00
func Test ( ) ( results ResultsByTest ) {
2014-11-14 01:03:06 +00:00
defer runBashUntil ( "watchEvents" , "$KUBECTL --watch-only get events" ) ( )
2014-12-07 23:29:30 +00:00
if ! IsUp ( ) {
log . Fatal ( "Testing requested, but e2e cluster not up!" )
}
2014-10-23 00:49:40 +00:00
// run tests!
dir , err := os . Open ( filepath . Join ( * root , "hack" , "e2e-suite" ) )
if err != nil {
log . Fatal ( "Couldn't open e2e-suite dir" )
}
defer dir . Close ( )
names , err := dir . Readdirnames ( 0 )
if err != nil {
log . Fatal ( "Couldn't read names in e2e-suite dir" )
}
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
toRun := make ( [ ] string , 0 , len ( names ) )
2014-10-23 00:49:40 +00:00
for i := range names {
name := names [ i ]
if name == "." || name == ".." {
continue
}
if match , err := path . Match ( * tests , name ) ; ! match && err == nil {
continue
}
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
if err != nil {
log . Fatal ( "Bad test pattern: %v" , tests )
}
toRun = append ( toRun , name )
}
if * orderseed == 0 {
// Use low order bits of NanoTime as the default seed. (Using
// all the bits makes for a long, very similar looking seed
// between runs.)
* orderseed = time . Now ( ) . UnixNano ( ) & ( 1 << 32 - 1 )
}
sort . Strings ( toRun )
2014-12-08 18:13:37 +00:00
if * times != 1 {
if * times <= 0 {
log . Fatal ( "Invalid --times (negative or no testing requested)!" )
}
newToRun := make ( [ ] string , 0 , * times * len ( toRun ) )
for i := 0 ; i < * times ; i ++ {
newToRun = append ( newToRun , toRun ... )
}
toRun = newToRun
}
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
shuffleStrings ( toRun , rand . New ( rand . NewSource ( * orderseed ) ) )
log . Printf ( "Running tests matching %v shuffled with seed %#x: %v" , * tests , * orderseed , toRun )
2014-12-08 18:13:37 +00:00
results = ResultsByTest { }
2014-12-11 19:00:29 +00:00
if * tap {
fmt . Printf ( "1..%v\n" , len ( toRun ) )
}
2014-12-08 18:13:37 +00:00
for i , name := range toRun {
2014-10-23 00:49:40 +00:00
absName := filepath . Join ( * root , "hack" , "e2e-suite" , name )
2014-12-08 18:13:37 +00:00
log . Printf ( "Starting test [%v/%v]: %v" , i + 1 , len ( toRun ) , name )
2014-12-17 00:46:50 +00:00
start := time . Now ( )
2014-12-08 18:13:37 +00:00
testResult := results [ name ]
2014-12-11 19:00:29 +00:00
res , stdout , stderr := runBashWithOutputs ( name , absName )
2014-12-17 18:04:09 +00:00
// The duration_ms output is an undocumented Jenkins TAP
// plugin feature for test duration. One might think _ms means
// milliseconds, but Jenkins interprets this field in seconds.
duration_secs := time . Now ( ) . Sub ( start ) . Seconds ( )
2014-12-11 19:00:29 +00:00
if res {
fmt . Printf ( "ok %v - %v\n" , i + 1 , name )
2014-12-17 00:46:50 +00:00
if * tap {
2014-12-17 18:04:09 +00:00
fmt . Printf ( " ---\n duration_ms: %.3f\n ...\n" , duration_secs )
2014-12-17 00:46:50 +00:00
}
2014-12-08 18:13:37 +00:00
testResult . Pass ++
2014-10-23 00:49:40 +00:00
} else {
2014-12-11 19:00:29 +00:00
fmt . Printf ( "not ok %v - %v\n" , i + 1 , name )
2014-12-17 00:46:50 +00:00
if * tap {
2014-12-17 18:04:09 +00:00
fmt . Printf ( " ---\n duration_ms: %.3f\n" , duration_secs )
2014-12-17 00:46:50 +00:00
}
2014-12-11 19:00:29 +00:00
printBashOutputs ( " " , " " , stdout , stderr )
2014-12-17 00:46:50 +00:00
if * tap {
fmt . Printf ( " ...\n" )
}
2014-12-08 18:13:37 +00:00
testResult . Fail ++
2014-10-23 00:49:40 +00:00
}
2014-12-08 18:13:37 +00:00
results [ name ] = testResult
2014-10-23 00:49:40 +00:00
}
2014-12-08 18:13:37 +00:00
return
}
func PrintResults ( results ResultsByTest ) bool {
failures := 0
passed := [ ] string { }
flaky := [ ] string { }
failed := [ ] string { }
for test , result := range results {
if result . Pass > 0 && result . Fail == 0 {
passed = append ( passed , test )
} else if result . Pass > 0 && result . Fail > 0 {
flaky = append ( flaky , test )
failures += result . Fail
} else {
failed = append ( failed , test )
failures += result . Fail
}
}
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
sort . Strings ( passed )
2014-12-08 18:13:37 +00:00
sort . Strings ( flaky )
Run e2e tests in deterministic random order
Currently, we run the e2e tests in whatever order readdir happens to
return, which is random on some filesystems, name sorted on others,
create order on others, etc. Eventually, we may want to be
automatically hermetic between e2e tests (especially as we introduce
more resource destructive tests), but until then, it would be useful
if we permute the test order randomly between runs to ensure that
developers don't accidentally rely on a particular order. This
introduces a form of forced hermeticism, since improper state cleanup
from one test may not perturb a given test, but there's probably *a*
test in the suite that the order will perturb, so the RNG will find
that order eventually.
Adds logging of the generated seed, and an --orderseed argument that
can be used to re-run in the same order. Also sorts the pass/fail list
now for easier human reading.
2014-12-07 17:19:17 +00:00
sort . Strings ( failed )
2014-12-08 18:13:37 +00:00
printSubreport ( "Passed" , passed , results )
printSubreport ( "Flaky" , flaky , results )
printSubreport ( "Failed" , failed , results )
if failures > 0 {
log . Printf ( "%v test(s) failed." , failures )
} else {
log . Printf ( "Success!" )
}
return failures > 0
}
func printSubreport ( title string , tests [ ] string , results ResultsByTest ) {
report := title + " tests:"
for _ , test := range tests {
result := results [ test ]
report += fmt . Sprintf ( " %v[%v/%v]" , test , result . Pass , result . Pass + result . Fail )
}
log . Printf ( report )
2014-10-23 00:49:40 +00:00
}
// All nonsense below is temporary until we have go versions of these things.
2014-12-16 00:03:11 +00:00
// Runs the provided bash without wrapping it in any kubernetes-specific gunk.
func runRawBashWithOutputs ( stepName , bash string ) ( bool , string , string ) {
2014-10-23 00:49:40 +00:00
cmd := exec . Command ( "bash" , "-s" )
2014-11-25 18:28:43 +00:00
if * trace_bash {
cmd . Args = append ( cmd . Args , "-x" )
}
2014-12-16 00:03:11 +00:00
cmd . Stdin = strings . NewReader ( bash )
2014-10-28 19:37:03 +00:00
return finishRunning ( stepName , cmd )
}
2014-12-16 00:03:11 +00:00
func runRawBash ( stepName , bashFragment string ) bool {
result , _ , _ := runRawBashWithOutputs ( stepName , bashFragment )
2014-12-11 19:00:29 +00:00
return result
}
2014-12-16 00:03:11 +00:00
func runBashWithOutputs ( stepName , bashFragment string ) ( bool , string , string ) {
return runRawBashWithOutputs ( stepName , bashWrap ( bashFragment ) )
}
func runBash ( stepName , bashFragment string ) bool {
return runRawBash ( stepName , bashWrap ( bashFragment ) )
}
2014-11-14 01:03:06 +00:00
// call the returned anonymous function to stop.
func runBashUntil ( stepName , bashFragment string ) func ( ) {
cmd := exec . Command ( "bash" , "-s" )
cmd . Stdin = strings . NewReader ( bashWrap ( bashFragment ) )
log . Printf ( "Running in background: %v" , stepName )
stdout , stderr := bytes . NewBuffer ( nil ) , bytes . NewBuffer ( nil )
cmd . Stdout , cmd . Stderr = stdout , stderr
if err := cmd . Start ( ) ; err != nil {
log . Printf ( "Unable to start '%v': '%v'" , stepName , err )
return func ( ) { }
}
return func ( ) {
cmd . Process . Signal ( os . Interrupt )
2014-12-11 19:00:29 +00:00
headerprefix := stepName + " "
lineprefix := " "
if * tap {
headerprefix = "# " + headerprefix
lineprefix = "# " + lineprefix
}
printBashOutputs ( headerprefix , lineprefix , string ( stdout . Bytes ( ) ) , string ( stderr . Bytes ( ) ) )
2014-11-14 01:03:06 +00:00
}
}
2014-12-11 19:00:29 +00:00
func finishRunning ( stepName string , cmd * exec . Cmd ) ( bool , string , string ) {
2014-10-28 19:37:03 +00:00
log . Printf ( "Running: %v" , stepName )
2014-10-23 00:49:40 +00:00
stdout , stderr := bytes . NewBuffer ( nil ) , bytes . NewBuffer ( nil )
if * verbose {
cmd . Stdout = os . Stdout
cmd . Stderr = os . Stderr
} else {
cmd . Stdout = stdout
cmd . Stderr = stderr
}
done := make ( chan struct { } )
defer close ( done )
go func ( ) {
for {
select {
case <- done :
return
case s := <- signals :
cmd . Process . Signal ( s )
}
}
} ( )
if err := cmd . Run ( ) ; err != nil {
log . Printf ( "Error running %v: %v" , stepName , err )
if ! * verbose {
2014-12-11 19:00:29 +00:00
return false , string ( stdout . Bytes ( ) ) , string ( stderr . Bytes ( ) )
} else {
return false , "" , ""
2014-10-23 00:49:40 +00:00
}
}
2014-12-11 19:00:29 +00:00
return true , "" , ""
}
func printBashOutputs ( headerprefix , lineprefix , stdout , stderr string ) {
// The |'s (plus appropriate prefixing) are to make this look
// "YAMLish" to the Jenkins TAP plugin
if stdout != "" {
fmt . Printf ( "%vstdout: |\n" , headerprefix )
printPrefixedLines ( lineprefix , stdout )
}
if stderr != "" {
fmt . Printf ( "%vstderr: |\n" , headerprefix )
printPrefixedLines ( lineprefix , stderr )
}
}
func printPrefixedLines ( prefix , s string ) {
for _ , line := range strings . Split ( s , "\n" ) {
fmt . Printf ( "%v%v\n" , prefix , line )
}
2014-10-23 00:49:40 +00:00
}
2014-11-11 01:46:11 +00:00
// returns either "", or a list of args intended for appending with the
// kubecfg or kubectl commands (begining with a space).
2014-11-12 21:30:53 +00:00
func kubecfgArgs ( ) string {
2014-11-11 01:46:11 +00:00
if * checkVersionSkew {
return " -expect_version_match"
}
return ""
}
2014-11-12 21:30:53 +00:00
// returns either "", or a list of args intended for appending with the
// kubectl command (begining with a space).
func kubectlArgs ( ) string {
if * checkVersionSkew {
return " --match-server-version"
}
return ""
}
2014-11-11 01:46:11 +00:00
func bashWrap ( cmd string ) string {
return `
2014-10-23 00:49:40 +00:00
set - o errexit
set - o nounset
set - o pipefail
export KUBE_CONFIG_FILE = "config-test.sh"
# TODO ( jbeda ) : This will break on usage if there is a space in
# $ { KUBE_ROOT } . Covert to an array ? Or an exported function ?
2014-12-16 00:03:11 +00:00
export KUBECFG = "` + versionRoot + `/cluster/kubecfg.sh` + kubecfgArgs() + `"
export KUBECTL = "` + versionRoot + `/cluster/kubectl.sh` + kubectlArgs() + `"
2014-10-23 00:49:40 +00:00
source "` + *root + `/cluster/kube-env.sh"
2014-12-16 00:03:11 +00:00
source "` + versionRoot + `/cluster/${KUBERNETES_PROVIDER}/util.sh"
2014-10-23 00:49:40 +00:00
2014-11-11 19:03:07 +00:00
prepare - e2e
2014-10-23 00:49:40 +00:00
2014-11-11 01:46:11 +00:00
` + cmd + `
2014-10-23 00:49:40 +00:00
`
}