| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package header |
| 18 | |
| 19 | import ( |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestGenAgentIdentifiers(t *testing.T) { |
| 25 | testCases := []struct { |
| 26 | desc string |
| 27 | idents string |
| 28 | |
| 29 | want Identifiers |
| 30 | wantErr bool |
| 31 | }{ |
| 32 | { |
| 33 | desc: "invalid url encoding", |
| 34 | idents: ";", |
| 35 | wantErr: true, |
| 36 | }, |
| 37 | { |
| 38 | desc: "invalid identifier type", |
| 39 | idents: "invalid=1.2.3.4", |
| 40 | want: Identifiers{}, |
| 41 | }, |
| 42 | { |
| 43 | desc: "ipv4", |
| 44 | idents: "ipv4=1.2.3.4", |
| 45 | want: Identifiers{ |
| 46 | IPv4: []string{"1.2.3.4"}, |
| 47 | }, |
| 48 | }, |
| 49 | { |
| 50 | desc: "ipv6", |
| 51 | idents: "ipv6=100::", |
| 52 | want: Identifiers{ |
| 53 | IPv6: []string{"100::"}, |
| 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | desc: "host", |
| 58 | idents: "host=node1.mydomain.com", |
| 59 | want: Identifiers{ |
| 60 | Host: []string{"node1.mydomain.com"}, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | desc: "cidr", |
| 65 | idents: "cidr=127.0.0.1/16", |
| 66 | want: Identifiers{ |
| 67 | CIDR: []string{"127.0.0.1/16"}, |
| 68 | }, |
| 69 | }, |
| 70 | { |
| 71 | desc: "default route true", |
| 72 | idents: "default-route=true", |
| 73 | want: Identifiers{ |
| 74 | DefaultRoute: true, |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | desc: "default route false", |
| 79 | idents: "default-route=false", |
| 80 | want: Identifiers{}, |
| 81 | }, |
| 82 | { |
| 83 | desc: "default route invalid", |
| 84 | idents: "default-route=invalid", |
| 85 | want: Identifiers{}, |
| 86 | }, |
| 87 | { |
| 88 | desc: "multiple default route", |
| 89 | // The first entry is used. |
| 90 | idents: "default-route=true&default-route=false", |
| 91 | want: Identifiers{ |
| 92 | DefaultRoute: true, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | desc: "success with multiple", |
| 97 | idents: "host=localhost&host=node1.mydomain.com&cidr=10.0.0.0/8&cidr=100::/64&ipv4=1.2.3.4&ipv4=5.6.7.8&ipv6=100::&ipv6=100::1&default-route=true", |
| 98 | want: Identifiers{ |
| 99 | IPv4: []string{"1.2.3.4", "5.6.7.8"}, |
| 100 | IPv6: []string{"100::", "100::1"}, |
| 101 | Host: []string{"localhost", "node1.mydomain.com"}, |
| 102 | CIDR: []string{"10.0.0.0/8", "100::/64"}, |
| 103 | DefaultRoute: true, |
| 104 | }, |
| 105 | }, |
| 106 | { |
| 107 | // UID is passed via header AgentID, not AgentIdentifers. |
| 108 | desc: "ignore IdentifierType UID", |
| 109 | idents: "uid=value", |
| 110 | want: Identifiers{}, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for _, tc := range testCases { |
| 115 | t.Run(tc.desc, func(t *testing.T) { |
| 116 | got, err := GenAgentIdentifiers(tc.idents) |
| 117 | if (err != nil) != tc.wantErr { |
| 118 | t.Errorf("GenAgentIdentifiers got err %q; wantErr = %t", err, tc.wantErr) |
| 119 | } |
| 120 | if !reflect.DeepEqual(tc.want, got) { |
| 121 | t.Errorf("expected %v, got %v", tc.want, got) |
| 122 | } |
| 123 | }) |
| 124 | } |
| 125 | } |