Debian Codegraph @

pkg/utils/utils_test.go

raw
1 // Copyright 2019 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 utils_test
16
17 import (
18 "reflect"
19 "testing"
20
21 "github.com/containernetworking/cni/pkg/types"
22 "github.com/containernetworking/cni/pkg/utils"
23 )
24
25 func TestValidateContainerID(t *testing.T) {
26 testData := []struct {
27 description string
28 containerID string
29 err *types.Error
30 }{
31 {
32 description: "empty containerID",
33 containerID: "",
34 err: types.NewError(types.ErrUnknownContainer, "missing containerID", ""),
35 },
36 {
37 description: "invalid characters in containerID",
38 containerID: "1234%%%",
39 err: types.NewError(types.ErrInvalidEnvironmentVariables, "invalid characters in containerID", "1234%%%"),
40 },
41 {
42 description: "normal containerID",
43 containerID: "a51debf7e1eb",
44 err: nil,
45 },
46 }
47
48 for _, tt := range testData {
49 err := utils.ValidateContainerID(tt.containerID)
50 if !reflect.DeepEqual(tt.err, err) {
51 t.Errorf("Expected '%v' but got '%v'", tt.err, err)
52 }
53 }
54 }
55
56 func TestValidateNetworkName(t *testing.T) {
57 testData := []struct {
58 description string
59 networkName string
60 err *types.Error
61 }{
62 {
63 description: "empty networkName",
64 networkName: "",
65 err: types.NewError(types.ErrInvalidNetworkConfig, "missing network name:", ""),
66 },
67 {
68 description: "invalid characters in networkName",
69 networkName: "1234%%%",
70 err: types.NewError(types.ErrInvalidNetworkConfig, "invalid characters found in network name", "1234%%%"),
71 },
72 {
73 description: "normal networkName",
74 networkName: "eth0",
75 err: nil,
76 },
77 }
78
79 for _, tt := range testData {
80 err := utils.ValidateNetworkName(tt.networkName)
81 if !reflect.DeepEqual(tt.err, err) {
82 t.Errorf("Expected '%v' but got '%v'", tt.err, err)
83 }
84 }
85 }
86
87 func TestValidateInterfaceName(t *testing.T) {
88 testData := []struct {
89 description string
90 interfaceName string
91 err *types.Error
92 }{
93 {
94 description: "empty interfaceName",
95 interfaceName: "",
96 err: types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is empty", ""),
97 },
98 {
99 description: "more than 16 characters in interfaceName",
100 interfaceName: "testnamemorethan16",
101 err: types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is too long", "interface name should be less than 16 characters"),
102 },
103 {
104 description: "interfaceName is .",
105 interfaceName: ".",
106 err: types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is . or ..", ""),
107 },
108 {
109 description: "interfaceName contains /",
110 interfaceName: "/testname",
111 err: types.NewError(types.ErrInvalidEnvironmentVariables, "interface name contains / or : or whitespace characters", ""),
112 },
113 {
114 description: "interfaceName contains whitespace characters",
115 interfaceName: "test name",
116 err: types.NewError(types.ErrInvalidEnvironmentVariables, "interface name contains / or : or whitespace characters", ""),
117 },
118 {
119 description: "normal interfaceName",
120 interfaceName: "testname",
121 err: nil,
122 },
123 }
124
125 for _, tt := range testData {
126 err := utils.ValidateInterfaceName(tt.interfaceName)
127 if !reflect.DeepEqual(tt.err, err) {
128 t.Errorf("Expected '%v' but got '%v'", tt.err, err)
129 }
130 }
131 }