github.com/john-lin/cni@v0.6.0-rc1.0.20170712150331-b69e640cc0e2/pkg/version/testhelpers/testhelpers_test.go (about) 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_test 15 16 import ( 17 "io/ioutil" 18 "os" 19 "os/exec" 20 "path/filepath" 21 22 "github.com/containernetworking/cni/pkg/version/testhelpers" 23 . "github.com/onsi/ginkgo" 24 . "github.com/onsi/gomega" 25 ) 26 27 var _ = Describe("BuildAt", func() { 28 var ( 29 gitRef string 30 outputFilePath string 31 outputDir string 32 programSource []byte 33 ) 34 BeforeEach(func() { 35 programSource = []byte(`package main 36 37 import "github.com/containernetworking/cni/pkg/skel" 38 39 func c(_ *skel.CmdArgs) error { return nil } 40 41 func main() { skel.PluginMain(c, c) } 42 `) 43 gitRef = "f4364185253" 44 45 var err error 46 outputDir, err = ioutil.TempDir("", "bin") 47 Expect(err).NotTo(HaveOccurred()) 48 outputFilePath = filepath.Join(outputDir, "some-binary") 49 }) 50 51 AfterEach(func() { 52 Expect(os.RemoveAll(outputDir)).To(Succeed()) 53 }) 54 55 It("builds the provided source code using the CNI library at the given git ref", func() { 56 Expect(outputFilePath).NotTo(BeAnExistingFile()) 57 58 err := testhelpers.BuildAt(programSource, gitRef, outputFilePath) 59 Expect(err).NotTo(HaveOccurred()) 60 61 Expect(outputFilePath).To(BeAnExistingFile()) 62 63 cmd := exec.Command(outputFilePath) 64 cmd.Env = []string{"CNI_COMMAND=VERSION"} 65 output, err := cmd.CombinedOutput() 66 Expect(err).To(BeAssignableToTypeOf(&exec.ExitError{})) 67 Expect(output).To(ContainSubstring("unknown CNI_COMMAND: VERSION")) 68 }) 69 }) 70 71 var _ = Describe("LocateCurrentGitRepo", func() { 72 It("returns the path to the root of the CNI git repo", func() { 73 path, err := testhelpers.LocateCurrentGitRepo() 74 Expect(err).NotTo(HaveOccurred()) 75 76 AssertItIsTheCNIRepoRoot(path) 77 }) 78 79 Context("when run from a different directory", func() { 80 BeforeEach(func() { 81 os.Chdir("..") 82 }) 83 84 It("still finds the CNI repo root", func() { 85 path, err := testhelpers.LocateCurrentGitRepo() 86 Expect(err).NotTo(HaveOccurred()) 87 88 AssertItIsTheCNIRepoRoot(path) 89 }) 90 }) 91 }) 92 93 func AssertItIsTheCNIRepoRoot(path string) { 94 Expect(path).To(BeADirectory()) 95 files, err := ioutil.ReadDir(path) 96 Expect(err).NotTo(HaveOccurred()) 97 98 names := []string{} 99 for _, file := range files { 100 names = append(names, file.Name()) 101 } 102 103 Expect(names).To(ContainElement("SPEC.md")) 104 Expect(names).To(ContainElement("libcni")) 105 Expect(names).To(ContainElement("cnitool")) 106 }