| 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/onsi/ginkgo/v2" |
| 19 |
. "github.com/onsi/gomega" |
| 20 |
|
| 21 |
"github.com/containernetworking/cni/pkg/version" |
| 22 |
) |
| 23 |
|
| 24 |
var _ = Describe("Decoding the version of network config", func() { |
| 25 |
var ( |
| 26 |
decoder *version.ConfigDecoder |
| 27 |
configBytes []byte |
| 28 |
) |
| 29 |
|
| 30 |
BeforeEach(func() { |
| 31 |
decoder = &version.ConfigDecoder{} |
| 32 |
configBytes = []byte(`{ "cniVersion": "4.3.2" }`) |
| 33 |
}) |
| 34 |
|
| 35 |
Context("when the version is explicit", func() { |
| 36 |
It("returns the version", func() { |
| 37 |
version, err := decoder.Decode(configBytes) |
| 38 |
Expect(err).NotTo(HaveOccurred()) |
| 39 |
|
| 40 |
Expect(version).To(Equal("4.3.2")) |
| 41 |
}) |
| 42 |
}) |
| 43 |
|
| 44 |
Context("when the version is not present in the config", func() { |
| 45 |
BeforeEach(func() { |
| 46 |
configBytes = []byte(`{ "not-a-version-field": "foo" }`) |
| 47 |
}) |
| 48 |
|
| 49 |
It("assumes the config is version 0.1.0", func() { |
| 50 |
version, err := decoder.Decode(configBytes) |
| 51 |
Expect(err).NotTo(HaveOccurred()) |
| 52 |
|
| 53 |
Expect(version).To(Equal("0.1.0")) |
| 54 |
}) |
| 55 |
}) |
| 56 |
|
| 57 |
Context("when the config data is malformed", func() { |
| 58 |
BeforeEach(func() { |
| 59 |
configBytes = []byte(`{{{`) |
| 60 |
}) |
| 61 |
|
| 62 |
It("returns a useful error", func() { |
| 63 |
_, err := decoder.Decode(configBytes) |
| 64 |
Expect(err).To(MatchError(HavePrefix( |
| 65 |
"decoding version from network config: invalid character", |
| 66 |
))) |
| 67 |
}) |
| 68 |
}) |
| 69 |
}) |