| 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 | nftconfig "github.com/networkplumbing/go-nft/nft/config" |
| 29 | "github.com/networkplumbing/go-nft/nft/schema" |
| 30 | ) |
| 31 | |
| 32 | func TestDefineEmptyConfig(t *testing.T) { |
| 33 | config := nftconfig.New() |
| 34 | |
| 35 | expected := []byte(`{"nftables":[]}`) |
| 36 | serializedConfig, err := config.ToJSON() |
| 37 | assert.NoError(t, err) |
| 38 | assert.Equal(t, string(expected), string(serializedConfig)) |
| 39 | } |
| 40 | |
| 41 | func TestReadEmptyConfigWithMetaInfo(t *testing.T) { |
| 42 | const version = "0.9.3" |
| 43 | const releaseName = "Topsy" |
| 44 | const schemaVersion = 1 |
| 45 | serializedConfig := []byte(fmt.Sprintf( |
| 46 | `{"nftables":[{"metainfo":{"version":%q,"release_name":%q,"json_schema_version":%d}}]}`, |
| 47 | version, releaseName, schemaVersion, |
| 48 | )) |
| 49 | |
| 50 | config := nftconfig.New() |
| 51 | assert.NoError(t, config.FromJSON(serializedConfig)) |
| 52 | |
| 53 | expectedConfig := nftconfig.New() |
| 54 | expectedConfig.Nftables = append(expectedConfig.Nftables, schema.Nftable{Metainfo: &schema.Metainfo{ |
| 55 | Version: version, |
| 56 | ReleaseName: releaseName, |
| 57 | JsonSchemaVersion: schemaVersion, |
| 58 | }}) |
| 59 | |
| 60 | assert.Equal(t, expectedConfig, config) |
| 61 | } |
| 62 | |
| 63 | func TestFlushRuleset(t *testing.T) { |
| 64 | config := nftconfig.New() |
| 65 | config.FlushRuleset() |
| 66 | |
| 67 | expected := []byte(`{"nftables":[{"flush":{"ruleset":null}}]}`) |
| 68 | serializedConfig, err := config.ToJSON() |
| 69 | assert.NoError(t, err) |
| 70 | assert.Equal(t, string(expected), string(serializedConfig)) |
| 71 | } |