|
1 | 1 | package middlewares |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "reflect" |
5 | 6 | "testing" |
6 | 7 |
|
| 8 | + "github.com/Azure/azure-container-networking/cni" |
7 | 9 | "github.com/Azure/azure-container-networking/cns" |
8 | 10 | "github.com/Azure/azure-container-networking/cns/middlewares/mock" |
9 | 11 | "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1" |
| 12 | + "github.com/stretchr/testify/require" |
10 | 13 | "gotest.tools/v3/assert" |
11 | 14 | ) |
12 | 15 |
|
@@ -101,4 +104,71 @@ func TestAddDefaultRoute(t *testing.T) { |
101 | 104 | } |
102 | 105 | } |
103 | 106 |
|
104 | | -//Add a test here that checks for equality between the default deny policies (in json) created by the function vs an what we expect the default deny json to look like |
| 107 | +func TestAddDefaultDenyACL(t *testing.T) { |
| 108 | + valueOut := []byte(`{ |
| 109 | + "Type": "ACL", |
| 110 | + "Action": "Block", |
| 111 | + "Direction": "Out", |
| 112 | + "Priority": 10000 |
| 113 | + }`) |
| 114 | + |
| 115 | + valueIn := []byte(`{ |
| 116 | + "Type": "ACL", |
| 117 | + "Action": "Block", |
| 118 | + "Direction": "In", |
| 119 | + "Priority": 10000 |
| 120 | + }`) |
| 121 | + |
| 122 | + expectedDefaultDenyACL := []cni.KVPair{ |
| 123 | + { |
| 124 | + Name: "EndpointPolicy", |
| 125 | + Value: valueOut, |
| 126 | + }, |
| 127 | + { |
| 128 | + Name: "EndpointPolicy", |
| 129 | + Value: valueIn, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + podIPInfo := cns.PodIpInfo{ |
| 134 | + PodIPConfig: cns.IPSubnet{ |
| 135 | + IPAddress: "20.240.1.242", |
| 136 | + PrefixLength: 32, |
| 137 | + }, |
| 138 | + NICType: cns.DelegatedVMNIC, |
| 139 | + MacAddress: "12:34:56:78:9a:bc", |
| 140 | + } |
| 141 | + |
| 142 | + addDefaultDenyACL(&podIPInfo) |
| 143 | + |
| 144 | + // Normalize both slices so there is no extra spacing, new lines, etc |
| 145 | + normalizedExpected := normalizeKVPairs(t, expectedDefaultDenyACL) |
| 146 | + normalizedActual := normalizeKVPairs(t, podIPInfo.DefaultDenyACL) |
| 147 | + if !reflect.DeepEqual(normalizedExpected, normalizedActual) { |
| 148 | + t.Errorf("got '%+v', expected '%+v'", podIPInfo.DefaultDenyACL, expectedDefaultDenyACL) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// normalizeKVPairs normalizes the JSON values in the KV pairs by unmarshaling them into a map, then marshaling them back to compact JSON to remove any extra space, new lines, etc |
| 153 | +func normalizeKVPairs(t *testing.T, kvPairs []cni.KVPair) []cni.KVPair { |
| 154 | + normalized := make([]cni.KVPair, len(kvPairs)) |
| 155 | + |
| 156 | + for i, kv := range kvPairs { |
| 157 | + var unmarshaledValue map[string]interface{} |
| 158 | + // Unmarshal the Value into a map |
| 159 | + err := json.Unmarshal(kv.Value, &unmarshaledValue) |
| 160 | + require.NoError(t, err, "Failed to unmarshal JSON value") |
| 161 | + |
| 162 | + // Marshal it back to compact JSON |
| 163 | + normalizedValue, err := json.Marshal(unmarshaledValue) |
| 164 | + require.NoError(t, err, "Failed to re-marshal JSON value") |
| 165 | + |
| 166 | + // Replace Value with the normalized compact JSON |
| 167 | + normalized[i] = cni.KVPair{ |
| 168 | + Name: kv.Name, |
| 169 | + Value: normalizedValue, |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return normalized |
| 174 | +} |
0 commit comments