github.com/appc/cni@v0.8.1/pkg/version/plugin_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  
    15  package version_test
    16  
    17  import (
    18  	"github.com/containernetworking/cni/pkg/version"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("Decoding versions reported by a plugin", func() {
    24  	var (
    25  		decoder       *version.PluginDecoder
    26  		versionStdout []byte
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		decoder = &version.PluginDecoder{}
    31  		versionStdout = []byte(`{
    32  			"cniVersion": "some-library-version",
    33  			"supportedVersions": [ "some-version", "some-other-version" ]
    34  		}`)
    35  	})
    36  
    37  	It("returns a PluginInfo that represents the given json bytes", func() {
    38  		pluginInfo, err := decoder.Decode(versionStdout)
    39  		Expect(err).NotTo(HaveOccurred())
    40  		Expect(pluginInfo).NotTo(BeNil())
    41  		Expect(pluginInfo.SupportedVersions()).To(Equal([]string{
    42  			"some-version",
    43  			"some-other-version",
    44  		}))
    45  	})
    46  
    47  	Context("when the bytes cannot be decoded as json", func() {
    48  		BeforeEach(func() {
    49  			versionStdout = []byte(`{{{`)
    50  		})
    51  
    52  		It("returns a meaningful error", func() {
    53  			_, err := decoder.Decode(versionStdout)
    54  			Expect(err).To(MatchError("decoding version info: invalid character '{' looking for beginning of object key string"))
    55  		})
    56  	})
    57  
    58  	Context("when the json bytes are missing the required CNIVersion field", func() {
    59  		BeforeEach(func() {
    60  			versionStdout = []byte(`{ "supportedVersions": [ "foo" ] }`)
    61  		})
    62  
    63  		It("returns a meaningful error", func() {
    64  			_, err := decoder.Decode(versionStdout)
    65  			Expect(err).To(MatchError("decoding version info: missing field cniVersion"))
    66  		})
    67  	})
    68  
    69  	Context("when there are no supported versions", func() {
    70  		BeforeEach(func() {
    71  			versionStdout = []byte(`{ "cniVersion": "0.2.0" }`)
    72  		})
    73  
    74  		It("assumes that the supported versions are 0.1.0 and 0.2.0", func() {
    75  			pluginInfo, err := decoder.Decode(versionStdout)
    76  			Expect(err).NotTo(HaveOccurred())
    77  			Expect(pluginInfo).NotTo(BeNil())
    78  			Expect(pluginInfo.SupportedVersions()).To(Equal([]string{
    79  				"0.1.0",
    80  				"0.2.0",
    81  			}))
    82  		})
    83  	})
    84  
    85  	Describe("ParseVersion", func() {
    86  		It("parses a valid version correctly", func() {
    87  			major, minor, micro, err := version.ParseVersion("1.2.3")
    88  			Expect(err).NotTo(HaveOccurred())
    89  			Expect(major).To(Equal(1))
    90  			Expect(minor).To(Equal(2))
    91  			Expect(micro).To(Equal(3))
    92  		})
    93  
    94  		It("returns an error for malformed versions", func() {
    95  			badVersions := []string{"asdfasdf", "asdf.", ".asdfas", "asdf.adsf.", "0.", "..", "1.2.3.4.5", ""}
    96  			for _, v := range badVersions {
    97  				_, _, _, err := version.ParseVersion(v)
    98  				Expect(err).To(HaveOccurred())
    99  			}
   100  		})
   101  	})
   102  
   103  	Describe("GreaterThanOrEqualTo", func() {
   104  		It("correctly compares versions", func() {
   105  			versions := [][2]string{
   106  				{"1.2.34", "1.2.14"},
   107  				{"2.5.4", "2.4.4"},
   108  				{"1.2.3", "0.2.3"},
   109  				{"0.4.0", "0.3.1"},
   110  			}
   111  			for _, v := range versions {
   112  				// Make sure the first is greater than the second
   113  				gt, err := version.GreaterThanOrEqualTo(v[0], v[1])
   114  				Expect(err).NotTo(HaveOccurred())
   115  				Expect(gt).To(Equal(true))
   116  
   117  				// And the opposite
   118  				gt, err = version.GreaterThanOrEqualTo(v[1], v[0])
   119  				Expect(err).NotTo(HaveOccurred())
   120  				Expect(gt).To(Equal(false))
   121  			}
   122  		})
   123  
   124  		It("returns true when versions are the same", func() {
   125  			gt, err := version.GreaterThanOrEqualTo("1.2.3", "1.2.3")
   126  			Expect(err).NotTo(HaveOccurred())
   127  			Expect(gt).To(Equal(true))
   128  		})
   129  
   130  		It("returns an error for malformed versions", func() {
   131  			versions := [][2]string{
   132  				{"1.2.34", "asdadf"},
   133  				{"adsfad", "2.5.4"},
   134  			}
   135  			for _, v := range versions {
   136  				_, err := version.GreaterThanOrEqualTo(v[0], v[1])
   137  				Expect(err).To(HaveOccurred())
   138  			}
   139  		})
   140  	})
   141  })