github.com/john-lin/cni@v0.6.0-rc1.0.20170712150331-b69e640cc0e2/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 })