| 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 | nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/ "github.com/networkplumbing/go-nft/nft/configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/" |
| 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 | func TestDefineEmptyConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/TestDefineEmptyConfig().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 33 | config := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().() |
| 34 | |
| 35 | expected := []byte(`{"nftables":[]}`) |
| 36 | serializedConfig, err := config.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().() |
| 37 | 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) |
| 38 | 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)) |
| 39 | } |
| 40 | |
| 41 | func TestReadEmptyConfigWithMetaInfoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/TestReadEmptyConfigWithMetaInfo().(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 | const version = "0.9.3" |
| 43 | const releaseName = "Topsy" |
| 44 | const schemaVersion = 1 |
| 45 | serializedConfig := []byte(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().( |
| 46 | `{"nftables":[{"metainfo":{"version":%q,"release_name":%q,"json_schema_version":%d}}]}`, |
| 47 | version, releaseName, schemaVersion, |
| 48 | )) |
| 49 | |
| 50 | config := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().() |
| 51 | 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, config.FromJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FromJSON().(serializedConfig)) |
| 52 | |
| 53 | expectedConfig := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().() |
| 54 | expectedConfig.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables. = append(expectedConfig.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables., schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Nftablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#{Metainfoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#Metainfo.: &schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Metainfoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Metainfo#{ |
| 55 | Versionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Metainfo#Version.: version, |
| 56 | ReleaseNamescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Metainfo#ReleaseName.: releaseName, |
| 57 | JsonSchemaVersionscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Metainfo#JsonSchemaVersion.: schemaVersion, |
| 58 | }}) |
| 59 | |
| 60 | 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, expectedConfig, config) |
| 61 | } |
| 62 | |
| 63 | func TestFlushRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config_test`/TestFlushRuleset().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 64 | config := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().() |
| 65 | config.FlushRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FlushRuleset().() |
| 66 | |
| 67 | expected := []byte(`{"nftables":[{"flush":{"ruleset":null}}]}`) |
| 68 | serializedConfig, err := config.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().() |
| 69 | 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) |
| 70 | 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)) |
| 71 | } |