| 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_testscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/ |
| 21 | |
| 22 | import ( |
| 23 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 24 | "testingscip-go gomod github.com/golang/go/src go1.16 testing/" |
| 25 | |
| 26 | assertscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/ "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/" |
| 27 | |
| 28 | "github.com/networkplumbing/go-nft/nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/" |
| 29 | "github.com/networkplumbing/go-nft/nft/schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/" |
| 30 | ) |
| 31 | |
| 32 | type tableActionFuncscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableActionFunc# func(*nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Table#) |
| 33 | |
| 34 | const tableNamescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableName. = "test-table" |
| 35 | |
| 36 | func TestTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/TestTable().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 37 | testTableActionsscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableActions().(t) |
| 38 | testTableLookupscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableLookup().(t) |
| 39 | } |
| 40 | |
| 41 | func testTableActionsscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableActions().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 42 | actions := map[nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableActionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableAction#]tableActionFuncscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableActionFunc#{ |
| 43 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableADDscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableADD.: func(c *nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, t *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Table#) { c.AddTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddTable().(t) }, |
| 44 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableDELETEscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableDELETE.: func(c *nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, t *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Table#) { c.DeleteTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#DeleteTable().(t) }, |
| 45 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableFLUSHscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableFLUSH.: func(c *nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, t *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Table#) { c.FlushTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FlushTable().(t) }, |
| 46 | } |
| 47 | families := []nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.AddressFamilyscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/AddressFamily#{ |
| 48 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyIPscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyIP., |
| 49 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyIP6scip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyIP6., |
| 50 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyINETscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyINET., |
| 51 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyBridgescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyBridge., |
| 52 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyARPscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyARP., |
| 53 | nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyNETDEVscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyNETDEV., |
| 54 | } |
| 55 | for actionName, actionFunc := range actions { |
| 56 | for _, family := range families { |
| 57 | testTableActionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableAction().(t, actionName, actionFunc, family) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func testTableActionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableAction().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#, actionName nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableActionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableAction#, actionFunc tableActionFuncscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableActionFunc#, family nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.AddressFamilyscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/AddressFamily#) { |
| 63 | testName := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s %s table", actionName, family) |
| 64 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(testName, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 65 | table := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewTable().(tableNamescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableName., family) |
| 66 | config := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewConfig().() |
| 67 | actionFunc(config, table) |
| 68 | |
| 69 | serializedConfig, err := config.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().() |
| 70 | assertscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 71 | |
| 72 | var expected []byte |
| 73 | if actionName == nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.TableADDscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/TableADD. { |
| 74 | expected = []byte(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(`{"nftables":[{"table":{"family":%q,"name":%q}}]}`, family, tableNamescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableName.)) |
| 75 | } else { |
| 76 | expected = []byte(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(`{"nftables":[{%q:{"table":{"family":%q,"name":%q}}}]}`, actionName, family, tableNamescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/tableName.)) |
| 77 | } |
| 78 | assertscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Equal().(t, string(expected), string(serializedConfig)) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | func testTableLookupscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/testTableLookup().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 83 | config := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewConfig().() |
| 84 | config.AddTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddTable().(nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewTable().("table-ip", nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyIPscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyIP.)) |
| 85 | config.AddTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddTable().(nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewTable().("table-ip", nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyIP6scip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyIP6.)) |
| 86 | table_br := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewTable().("table-br", nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyBridgescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyBridge.) |
| 87 | config.AddTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddTable().(table_br) |
| 88 | |
| 89 | config.AddChainscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddChain().(nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewRegularChainscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewRegularChain().(table_br, "chain-br")) |
| 90 | |
| 91 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().("Lookup an existing table", func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 92 | table := config.LookupTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#LookupTable().(table_br) |
| 93 | assertscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Equal().(t, *table_br, *table) |
| 94 | }) |
| 95 | |
| 96 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().("Lookup a missing table", func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 97 | table := config.LookupTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#LookupTable().(nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewTablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewTable().("table-na", nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.FamilyBridgescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/FamilyBridge.)) |
| 98 | assertscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Nilscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Nil().(t, table) |
| 99 | }) |
| 100 | } |