| 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 types_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"net" |
| 19 |
"reflect" |
| 20 |
|
| 21 |
. "github.com/onsi/ginkgo/v2" |
| 22 |
. "github.com/onsi/gomega" |
| 23 |
|
| 24 |
. "github.com/containernetworking/cni/pkg/types" |
| 25 |
) |
| 26 |
|
| 27 |
var _ = Describe("UnmarshallableBool UnmarshalText", func() { |
| 28 |
DescribeTable("string to bool detection should succeed in all cases", |
| 29 |
func(inputs []string, expected bool) { |
| 30 |
for _, s := range inputs { |
| 31 |
var ub UnmarshallableBool |
| 32 |
err := ub.UnmarshalText([]byte(s)) |
| 33 |
Expect(err).ToNot(HaveOccurred()) |
| 34 |
Expect(ub).To(Equal(UnmarshallableBool(expected))) |
| 35 |
} |
| 36 |
}, |
| 37 |
Entry("parse to true", []string{"True", "true", "1"}, true), |
| 38 |
Entry("parse to false", []string{"False", "false", "0"}, false), |
| 39 |
) |
| 40 |
|
| 41 |
Context("When passed an invalid value", func() { |
| 42 |
It("should result in an error", func() { |
| 43 |
var ub UnmarshallableBool |
| 44 |
err := ub.UnmarshalText([]byte("invalid")) |
| 45 |
Expect(err).To(HaveOccurred()) |
| 46 |
}) |
| 47 |
}) |
| 48 |
}) |
| 49 |
|
| 50 |
var _ = Describe("UnmarshallableString UnmarshalText", func() { |
| 51 |
DescribeTable("string to string detection should succeed in all cases", |
| 52 |
func(inputs []string, expected string) { |
| 53 |
for _, s := range inputs { |
| 54 |
var us UnmarshallableString |
| 55 |
err := us.UnmarshalText([]byte(s)) |
| 56 |
Expect(err).ToNot(HaveOccurred()) |
| 57 |
Expect(string(us)).To(Equal(expected)) |
| 58 |
} |
| 59 |
}, |
| 60 |
Entry("parse empty string", []string{""}, ""), |
| 61 |
Entry("parse non-empty string", []string{"notempty"}, "notempty"), |
| 62 |
) |
| 63 |
}) |
| 64 |
|
| 65 |
var _ = Describe("GetKeyField", func() { |
| 66 |
type testcontainer struct { |
| 67 |
Valid string `json:"valid,omitempty"` |
| 68 |
} |
| 69 |
var ( |
| 70 |
container = testcontainer{Valid: "valid"} |
| 71 |
containerInterface = func(i interface{}) interface{} { return i }(&container) |
| 72 |
containerValue = reflect.ValueOf(containerInterface) |
| 73 |
) |
| 74 |
Context("When a valid field is provided", func() { |
| 75 |
It("should return the correct field", func() { |
| 76 |
field := GetKeyField("Valid", containerValue) |
| 77 |
Expect(field.String()).To(Equal("valid")) |
| 78 |
}) |
| 79 |
}) |
| 80 |
}) |
| 81 |
|
| 82 |
var _ = Describe("LoadArgs", func() { |
| 83 |
Context("When no arguments are passed", func() { |
| 84 |
It("LoadArgs should succeed", func() { |
| 85 |
err := LoadArgs("", struct{}{}) |
| 86 |
Expect(err).NotTo(HaveOccurred()) |
| 87 |
}) |
| 88 |
}) |
| 89 |
|
| 90 |
Context("When unknown arguments are passed and ignored", func() { |
| 91 |
It("LoadArgs should succeed", func() { |
| 92 |
ca := CommonArgs{} |
| 93 |
err := LoadArgs("IgnoreUnknown=True;Unk=nown", &ca) |
| 94 |
Expect(err).NotTo(HaveOccurred()) |
| 95 |
}) |
| 96 |
}) |
| 97 |
|
| 98 |
Context("When unknown arguments are passed and not ignored", func() { |
| 99 |
It("LoadArgs should fail", func() { |
| 100 |
ca := CommonArgs{} |
| 101 |
err := LoadArgs("Unk=nown", &ca) |
| 102 |
Expect(err).To(HaveOccurred()) |
| 103 |
}) |
| 104 |
}) |
| 105 |
|
| 106 |
Context("When unknown arguments are passed and explicitly not ignored", func() { |
| 107 |
It("LoadArgs should fail", func() { |
| 108 |
ca := CommonArgs{} |
| 109 |
err := LoadArgs("IgnoreUnknown=0, Unk=nown", &ca) |
| 110 |
Expect(err).To(HaveOccurred()) |
| 111 |
}) |
| 112 |
}) |
| 113 |
|
| 114 |
Context("When known arguments are passed", func() { |
| 115 |
It("LoadArgs should succeed", func() { |
| 116 |
ca := CommonArgs{} |
| 117 |
err := LoadArgs("IgnoreUnknown=1", &ca) |
| 118 |
Expect(err).NotTo(HaveOccurred()) |
| 119 |
}) |
| 120 |
}) |
| 121 |
|
| 122 |
Context("When known arguments are passed and cannot be unmarshalled", func() { |
| 123 |
It("LoadArgs should fail", func() { |
| 124 |
conf := struct { |
| 125 |
IP IPNet |
| 126 |
}{} |
| 127 |
err := LoadArgs("IP=10.0.0.0/24", &conf) |
| 128 |
Expect(err).To(HaveOccurred()) |
| 129 |
}) |
| 130 |
}) |
| 131 |
|
| 132 |
Context("When loading known arguments", func() { |
| 133 |
It("should succeed if argument is marshallable value type", func() { |
| 134 |
conf := struct { |
| 135 |
IP net.IP |
| 136 |
CommonArgs |
| 137 |
}{} |
| 138 |
err := LoadArgs("IP=10.0.0.0", &conf) |
| 139 |
Expect(err).NotTo(HaveOccurred()) |
| 140 |
Expect(conf.IP.String()).To(Equal("10.0.0.0")) |
| 141 |
}) |
| 142 |
|
| 143 |
It("should succeed if argument is marshallable pointer type", func() { |
| 144 |
conf := struct { |
| 145 |
IP *net.IP |
| 146 |
CommonArgs |
| 147 |
}{} |
| 148 |
err := LoadArgs("IP=10.0.0.0", &conf) |
| 149 |
Expect(err).NotTo(HaveOccurred()) |
| 150 |
Expect(conf.IP.String()).To(Equal("10.0.0.0")) |
| 151 |
}) |
| 152 |
|
| 153 |
It("should fail if argument is pointer of marshallable pointer type", func() { |
| 154 |
conf := struct { |
| 155 |
IP **net.IP |
| 156 |
CommonArgs |
| 157 |
}{} |
| 158 |
err := LoadArgs("IP=10.0.0.0", &conf) |
| 159 |
Expect(err).To(HaveOccurred()) |
| 160 |
}) |
| 161 |
}) |
| 162 |
}) |