|
| 1 | +import glob |
| 2 | +import json |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | +from collections import defaultdict |
| 10 | + |
| 11 | +""" |
| 12 | + Filter FDB entries test vector |
| 13 | +""" |
| 14 | +filterFdbEntriesTestVector = [ |
| 15 | + { |
| 16 | + "arp":[ |
| 17 | + ], |
| 18 | + "fdb": [ |
| 19 | + ], |
| 20 | + "expected_fdb": [ |
| 21 | + ], |
| 22 | + }, |
| 23 | + { |
| 24 | + "arp":[ |
| 25 | + { |
| 26 | + "NEIGH_TABLE:Vlan1000:192.168.0.10": { |
| 27 | + "neigh": "72:06:00:01:00:08", |
| 28 | + "family": "IPv4" |
| 29 | + }, |
| 30 | + "OP": "SET" |
| 31 | + }, |
| 32 | + ], |
| 33 | + "fdb": [ |
| 34 | + { |
| 35 | + "FDB_TABLE:Vlan1000:72-06-00-01-01-16": { |
| 36 | + "type": "dynamic", |
| 37 | + "port": "Ethernet22" |
| 38 | + }, |
| 39 | + "OP": "SET" |
| 40 | + }, |
| 41 | + ], |
| 42 | + "expected_fdb": [ |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + "arp":[ |
| 47 | + { |
| 48 | + "NEIGH_TABLE:Vlan1000:192.168.0.10": { |
| 49 | + "neigh": "72:06:00:01:01:16", |
| 50 | + "family": "IPv4" |
| 51 | + }, |
| 52 | + "OP": "SET" |
| 53 | + }, |
| 54 | + ], |
| 55 | + "fdb": [ |
| 56 | + { |
| 57 | + "FDB_TABLE:Vlan1000:72-06-00-01-01-16": { |
| 58 | + "type": "dynamic", |
| 59 | + "port": "Ethernet22" |
| 60 | + }, |
| 61 | + "OP": "SET" |
| 62 | + }, |
| 63 | + ], |
| 64 | + "expected_fdb": [ |
| 65 | + { |
| 66 | + "FDB_TABLE:Vlan1000:72-06-00-01-01-16": { |
| 67 | + "type": "dynamic", |
| 68 | + "port": "Ethernet22" |
| 69 | + }, |
| 70 | + "OP": "SET" |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + "arp": "sonic-utilities-tests/filter_fdb_input/arp.json", |
| 76 | + "fdb": "sonic-utilities-tests/filter_fdb_input/fdb.json", |
| 77 | + "expected_fdb": "sonic-utilities-tests/filter_fdb_input/expected_fdb.json" |
| 78 | + }, |
| 79 | +] |
| 80 | + |
| 81 | +class TestFilterFdbEntries(object): |
| 82 | + """ |
| 83 | + Test Filter FDb entries |
| 84 | + """ |
| 85 | + ARP_FILENAME = "/tmp/arp.json" |
| 86 | + FDB_FILENAME = "/tmp/fdb.json" |
| 87 | + EXPECTED_FDB_FILENAME = "/tmp/expected_fdb.json" |
| 88 | + |
| 89 | + def __setUp(self, testData): |
| 90 | + """ |
| 91 | + Sets up test data |
| 92 | +
|
| 93 | + Builds arp.json and fdb.json input files to /tmp and also build expected fdb entries files int /tmp |
| 94 | +
|
| 95 | + Args: |
| 96 | + testData(dist): Current test vector data |
| 97 | +
|
| 98 | + Returns: |
| 99 | + None |
| 100 | + """ |
| 101 | + def create_file_or_raise(data, filename): |
| 102 | + """ |
| 103 | + Create test data files |
| 104 | +
|
| 105 | + If the data is string, it will be dump to a json filename. |
| 106 | + If data is a file, it will be coppied to filename |
| 107 | +
|
| 108 | + Args: |
| 109 | + data(str|list): source of test data |
| 110 | + filename(str): filename for test data |
| 111 | +
|
| 112 | + Returns: |
| 113 | + None |
| 114 | +
|
| 115 | + Raises: |
| 116 | + Exception if data type is not supported |
| 117 | + """ |
| 118 | + if isinstance(data, list): |
| 119 | + with open(filename, 'w') as fp: |
| 120 | + json.dump(data, fp, indent=2, separators=(',', ': ')) |
| 121 | + elif isinstance(data, str): |
| 122 | + shutil.copyfile(data, filename) |
| 123 | + else: |
| 124 | + raise Exception("Unknown test data type: {0}".format(type(test_data))) |
| 125 | + |
| 126 | + create_file_or_raise(testData["arp"], self.ARP_FILENAME) |
| 127 | + create_file_or_raise(testData["fdb"], self.FDB_FILENAME) |
| 128 | + create_file_or_raise(testData["expected_fdb"], self.EXPECTED_FDB_FILENAME) |
| 129 | + |
| 130 | + def __tearDown(self): |
| 131 | + """ |
| 132 | + Tear down current test case setup |
| 133 | +
|
| 134 | + Args: |
| 135 | + None |
| 136 | +
|
| 137 | + Returns: |
| 138 | + None |
| 139 | + """ |
| 140 | + os.remove(self.ARP_FILENAME) |
| 141 | + os.remove(self.EXPECTED_FDB_FILENAME) |
| 142 | + fdbFiles = glob.glob(self.FDB_FILENAME + '*') |
| 143 | + for file in fdbFiles: |
| 144 | + os.remove(file) |
| 145 | + |
| 146 | + def __runCommand(self, cmds): |
| 147 | + """ |
| 148 | + Runs command 'cmds' on host |
| 149 | +
|
| 150 | + Args: |
| 151 | + cmds(list): command to be run on localhost |
| 152 | +
|
| 153 | + Returns: |
| 154 | + stdout(str): stdout gathered during command execution |
| 155 | + stderr(str): stderr gathered during command execution |
| 156 | + returncode(int): command exit code |
| 157 | + """ |
| 158 | + process = subprocess.Popen( |
| 159 | + cmds, |
| 160 | + shell=False, |
| 161 | + stdout=subprocess.PIPE, |
| 162 | + stderr=subprocess.PIPE |
| 163 | + ) |
| 164 | + stdout, stderr = process.communicate() |
| 165 | + |
| 166 | + return stdout, stderr, process.returncode |
| 167 | + |
| 168 | + def __getFdbEntriesMap(self, filename): |
| 169 | + """ |
| 170 | + Generate map for FDB entries |
| 171 | +
|
| 172 | + FDB entry map is using the FDB_TABLE:... as a key for the FDB entry. |
| 173 | +
|
| 174 | + Args: |
| 175 | + filename(str): FDB entry file name |
| 176 | +
|
| 177 | + Returns: |
| 178 | + fdbMap(defaultdict) map of FDB entries using MAC as key. |
| 179 | + """ |
| 180 | + with open(filename, 'r') as fp: |
| 181 | + fdbEntries = json.load(fp) |
| 182 | + |
| 183 | + fdbMap = defaultdict() |
| 184 | + for fdb in fdbEntries: |
| 185 | + for key, config in fdb.items(): |
| 186 | + if "FDB_TABLE" in key: |
| 187 | + fdbMap[key] = fdb |
| 188 | + |
| 189 | + return fdbMap |
| 190 | + |
| 191 | + def __verifyOutput(self): |
| 192 | + """ |
| 193 | + Verifies FDB entries match expected FDB entries |
| 194 | +
|
| 195 | + Args: |
| 196 | + None |
| 197 | +
|
| 198 | + Retruns: |
| 199 | + isEqual(bool): True if FDB entries match, False otherwise |
| 200 | + """ |
| 201 | + fdbMap = self.__getFdbEntriesMap(self.FDB_FILENAME) |
| 202 | + with open(self.EXPECTED_FDB_FILENAME, 'r') as fp: |
| 203 | + expectedFdbEntries = json.load(fp) |
| 204 | + |
| 205 | + isEqual = len(fdbMap) == len(expectedFdbEntries) |
| 206 | + if isEqual: |
| 207 | + for expectedFdbEntry in expectedFdbEntries: |
| 208 | + fdbEntry = {} |
| 209 | + for key, config in expectedFdbEntry.items(): |
| 210 | + if "FDB_TABLE" in key: |
| 211 | + fdbEntry = fdbMap[key] |
| 212 | + |
| 213 | + isEqual = len(fdbEntry) == len(expectedFdbEntry) |
| 214 | + for key, config in expectedFdbEntry.items(): |
| 215 | + isEqual = isEqual and fdbEntry[key] == config |
| 216 | + |
| 217 | + if not isEqual: |
| 218 | + break |
| 219 | + |
| 220 | + return isEqual |
| 221 | + |
| 222 | + @pytest.mark.parametrize("testData", filterFdbEntriesTestVector) |
| 223 | + def testFilterFdbEntries(self, testData): |
| 224 | + """ |
| 225 | + Test Filter FDB entries script |
| 226 | +
|
| 227 | + Args: |
| 228 | + testData(dict): Map containing ARP entries, FDB entries, and expected FDB entries |
| 229 | + """ |
| 230 | + try: |
| 231 | + self.__setUp(testData) |
| 232 | + |
| 233 | + stdout, stderr, rc = self.__runCommand([ |
| 234 | + "scripts/filter_fdb_entries.py", |
| 235 | + "-a", |
| 236 | + self.ARP_FILENAME, |
| 237 | + "-f", |
| 238 | + self.FDB_FILENAME, |
| 239 | + ]) |
| 240 | + assert rc == 0, "CFilter_fbd_entries.py failed with '{0}'".format(stderr) |
| 241 | + assert self.__verifyOutput(), "Test failed for test data: {0}".format(testData) |
| 242 | + finally: |
| 243 | + self.__tearDown() |
0 commit comments