| 1 |
// Copyright 2016 CNI authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package testhelpers supports testing of CNI components of different versions |
| 16 |
// |
| 17 |
// For example, to build a plugin against an old version of the CNI library, |
| 18 |
// we can pass the plugin's source and the old git commit reference to BuildAt. |
| 19 |
// We could then test how the built binary responds when called by the latest |
| 20 |
// version of this library. |
| 21 |
package testhelpers |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"os" |
| 26 |
"os/exec" |
| 27 |
"path/filepath" |
| 28 |
"strings" |
| 29 |
) |
| 30 |
|
| 31 |
func run(cmd *exec.Cmd) error { |
| 32 |
out, err := cmd.CombinedOutput() |
| 33 |
if err != nil { |
| 34 |
command := strings.Join(cmd.Args, " ") |
| 35 |
return fmt.Errorf("running %q: %s", command, out) |
| 36 |
} |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
// unset GOPATH if it's set, so we use modules |
| 41 |
func goBuildEnviron() []string { |
| 42 |
out := []string{} |
| 43 |
for _, kvp := range os.Environ() { |
| 44 |
if !strings.HasPrefix(kvp, "GOPATH=") { |
| 45 |
out = append(out, kvp) |
| 46 |
} |
| 47 |
} |
| 48 |
return out |
| 49 |
} |
| 50 |
|
| 51 |
func buildGoProgram(modPath, outputFilePath string) error { |
| 52 |
cmd := exec.Command("go", "build", "-o", outputFilePath, ".") |
| 53 |
cmd.Dir = modPath |
| 54 |
cmd.Env = goBuildEnviron() |
| 55 |
return run(cmd) |
| 56 |
} |
| 57 |
|
| 58 |
func modInit(path, name string) error { |
| 59 |
cmd := exec.Command("go", "mod", "init", name) |
| 60 |
cmd.Dir = path |
| 61 |
return run(cmd) |
| 62 |
} |
| 63 |
|
| 64 |
// addLibcni will execute `go mod edit -replace` to fix libcni at a specified version |
| 65 |
func addLibcni(path, gitRef string) error { |
| 66 |
cmd := exec.Command("go", "mod", "edit", "-replace=github.com/containernetworking/cni=github.com/containernetworking/cni@"+gitRef) |
| 67 |
cmd.Dir = path |
| 68 |
return run(cmd) |
| 69 |
} |
| 70 |
|
| 71 |
// modTidy will execute `go mod tidy` to ensure all necessary dependencies |
| 72 |
func modTidy(path string) error { |
| 73 |
cmd := exec.Command("go", "mod", "tidy") |
| 74 |
cmd.Dir = path |
| 75 |
return run(cmd) |
| 76 |
} |
| 77 |
|
| 78 |
// BuildAt builds the go programSource using the version of the CNI library |
| 79 |
// at gitRef, and saves the resulting binary file at outputFilePath |
| 80 |
func BuildAt(programSource []byte, gitRef string, outputFilePath string) error { |
| 81 |
tempDir, err := os.MkdirTemp(os.Getenv("GOTMPDIR"), "cni-test-") |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
defer os.RemoveAll(tempDir) |
| 86 |
|
| 87 |
modName := filepath.Base(tempDir) |
| 88 |
|
| 89 |
if err := modInit(tempDir, modName); err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
|
| 93 |
if err := addLibcni(tempDir, gitRef); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
if err := os.WriteFile(filepath.Join(tempDir, "main.go"), programSource, 0o600); err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
|
| 101 |
if err := modTidy(tempDir); err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
err = buildGoProgram(tempDir, outputFilePath) |
| 106 |
if err != nil { |
| 107 |
return fmt.Errorf("failed to build: %w", err) |
| 108 |
} |
| 109 |
|
| 110 |
return nil |
| 111 |
} |