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
15package types_test
16
17import (
18	"reflect"
19
20	. "github.com/containernetworking/cni/pkg/types"
21
22	. "github.com/onsi/ginkgo"
23	. "github.com/onsi/ginkgo/extensions/table"
24	. "github.com/onsi/gomega"
25)
26
27var _ = 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
50var _ = 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
65var _ = 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
82var _ = 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})
133