cisco_node_utils 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +293 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CONTRIBUTING.md +31 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +201 -0
  9. data/README.md +113 -0
  10. data/Rakefile +4 -0
  11. data/cisco_node_utils.gemspec +30 -0
  12. data/lib/cisco_node_utils.rb +33 -0
  13. data/lib/cisco_node_utils/README_YAML.md +333 -0
  14. data/lib/cisco_node_utils/cisco_cmn_utils.rb +92 -0
  15. data/lib/cisco_node_utils/command_reference.rb +415 -0
  16. data/lib/cisco_node_utils/command_reference_common.yaml +845 -0
  17. data/lib/cisco_node_utils/command_reference_n3064.yaml +13 -0
  18. data/lib/cisco_node_utils/command_reference_n7k.yaml +48 -0
  19. data/lib/cisco_node_utils/command_reference_n9k.yaml +35 -0
  20. data/lib/cisco_node_utils/configparser_lib.rb +196 -0
  21. data/lib/cisco_node_utils/interface.rb +501 -0
  22. data/lib/cisco_node_utils/interface_ospf.rb +241 -0
  23. data/lib/cisco_node_utils/node.rb +673 -0
  24. data/lib/cisco_node_utils/platform.rb +184 -0
  25. data/lib/cisco_node_utils/platform_info.rb +58 -0
  26. data/lib/cisco_node_utils/platform_info.yaml +10 -0
  27. data/lib/cisco_node_utils/router_ospf.rb +96 -0
  28. data/lib/cisco_node_utils/router_ospf_vrf.rb +258 -0
  29. data/lib/cisco_node_utils/snmpcommunity.rb +91 -0
  30. data/lib/cisco_node_utils/snmpgroup.rb +55 -0
  31. data/lib/cisco_node_utils/snmpserver.rb +150 -0
  32. data/lib/cisco_node_utils/snmpuser.rb +342 -0
  33. data/lib/cisco_node_utils/tacacs_server.rb +175 -0
  34. data/lib/cisco_node_utils/tacacs_server_host.rb +128 -0
  35. data/lib/cisco_node_utils/version.rb +17 -0
  36. data/lib/cisco_node_utils/vlan.rb +153 -0
  37. data/lib/cisco_node_utils/vtp.rb +127 -0
  38. data/lib/cisco_node_utils/yum.rb +84 -0
  39. data/tests/basetest.rb +93 -0
  40. data/tests/ciscotest.rb +136 -0
  41. data/tests/cmd_config.yaml +51 -0
  42. data/tests/cmd_config_invalid.yaml +16 -0
  43. data/tests/test_all_cisco.rb +46 -0
  44. data/tests/test_command_config.rb +192 -0
  45. data/tests/test_command_reference.rb +222 -0
  46. data/tests/test_interface.rb +1017 -0
  47. data/tests/test_interface_ospf.rb +763 -0
  48. data/tests/test_interface_svi.rb +267 -0
  49. data/tests/test_interface_switchport.rb +722 -0
  50. data/tests/test_node.rb +108 -0
  51. data/tests/test_node_ext.rb +450 -0
  52. data/tests/test_platform.rb +188 -0
  53. data/tests/test_router_ospf.rb +164 -0
  54. data/tests/test_router_ospf_vrf.rb +753 -0
  55. data/tests/test_snmpcommunity.rb +344 -0
  56. data/tests/test_snmpgroup.rb +71 -0
  57. data/tests/test_snmpserver.rb +443 -0
  58. data/tests/test_snmpuser.rb +803 -0
  59. data/tests/test_tacacs_server.rb +388 -0
  60. data/tests/test_tacacs_server_host.rb +391 -0
  61. data/tests/test_vlan.rb +264 -0
  62. data/tests/test_vtp.rb +319 -0
  63. data/tests/test_yum.rb +106 -0
  64. metadata +188 -0
@@ -0,0 +1,388 @@
1
+ # Copyright (c) 2014-2015 Cisco and/or its affiliates.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require File.expand_path("../ciscotest", __FILE__)
16
+ require File.expand_path("../../lib/cisco_node_utils/tacacs_server", __FILE__)
17
+
18
+ class TestTacacsServer < CiscoTestCase
19
+ def get_tacacsserver_feature
20
+ s = @device.cmd("show run all | no-more")
21
+ cmd = "feature tacacs+"
22
+ line = /#{cmd}/.match(s)
23
+ end
24
+
25
+ # Helper routine to get the tacacs config. Ideally we should be able
26
+ # to use 'sh run tacacs all' but that does not work for 'directed-request'
27
+ # why 'sh run aaa all' is used.
28
+ def get_tacacsserver_match_line(name)
29
+ s = @device.cmd("show run tacacs all | no-more ; show run aaa all | no-more")
30
+ cmd = "tacacs-server"
31
+ pattern = (/#{cmd} #{name}/)
32
+ line = pattern.match(s)
33
+ end
34
+
35
+ def get_match_line(name)
36
+ s = @device.cmd("show run all | no-more")
37
+ line = /#{name}/.match(s)
38
+ end
39
+
40
+ def test_tacacsserver_create_valid
41
+ tacacs = TacacsServer.new
42
+ line = get_tacacsserver_feature
43
+ refute_nil(line, "Error: Tacacs feature not set")
44
+ tacacs.destroy
45
+ end
46
+
47
+ def test_tacacsserver_get_encryption_type
48
+ s = @device.cmd("conf t ; no feature tacacs+ ; feature tacacs+ ; end")
49
+ node.cache_flush
50
+ encryption_type = TACACS_SERVER_ENC_UNKNOWN
51
+ # Get encryption password when not configured
52
+ tacacs = TacacsServer.new
53
+ assert_equal(encryption_type,
54
+ tacacs.encryption_type,
55
+ "Error: Tacacs Server, encryption type incorrect")
56
+ tacacs.destroy
57
+
58
+ # Get encryption password when configured
59
+ encryption_type = TACACS_SERVER_ENC_NONE
60
+ # This one is needed since the 'sh run' will always display the type
61
+ # differently than the used encryption config type.
62
+ sh_run_encryption_type = TACACS_SERVER_ENC_CISCO_TYPE_7
63
+ s = @device.cmd("configure terminal")
64
+ s = @device.cmd("feature tacacs+")
65
+ s = @device.cmd("tacacs-server key #{encryption_type} TEST")
66
+ s = @device.cmd("end")
67
+ node.cache_flush
68
+
69
+ tacacs = TacacsServer.new
70
+ assert_equal(sh_run_encryption_type,
71
+ tacacs.encryption_type,
72
+ "Error: Tacacs Server, encryption type incorrect")
73
+
74
+ encryption_type = TACACS_SERVER_ENC_CISCO_TYPE_7
75
+ s = @device.cmd("configure terminal")
76
+ s = @device.cmd("tacacs-server key #{encryption_type} TEST")
77
+ s = @device.cmd("end")
78
+ node.cache_flush
79
+
80
+ assert_equal(sh_run_encryption_type,
81
+ tacacs.encryption_type,
82
+ "Error: Tacacs Server, encryption type incorrect")
83
+ tacacs.destroy
84
+ end
85
+
86
+ def test_tacacsserver_get_default_encryption
87
+ # Ruby can use defines, but only they're not initialized from an enum
88
+ assert_equal(TACACS_SERVER_ENC_NONE,
89
+ TacacsServer.default_encryption_type,
90
+ "Error: Tacacs Server, default encryption incorrect")
91
+ end
92
+
93
+ def test_tacacsserver_get_encryption_password
94
+ # Get encryption password when not configured
95
+ s = @device.cmd("conf t ; no feature tacacs+ ; end")
96
+ node.cache_flush
97
+ tacacs = TacacsServer.new
98
+ assert_equal(node.config_get_default("tacacs_server", "encryption_password"),
99
+ tacacs.encryption_password,
100
+ "Error: Tacacs Server, encryption password incorrect")
101
+ tacacs.destroy
102
+
103
+ # Get encryption password when configured
104
+ sh_run_encryption_password = "WAWY"
105
+ encryption_type = TACACS_SERVER_ENC_NONE
106
+ # This one is needed since the 'sh run' will always display the password
107
+ # differently than the used encryption config type.
108
+ s = @device.cmd("configure terminal")
109
+ s = @device.cmd("feature tacacs+")
110
+ s = @device.cmd("tacacs-server key #{encryption_type} TEST")
111
+ s = @device.cmd("end")
112
+ # Flush the cache since we've modified the device
113
+ node.cache_flush
114
+ tacacs = TacacsServer.new
115
+ assert_equal(sh_run_encryption_password,
116
+ tacacs.encryption_password,
117
+ "Error: Tacacs Server, encryption password incorrect")
118
+ tacacs.destroy
119
+ end
120
+
121
+ def test_tacacsserver_get_default_encryption_password
122
+ assert_equal(node.config_get_default("tacacs_server", "encryption_password"),
123
+ TacacsServer.default_encryption_password,
124
+ "Error: Tacacs Server, default encryption password incorrect")
125
+ end
126
+
127
+ def test_tacacsserver_key_set
128
+ enc_type = TACACS_SERVER_ENC_NONE
129
+ # This one is needed since the 'sh run' will always display the type
130
+ # differently than the used encryption config type.
131
+ sh_run_encryption_type = TACACS_SERVER_ENC_CISCO_TYPE_7
132
+ password = "TEST_NEW"
133
+
134
+ tacacs = TacacsServer.new
135
+ tacacs.encryption_key_set(enc_type, password)
136
+ # Get the password from the running config since its encoded
137
+ line = get_tacacsserver_match_line("key\s#{sh_run_encryption_type}\s\".*\"")
138
+ refute_nil(line, "Error: Tacacs Server, key not configured")
139
+ # Extract encrypted password, and git rid of the "" around the pasword
140
+ md = line.to_s
141
+ encrypted_password = md.to_s.split(" ").last.tr('\"', '')
142
+ # Extract encryption type
143
+ md = /tacacs-server\skey\s\d/.match(line.to_s)
144
+ encrypted_type = md.to_s.split(" ").last.to_i
145
+ assert_equal(encrypted_type, tacacs.encryption_type,
146
+ "Error: Tacacs Server, encryption type incorrect")
147
+ assert_equal(encrypted_password, tacacs.encryption_password,
148
+ "Error: Tacacs Server, encryption password incorrect")
149
+ tacacs.destroy
150
+ end
151
+
152
+ def test_tacacsserver_key_unconfigure
153
+ s = @device.cmd("conf t ; no feature tacacs+ ; end")
154
+ node.cache_flush
155
+ enc_type = TACACS_SERVER_ENC_NONE
156
+ # This one is needed since the 'sh run' will always display the type
157
+ # differently than the used encryption config type.
158
+ sh_run_encryption_type = TACACS_SERVER_ENC_CISCO_TYPE_7
159
+ password = "TEST_NEW"
160
+
161
+ tacacs = TacacsServer.new
162
+ tacacs.encryption_key_set(enc_type, password)
163
+ line = get_tacacsserver_match_line("key\s#{sh_run_encryption_type}\s\".*\"")
164
+ refute_nil(line, "Error: Tacacs Server, key not configured")
165
+
166
+ enc_type = TACACS_SERVER_ENC_UNKNOWN
167
+ password = ""
168
+ tacacs.encryption_key_set(enc_type, password)
169
+ line = get_tacacsserver_match_line("key\s#{sh_run_encryption_type}\s\".*\"")
170
+ assert_nil(line, "Error: Tacacs Server, key configured")
171
+ tacacs.destroy
172
+ end
173
+
174
+ def test_tacacsserver_get_timeout
175
+ tacacs = TacacsServer.new
176
+ timeout = node.config_get_default("tacacs_server", "timeout")
177
+ assert_equal(timeout, tacacs.timeout,
178
+ "Error: Tacacs Server, timeout not default")
179
+
180
+ timeout = 35
181
+ s = @device.cmd("configure terminal")
182
+ s = @device.cmd("tacacs-server timeout #{timeout}")
183
+ s = @device.cmd("end")
184
+ # Flush the cache since we've modified the device
185
+ node.cache_flush
186
+ assert_equal(timeout, tacacs.timeout,
187
+ "Error: Tacacs Server, timeout not configured")
188
+ tacacs.destroy
189
+ end
190
+
191
+ def test_tacacsserver_get_default_timeout
192
+ assert_equal(node.config_get_default("tacacs_server", "timeout"),
193
+ TacacsServer.default_timeout,
194
+ "Error: Tacacs Server, default timeout incorrect")
195
+ end
196
+
197
+ def test_tacacsserver_set_timeout
198
+ timeout = 45
199
+
200
+ tacacs = TacacsServer.new
201
+ tacacs.timeout = timeout
202
+ line = get_tacacsserver_match_line("timeout\s.*")
203
+ # Extract timeout
204
+ md = /tacacs-server\stimeout\s\d*/.match(line.to_s)
205
+ sh_run_timeout = md.to_s.split(" ").last.to_i
206
+ # Need a better way to extract the timeout
207
+ refute_nil(line, "Error: Tacacs Server, timeout not configured")
208
+ assert_equal(sh_run_timeout, tacacs.timeout,
209
+ "Error: Tacacs Server, timeout value incorrect")
210
+
211
+ # Invalid case
212
+ timeout = 80
213
+ assert_raises(Cisco::CliError) do
214
+ tacacs.timeout = timeout
215
+ end
216
+ tacacs.destroy
217
+ end
218
+
219
+ def test_tacacsserver_get_deadtime
220
+ tacacs = TacacsServer.new
221
+ deadtime = node.config_get_default("tacacs_server", "deadtime")
222
+ assert_equal(deadtime, tacacs.deadtime,
223
+ "Error: Tacacs Server, deadtime not default")
224
+
225
+ deadtime = 850
226
+ s = @device.cmd("configure terminal")
227
+ s = @device.cmd("tacacs-server deadtime #{deadtime}")
228
+ s = @device.cmd("end")
229
+ # Flush the cache since we've modified the device
230
+ node.cache_flush
231
+ assert_equal(deadtime, tacacs.deadtime,
232
+ "Error: Tacacs Server, deadtime not configured")
233
+ tacacs.destroy
234
+ end
235
+
236
+ def test_tacacsserver_get_default_deadtime
237
+ assert_equal(node.config_get_default("tacacs_server", "deadtime"),
238
+ TacacsServer.default_deadtime,
239
+ "Error: Tacacs Server, default deadtime incorrect")
240
+ end
241
+
242
+ def test_tacacsserver_set_deadtime
243
+ deadtime = 1250
244
+
245
+ tacacs = TacacsServer.new
246
+ tacacs.deadtime = deadtime
247
+ line = get_tacacsserver_match_line("deadtime\s.*")
248
+ # Extract deadtime
249
+ md = /tacacs-server\sdeadtime\s\d*/.match(line.to_s)
250
+ sh_run_deadtime = md.to_s.split(" ").last.to_i
251
+ refute_nil(line, "Error: Tacacs Server, deadtime not configured")
252
+ assert_equal(sh_run_deadtime, tacacs.deadtime,
253
+ "Error: Tacacs Server, deadtime incorrect")
254
+ # Invalid case
255
+ deadtime = 2450
256
+ assert_raises(Cisco::CliError) do
257
+ tacacs.deadtime = deadtime
258
+ end
259
+ tacacs.destroy
260
+ end
261
+
262
+ def test_tacacsserver_get_directed_request
263
+ s = @device.cmd("conf t ; feature tacacs ; tacacs-server directed-request ; end")
264
+ # Flush the cache since we've modified the device
265
+ node.cache_flush
266
+ tacacs = TacacsServer.new
267
+ assert(tacacs.directed_request?,
268
+ "Error: Tacacs Server, directed-request not set")
269
+
270
+ s = @device.cmd("conf t ; no tacacs-server directed-request ; end")
271
+ node.cache_flush
272
+ refute(tacacs.directed_request?,
273
+ "Error: Tacacs Server, directed-request set")
274
+ tacacs.destroy
275
+ end
276
+
277
+ def test_tacacsserver_get_default_directed_request
278
+ assert_equal(node.config_get_default("tacacs_server", "directed_request"),
279
+ TacacsServer.default_directed_request,
280
+ "Error: Tacacs Server, default directed-request incorrect")
281
+ end
282
+
283
+ def test_tacacsserver_set_directed_request
284
+ s = @device.cmd("conf t ; feature tacacs ; tacacs-server directed-request ; end")
285
+ state = true
286
+ tacacs = TacacsServer.new
287
+ tacacs.directed_request = state
288
+ line = get_tacacsserver_match_line("directed-request")
289
+ refute_nil(line, "Error: Tacacs Server, directed-request not configured")
290
+ assert(tacacs.directed_request?,
291
+ "Error: Tacacs Server, directed-request not set")
292
+
293
+ # Turn it off
294
+ s = @device.cmd("conf t ; no tacacs-server directed-request ; end")
295
+ node.cache_flush
296
+ refute(tacacs.directed_request?,
297
+ "Error: Tacacs Server, directed-request set")
298
+
299
+ # Turn it back on then go to default
300
+ s = @device.cmd("conf t ; no tacacs-server directed-request ; end")
301
+ state = node.config_get_default("tacacs_server", "directed_request")
302
+ tacacs.directed_request = state
303
+ line = get_match_line("no tacacs-server directed-request")
304
+ refute_nil(line,
305
+ "Error: Tacacs Server, default directed-request not configured")
306
+
307
+ # Extract the state of directed-request
308
+ sh_run_directed_request = line.to_s.split(" ").first
309
+ assert_equal("no", sh_run_directed_request,
310
+ "Error: Tacacs Server, directed-request not unconfigured")
311
+
312
+ refute(tacacs.directed_request?,
313
+ "Error: Tacacs Server, directed-request set")
314
+
315
+ # Invalid case
316
+ state = "TEST"
317
+ assert_raises(TypeError) do
318
+ tacacs.directed_request = state
319
+ end
320
+ tacacs.destroy
321
+ end
322
+
323
+ def test_tacacsserver_get_source_interface
324
+ s = @device.cmd("configure terminal")
325
+ s = @device.cmd("no ip tacacs source-interface")
326
+ s = @device.cmd("end")
327
+ tacacs = TacacsServer.new
328
+ intf = node.config_get_default("tacacs_server", "source_interface")
329
+ assert_equal(intf, tacacs.source_interface,
330
+ "Error: Tacacs Server, source-interface set")
331
+
332
+ intf = "Ethernet1/1"
333
+ s = @device.cmd("configure terminal")
334
+ s = @device.cmd("ip tacacs source-interface #{intf}")
335
+ s = @device.cmd("end")
336
+ # Flush the cache since we've modified the device
337
+ node.cache_flush
338
+ assert_equal(intf, tacacs.source_interface,
339
+ "Error: Tacacs Server, source-interface not correct")
340
+ tacacs.destroy
341
+ end
342
+
343
+ def test_tacacsserver_get_default_source_interface
344
+ assert_equal(node.config_get_default("tacacs_server", "source_interface"),
345
+ TacacsServer.default_source_interface,
346
+ "Error: Tacacs Server, default source-interface incorrect")
347
+ end
348
+
349
+ def test_tacacsserver_set_source_interface
350
+ s = @device.cmd("conf t ; feature tacacs+ ; no ip tacacs source-int ; end")
351
+ node.cache_flush
352
+ intf = node.config_get_default("tacacs_server", "source_interface")
353
+ tacacs = TacacsServer.new
354
+ assert_equal(intf, tacacs.source_interface,
355
+ "Error: Tacacs Server, source-interface set")
356
+
357
+ intf = "Ethernet1/1"
358
+ tacacs.source_interface = intf
359
+ line = get_match_line("ip tacacs source-interface #{intf}")
360
+ # Extract source-interface
361
+ sh_run_source_interface = line.to_s.split(" ").last
362
+ refute_nil(line, "Error: Tacacs Server, source-interface not configured")
363
+ assert_equal(sh_run_source_interface, tacacs.source_interface,
364
+ "Error: Tacacs Server, source-interface not correct")
365
+
366
+ # Now bring it back to default
367
+ intf = node.config_get_default("tacacs_server", "source_interface")
368
+ tacacs.source_interface = intf
369
+ line = get_match_line("no ip tacacs source-interface")
370
+ refute_nil(line, "Error: Tacacs Server, source-interface not default")
371
+
372
+ # Invalid case
373
+ state = true
374
+ assert_raises(TypeError) do
375
+ tacacs.source_interface = state
376
+ end
377
+ tacacs.destroy
378
+ end
379
+
380
+ def test_tacacsserver_destroy
381
+ tacacs = TacacsServer.new
382
+ line = get_tacacsserver_feature
383
+ refute_nil(line, "Error: Tacacs feature not set")
384
+ tacacs.destroy
385
+ line = get_tacacsserver_feature
386
+ assert_nil(line, "Error: Tacacs feature still present")
387
+ end
388
+ end
@@ -0,0 +1,391 @@
1
+ # Copyright (c) 2014-2015 Cisco and/or its affiliates.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require File.expand_path("../ciscotest", __FILE__)
16
+ require File.expand_path("../../lib/cisco_node_utils/tacacs_server_host", __FILE__)
17
+
18
+ include Cisco
19
+
20
+ DEFAULT_TACACS_SERVER_HOST_PORT = 49
21
+ DEFAULT_TACACS_SERVER_HOST_TIMEOUT = 0
22
+ DEFAULT_TACACS_SERVER_HOST_ENCRYPTION_PASSWORD = ""
23
+
24
+ class TestTacacsServerHost < CiscoTestCase
25
+ def get_tacacsserverhost_match_line(host_name)
26
+ s = @device.cmd("show run all | no-more")
27
+ cmd = "tacacs-server host"
28
+ pattern = /#{cmd}\s(#{host_name})(.*)/
29
+ pattern.match(s)
30
+ end
31
+
32
+ def test_tacacsserverhost_collection_empty
33
+ hosts = TacacsServerHost.hosts
34
+ hosts.each { |name, host| host.destroy }
35
+ hosts = TacacsServerHost.hosts
36
+
37
+ assert_empty(hosts, "Error: Tacacs Host collection is not empty")
38
+ end
39
+
40
+ def test_tacacsserverhost_collection
41
+ hosts_hash = {}
42
+ hosts_hash["testhost1"] = 1138
43
+ hosts_hash["testhost2"] = DEFAULT_TACACS_SERVER_HOST_PORT
44
+
45
+ hosts_hash.each { |name, port|
46
+ host = TacacsServerHost.new(name)
47
+ host.port = port
48
+ }
49
+
50
+ hosts = TacacsServerHost.hosts
51
+ refute_empty(hosts, "Error: Tacacs Host collection is empty")
52
+ hosts_hash.each { |name, port|
53
+ # host must have been created to be found in the list
54
+ assert(hosts.include?(name),
55
+ "Error: Tacacs Host #{name} not in collection")
56
+ # port numbers differentiate the hosts
57
+ assert_equal(port, hosts[name].port,
58
+ "Error: Tacacs Host #{name} port mismatch")
59
+ }
60
+
61
+ hosts_hash.each { |name, host| hosts[name].destroy }
62
+ end
63
+
64
+ def test_tacacsserverhost_create_server_nil
65
+ assert_raises(TypeError) do
66
+ host = TacacsServerHost.new(nil)
67
+ end
68
+ end
69
+
70
+ def test_tacacsserverhost_create_name_zero_length
71
+ assert_raises(ArgumentError) do
72
+ host = TacacsServerHost.new("")
73
+ end
74
+ end
75
+
76
+ def test_tacacsserverhost_create_valid
77
+ host = TacacsServerHost.new("testhost")
78
+ line = get_tacacsserverhost_match_line("testhost")
79
+ refute_nil(line, "Error: Tacacs Host not created")
80
+ host.destroy
81
+ end
82
+
83
+ def test_tacacsserverhost_destroy
84
+ host_name = "testhost"
85
+ host = TacacsServerHost.new(host_name)
86
+ line = get_tacacsserverhost_match_line(host_name)
87
+ refute_nil(line, "Error: Tacacs Host not created")
88
+ host.destroy
89
+
90
+ line = get_tacacsserverhost_match_line(host_name)
91
+ assert_nil(line, "Error: Tacacs Host still present")
92
+ end
93
+
94
+ def test_tacacsserverhost_get_name
95
+ host_name = "testhost"
96
+ host = TacacsServerHost.new(host_name)
97
+ line = get_tacacsserverhost_match_line(host_name)
98
+ refute_nil(line, "Error: Tacacs Host not found")
99
+ assert_equal(host_name, line.captures[0],
100
+ "Error: #{host_name} name mismatch")
101
+ assert_equal(host_name, host.name,
102
+ "Error: #{host_name} name get value mismatch")
103
+ host.destroy
104
+ end
105
+
106
+ def test_tacacsserverhost_get_name_preconfigured
107
+ host_name = "testhost"
108
+
109
+ s = @device.cmd("configure terminal")
110
+ s = @device.cmd("tacacs-server host #{host_name}")
111
+ s = @device.cmd("end")
112
+ node.cache_flush
113
+
114
+ line = get_tacacsserverhost_match_line(host_name)
115
+ hosts = TacacsServerHost.hosts()
116
+
117
+ refute_nil(line, "Error: Tacacs Host not found")
118
+ assert_equal(host_name, line.captures[0],
119
+ "Error: #{host_name} name mismatch")
120
+ refute_nil(hosts[host_name], "Error: #{host_name} not retrieved.")
121
+ assert_equal(host_name, hosts[host_name].name,
122
+ "Error: #{host_name} name get value mismatch")
123
+
124
+ hosts.each { |name, host| host.destroy }
125
+ end
126
+
127
+ def test_tacacsserverhost_get_name_formats
128
+ host_name = "testhost.example.com"
129
+ host_ip = "192.168.1.1"
130
+
131
+ s = @device.cmd("configure terminal")
132
+ s = @device.cmd("tacacs-server host #{host_name}")
133
+ s = @device.cmd("tacacs-server host #{host_ip}")
134
+ s = @device.cmd("end")
135
+ node.cache_flush
136
+
137
+ line_name = get_tacacsserverhost_match_line(host_name)
138
+ line_ip = get_tacacsserverhost_match_line(host_ip)
139
+ hosts = TacacsServerHost.hosts
140
+
141
+ refute_nil(line_name, "Error: Tacacs Host not found")
142
+ assert_equal(host_name, line_name.captures[0],
143
+ "Error: #{host_name} name mismatch")
144
+ refute_nil(hosts[host_name], "Error: #{host_name} not retrieved.")
145
+ assert_equal(host_name, hosts[host_name].name,
146
+ "Error: #{host_name} name get value mismatch")
147
+
148
+ refute_nil(line_ip, "Error: Tacacs Host not found")
149
+ assert_equal(host_ip, line_ip.captures[0],
150
+ "Error: #{host_ip} name mismatch")
151
+ refute_nil(hosts[host_ip], "Error: #{host_ip} not retrieved.")
152
+ assert_equal(host_ip, hosts[host_ip].name,
153
+ "Error: #{host_ip} name get value mismatch")
154
+
155
+ hosts.each { |name, host| host.destroy }
156
+ end
157
+
158
+ def test_tacacsserverhost_get_port
159
+ host_name = "testhost"
160
+ host = TacacsServerHost.new(host_name)
161
+
162
+ # not previously configured
163
+ port = DEFAULT_TACACS_SERVER_HOST_PORT
164
+ assert_equal(port, host.port, "Error: Tacacs Host port incorrect")
165
+
166
+ # when configured
167
+ port = 1138
168
+ s = @device.cmd("configure terminal")
169
+ s = @device.cmd("tacacs-server host #{host_name} port #{port}")
170
+ s = @device.cmd("end")
171
+ node.cache_flush
172
+ assert_equal(port, host.port, "Error: Tacacs Host port incorrect")
173
+
174
+ host.destroy
175
+ end
176
+
177
+ def test_tacacsserverhost_get_default_port
178
+ host = TacacsServerHost.new("testhost")
179
+
180
+ port = DEFAULT_TACACS_SERVER_HOST_PORT
181
+ assert_equal(port, TacacsServerHost.default_port,
182
+ "Error: Tacacs Host default port incorrect")
183
+ host.destroy
184
+ end
185
+
186
+ def test_tacacsserverhost_set_port
187
+ host_name = "testhost"
188
+ host = TacacsServerHost.new(host_name)
189
+
190
+ port = 1138
191
+ host.port = port
192
+ line = get_tacacsserverhost_match_line(host_name)
193
+ refute_nil(line, "Error: Tacacs Host not found")
194
+ md = /port\s(\d*)/.match(line.captures[1])
195
+ refute_nil(md, "Error: Tacacs Host port not found")
196
+ assert_equal(port, md.captures[0].to_i, "Error: Tacacs Host port mismatch")
197
+ assert_equal(port, host.port, "Error: Tacacs Host port incorrect")
198
+
199
+ host.destroy
200
+ end
201
+
202
+ def test_tacacsserverhost_get_timeout
203
+ # Cleanup first
204
+ s = @device.cmd("show run | i 'tacacs.*timeout'")[/^tacacs.*timeout.*$/]
205
+ if s
206
+ s = @device.cmd("conf t ; no #{s} ; end")
207
+ # puts "s is >#{s}<"
208
+ node.cache_flush
209
+ end
210
+
211
+ host_name = "testhost"
212
+ host = TacacsServerHost.new(host_name)
213
+
214
+ # not previously configured
215
+ timeout = DEFAULT_TACACS_SERVER_HOST_TIMEOUT
216
+ assert_equal(timeout, host.timeout, "Error: Tacacs Host timeout incorrect")
217
+
218
+ # when configured
219
+ timeout = 30
220
+ s = @device.cmd("configure terminal")
221
+ s = @device.cmd("tacacs-server host #{host_name} timeout #{timeout}")
222
+ s = @device.cmd("end")
223
+ node.cache_flush
224
+ assert_equal(timeout, host.timeout, "Error: Tacacs Host timeout incorrect")
225
+
226
+ host.destroy
227
+ end
228
+
229
+ def test_tacacsserverhost_get_default_timeout
230
+ host = TacacsServerHost.new("testhost")
231
+
232
+ timeout = DEFAULT_TACACS_SERVER_HOST_TIMEOUT
233
+ assert_equal(timeout, TacacsServerHost.default_timeout,
234
+ "Error: Tacacs Host default timeout incorrect")
235
+ host.destroy
236
+ end
237
+
238
+ def test_tacacsserverhost_set_timeout
239
+ host_name = "testhost"
240
+ host = TacacsServerHost.new(host_name)
241
+
242
+ timeout = 30
243
+ host.timeout = timeout
244
+ line = get_tacacsserverhost_match_line(host_name)
245
+ refute_nil(line, "Error: Tacacs Host not found")
246
+ md = /timeout\s(\d*)/.match(line.captures[1])
247
+ refute_nil(md, "Error: Tacacs Host timeout not found")
248
+ assert_equal(timeout, md.captures[0].to_i,
249
+ "Error: Tacacs Host timeout mismatch")
250
+ assert_equal(timeout, host.timeout, "Error: Tacacs Host timeout incorrect")
251
+
252
+ host.destroy
253
+ end
254
+
255
+ def test_tacacsserverhost_unset_timeout
256
+ host_name = "testhost"
257
+ host = TacacsServerHost.new(host_name)
258
+
259
+ timeout = DEFAULT_TACACS_SERVER_HOST_TIMEOUT
260
+ host.timeout = timeout
261
+ line = get_tacacsserverhost_match_line(host_name)
262
+ refute_nil(line, "Error: Tacacs Host not found")
263
+ md = /timeout\s(\d*)/.match(line.captures[1])
264
+ assert_nil(md, "Error: Tacacs Host timeout found")
265
+ assert_equal(timeout, host.timeout, "Error: Tacacs Host timeout incorrect")
266
+
267
+ host.destroy
268
+ end
269
+
270
+ def test_tacacsserverhost_get_encryption_type
271
+ host_name = "testhost"
272
+ host = TacacsServerHost.new(host_name)
273
+
274
+ # when not configured
275
+ enctype = TACACS_SERVER_ENC_UNKNOWN
276
+
277
+ assert_equal(enctype, host.encryption_type,
278
+ "Error: Tacacs Host encryption type incorrect")
279
+
280
+ # when configured
281
+ enctype = TACACS_SERVER_ENC_NONE
282
+ sh_run_enctype = TACACS_SERVER_ENC_CISCO_TYPE_7
283
+ s = @device.cmd("configure terminal")
284
+ s = @device.cmd("tacacs-server host #{host_name} key #{enctype} TEST")
285
+ s = @device.cmd("end")
286
+ node.cache_flush
287
+ assert_equal(sh_run_enctype, host.encryption_type,
288
+ "Error: Tacacs Host encryption type incorrect")
289
+ host.destroy
290
+ end
291
+
292
+ def test_tacacsserverhost_get_default_encryption_type
293
+ host = TacacsServerHost.new("testhost")
294
+
295
+ assert_equal(TACACS_SERVER_ENC_NONE,
296
+ TacacsServerHost.default_encryption_type,
297
+ "Error: Tacacs Host default encryption type incorrect")
298
+ host.destroy
299
+ end
300
+
301
+ def test_tacacsserverhost_get_encryption_password
302
+ host_name = "testhost"
303
+ host = TacacsServerHost.new(host_name)
304
+
305
+ # when not configured
306
+ pass = DEFAULT_TACACS_SERVER_HOST_ENCRYPTION_PASSWORD
307
+ assert_equal(pass, host.encryption_password,
308
+ "Error: Tacacs Host encryption password incorrect")
309
+
310
+ # when configured
311
+ pass = "TEST"
312
+ sh_run_pass = "WAWY"
313
+ s = @device.cmd("configure terminal")
314
+ s = @device.cmd("tacacs-server host #{host_name} key 0 #{pass}")
315
+ s = @device.cmd("end")
316
+ node.cache_flush
317
+ assert_equal(sh_run_pass, host.encryption_password,
318
+ "Error: Tacacs Host encryption password incorrect")
319
+ host.destroy
320
+ end
321
+
322
+ def test_tacacsserverhost_get_default_encryption_password
323
+ host = TacacsServerHost.new("testhost")
324
+
325
+ assert_equal("", TacacsServerHost.default_encryption_password,
326
+ "Error: Tacacs Host default encryption password incorrect")
327
+ host.destroy
328
+ end
329
+
330
+ def test_tacacsserverhost_set_key
331
+ host_name = "testhost"
332
+ host = TacacsServerHost.new(host_name)
333
+
334
+ enctype = TACACS_SERVER_ENC_NONE
335
+ sh_run_enctype = TACACS_SERVER_ENC_CISCO_TYPE_7
336
+ pass = "TEST"
337
+ sh_run_pass = "WAWY"
338
+ host.encryption_key_set(enctype, pass)
339
+
340
+ line = get_tacacsserverhost_match_line(host_name)
341
+ refute_nil(line, "Error: Tacacs Host not found")
342
+ md = /key\s(\d*)\s(\S*)/.match(line.captures[1])
343
+ refute_nil(md, "Error: Tacacs Host encryption not found")
344
+ assert_equal(sh_run_enctype, md.captures[0].to_i,
345
+ "Error: Tacacs Host encryption type mismatch")
346
+ assert_equal(sh_run_enctype, host.encryption_type,
347
+ "Error: Tacacs Host encryption type incorrect")
348
+ # remove quotes surrounding the encrypted password
349
+ pass_no_quotes = md.captures[1].gsub(/(?:^\")|(?:\"$)/, '')
350
+ assert_equal(sh_run_pass, pass_no_quotes,
351
+ "Error: Tacacs Host encryption password mismatch")
352
+ assert_equal(sh_run_pass, host.encryption_password,
353
+ "Error: Tacacs Host encryption password incorrect")
354
+
355
+ host.destroy
356
+ end
357
+
358
+ def test_tacacsserverhost_unset_key
359
+ # Cleanup first
360
+ s = @device.cmd("show run | i 'tacacs.*host'")[/^tacacs.*host.*$/]
361
+ if s
362
+ s = @device.cmd("conf t ; no #{s} ; end")
363
+ # puts "s is >#{s}<"
364
+ node.cache_flush
365
+ end
366
+
367
+ host_name = "testhost"
368
+ host = TacacsServerHost.new(host_name)
369
+
370
+ # First configure key value. Whether that can be passed
371
+ # will be decided by test_tacacsserverhost_set_key
372
+ enctype = TACACS_SERVER_ENC_NONE
373
+ pass = "TEST"
374
+ host.encryption_key_set(enctype, pass)
375
+
376
+ # Now unconfigure the key and verify
377
+ enctype = TACACS_SERVER_ENC_UNKNOWN
378
+ pass = DEFAULT_TACACS_SERVER_HOST_ENCRYPTION_PASSWORD
379
+ host.encryption_key_set(enctype, pass)
380
+
381
+ line = get_tacacsserverhost_match_line(host_name)
382
+ refute_nil(line, "Error: Tacacs Host not found")
383
+ md = /key\s(\d*)\s(\S*)/.match(line.captures[1])
384
+ assert_nil(md, "Error: Tacacs Host encryption found")
385
+ assert_equal(enctype, host.encryption_type,
386
+ "Error: Tacacs Host encryption type incorrect")
387
+ assert_equal(pass, host.encryption_password,
388
+ "Error: Tacacs Host encryption password incorrect")
389
+ host.destroy
390
+ end
391
+ end