Skip to content

Commit 8cbd8c5

Browse files
committed
Use the compiled kubernetes binaries in the e2e tests in CI
1 parent 35ab61a commit 8cbd8c5

File tree

2 files changed

+121
-9
lines changed

2 files changed

+121
-9
lines changed

test/k8s-integration/fs.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"io"
21+
"os"
22+
"path/filepath"
23+
)
24+
25+
// CopyFile copies a file from src to dst
26+
func CopyFile(src, dst string) (err error) {
27+
// get source information
28+
info, err := os.Stat(src)
29+
if err != nil {
30+
return err
31+
}
32+
return copyFile(src, dst, info)
33+
}
34+
35+
func copyFile(src, dst string, info os.FileInfo) error {
36+
// open src for reading
37+
in, err := os.Open(src)
38+
if err != nil {
39+
return err
40+
}
41+
defer func() {
42+
closeErr := in.Close()
43+
// if we weren't returning an error
44+
if err == nil {
45+
err = closeErr
46+
}
47+
}()
48+
if err := os.MkdirAll(filepath.Dir(dst), os.ModePerm); err != nil {
49+
return err
50+
}
51+
// create dst file
52+
// this is like f, err := os.Create(dst); os.Chmod(f.Name(), src.Mode())
53+
out, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode())
54+
if err != nil {
55+
return err
56+
}
57+
// make sure we close the file
58+
defer func() {
59+
closeErr := out.Close()
60+
// if we weren't returning an error
61+
if err == nil {
62+
err = closeErr
63+
}
64+
}()
65+
// actually copy
66+
if _, err = io.Copy(out, in); err != nil {
67+
return err
68+
}
69+
err = out.Sync()
70+
return err
71+
}

test/k8s-integration/main.go

+50-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"os/exec"
2222
"path/filepath"
23+
"runtime"
2324
"strings"
2425
"syscall"
2526

@@ -662,13 +663,7 @@ func runTestsWithConfig(testParams *testParameters, testConfigArg, reportPrefix
662663

663664
testArgs := fmt.Sprintf("%s %s", ginkgoArgs, testConfigArg)
664665

665-
kubeTestArgs := []string{
666-
"--test",
667-
"--ginkgo-parallel",
668-
"--check-version-skew=false",
669-
fmt.Sprintf("--test_args=%s", testArgs),
670-
}
671-
666+
// kubetest2 flags
672667
kubeTest2Args := []string{
673668
*deploymentStrat,
674669
"--test=ginkgo",
@@ -678,14 +673,33 @@ func runTestsWithConfig(testParams *testParameters, testConfigArg, reportPrefix
678673
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--artifacts=%s", kubetestDumpDir))
679674
}
680675
kubeTest2Args = append(kubeTest2Args, "--")
681-
if len(*testVersion) != 0 && *testVersion != "master" {
682-
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-package-marker=latest-%s.txt", *testVersion))
676+
if len(*testVersion) != 0 {
677+
if *testVersion == "master" {
678+
// the kubernetes binaries should've already been built above
679+
// or by the user if --localK8sDir was set, these binaries should be copied to the
680+
// path sent to kubetest2 through its --artifacts path
681+
klog.Infof("Copying kubernetes binaries to path=%s to run the tests", kubetestDumpDir)
682+
err := copyKubernetesTestBinaries(testParams.k8sSourceDir, kubetestDumpDir)
683+
if err != nil {
684+
return fmt.Errorf("Failed to copy the kubernetes test binaries, err=%v", err)
685+
}
686+
kubeTest2Args = append(kubeTest2Args, "--use-built-binaries")
687+
} else {
688+
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-package-marker=latest-%s.txt", *testVersion))
689+
}
683690
}
684691
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--focus-regex=%s", testParams.testFocus))
685692
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--skip-regex=%s", testParams.testSkip))
686693
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--parallel=%d", testParams.parallel))
687694
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-args=%s %s", testConfigArg, windowsArgs))
688695

696+
// kubetest flags
697+
kubeTestArgs := []string{
698+
"--test",
699+
"--ginkgo-parallel",
700+
"--check-version-skew=false",
701+
fmt.Sprintf("--test_args=%s", testArgs),
702+
}
689703
if kubetestDumpDir != "" {
690704
kubeTestArgs = append(kubeTestArgs, fmt.Sprintf("--dump=%s", kubetestDumpDir))
691705
}
@@ -702,3 +716,30 @@ func runTestsWithConfig(testParams *testParameters, testConfigArg, reportPrefix
702716

703717
return nil
704718
}
719+
720+
var (
721+
kubernetesTestBinaries = []string{
722+
"kubectl",
723+
"e2e.test",
724+
"ginkgo",
725+
}
726+
)
727+
728+
// copyKubernetesBinariesForTest copies the common test binaries to the output directory
729+
func copyKubernetesTestBinaries(kuberoot string, outroot string) error {
730+
const dockerizedOutput = "_output/dockerized"
731+
root := filepath.Join(kuberoot, dockerizedOutput, "bin", runtime.GOOS, runtime.GOARCH)
732+
for _, binary := range kubernetesTestBinaries {
733+
source := filepath.Join(root, binary)
734+
dest := filepath.Join(outroot, binary)
735+
if _, err := os.Stat(source); err == nil {
736+
klog.Infof("copying %s to %s", source, dest)
737+
if err := CopyFile(source, dest); err != nil {
738+
return fmt.Errorf("failed to copy %s to %s: %v", source, dest, err)
739+
}
740+
} else {
741+
return fmt.Errorf("could not find %s: %v", source, err)
742+
}
743+
}
744+
return nil
745+
}

0 commit comments

Comments
 (0)