Debian Codegraph @

tests/config_test.go

raw symbols SCIP
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 tests
21
22 import (
23 "testing"
24
25 assert "github.com/stretchr/testify/require"
26
27 "github.com/networkplumbing/go-nft/tests/testlib"
28
29 "github.com/networkplumbing/go-nft/nft"
30 "github.com/networkplumbing/go-nft/nft/schema"
31
32 "context"
33 "time"
34 )
35
36 func TestConfig(t *testing.T) {
37 testlib.RunTestWithFlushTable(t, testReadEmptyConfig)
38 testlib.RunTestWithFlushTable(t, testApplyConfigWithAnEmptyTable)
39 testlib.RunTestWithFlushTable(t, testReadFilteredConfig)
40 testlib.RunTestWithFlushTable(t, testApplyConfigWithSampleStatements)
41 }
42
43 func testReadEmptyConfig(t *testing.T) {
44 config, err := nft.ReadConfig()
45 assert.NoError(t, err)
46 assert.Len(t, config.Nftables, 1, "Expecting just the metainfo entry")
47
48 expectedConfig := nft.NewConfig()
49 expectedConfig.Nftables = append(expectedConfig.Nftables, schema.Nftable{Metainfo: &schema.Metainfo{JsonSchemaVersion: 1}})
50
51 // The underling nftable userspace version depends on where it is ran, therefore remove it from the diff.
52 expectedConfig.Nftables[0].Metainfo.Version = config.Nftables[0].Metainfo.Version
53 expectedConfig.Nftables[0].Metainfo.ReleaseName = config.Nftables[0].Metainfo.ReleaseName
54 assert.Equal(t, expectedConfig, config)
55 }
56
57 func testApplyConfigWithAnEmptyTable(t *testing.T) {
58 config := nft.NewConfig()
59 config.AddTable(nft.NewTable("mytable", nft.FamilyIP))
60
61 assert.NoError(t, nft.ApplyConfig(config))
62
63 newConfig, err := nft.ReadConfig()
64 assert.NoError(t, err)
65
66 assert.Len(t, newConfig.Nftables, 2, "Expecting the metainfo and an empty table entry")
67 assert.Equal(t, config.Nftables[0], newConfig.Nftables[1])
68 }
69
70 func testReadFilteredConfig(t *testing.T) {
71 const (
72 tableName1 = "mytable1"
73 tableName2 = "mytable2"
74 chainName1 = "mychain1"
75 chainName2 = "mychain2"
76 )
77 config := nft.NewConfig()
78 table1 := nft.NewTable(tableName1, nft.FamilyIP)
79 table2 := nft.NewTable(tableName2, nft.FamilyIP)
80 config.AddTable(table1)
81 config.AddTable(table2)
82
83 chain1 := nft.NewChain(table1, chainName1, nil, nil, nil, nil)
84 chain2 := nft.NewChain(table1, chainName2, nil, nil, nil, nil)
85 config.AddChain(chain1)
86 config.AddChain(chain2)
87 assert.NoError(t, nft.ApplyConfig(config))
88
89 singleTableConfig, err := nft.ReadConfig("table", table2.Family, table2.Name)
90 assert.NoError(t, err)
91 assert.Len(t, singleTableConfig.Nftables, 2, "Expecting the metainfo and an empty table entry")
92 assert.Equal(t, config.Nftables[1], singleTableConfig.Nftables[1])
93
94 singleChainConfig, err := nft.ReadConfig("chain", chain1.Family, chain1.Table, chain1.Name)
95 assert.NoError(t, err)
96 assert.Len(t, singleChainConfig.Nftables, 2, "Expecting the metainfo and an empty chain entry")
97 assert.Equal(t, config.Nftables[2], singleChainConfig.Nftables[1])
98 }
99
100 func testApplyConfigWithSampleStatements(t *testing.T) {
101 testApplyConfigWithStatements(t,
102 schema.Statement{Counter: &schema.Counter{}},
103 )
104 }
105
106 func testApplyConfigWithStatements(t *testing.T, statements ...schema.Statement) {
107 const tableName = "mytable"
108 config := nft.NewConfig()
109 table := nft.NewTable(tableName, nft.FamilyIP)
110 config.AddTable(table)
111
112 const chainName = "mychain"
113 chain := nft.NewChain(table, chainName, nil, nil, nil, nil)
114 config.AddChain(chain)
115
116 rule := nft.NewRule(table, chain, statements, nil, nil, "test")
117 config.AddRule(rule)
118
119 assert.NoError(t, nft.ApplyConfig(config))
120
121 newConfig, err := nft.ReadConfig()
122 assert.NoError(t, err)
123
124 config = testlib.NormalizeConfigForComparison(config)
125 newConfig = testlib.NormalizeConfigForComparison(newConfig)
126 assert.Equal(t, config.Nftables, newConfig.Nftables)
127
128 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
129 defer cancel()
130
131 newConfig, err = nft.ApplyConfigEcho(ctx, config)
132 assert.NoError(t, err)
133
134 newConfig = testlib.NormalizeConfigForComparison(newConfig)
135 assert.Equal(t, config.Nftables, newConfig.Nftables)
136 }