|
1 | 1 | import os
|
2 | 2 | import sys
|
3 | 3 | import pytest
|
| 4 | +import mock |
| 5 | + |
4 | 6 | from click.testing import CliRunner
|
5 | 7 | from utilities_common.db import Db
|
6 | 8 |
|
7 | 9 | import show.main as show
|
8 |
| -import mock_tables.dbconnector |
| 10 | +import config.main as config |
| 11 | + |
| 12 | +config.asic_type = mock.MagicMock(return_value = "broadcom") |
9 | 13 |
|
10 | 14 | # Expected output for 'show sflow'
|
11 | 15 | show_sflow_output = ''+ \
|
12 | 16 | """
|
13 | 17 | sFlow Global Information:
|
14 | 18 | sFlow Admin State: up
|
15 | 19 | sFlow Polling Interval: 0
|
16 |
| - sFlow AgentID: eth0 |
| 20 | + sFlow AgentID: default |
17 | 21 |
|
18 | 22 | 2 Collectors configured:
|
19 | 23 | Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343
|
@@ -46,17 +50,219 @@ def setup_class(cls):
|
46 | 50 | def test_show_sflow(self):
|
47 | 51 | runner = CliRunner()
|
48 | 52 | result = runner.invoke(show.cli.commands["sflow"], [], obj=Db())
|
49 |
| - print(sys.stderr, result.output) |
| 53 | + print(result.exit_code, result.output) |
50 | 54 | assert result.exit_code == 0
|
51 | 55 | assert result.output == show_sflow_output
|
52 | 56 |
|
53 | 57 | def test_show_sflow_intf(self):
|
54 | 58 | runner = CliRunner()
|
55 |
| - result = runner.invoke(show.cli.commands["sflow"].commands["interface"], [], obj=Db()) |
56 |
| - print(sys.stderr, result.output) |
| 59 | + result = runner.invoke(show.cli.commands["sflow"].commands["interface"], \ |
| 60 | + [], obj=Db()) |
| 61 | + print(result.exit_code, result.output) |
57 | 62 | assert result.exit_code == 0
|
58 | 63 | assert result.output == show_sflow_intf_output
|
59 | 64 |
|
| 65 | + def test_config_sflow_disable_enable(self): |
| 66 | + # config sflow <enable|disable> |
| 67 | + db = Db() |
| 68 | + runner = CliRunner() |
| 69 | + obj = {'db':db.cfgdb} |
| 70 | + |
| 71 | + #disable |
| 72 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 73 | + commands["disable"], [], obj=obj) |
| 74 | + print(result.exit_code, result.output) |
| 75 | + assert result.exit_code == 0 |
| 76 | + |
| 77 | + # change the output |
| 78 | + global show_sflow_output |
| 79 | + show_sflow_output_local = show_sflow_output.replace(\ |
| 80 | + 'Admin State: up', \ |
| 81 | + 'Admin State: down') |
| 82 | + |
| 83 | + # run show and check |
| 84 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 85 | + print(result.exit_code, result.output, show_sflow_output_local) |
| 86 | + assert result.exit_code == 0 |
| 87 | + assert result.output == show_sflow_output_local |
| 88 | + |
| 89 | + #enable |
| 90 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 91 | + commands["enable"], [], obj=obj) |
| 92 | + print(result.exit_code, result.output) |
| 93 | + assert result.exit_code == 0 |
| 94 | + |
| 95 | + # run show and check |
| 96 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 97 | + print(result.exit_code, result.output) |
| 98 | + assert result.exit_code == 0 |
| 99 | + assert result.output == show_sflow_output |
| 100 | + |
| 101 | + return |
| 102 | + |
| 103 | + def test_config_sflow_agent_id(self): |
| 104 | + db = Db() |
| 105 | + runner = CliRunner() |
| 106 | + obj = {'db':db.cfgdb} |
| 107 | + |
| 108 | + # mock netifaces.interface |
| 109 | + config.netifaces.interfaces = mock.MagicMock(return_value = "Ethernet0") |
| 110 | + |
| 111 | + # set agent-id |
| 112 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 113 | + commands["agent-id"].commands["add"], ["Ethernet0"], obj=obj) |
| 114 | + print(result.exit_code, result.output) |
| 115 | + assert result.exit_code == 0 |
| 116 | + |
| 117 | + # change the output |
| 118 | + global show_sflow_output |
| 119 | + show_sflow_output_local = \ |
| 120 | + show_sflow_output.replace('default', 'Ethernet0') |
| 121 | + |
| 122 | + # run show and check |
| 123 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 124 | + print(result.exit_code, result.output, show_sflow_output_local) |
| 125 | + assert result.exit_code == 0 |
| 126 | + assert result.output == show_sflow_output_local |
| 127 | + |
| 128 | + #del agent id |
| 129 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 130 | + commands["agent-id"].commands["del"], [], obj=obj) |
| 131 | + print(result.exit_code, result.output) |
| 132 | + assert result.exit_code == 0 |
| 133 | + |
| 134 | + # run show and check |
| 135 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 136 | + print(result.exit_code, result.output) |
| 137 | + assert result.exit_code == 0 |
| 138 | + assert result.output == show_sflow_output |
| 139 | + |
| 140 | + return |
| 141 | + |
| 142 | + def test_config_sflow_collector(self): |
| 143 | + db = Db() |
| 144 | + runner = CliRunner() |
| 145 | + obj = {'db':db.cfgdb} |
| 146 | + |
| 147 | + # del a collector |
| 148 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 149 | + commands["collector"].commands["del"], ["prod"], obj=obj) |
| 150 | + print(result.exit_code, result.output) |
| 151 | + assert result.exit_code == 0 |
| 152 | + |
| 153 | + # change the output |
| 154 | + global show_sflow_output |
| 155 | + show_sflow_output_local = show_sflow_output.replace(\ |
| 156 | + "2 Collectors configured:\n\ |
| 157 | + Name: prod IP addr: fe80::6e82:6aff:fe1e:cd8e UDP port: 6343\n\ |
| 158 | + Name: ser5 IP addr: 172.21.35.15 UDP port: 6343", \ |
| 159 | + "1 Collectors configured:\n\ |
| 160 | + Name: ser5 IP addr: 172.21.35.15 UDP port: 6343") |
| 161 | + |
| 162 | + # run show and check |
| 163 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 164 | + print(result.exit_code, result.output, show_sflow_output_local) |
| 165 | + assert result.exit_code == 0 |
| 166 | + assert result.output == show_sflow_output_local |
| 167 | + |
| 168 | + # add collector |
| 169 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 170 | + commands["collector"].commands["add"], \ |
| 171 | + ["prod", "fe80::6e82:6aff:fe1e:cd8e"], obj=obj) |
| 172 | + assert result.exit_code == 0 |
| 173 | + |
| 174 | + # run show and check |
| 175 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 176 | + print(result.exit_code, result.output) |
| 177 | + assert result.exit_code == 0 |
| 178 | + assert result.output == show_sflow_output |
| 179 | + |
| 180 | + return |
| 181 | + |
| 182 | + def test_config_sflow_polling_interval(self): |
| 183 | + db = Db() |
| 184 | + runner = CliRunner() |
| 185 | + obj = {'db':db.cfgdb} |
| 186 | + |
| 187 | + # set to 20 |
| 188 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 189 | + commands["polling-interval"], ["20"], obj=obj) |
| 190 | + print(result.exit_code, result.output) |
| 191 | + assert result.exit_code == 0 |
| 192 | + |
| 193 | + # change the expected output |
| 194 | + global show_sflow_output |
| 195 | + show_sflow_output_local = show_sflow_output.replace(\ |
| 196 | + 'sFlow Polling Interval: 0', \ |
| 197 | + 'sFlow Polling Interval: 20') |
| 198 | + |
| 199 | + # run show and check |
| 200 | + result = runner.invoke(show.cli.commands["sflow"], [], obj=db) |
| 201 | + print(result.exit_code, result.output) |
| 202 | + assert result.exit_code == 0 |
| 203 | + assert result.output == show_sflow_output_local |
| 204 | + |
| 205 | + #reset to 0, no need to verify this one |
| 206 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 207 | + commands["polling-interval"], ["0"], obj=obj) |
| 208 | + print(result.exit_code, result.output) |
| 209 | + assert result.exit_code == 0 |
| 210 | + |
| 211 | + return |
| 212 | + |
| 213 | + def test_config_sflow_intf_enable_disable(self): |
| 214 | + db = Db() |
| 215 | + runner = CliRunner() |
| 216 | + obj = {'db':db.cfgdb} |
| 217 | + |
| 218 | + # mock interface_name_is_valid |
| 219 | + config.interface_name_is_valid = mock.MagicMock(return_value = True) |
| 220 | + |
| 221 | + # intf enable |
| 222 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 223 | + commands["interface"].commands["enable"], ["Ethernet1"], obj=obj) |
| 224 | + print(result.exit_code, result.output) |
| 225 | + assert result.exit_code == 0 |
| 226 | + |
| 227 | + # we can not use 'show sflow interface', becasue 'show sflow interface' |
| 228 | + # gets data from appDB, we need to fetch data from configDB for verification |
| 229 | + sflowSession = db.cfgdb.get_table('SFLOW_SESSION') |
| 230 | + assert sflowSession["Ethernet1"]["admin_state"] == "up" |
| 231 | + |
| 232 | + # intf disable |
| 233 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 234 | + commands["interface"].commands["disable"], ["Ethernet1"], obj=obj) |
| 235 | + print(result.exit_code, result.output) |
| 236 | + assert result.exit_code == 0 |
| 237 | + |
| 238 | + # verify in configDb |
| 239 | + sflowSession = db.cfgdb.get_table('SFLOW_SESSION') |
| 240 | + assert sflowSession["Ethernet1"]["admin_state"] == "down" |
| 241 | + |
| 242 | + return |
| 243 | + |
| 244 | + def test_config_sflow_intf_sample_rate(self): |
| 245 | + db = Db() |
| 246 | + runner = CliRunner() |
| 247 | + obj = {'db':db.cfgdb} |
| 248 | + |
| 249 | + # mock interface_name_is_valid |
| 250 | + config.interface_name_is_valid = mock.MagicMock(return_value = True) |
| 251 | + |
| 252 | + # set sample-rate to 2500 |
| 253 | + result = runner.invoke(config.config.commands["sflow"].\ |
| 254 | + commands["interface"].commands["sample-rate"], \ |
| 255 | + ["Ethernet2", "2500"], obj=obj) |
| 256 | + print(result.exit_code, result.output) |
| 257 | + assert result.exit_code == 0 |
| 258 | + |
| 259 | + # we can not use 'show sflow interface', becasue 'show sflow interface' |
| 260 | + # gets data from appDB, we need to fetch data from configDB for verification |
| 261 | + sflowSession = db.cfgdb.get_table('SFLOW_SESSION') |
| 262 | + assert sflowSession["Ethernet2"]["sample_rate"] == "2500" |
| 263 | + |
| 264 | + return |
| 265 | + |
60 | 266 | @classmethod
|
61 | 267 | def teardown_class(cls):
|
62 | 268 | print("TEARDOWN")
|
|
0 commit comments