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