|
| 1 | +package netpol |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + v1 "k8s.io/api/core/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func testNamePrefix(t *testing.T, testString string, isIPv6 bool) { |
| 12 | + if isIPv6 { |
| 13 | + assert.Truef(t, strings.HasPrefix(testString, "inet6:"), "%s is IPv6 and should begin with inet6:", testString) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func Test_policySourcePodIPSetName(t *testing.T) { |
| 18 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 19 | + setName := policySourcePodIPSetName("foo", "bar", v1.IPv4Protocol) |
| 20 | + testNamePrefix(t, setName, false) |
| 21 | + setName = policySourcePodIPSetName("foo", "bar", v1.IPv6Protocol) |
| 22 | + testNamePrefix(t, setName, true) |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func Test_policyDestinationPodIPSetName(t *testing.T) { |
| 27 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 28 | + setName := policyDestinationPodIPSetName("foo", "bar", v1.IPv4Protocol) |
| 29 | + testNamePrefix(t, setName, false) |
| 30 | + setName = policyDestinationPodIPSetName("foo", "bar", v1.IPv6Protocol) |
| 31 | + testNamePrefix(t, setName, true) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func Test_policyIndexedSourcePodIPSetName(t *testing.T) { |
| 36 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 37 | + setName := policyIndexedSourcePodIPSetName("foo", "bar", 1, v1.IPv4Protocol) |
| 38 | + testNamePrefix(t, setName, false) |
| 39 | + setName = policyIndexedSourcePodIPSetName("foo", "bar", 1, v1.IPv6Protocol) |
| 40 | + testNamePrefix(t, setName, true) |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func Test_policyIndexedDestinationPodIPSetName(t *testing.T) { |
| 45 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 46 | + setName := policyIndexedDestinationPodIPSetName("foo", "bar", 1, v1.IPv4Protocol) |
| 47 | + testNamePrefix(t, setName, false) |
| 48 | + setName = policyIndexedDestinationPodIPSetName("foo", "bar", 1, v1.IPv6Protocol) |
| 49 | + testNamePrefix(t, setName, true) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func Test_policyIndexedSourceIPBlockIPSetName(t *testing.T) { |
| 54 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 55 | + setName := policyIndexedSourceIPBlockIPSetName("foo", "bar", 1, v1.IPv4Protocol) |
| 56 | + testNamePrefix(t, setName, false) |
| 57 | + setName = policyIndexedSourceIPBlockIPSetName("foo", "bar", 1, v1.IPv6Protocol) |
| 58 | + testNamePrefix(t, setName, true) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func Test_policyIndexedDestinationIPBlockIPSetName(t *testing.T) { |
| 63 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 64 | + setName := policyIndexedDestinationIPBlockIPSetName("foo", "bar", 1, v1.IPv4Protocol) |
| 65 | + testNamePrefix(t, setName, false) |
| 66 | + setName = policyIndexedDestinationIPBlockIPSetName("foo", "bar", 1, v1.IPv6Protocol) |
| 67 | + testNamePrefix(t, setName, true) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func Test_policyIndexedIngressNamedPortIPSetName(t *testing.T) { |
| 72 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 73 | + setName := policyIndexedIngressNamedPortIPSetName("foo", "bar", 1, 1, v1.IPv4Protocol) |
| 74 | + testNamePrefix(t, setName, false) |
| 75 | + setName = policyIndexedIngressNamedPortIPSetName("foo", "bar", 1, 1, v1.IPv6Protocol) |
| 76 | + testNamePrefix(t, setName, true) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func Test_policyIndexedEgressNamedPortIPSetName(t *testing.T) { |
| 81 | + t.Run("Check IPv4 and IPv6 names are correct", func(t *testing.T) { |
| 82 | + setName := policyIndexedEgressNamedPortIPSetName("foo", "bar", 1, 1, v1.IPv4Protocol) |
| 83 | + testNamePrefix(t, setName, false) |
| 84 | + setName = policyIndexedEgressNamedPortIPSetName("foo", "bar", 1, 1, v1.IPv6Protocol) |
| 85 | + testNamePrefix(t, setName, true) |
| 86 | + }) |
| 87 | +} |
0 commit comments