2016-11-29 01:17:10 +00:00
/ *
Copyright 2016 The Kubernetes Authors .
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 .
* /
package remote
import (
"fmt"
"os"
"os/exec"
"path/filepath"
2016-11-30 01:55:18 +00:00
"strings"
2016-11-29 01:17:10 +00:00
"time"
2016-11-30 01:55:18 +00:00
"github.com/golang/glog"
2016-11-29 01:17:10 +00:00
"k8s.io/kubernetes/test/e2e_node/builder"
)
2017-02-22 22:07:10 +00:00
const localCOSMounterPath = "cluster/gce/gci/mounter/mounter"
2016-11-30 01:55:18 +00:00
// NodeE2ERemote contains the specific functions in the node e2e test suite.
2016-11-29 01:17:10 +00:00
type NodeE2ERemote struct { }
func InitNodeE2ERemote ( ) TestSuite {
// TODO: Register flags.
return & NodeE2ERemote { }
}
// SetupTestPackage sets up the test package with binaries k8s required for node e2e tests
func ( n * NodeE2ERemote ) SetupTestPackage ( tardir string ) error {
// Build the executables
if err := builder . BuildGo ( ) ; err != nil {
return fmt . Errorf ( "failed to build the depedencies: %v" , err )
}
// Make sure we can find the newly built binaries
buildOutputDir , err := builder . GetK8sBuildOutputDir ( )
if err != nil {
return fmt . Errorf ( "failed to locate kubernetes build output directory %v" , err )
}
// Copy binaries
requiredBins := [ ] string { "kubelet" , "e2e_node.test" , "ginkgo" }
for _ , bin := range requiredBins {
source := filepath . Join ( buildOutputDir , bin )
if _ , err := os . Stat ( source ) ; err != nil {
return fmt . Errorf ( "failed to locate test binary %s: %v" , bin , err )
}
out , err := exec . Command ( "cp" , source , filepath . Join ( tardir , bin ) ) . CombinedOutput ( )
if err != nil {
return fmt . Errorf ( "failed to copy %q: %v Output: %q" , bin , err , out )
}
}
2017-01-23 23:00:39 +00:00
// Include the GCI/COS mounter artifacts in the deployed tarball
2017-02-22 22:07:10 +00:00
err = tarAddCOSMounter ( tardir )
if err != nil {
return err
}
return nil
}
// dest is relative to the root of the tar
func tarAddFile ( tar , source , dest string ) error {
dir := filepath . Dir ( dest )
tardir := filepath . Join ( tar , dir )
tardest := filepath . Join ( tar , dest )
out , err := exec . Command ( "mkdir" , "-p" , tardir ) . CombinedOutput ( )
if err != nil {
return fmt . Errorf ( "failed to create archive bin subdir %q, was dest for file %q. Err: %v. Output:\n%s" , tardir , source , err , out )
}
out , err = exec . Command ( "cp" , source , tardest ) . CombinedOutput ( )
if err != nil {
return fmt . Errorf ( "failed to copy file %q to the archive bin subdir %q. Err: %v. Output:\n%s" , source , tardir , err , out )
}
return nil
}
// Includes the GCI/COS mounter artifacts in the deployed tarball
func tarAddCOSMounter ( tar string ) error {
2016-11-29 01:17:10 +00:00
k8sDir , err := builder . GetK8sRootDir ( )
if err != nil {
return fmt . Errorf ( "Could not find K8s root dir! Err: %v" , err )
}
2017-02-22 22:07:10 +00:00
2017-01-23 23:00:39 +00:00
source := filepath . Join ( k8sDir , localCOSMounterPath )
2016-11-29 01:17:10 +00:00
2017-01-23 23:00:39 +00:00
// Require the GCI/COS mounter script, we want to make sure the remote test runner stays up to date if the mounter file moves
2016-11-29 01:17:10 +00:00
if _ , err := os . Stat ( source ) ; err != nil {
2017-01-23 23:00:39 +00:00
return fmt . Errorf ( "Could not find GCI/COS mounter script at %q! If this script has been (re)moved, please update the e2e node remote test runner accordingly! Err: %v" , source , err )
2016-11-29 01:17:10 +00:00
}
2017-02-22 22:07:10 +00:00
tarAddFile ( tar , source , localCOSMounterPath )
2016-11-29 01:17:10 +00:00
return nil
}
2017-05-10 18:38:26 +00:00
// updateCOSKubeletFlags updates kubelet flags to set gci mounter path, and enables memcg notifications. This will only take effect for
2017-01-23 23:00:39 +00:00
// GCI/COS image.
2017-05-10 18:38:26 +00:00
func updateCOSKubeletFlags ( args , host , workspace string ) ( string , error ) {
2017-01-23 23:00:39 +00:00
// Determine if tests will run on a GCI/COS node.
2016-11-30 01:55:18 +00:00
output , err := SSH ( host , "cat" , "/etc/os-release" )
if err != nil {
return args , fmt . Errorf ( "issue detecting node's OS via node's /etc/os-release. Err: %v, Output:\n%s" , err , output )
}
2017-01-23 23:00:39 +00:00
if ! strings . Contains ( output , "ID=gci" ) && ! strings . Contains ( output , "ID=cos" ) {
// This is not a GCI/COS image
2016-11-30 01:55:18 +00:00
return args , nil
}
2017-01-23 23:00:39 +00:00
// If we are testing on a GCI/COS node, we chmod 544 the mounter and specify a different mounter path in the test args.
2016-11-30 01:55:18 +00:00
// We do this here because the local var `workspace` tells us which /tmp/node-e2e-%d is relevant to the current test run.
2017-01-23 23:00:39 +00:00
// Determine if the GCI/COS mounter script exists locally.
2016-11-30 01:55:18 +00:00
k8sDir , err := builder . GetK8sRootDir ( )
if err != nil {
return args , fmt . Errorf ( "could not find K8s root dir! Err: %v" , err )
}
2017-01-23 23:00:39 +00:00
source := filepath . Join ( k8sDir , localCOSMounterPath )
2016-11-30 01:55:18 +00:00
2017-01-23 23:00:39 +00:00
// Require the GCI/COS mounter script, we want to make sure the remote test runner stays up to date if the mounter file moves
2016-11-30 01:55:18 +00:00
if _ , err = os . Stat ( source ) ; err != nil {
2017-01-23 23:00:39 +00:00
return args , fmt . Errorf ( "could not find GCI/COS mounter script at %q! If this script has been (re)moved, please update the e2e node remote test runner accordingly! Err: %v" , source , err )
2016-11-30 01:55:18 +00:00
}
2017-01-23 23:00:39 +00:00
glog . V ( 2 ) . Infof ( "GCI/COS node and GCI/COS mounter both detected, modifying --experimental-mounter-path accordingly" )
2016-11-30 01:55:18 +00:00
// Note this implicitly requires the script to be where we expect in the tarball, so if that location changes the error
// here will tell us to update the remote test runner.
2017-01-23 23:00:39 +00:00
mounterPath := filepath . Join ( workspace , localCOSMounterPath )
2016-11-30 01:55:18 +00:00
output , err = SSH ( host , "sh" , "-c" , fmt . Sprintf ( "'chmod 544 %s'" , mounterPath ) )
if err != nil {
2017-01-23 23:00:39 +00:00
return args , fmt . Errorf ( "unabled to chmod 544 GCI/COS mounter script. Err: %v, Output:\n%s" , err , output )
2016-11-30 01:55:18 +00:00
}
// Insert args at beginning of test args, so any values from command line take precedence
2017-05-10 18:38:26 +00:00
args = fmt . Sprintf ( "--kubelet-flags='--experimental-kernel-memcg-notification=true --experimental-mounter-path=%s'" , mounterPath ) + args
2016-11-30 01:55:18 +00:00
return args , nil
}
2016-11-29 01:17:10 +00:00
// RunTest runs test on the node.
2016-11-30 01:55:18 +00:00
func ( n * NodeE2ERemote ) RunTest ( host , workspace , results , junitFilePrefix , testArgs , ginkgoArgs string , timeout time . Duration ) ( string , error ) {
2017-04-11 23:23:54 +00:00
// Install the cni plugins and add a basic CNI configuration.
if err := setupCNI ( host , workspace ) ; err != nil {
2016-11-30 01:55:18 +00:00
return "" , err
}
// Configure iptables firewall rules
if err := configureFirewall ( host ) ; err != nil {
return "" , err
}
// Kill any running node processes
cleanupNodeProcesses ( host )
2017-05-10 18:38:26 +00:00
testArgs , err := updateCOSKubeletFlags ( testArgs , host , workspace )
2016-11-30 01:55:18 +00:00
if err != nil {
return "" , err
}
// Run the tests
2016-12-14 03:08:15 +00:00
glog . V ( 2 ) . Infof ( "Starting tests on %q" , host )
2016-11-30 01:55:18 +00:00
cmd := getSSHCommand ( " && " ,
fmt . Sprintf ( "cd %s" , workspace ) ,
fmt . Sprintf ( "timeout -k 30s %fs ./ginkgo %s ./e2e_node.test -- --logtostderr --v 4 --node-name=%s --report-dir=%s --report-prefix=%s %s" ,
timeout . Seconds ( ) , ginkgoArgs , host , results , junitFilePrefix , testArgs ) ,
)
return SSH ( host , "sh" , "-c" , cmd )
2016-11-29 01:17:10 +00:00
}