Skip to content

Commit 228bea0

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

File tree

2 files changed

+138
-7
lines changed

2 files changed

+138
-7
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

+67-7
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,30 +663,62 @@ 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),
666+
// kubetest2 flags
667+
668+
var runID string
669+
if uid, exists := os.LookupEnv("PROW_JOB_ID"); exists && uid != "" {
670+
// reuse uid for CI use cases
671+
runID = uid
672+
} else {
673+
runID = string(uuid.NewUUID())
670674
}
671675

672676
kubeTest2Args := []string{
673677
*deploymentStrat,
678+
fmt.Sprintf("--run-id=%s", runID),
674679
"--test=ginkgo",
675680
}
676681
kubeTest2Args = append(kubeTest2Args, testParams.cloudProviderArgs...)
677682
if kubetestDumpDir != "" {
678683
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--artifacts=%s", kubetestDumpDir))
679684
}
680685
kubeTest2Args = append(kubeTest2Args, "--")
681-
if len(*testVersion) != 0 && *testVersion != "master" {
682-
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-package-marker=latest-%s.txt", *testVersion))
686+
if len(*testVersion) != 0 {
687+
if *testVersion == "master" {
688+
// the kubernetes binaries should've already been built above
689+
// or by the user if --localK8sDir was set, these binaries should be copied to the
690+
// path sent to kubetest2 through its --artifacts path
691+
692+
// pkg/_artifacts is the default value that kubetests uses for --artifacts
693+
kubernetesTestBinariesPath := filepath.Join(testParams.pkgDir, "_artifacts")
694+
if kubetestDumpDir != "" {
695+
// a custom artifacts dir was set
696+
kubernetesTestBinariesPath = kubetestDumpDir
697+
}
698+
kubernetesTestBinariesPath = filepath.Join(kubernetesTestBinariesPath, runID)
699+
700+
klog.Infof("Copying kubernetes binaries to path=%s to run the tests", kubernetesTestBinariesPath)
701+
err := copyKubernetesTestBinaries(testParams.k8sSourceDir, kubernetesTestBinariesPath)
702+
if err != nil {
703+
return fmt.Errorf("Failed to copy the kubernetes test binaries, err=%v", err)
704+
}
705+
kubeTest2Args = append(kubeTest2Args, "--use-built-binaries")
706+
} else {
707+
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-package-marker=latest-%s.txt", *testVersion))
708+
}
683709
}
684710
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--focus-regex=%s", testParams.testFocus))
685711
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--skip-regex=%s", testParams.testSkip))
686712
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--parallel=%d", testParams.parallel))
687713
kubeTest2Args = append(kubeTest2Args, fmt.Sprintf("--test-args=%s %s", testConfigArg, windowsArgs))
688714

715+
// kubetest flags
716+
kubeTestArgs := []string{
717+
"--test",
718+
"--ginkgo-parallel",
719+
"--check-version-skew=false",
720+
fmt.Sprintf("--test_args=%s", testArgs),
721+
}
689722
if kubetestDumpDir != "" {
690723
kubeTestArgs = append(kubeTestArgs, fmt.Sprintf("--dump=%s", kubetestDumpDir))
691724
}
@@ -702,3 +735,30 @@ func runTestsWithConfig(testParams *testParameters, testConfigArg, reportPrefix
702735

703736
return nil
704737
}
738+
739+
var (
740+
kubernetesTestBinaries = []string{
741+
"kubectl",
742+
"e2e.test",
743+
"ginkgo",
744+
}
745+
)
746+
747+
// copyKubernetesBinariesForTest copies the common test binaries to the output directory
748+
func copyKubernetesTestBinaries(kuberoot string, outroot string) error {
749+
const dockerizedOutput = "_output/dockerized"
750+
root := filepath.Join(kuberoot, dockerizedOutput, "bin", runtime.GOOS, runtime.GOARCH)
751+
for _, binary := range kubernetesTestBinaries {
752+
source := filepath.Join(root, binary)
753+
dest := filepath.Join(outroot, binary)
754+
if _, err := os.Stat(source); err == nil {
755+
klog.Infof("copying %s to %s", source, dest)
756+
if err := CopyFile(source, dest); err != nil {
757+
return fmt.Errorf("failed to copy %s to %s: %v", source, dest, err)
758+
}
759+
} else {
760+
return fmt.Errorf("could not find %s: %v", source, err)
761+
}
762+
}
763+
return nil
764+
}

0 commit comments

Comments
 (0)