Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to replace image names for examples tests based on arch #3299

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions test/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func waitValidateTaskRunDone(t *testing.T, c *clients, taskRunName string) {
}

// substituteEnv substitutes docker repos and bucket paths from the system
// environment for input to allow tests on local clusters. It also unsets the
// namespace for ServiceAccounts so that they work under test.
// environment for input to allow tests on local clusters. It unsets the
// namespace for ServiceAccounts so that they work under test. It also
// replaces image names to arch specific ones, based on provided mapping.
func substituteEnv(input []byte, namespace string) ([]byte, error) {
// Replace the placeholder image repo with the value of the
// KO_DOCKER_REPO env var.
Expand All @@ -81,6 +82,10 @@ func substituteEnv(input []byte, namespace string) ([]byte, error) {
// the test namespace using `ko create -n`
output = defaultNamespaceRE.ReplaceAll(output, []byte("namespace: "+namespace))

// Replace image names to arch specific ones, where it's necessary
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
for existingImage, archSpecificImage := range imagesMappingRE {
output = existingImage.ReplaceAll(output, archSpecificImage)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
}
return output, nil
}

Expand Down
46 changes: 39 additions & 7 deletions test/multiarch_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ package test

import (
"os"
"regexp"
"runtime"
"testing"

"k8s.io/apimachinery/pkg/util/sets"
)

var (
imageNames = initImageNames()
excludedTests = initExcludedTests()
imageNames = initImageNames()
excludedTests = initExcludedTests()
imagesMappingRE map[*regexp.Regexp][]byte
)

const (
Expand All @@ -36,7 +38,11 @@ const (
registryImage
)

// return architecture of the cluster where test suites will be executed.
func init() {
imagesMappingRE = getImagesMappingRE()
}

// getTestArch returns architecture of the cluster where test suites will be executed.
// default value is similar to build architecture, TEST_RUNTIME_ARCH is used when test target cluster has another architecture
func getTestArch() string {
val, ok := os.LookupEnv("TEST_RUNTIME_ARCH")
Expand All @@ -46,6 +52,7 @@ func getTestArch() string {
return runtime.GOARCH
}

// initImageNames returns the map with arch dependent image names for e2e tests
func initImageNames() map[int]string {
if getTestArch() == "s390x" {
return map[int]string{
Expand All @@ -59,6 +66,33 @@ func initImageNames() map[int]string {
}
}

// getImagesMappingRE generates the map ready to search and replace image names with regexp for examples files.
// search is done using "image: <name>" pattern.
func getImagesMappingRE() map[*regexp.Regexp][]byte {
imageNamesMapping := imageNamesMapping()
imageMappingRE := make(map[*regexp.Regexp][]byte, len(imageNamesMapping))

for existingImage, archSpecificImage := range imageNamesMapping {
imageMappingRE[regexp.MustCompile("(?im)image: "+existingImage+"$")] = []byte("image: " + archSpecificImage)
}

return imageMappingRE
}

// imageNamesMapping provides mapping between image name in the examples yaml files and desired image name for specific arch.
// by default empty map is returned.
func imageNamesMapping() map[string]string {
if getTestArch() == "s390x" {
return map[string]string{
"registry": getTestImage(registryImage),
"node": "node:alpine3.11",
}
}

return make(map[string]string)
}

// initExcludedTests provides list of excluded tests for e2e and exanples tests
func initExcludedTests() sets.String {
if getTestArch() == "s390x" {
return sets.NewString(
Expand Down Expand Up @@ -90,8 +124,6 @@ func initExcludedTests() sets.String {
"TestExamples/v1alpha1/taskruns/pullrequest_input_copystep_output",
"TestExamples/v1beta1/taskruns/pullrequest",
"TestExamples/v1alpha1/taskruns/pullrequest",
"TestExamples/v1beta1/taskruns/step-script",
"TestExamples/v1alpha1/taskruns/step-script",
"TestExamples/v1beta1/pipelineruns/conditional-pipelinerun",
"TestExamples/v1alpha1/pipelineruns/pipelinerun-with-resourcespec",
"TestExamples/v1beta1/pipelineruns/pipelinerun-with-resourcespec",
Expand Down Expand Up @@ -120,12 +152,12 @@ func initExcludedTests() sets.String {
return sets.NewString()
}

// get test image based on unique id
// getTestImage gets test image based on unique id
func getTestImage(image int) string {
return imageNames[image]
}

// check if test name is in the excluded list and skip it
// skipIfExcluded checks if test name is in the excluded list and skip it
func skipIfExcluded(t *testing.T) {
if excludedTests.Has(t.Name()) {
t.Skipf("skip for %s architecture", getTestArch())
Expand Down