| 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 |
package testhelpers |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"os/exec" |
| 19 |
"path/filepath" |
| 20 |
"runtime" |
| 21 |
|
| 22 |
. "github.com/onsi/ginkgo/v2" |
| 23 |
. "github.com/onsi/gomega" |
| 24 |
) |
| 25 |
|
| 26 |
var _ = Describe("BuildAt", func() { |
| 27 |
var ( |
| 28 |
gitRef string |
| 29 |
outputFilePath string |
| 30 |
outputDir string |
| 31 |
programSource []byte |
| 32 |
) |
| 33 |
BeforeEach(func() { |
| 34 |
programSource = []byte(`package main |
| 35 |
|
| 36 |
import "github.com/containernetworking/cni/pkg/skel" |
| 37 |
|
| 38 |
func c(_ *skel.CmdArgs) error { return nil } |
| 39 |
|
| 40 |
func main() { skel.PluginMain(c, c) } |
| 41 |
`) |
| 42 |
gitRef = "f4364185253" |
| 43 |
|
| 44 |
var err error |
| 45 |
outputDir, err = os.MkdirTemp("", "bin") |
| 46 |
Expect(err).NotTo(HaveOccurred()) |
| 47 |
outputFilePath = filepath.Join(outputDir, "some-binary") |
| 48 |
if runtime.GOOS == "windows" { |
| 49 |
outputFilePath += ".exe" |
| 50 |
} |
| 51 |
}) |
| 52 |
|
| 53 |
AfterEach(func() { |
| 54 |
Expect(os.RemoveAll(outputDir)).To(Succeed()) |
| 55 |
}) |
| 56 |
|
| 57 |
It("builds the provided source code using the CNI library at the given git ref", func() { |
| 58 |
Expect(outputFilePath).NotTo(BeAnExistingFile()) |
| 59 |
|
| 60 |
err := BuildAt(programSource, gitRef, outputFilePath) |
| 61 |
Expect(err).NotTo(HaveOccurred()) |
| 62 |
|
| 63 |
Expect(outputFilePath).To(BeAnExistingFile()) |
| 64 |
|
| 65 |
cmd := exec.Command(outputFilePath) |
| 66 |
cmd.Env = []string{"CNI_COMMAND=VERSION"} |
| 67 |
output, err := cmd.CombinedOutput() |
| 68 |
Expect(err).To(BeAssignableToTypeOf(&exec.ExitError{})) |
| 69 |
Expect(output).To(ContainSubstring("unknown CNI_COMMAND: VERSION")) |
| 70 |
}) |
| 71 |
}) |