| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 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 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package config_test |
| 21 | |
| 22 | import ( |
| 23 | "fmt" |
| 24 | "testing" |
| 25 | |
| 26 | assert "github.com/stretchr/testify/require" |
| 27 | |
| 28 | "github.com/networkplumbing/go-nft/nft" |
| 29 | "github.com/networkplumbing/go-nft/nft/schema" |
| 30 | ) |
| 31 | |
| 32 | type tableActionFunc func(*nft.Config, *schema.Table) |
| 33 | |
| 34 | const tableName = "test-table" |
| 35 | |
| 36 | func TestTable(t *testing.T) { |
| 37 | testTableActions(t) |
| 38 | testTableLookup(t) |
| 39 | } |
| 40 | |
| 41 | func testTableActions(t *testing.T) { |
| 42 | actions := map[nft.TableAction]tableActionFunc{ |
| 43 | nft.TableADD: func(c *nft.Config, t *schema.Table) { c.AddTable(t) }, |
| 44 | nft.TableDELETE: func(c *nft.Config, t *schema.Table) { c.DeleteTable(t) }, |
| 45 | nft.TableFLUSH: func(c *nft.Config, t *schema.Table) { c.FlushTable(t) }, |
| 46 | } |
| 47 | families := []nft.AddressFamily{ |
| 48 | nft.FamilyIP, |
| 49 | nft.FamilyIP6, |
| 50 | nft.FamilyINET, |
| 51 | nft.FamilyBridge, |
| 52 | nft.FamilyARP, |
| 53 | nft.FamilyNETDEV, |
| 54 | } |
| 55 | for actionName, actionFunc := range actions { |
| 56 | for _, family := range families { |
| 57 | testTableAction(t, actionName, actionFunc, family) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func testTableAction(t *testing.T, actionName nft.TableAction, actionFunc tableActionFunc, family nft.AddressFamily) { |
| 63 | testName := fmt.Sprintf("%s %s table", actionName, family) |
| 64 | t.Run(testName, func(t *testing.T) { |
| 65 | table := nft.NewTable(tableName, family) |
| 66 | config := nft.NewConfig() |
| 67 | actionFunc(config, table) |
| 68 | |
| 69 | serializedConfig, err := config.ToJSON() |
| 70 | assert.NoError(t, err) |
| 71 | |
| 72 | var expected []byte |
| 73 | if actionName == nft.TableADD { |
| 74 | expected = []byte(fmt.Sprintf(`{"nftables":[{"table":{"family":%q,"name":%q}}]}`, family, tableName)) |
| 75 | } else { |
| 76 | expected = []byte(fmt.Sprintf(`{"nftables":[{%q:{"table":{"family":%q,"name":%q}}}]}`, actionName, family, tableName)) |
| 77 | } |
| 78 | assert.Equal(t, string(expected), string(serializedConfig)) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | func testTableLookup(t *testing.T) { |
| 83 | config := nft.NewConfig() |
| 84 | config.AddTable(nft.NewTable("table-ip", nft.FamilyIP)) |
| 85 | config.AddTable(nft.NewTable("table-ip", nft.FamilyIP6)) |
| 86 | table_br := nft.NewTable("table-br", nft.FamilyBridge) |
| 87 | config.AddTable(table_br) |
| 88 | |
| 89 | config.AddChain(nft.NewRegularChain(table_br, "chain-br")) |
| 90 | |
| 91 | t.Run("Lookup an existing table", func(t *testing.T) { |
| 92 | table := config.LookupTable(table_br) |
| 93 | assert.Equal(t, *table_br, *table) |
| 94 | }) |
| 95 | |
| 96 | t.Run("Lookup a missing table", func(t *testing.T) { |
| 97 | table := config.LookupTable(nft.NewTable("table-na", nft.FamilyBridge)) |
| 98 | assert.Nil(t, table) |
| 99 | }) |
| 100 | } |