|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 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 | + |
| 17 | +package wrappers |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestBuildSourceWithWrappers(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + cfg *Config |
| 31 | + asserts func(*testing.T, *Config) |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "configuration with target filter wrapper", |
| 35 | + cfg: NewConfig( |
| 36 | + WithTargetNetFilter([]string{"10.0.0.0/8"}), |
| 37 | + ), |
| 38 | + asserts: func(t *testing.T, cfg *Config) { |
| 39 | + assert.True(t, cfg.isSourceWrapperInstrumented("target-filter")) |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "configuration with nat64 networks", |
| 44 | + cfg: NewConfig( |
| 45 | + WithNAT64Networks([]string{"2001:db8::/96"}), |
| 46 | + ), |
| 47 | + asserts: func(t *testing.T, cfg *Config) { |
| 48 | + assert.True(t, cfg.isSourceWrapperInstrumented("nat64")) |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "default configuration", |
| 53 | + cfg: NewConfig(), |
| 54 | + asserts: func(t *testing.T, cfg *Config) { |
| 55 | + assert.True(t, cfg.isSourceWrapperInstrumented("dedup")) |
| 56 | + assert.False(t, cfg.isSourceWrapperInstrumented("nat64")) |
| 57 | + assert.False(t, cfg.isSourceWrapperInstrumented("target-filter")) |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "with TTL and NAT64", |
| 62 | + cfg: NewConfig( |
| 63 | + WithMinTTL(300), |
| 64 | + WithNAT64Networks([]string{"2001:db8::/96"}), |
| 65 | + ), |
| 66 | + asserts: func(t *testing.T, cfg *Config) { |
| 67 | + assert.True(t, cfg.isSourceWrapperInstrumented("dedup")) |
| 68 | + assert.True(t, cfg.isSourceWrapperInstrumented("nat64")) |
| 69 | + assert.True(t, cfg.isSourceWrapperInstrumented("post-processor")) |
| 70 | + assert.False(t, cfg.isSourceWrapperInstrumented("target-filter")) |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + _, err := WrapSources(nil, tt.cfg) |
| 78 | + require.NoError(t, err) |
| 79 | + tt.asserts(t, tt.cfg) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestWrapSources_NAT64Error(t *testing.T) { |
| 85 | + cfg := NewConfig(WithNAT64Networks([]string{"badnet"})) |
| 86 | + src, err := WrapSources(nil, cfg) |
| 87 | + assert.Nil(t, src) |
| 88 | + assert.Error(t, err) |
| 89 | + assert.Contains(t, err.Error(), "failed to create NAT64 source wrapper") |
| 90 | +} |
| 91 | + |
| 92 | +func TestWithDefaultTargets(t *testing.T) { |
| 93 | + cfg := &Config{} |
| 94 | + opt := WithDefaultTargets([]string{"1.2.3.4"}) |
| 95 | + opt(cfg) |
| 96 | + assert.Equal(t, []string{"1.2.3.4"}, cfg.defaultTargets) |
| 97 | +} |
| 98 | + |
| 99 | +func TestWithForceDefaultTargets(t *testing.T) { |
| 100 | + cfg := &Config{} |
| 101 | + opt := WithForceDefaultTargets(true) |
| 102 | + opt(cfg) |
| 103 | + assert.True(t, cfg.forceDefaultTargets) |
| 104 | +} |
| 105 | + |
| 106 | +func TestWithNAT64Networks(t *testing.T) { |
| 107 | + cfg := &Config{} |
| 108 | + opt := WithNAT64Networks([]string{"2001:db8::/96"}) |
| 109 | + opt(cfg) |
| 110 | + assert.Equal(t, []string{"2001:db8::/96"}, cfg.nat64Networks) |
| 111 | +} |
| 112 | + |
| 113 | +func TestWithTargetNetFilter(t *testing.T) { |
| 114 | + cfg := &Config{} |
| 115 | + opt := WithTargetNetFilter([]string{"10.0.0.0/8"}) |
| 116 | + opt(cfg) |
| 117 | + assert.Equal(t, []string{"10.0.0.0/8"}, cfg.targetNetFilter) |
| 118 | +} |
| 119 | + |
| 120 | +func TestWithExcludeTargetNets(t *testing.T) { |
| 121 | + cfg := &Config{} |
| 122 | + opt := WithExcludeTargetNets([]string{"192.168.0.0/16"}) |
| 123 | + opt(cfg) |
| 124 | + assert.Equal(t, []string{"192.168.0.0/16"}, cfg.excludeTargetNets) |
| 125 | +} |
| 126 | + |
| 127 | +func TestWithMinTTL(t *testing.T) { |
| 128 | + cfg := &Config{} |
| 129 | + opt := WithMinTTL(300 * time.Second) |
| 130 | + opt(cfg) |
| 131 | + assert.Equal(t, 300*time.Second, cfg.minTTL) |
| 132 | +} |
| 133 | + |
| 134 | +func TestAddSourceWrapperAndIsSourceWrapperInstrumented(t *testing.T) { |
| 135 | + cfg := &Config{} |
| 136 | + assert.False(t, cfg.isSourceWrapperInstrumented("dedup")) |
| 137 | + cfg.addSourceWrapper("dedup") |
| 138 | + assert.True(t, cfg.isSourceWrapperInstrumented("dedup")) |
| 139 | + cfg.addSourceWrapper("nat64") |
| 140 | + assert.True(t, cfg.isSourceWrapperInstrumented("nat64")) |
| 141 | + assert.False(t, cfg.isSourceWrapperInstrumented("target-filter")) |
| 142 | +} |
0 commit comments