| 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 knftables |
| 18 | |
| 19 | import ( |
| 20 | "net" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestConcat(t *testing.T) { |
| 25 | _, cidr, _ := net.ParseCIDR("10.2.0.0/24") |
| 26 | |
| 27 | for _, tc := range []struct { |
| 28 | name string |
| 29 | values []interface{} |
| 30 | out string |
| 31 | }{ |
| 32 | { |
| 33 | name: "empty", |
| 34 | values: nil, |
| 35 | out: "", |
| 36 | }, |
| 37 | { |
| 38 | name: "one string", |
| 39 | values: []interface{}{ |
| 40 | "foo", |
| 41 | }, |
| 42 | out: "foo", |
| 43 | }, |
| 44 | { |
| 45 | name: "one array", |
| 46 | values: []interface{}{ |
| 47 | []string{"foo", "bar", "baz"}, |
| 48 | }, |
| 49 | out: "foo bar baz", |
| 50 | }, |
| 51 | { |
| 52 | name: "mixed", |
| 53 | values: []interface{}{ |
| 54 | "foo", []string{"bar", "baz"}, "quux", |
| 55 | }, |
| 56 | out: "foo bar baz quux", |
| 57 | }, |
| 58 | { |
| 59 | name: "empty array", |
| 60 | values: []interface{}{ |
| 61 | "foo", []string{}, "bar", "baz", |
| 62 | }, |
| 63 | // no extra spaces |
| 64 | out: "foo bar baz", |
| 65 | }, |
| 66 | { |
| 67 | name: "numbers", |
| 68 | values: []interface{}{ |
| 69 | 1, uint16(65535), int64(-123456789), |
| 70 | }, |
| 71 | out: "1 65535 -123456789", |
| 72 | }, |
| 73 | { |
| 74 | name: "everything", |
| 75 | values: []interface{}{ |
| 76 | IPv4Family, "saddr", cidr, |
| 77 | []string{}, |
| 78 | "th port", 8080, |
| 79 | "@", "mySetName", |
| 80 | []string{"@", "myMapName"}, |
| 81 | []string{"ct", "state", "established"}, |
| 82 | "drop", |
| 83 | }, |
| 84 | out: "ip saddr 10.2.0.0/24 th port 8080 @mySetName @myMapName ct state established drop", |
| 85 | }, |
| 86 | } { |
| 87 | t.Run(tc.name, func(t *testing.T) { |
| 88 | out := Concat(tc.values...) |
| 89 | if out != tc.out { |
| 90 | t.Errorf("expected %q got %q", tc.out, out) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |