cisco_node_utils 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rubocop.yml +81 -1
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +72 -6
  6. data/CONTRIBUTING.md +32 -7
  7. data/README.md +70 -7
  8. data/Rakefile +17 -0
  9. data/bin/check_metric_limits.rb +109 -0
  10. data/bin/git/hooks/commit-msg/enforce_style +81 -0
  11. data/bin/git/hooks/hook_lib +108 -0
  12. data/bin/git/hooks/hooks-wrapper +38 -0
  13. data/bin/git/hooks/post-flow-hotfix-start/update-version +24 -0
  14. data/bin/git/hooks/post-flow-release-finish/update-version +29 -0
  15. data/bin/git/hooks/post-flow-release-start/update-version +19 -0
  16. data/bin/git/hooks/post-merge/update-hooks +6 -0
  17. data/bin/git/hooks/post-rewrite/update-hooks +6 -0
  18. data/bin/git/hooks/pre-commit/rubocop +20 -0
  19. data/bin/git/hooks/pre-commit/validate-diffs +31 -0
  20. data/bin/git/hooks/pre-push/check-changelog +24 -0
  21. data/bin/git/hooks/pre-push/rubocop +7 -0
  22. data/bin/git/update-hooks +65 -0
  23. data/cisco_node_utils.gemspec +9 -3
  24. data/docs/README-develop-best-practices.md +404 -0
  25. data/docs/README-develop-node-utils-APIs.md +215 -365
  26. data/docs/README-maintainers.md +33 -3
  27. data/docs/template-router.rb +89 -91
  28. data/docs/template-test_router.rb +52 -55
  29. data/lib/.rubocop.yml +18 -0
  30. data/lib/cisco_node_utils.rb +2 -19
  31. data/lib/cisco_node_utils/README_YAML.md +1 -9
  32. data/lib/cisco_node_utils/bgp.rb +664 -0
  33. data/lib/cisco_node_utils/bgp_af.rb +530 -0
  34. data/lib/cisco_node_utils/bgp_neighbor.rb +425 -0
  35. data/lib/cisco_node_utils/bgp_neighbor_af.rb +709 -0
  36. data/lib/cisco_node_utils/cisco_cmn_utils.rb +59 -25
  37. data/lib/cisco_node_utils/command_reference.rb +72 -74
  38. data/lib/cisco_node_utils/command_reference_common.yaml +174 -9
  39. data/lib/cisco_node_utils/command_reference_common_bgp.yaml +535 -0
  40. data/lib/cisco_node_utils/command_reference_n7k.yaml +4 -0
  41. data/lib/cisco_node_utils/command_reference_n9k.yaml +0 -9
  42. data/lib/cisco_node_utils/configparser_lib.rb +152 -147
  43. data/lib/cisco_node_utils/dns_domain.rb +79 -0
  44. data/lib/cisco_node_utils/domain_name.rb +71 -0
  45. data/lib/cisco_node_utils/interface.rb +167 -161
  46. data/lib/cisco_node_utils/interface_ospf.rb +78 -81
  47. data/lib/cisco_node_utils/name_server.rb +64 -0
  48. data/lib/cisco_node_utils/node.rb +154 -198
  49. data/lib/cisco_node_utils/node_util.rb +61 -0
  50. data/lib/cisco_node_utils/ntp_config.rb +65 -0
  51. data/lib/cisco_node_utils/ntp_server.rb +76 -0
  52. data/lib/cisco_node_utils/platform.rb +174 -165
  53. data/lib/cisco_node_utils/radius_global.rb +146 -0
  54. data/lib/cisco_node_utils/radius_server.rb +295 -0
  55. data/lib/cisco_node_utils/router_ospf.rb +59 -63
  56. data/lib/cisco_node_utils/router_ospf_vrf.rb +226 -210
  57. data/lib/cisco_node_utils/snmpcommunity.rb +52 -58
  58. data/lib/cisco_node_utils/snmpgroup.rb +22 -23
  59. data/lib/cisco_node_utils/snmpserver.rb +99 -103
  60. data/lib/cisco_node_utils/snmpuser.rb +294 -274
  61. data/lib/cisco_node_utils/syslog_server.rb +92 -0
  62. data/lib/cisco_node_utils/syslog_settings.rb +69 -0
  63. data/lib/cisco_node_utils/tacacs_server.rb +137 -133
  64. data/lib/cisco_node_utils/tacacs_server_host.rb +84 -87
  65. data/lib/cisco_node_utils/version.rb +2 -1
  66. data/lib/cisco_node_utils/vlan.rb +28 -31
  67. data/lib/cisco_node_utils/vrf.rb +80 -0
  68. data/lib/cisco_node_utils/vtp.rb +100 -97
  69. data/lib/cisco_node_utils/yum.rb +15 -17
  70. data/tests/.rubocop.yml +15 -0
  71. data/tests/basetest.rb +81 -36
  72. data/tests/ciscotest.rb +38 -78
  73. data/{lib/cisco_node_utils → tests}/platform_info.rb +12 -8
  74. data/{lib/cisco_node_utils → tests}/platform_info.yaml +1 -1
  75. data/tests/test_bgp_af.rb +920 -0
  76. data/tests/test_bgp_neighbor.rb +403 -0
  77. data/tests/test_bgp_neighbor_af.rb +589 -0
  78. data/tests/test_command_config.rb +65 -62
  79. data/tests/test_command_reference.rb +31 -45
  80. data/tests/test_dns_domain.rb +113 -0
  81. data/tests/test_domain_name.rb +86 -0
  82. data/tests/test_interface.rb +424 -548
  83. data/tests/test_interface_ospf.rb +248 -432
  84. data/tests/test_interface_svi.rb +56 -79
  85. data/tests/test_interface_switchport.rb +196 -272
  86. data/tests/test_name_server.rb +85 -0
  87. data/tests/test_node.rb +7 -6
  88. data/tests/test_node_ext.rb +133 -186
  89. data/tests/test_ntp_config.rb +49 -0
  90. data/tests/test_ntp_server.rb +74 -0
  91. data/tests/test_platform.rb +58 -37
  92. data/tests/test_radius_global.rb +78 -0
  93. data/tests/test_radius_server.rb +185 -0
  94. data/tests/test_router_bgp.rb +838 -0
  95. data/tests/test_router_ospf.rb +49 -80
  96. data/tests/test_router_ospf_vrf.rb +274 -392
  97. data/tests/test_snmpcommunity.rb +128 -172
  98. data/tests/test_snmpgroup.rb +12 -14
  99. data/tests/test_snmpserver.rb +160 -189
  100. data/tests/test_snmpuser.rb +568 -717
  101. data/tests/test_syslog_server.rb +88 -0
  102. data/tests/test_syslog_settings.rb +54 -0
  103. data/tests/test_tacacs_server.rb +113 -148
  104. data/tests/test_tacacs_server_host.rb +108 -161
  105. data/tests/test_vlan.rb +63 -79
  106. data/tests/test_vrf.rb +92 -0
  107. data/tests/test_vtp.rb +108 -126
  108. data/tests/test_yum.rb +47 -41
  109. metadata +92 -56
  110. data/.rubocop_todo.yml +0 -293
  111. data/docs/.rubocop.yml +0 -13
  112. data/docs/template-feature.rb +0 -45
  113. data/docs/template-test_feature.rb +0 -51
  114. data/tests/test_all_cisco.rb +0 -46
@@ -0,0 +1,85 @@
1
+ #
2
+ # Minitest for NameServer class
3
+ #
4
+ # Copyright (c) 2014-2015 Cisco and/or its affiliates.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require_relative 'ciscotest'
19
+ require_relative '../lib/cisco_node_utils/name_server'
20
+
21
+ # TestNameServer - Minitest for NameServer node utility.
22
+ class TestNameServer < CiscoTestCase
23
+ def setup
24
+ # setup runs at the beginning of each test
25
+ super
26
+ no_nameserver_google
27
+ end
28
+
29
+ def teardown
30
+ # teardown runs at the end of each test
31
+ no_nameserver_google
32
+ super
33
+ end
34
+
35
+ def no_nameserver_google
36
+ # Turn the feature off for a clean test.
37
+ config('no ip name-server 7.7.7.7',
38
+ 'no ip name-server 2001:4860:4860::7777')
39
+ end
40
+
41
+ # TESTS
42
+
43
+ def test_nameserver_create_destroy_single_ipv4
44
+ id = '7.7.7.7'
45
+ refute_includes(Cisco::NameServer.nameservers, id)
46
+
47
+ ns = Cisco::NameServer.new(id)
48
+ assert_includes(Cisco::NameServer.nameservers, id)
49
+ assert_equal(Cisco::NameServer.nameservers[id], ns)
50
+
51
+ ns.destroy
52
+ refute_includes(Cisco::NameServer.nameservers, id)
53
+ end
54
+
55
+ def test_nameserver_create_destroy_single_ipv6
56
+ id = '2001:4860:4860::7777'
57
+ refute_includes(Cisco::NameServer.nameservers, id)
58
+
59
+ ns = Cisco::NameServer.new(id)
60
+ assert_includes(Cisco::NameServer.nameservers, id)
61
+ assert_equal(Cisco::NameServer.nameservers[id], ns)
62
+
63
+ ns.destroy
64
+ refute_includes(Cisco::NameServer.nameservers, id)
65
+ end
66
+
67
+ def test_router_create_destroy_multiple
68
+ id1 = '7.7.7.7'
69
+ id2 = '2001:4860:4860::7777'
70
+ refute_includes(Cisco::NameServer.nameservers, id1)
71
+ refute_includes(Cisco::NameServer.nameservers, id2)
72
+
73
+ ns1 = Cisco::NameServer.new(id1)
74
+ ns2 = Cisco::NameServer.new(id2)
75
+ assert_includes(Cisco::NameServer.nameservers, id1)
76
+ assert_includes(Cisco::NameServer.nameservers, id2)
77
+ assert_equal(Cisco::NameServer.nameservers[id1], ns1)
78
+ assert_equal(Cisco::NameServer.nameservers[id2], ns2)
79
+
80
+ ns1.destroy
81
+ ns2.destroy
82
+ refute_includes(Cisco::NameServer.nameservers, id1)
83
+ refute_includes(Cisco::NameServer.nameservers, id2)
84
+ end
85
+ end
data/tests/test_node.rb CHANGED
@@ -12,15 +12,16 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require File.expand_path("../basetest", __FILE__)
15
+ require_relative 'basetest'
16
16
 
17
- require File.expand_path("../../lib/cisco_node_utils/node", __FILE__)
18
- require File.expand_path("../../lib/cisco_node_utils/command_reference", __FILE__)
17
+ require_relative '../lib/cisco_node_utils/node'
18
+ require_relative '../lib/cisco_node_utils/command_reference'
19
19
 
20
20
  include Cisco
21
21
 
22
22
  Node.lazy_connect = true # we'll specify the connection info later
23
23
 
24
+ # TestNode - Minitest for core functionality of Node class
24
25
  class TestNode < TestCase
25
26
  def setup
26
27
  end
@@ -33,7 +34,7 @@ class TestNode < TestCase
33
34
 
34
35
  def test_node_create_not_allowed
35
36
  assert_raises(NoMethodError) do
36
- node = Node.new
37
+ Node.new
37
38
  end
38
39
  end
39
40
 
@@ -76,7 +77,7 @@ class TestNode < TestCase
76
77
  def test_node_connect_username_zero_length
77
78
  node = Node.instance
78
79
  assert_raises(ArgumentError) do
79
- node.connect(address, "", password)
80
+ node.connect(address, '', password)
80
81
  end
81
82
  end
82
83
 
@@ -97,7 +98,7 @@ class TestNode < TestCase
97
98
  def test_node_connect_password_zero_length
98
99
  node = Node.instance
99
100
  assert_raises(ArgumentError) do
100
- node.connect(address, username, "")
101
+ node.connect(address, username, '')
101
102
  end
102
103
  end
103
104
 
@@ -12,12 +12,17 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require File.expand_path("../ciscotest", __FILE__)
15
+ require_relative 'ciscotest'
16
16
 
17
+ # TestNodeExt - Minitest for abstracted Node APIs
17
18
  class TestNodeExt < CiscoTestCase
18
- # Test cases for abstracted Node APIs
19
+ def assert_output_check(command: nil, pattern: nil, msg: nil, check: nil)
20
+ md = assert_show_match(command: command, pattern: pattern, msg: msg)
21
+ assert_equal(md[1], check, msg)
22
+ end
19
23
 
20
- @@show_run_ospf = "\
24
+ def show_run_ospf
25
+ "\
21
26
  router ospf foo
22
27
  vrf red
23
28
  log-adjacency-changes
@@ -29,9 +34,10 @@ router ospf bar
29
34
  !
30
35
  router ospf baz
31
36
  log-adjacency-changes detail"
37
+ end
32
38
 
33
39
  def test_node_find_subconfig
34
- result = find_subconfig(@@show_run_ospf, /router ospf bar/)
40
+ result = find_subconfig(show_run_ospf, /router ospf bar/)
35
41
  assert_equal("\
36
42
  log-adjacency-changes
37
43
  vrf red
@@ -41,7 +47,7 @@ vrf blue",
41
47
 
42
48
  assert_nil(find_subconfig(result, /vrf blue/))
43
49
 
44
- assert_equal("log-adjacency-changes detail",
50
+ assert_equal('log-adjacency-changes detail',
45
51
  find_subconfig(result, /vrf red/))
46
52
 
47
53
  assert_nil(find_subconfig(result, /vrf green/))
@@ -49,40 +55,40 @@ vrf blue",
49
55
 
50
56
  def test_node_find_ascii
51
57
  # Find an entry in the parent submode, ignoring nested submodes
52
- assert_equal(["log-adjacency-changes"],
53
- find_ascii(@@show_run_ospf, /^log-adjacency-changes.*$/,
58
+ assert_equal(['log-adjacency-changes'],
59
+ find_ascii(show_run_ospf, /^log-adjacency-changes.*$/,
54
60
  /router ospf bar/))
55
61
  # Find an entry in a nested submode
56
- assert_equal(["log-adjacency-changes detail"],
57
- find_ascii(@@show_run_ospf, /^log-adjacency-changes.*$/,
62
+ assert_equal(['log-adjacency-changes detail'],
63
+ find_ascii(show_run_ospf, /^log-adjacency-changes.*$/,
58
64
  /router ospf bar/, /vrf red/))
59
65
  # Submode exists but does not have a match
60
- assert_nil(find_ascii(@@show_run_ospf, /^log-adjacency-changes.*$/,
66
+ assert_nil(find_ascii(show_run_ospf, /^log-adjacency-changes.*$/,
61
67
  /router ospf bar/, /vrf blue/))
62
68
  # Submode does not exist
63
- assert_nil(find_ascii(@@show_run_ospf, /^log-adjacency-changes.*$/,
69
+ assert_nil(find_ascii(show_run_ospf, /^log-adjacency-changes.*$/,
64
70
  /router ospf bar/, /vrf green/))
65
71
 
66
72
  # Entry exists in submode only
67
- assert_nil(find_ascii(@@show_run_ospf, /^log-adjacency-changes.*$/,
73
+ assert_nil(find_ascii(show_run_ospf, /^log-adjacency-changes.*$/,
68
74
  /router ospf foo/))
69
75
  end
70
76
 
71
77
  def test_node_config_get
72
- result = node.config_get("show_version", "system_image")
78
+ result = node.config_get('show_version', 'system_image')
73
79
  assert_equal(result, node.system)
74
80
  end
75
81
 
76
82
  def test_node_config_get_regexp_tokens
77
- node.client.config(["interface loopback0", "shutdown"])
78
- node.client.config(["interface loopback1", "no shutdown"])
83
+ node.client.config(['interface loopback0', 'shutdown'])
84
+ node.client.config(['interface loopback1', 'no shutdown'])
79
85
 
80
- result = node.config_get("interface", "shutdown", "loopback1")
86
+ result = node.config_get('interface', 'shutdown', 'loopback1')
81
87
  assert_nil(result)
82
88
  end
83
89
 
84
90
  def test_node_token_str_to_regexp
85
- token = ["/%s/i", "/%s foo %s/", "/zzz/i"]
91
+ token = ['/%s/i', '/%s foo %s/', '/zzz/i']
86
92
  args = %w(LoopBack2 no bar)
87
93
  expected = [/LoopBack2/i, /no foo bar/, /zzz/i]
88
94
 
@@ -93,140 +99,120 @@ vrf blue",
93
99
 
94
100
  def test_node_config_get_invalid
95
101
  assert_raises IndexError do # no entry
96
- result = node.config_get("feature", "name")
102
+ node.config_get('feature', 'name')
97
103
  end
98
104
  assert_raises IndexError do # entry but no config_get
99
- result = node.config_get("show_system", "resources")
105
+ node.config_get('show_system', 'resources')
100
106
  end
101
107
  end
102
108
 
103
109
  def test_node_config_get_default
104
- result = node.config_get_default("snmp_server", "aaa_user_cache_timeout")
110
+ result = node.config_get_default('snmp_server', 'aaa_user_cache_timeout')
105
111
  assert_equal(result, 3600)
106
112
  end
107
113
 
108
114
  def test_node_config_get_default_invalid
109
115
  assert_raises IndexError do # no name entry
110
- result = node.config_get_default("show_version", "foobar")
116
+ node.config_get_default('show_version', 'foobar')
111
117
  end
112
118
  assert_raises IndexError do # no feature entry
113
- result = node.config_get_default("feature", "name")
119
+ node.config_get_default('feature', 'name')
114
120
  end
115
121
  assert_raises IndexError do # no default_value defined
116
- result = node.config_get_default("show_version", "version")
122
+ node.config_get_default('show_version', 'version')
117
123
  end
118
124
  end
119
125
 
120
126
  def test_node_config_set
121
- result = node.config_set("snmp_server",
122
- "aaa_user_cache_timeout",
123
- "", 100)
124
- run = node.client.show("show run all | inc snmp")
127
+ node.config_set('snmp_server', 'aaa_user_cache_timeout', '', 100)
128
+ run = node.client.show('show run all | inc snmp')
125
129
  val = find_one_ascii(run, /snmp-server aaa-user cache-timeout (\d+)/)
126
- assert_equal("100", val)
130
+ assert_equal('100', val)
127
131
 
128
- result = node.config_set("snmp_server",
129
- "aaa_user_cache_timeout",
130
- "no", 100)
131
- run = node.client.show("show run all | inc snmp")
132
+ node.config_set('snmp_server', 'aaa_user_cache_timeout', 'no', 100)
133
+ run = node.client.show('show run all | inc snmp')
132
134
  val = find_one_ascii(run, /snmp-server aaa-user cache-timeout (\d+)/)
133
- assert_equal("3600", val)
135
+ assert_equal('3600', val)
134
136
  end
135
137
 
136
138
  def test_node_config_set_invalid
137
139
  assert_raises IndexError do
138
- result = node.config_set("feature", "name")
140
+ node.config_set('feature', 'name')
139
141
  end
140
142
  assert_raises IndexError do # feature exists but no config_set
141
- result = node.config_set("show_version", "system_image")
143
+ node.config_set('show_version', 'system_image')
142
144
  end
143
145
  assert_raises ArgumentError do # not enough args
144
- result = node.config_set("vtp", "domain")
146
+ node.config_set('vtp', 'domain')
145
147
  end
146
148
  assert_raises ArgumentError do # too many args
147
- result = node.config_set("vtp", "domain", "example.com", "baz")
149
+ node.config_set('vtp', 'domain', 'example.com', 'baz')
148
150
  end
149
151
  end
150
152
 
151
153
  def test_node_cli_caching
152
- s = @device.cmd("conf t ; ip domain-name minitest ; end")
154
+ # don't use config() here because we are testing caching and flushing
155
+ @device.cmd('conf t ; ip domain-name minitest ; end')
153
156
  dom1 = node.domain_name
154
- s = @device.cmd("conf t ; no ip domain-name minitest ; end")
157
+ @device.cmd('conf t ; no ip domain-name minitest ; end')
155
158
  dom2 = node.domain_name
156
159
  assert_equal(dom1, dom2) # cached output was used for dom2
157
160
 
158
161
  node.cache_flush
159
162
  dom3 = node.domain_name
160
- assert_not_equal(dom1, dom3)
163
+ refute_equal(dom1, dom3)
161
164
  end
162
165
 
163
166
  def test_node_get_product_description
164
167
  product_description = node.product_description
165
- ref = cmd_ref.lookup("show_version", "description")
166
- assert(ref, "Error, reference not found")
168
+ ref = cmd_ref.lookup('show_version', 'description')
169
+ assert(ref, 'Error, reference not found')
167
170
 
168
- s = @device.cmd("#{ref.test_config_get}")
169
- pattern = ref.test_config_get_regex
170
- md = pattern.match(s)
171
- assert(md, "Error, no match found for #{pattern}")
172
- assert_equal(md[1], product_description,
173
- "Error, Product description does not match")
171
+ assert_output_check(command: ref.test_config_get,
172
+ pattern: ref.test_config_get_regex,
173
+ check: product_description,
174
+ msg: 'Error, Product description does not match')
174
175
  end
175
176
 
176
177
  def test_node_get_product_id
177
- product_id = node.product_id
178
- s = @device.cmd("show inventory | no-more")
179
- pattern = /NAME: \"Chassis\".*\nPID: (\S+)/
180
- md = pattern.match(s)
181
- assert(md, "Error, no match found for #{pattern}")
182
- assert_equal(md[1], product_id,
183
- "Error, Product id does not match")
178
+ assert_output_check(command: 'show inventory | no-more',
179
+ pattern: /NAME: \"Chassis\".*\nPID: (\S+)/,
180
+ check: node.product_id,
181
+ msg: 'Error, Product id does not match')
184
182
  end
185
183
 
186
184
  def test_node_get_product_version_id
187
- version_id = node.product_version_id
188
- s = @device.cmd("show inventory | no-more")
189
- pattern = /NAME: \"Chassis\".*\n.*VID: (\w+)/
190
- md = pattern.match(s)
191
- assert(md, "Error, no match found for #{pattern}")
192
- assert_equal(md[1], version_id,
193
- "Error, Version id does not match")
185
+ assert_output_check(command: 'show inventory | no-more',
186
+ pattern: /NAME: \"Chassis\".*\n.*VID: (\w+)/,
187
+ check: node.product_version_id,
188
+ msg: 'Error, Version id does not match')
194
189
  end
195
190
 
196
191
  def test_node_get_product_serial_number
197
- serial_number = node.product_serial_number
198
- s = @device.cmd("show inventory | no-more")
199
- pattern = /NAME: \"Chassis\".*\n.*SN: (\w+)/
200
- md = pattern.match(s)
201
- assert(md, "Error, no match found for #{pattern}")
202
- assert_equal(md[1], serial_number,
203
- "Error, Serial number does not match")
192
+ assert_output_check(command: 'show inventory | no-more',
193
+ pattern: /NAME: \"Chassis\".*\n.*SN: (\w+)/,
194
+ check: node.product_serial_number,
195
+ msg: 'Error, Serial number does not match')
204
196
  end
205
197
 
206
198
  def test_node_get_os
207
- os = node.os
208
- s = @device.cmd("show version | no-more")
209
- pattern = /\n(Cisco.*)\n/
210
- md = pattern.match(s)
211
- assert(md, "Error, no match found for #{pattern}")
212
- assert_equal(md[1], os,
213
- "Error, OS version does not match")
199
+ assert_output_check(command: 'show version | no-more',
200
+ pattern: /\n(Cisco.*)\n/,
201
+ check: node.os,
202
+ msg: 'Error, OS version does not match')
214
203
  end
215
204
 
216
205
  def test_node_get_os_version
217
- os_version = node.os_version
218
- ref = cmd_ref.lookup("show_version", "version")
219
- assert(ref, "Error, reference not found")
220
- s = @device.cmd("#{ref.test_config_get}")
221
- pattern = ref.test_config_get_regex[1]
222
- md = pattern.match(s)
223
- assert(md, "Error, no match found for #{pattern}")
224
- assert_equal(md[1], os_version,
225
- "Error, OS version does not match")
206
+ ref = cmd_ref.lookup('show_version', 'version')
207
+ assert(ref, 'Error, reference not found')
208
+ assert_output_check(command: ref.test_config_get,
209
+ pattern: ref.test_config_get_regex[1],
210
+ check: node.os_version,
211
+ msg: 'Error, OS version does not match')
226
212
  end
227
213
 
228
214
  def test_node_get_host_name_when_not_set
229
- s = @device.cmd("show running-config all | no-more")
215
+ s = @device.cmd('show running-config all | no-more')
230
216
  pattern = /.*\nhostname (\S+)/
231
217
  md = pattern.match(s)
232
218
  if md
@@ -243,31 +229,19 @@ vrf blue",
243
229
  configured_name = nil
244
230
  end
245
231
  end
246
- node.cache_flush
247
232
 
248
- @device.cmd("configure terminal")
249
- @device.cmd("no hostname") if (switchname == false)
250
- @device.cmd("no switchname") if (switchname == true)
251
- @device.cmd("end")
252
- node.cache_flush
233
+ switchname ? config('no switchname') : config('no hostname')
253
234
 
254
235
  name = node.host_name
255
- assert_equal("switch", name)
236
+ assert_equal('switch', name)
256
237
 
257
- @device.cmd("configure terminal")
258
- if configured_name
259
- @device.cmd("hostname #{configured_name}") if (switchname == false)
260
- @device.cmd("switchname #{configured_name}") if (switchname == true)
261
- else
262
- @device.cmd("no hostname") if (switchname == false)
263
- @device.cmd("no switchname") if (switchname == true)
264
- end
265
- @device.cmd("end")
266
- node.cache_flush
238
+ return unless configured_name
239
+ config("hostname #{configured_name}") if (switchname == false)
240
+ config("switchname #{configured_name}") if (switchname == true)
267
241
  end
268
242
 
269
243
  def test_node_get_host_name_when_set
270
- s = @device.cmd("show running-config all | no-more")
244
+ s = @device.cmd('show running-config all | no-more')
271
245
  pattern = /.*\nhostname (\S+)/
272
246
  md = pattern.match(s)
273
247
  if md
@@ -285,27 +259,18 @@ vrf blue",
285
259
  switchname = false
286
260
  end
287
261
  end
288
- node.cache_flush
289
262
 
290
- @device.cmd("configure terminal")
291
- @device.cmd("hostname xyz") if (switchname == false)
292
- @device.cmd("switchname xyz") if (switchname == true)
293
- @device.cmd("end")
294
- node.cache_flush
263
+ switchname ? config('switchname xyz') : config('hostname xyz')
295
264
 
296
265
  host_name = node.host_name
297
- assert_equal("xyz", host_name)
266
+ assert_equal('xyz', host_name)
298
267
 
299
- @device.cmd("configure terminal")
300
268
  if configured_name
301
- @device.cmd("hostname #{configured_name}") if (switchname == false)
302
- @device.cmd("switchname #{configured_name}") if (switchname == true)
269
+ config("hostname #{configured_name}") if (switchname == false)
270
+ config("switchname #{configured_name}") if (switchname == true)
303
271
  else
304
- @device.cmd("no hostname") if (switchname == false)
305
- @device.cmd("no switchname") if (switchname == true)
272
+ switchname ? config('no switchname') : config('no hostname')
306
273
  end
307
- @device.cmd("end")
308
- node.cache_flush
309
274
  end
310
275
 
311
276
  def test_node_get_domain_name_when_not_set
@@ -318,28 +283,21 @@ vrf blue",
318
283
  else
319
284
  configured_domain_name = nil
320
285
  end
321
- node.cache_flush
322
286
 
323
- @device.cmd("configure terminal")
324
- @device.cmd("no ip domain-name #{configured_domain_name}")
325
- @device.cmd("end")
326
- node.cache_flush
287
+ config("no ip domain-name #{configured_domain_name}")
327
288
 
328
289
  domain_name = node.domain_name
329
- assert_equal("", domain_name)
290
+ assert_equal('', domain_name)
330
291
 
331
- @device.cmd("configure terminal")
332
292
  if configured_domain_name
333
- @device.cmd("ip domain-name #{configured_domain_name}")
293
+ config("ip domain-name #{configured_domain_name}")
334
294
  else
335
- @device.cmd("no ip domain-name abc.com")
295
+ config('no ip domain-name abc.com')
336
296
  end
337
- @device.cmd("end")
338
- node.cache_flush
339
297
  end
340
298
 
341
299
  def test_node_get_domain_name_when_set
342
- s = @device.cmd("show running-config | no-more")
300
+ s = @device.cmd('show running-config | no-more')
343
301
  pattern = /.*\nip domain-name (\S+)/
344
302
  md = pattern.match(s)
345
303
  if md
@@ -347,38 +305,35 @@ vrf blue",
347
305
  else
348
306
  configured_domain_name = nil
349
307
  end
350
- node.cache_flush
351
308
 
352
- @device.cmd("configure terminal")
353
- @device.cmd("ip domain-name abc.com")
354
- @device.cmd("end")
355
- node.cache_flush
309
+ config('ip domain-name abc.com')
356
310
 
357
311
  domain_name = node.domain_name
358
- assert_equal("abc.com", domain_name)
312
+ assert_equal('abc.com', domain_name)
359
313
 
360
- @device.cmd("configure terminal")
361
314
  if configured_domain_name
362
- @device.cmd("ip domain-name #{configured_domain_name}")
315
+ config("ip domain-name #{configured_domain_name}")
363
316
  else
364
- @device.cmd("no ip domain-name abc.com")
317
+ config('no ip domain-name abc.com')
365
318
  end
366
- @device.cmd("end")
367
- node.cache_flush
368
319
  end
369
320
 
370
321
  def test_node_get_system_uptime
371
322
  node.cache_flush
323
+ # rubocop:disable Metrics/LineLength
372
324
  pattern = /.*System uptime:\s+(\d+) days, (\d+) hours, (\d+) minutes, (\d+) seconds/
325
+ # rubocop:enable Metrics/LineLength
373
326
 
374
- s = @device.cmd("show system uptime | no-more")
327
+ md = assert_show_match(command: 'show system uptime | no-more',
328
+ pattern: pattern)
375
329
  node_uptime = node.system_uptime
376
330
 
377
- md = pattern.match(s)
378
- assert(md, "Error, no match found for #{pattern}")
379
-
380
- observed_system_uptime =
381
- (md[1].to_i * 86400) + (md[2].to_i * 3600) + (md[3].to_i * 60) + (md[4].to_i)
331
+ observed_system_uptime = (
332
+ (md[1].to_i * 86_400) +
333
+ (md[2].to_i * 3600) +
334
+ (md[3].to_i * 60) +
335
+ (md[4].to_i)
336
+ )
382
337
  delta = node_uptime - observed_system_uptime
383
338
  assert(delta < 10,
384
339
  "Error, System uptime delta is (#{delta}), expected (delta < 10)")
@@ -386,42 +341,36 @@ vrf blue",
386
341
 
387
342
  def test_node_get_last_reset_time
388
343
  last_reset_time = node.last_reset_time
389
- ref = cmd_ref.lookup("show_version", "last_reset_time")
390
- assert(ref, "Error, reference not found")
391
- s = @device.cmd("#{ref.test_config_get}")
392
- pattern = ref.test_config_get_regex
393
- md = pattern.match(s)
344
+ ref = cmd_ref.lookup('show_version', 'last_reset_time')
345
+ assert(ref, 'Error, reference not found')
394
346
  # N9k doesn't provide this info at present.
395
347
  if !last_reset_time.empty?
396
- assert(md, "Error, no match found for #{pattern}")
397
- assert_equal(md[1], last_reset_time,
398
- "Error, Last reset time does not match")
348
+ assert_output_check(command: ref.test_config_get,
349
+ pattern: ref.test_config_get_regex,
350
+ check: last_reset_time,
351
+ msg: 'Error, Last reset time does not match')
399
352
  else
400
- assert(!md, "Error, output found in ASCII '#{md}' but not in node")
353
+ refute_show_match(command: ref.test_config_get,
354
+ pattern: ref.test_config_get_regex,
355
+ msg: 'output found in ASCII but not in node')
401
356
  end
402
357
  end
403
358
 
404
359
  def test_node_get_last_reset_reason
405
- last_reset_reason = node.last_reset_reason
406
- ref = cmd_ref.lookup("show_version", "last_reset_reason")
407
- assert(ref, "Error, reference not found")
408
- s = @device.cmd("#{ref.test_config_get}")
409
- pattern = ref.test_config_get_regex
410
- md = pattern.match(s)
411
- refute_nil(md, "ERROR: last reset reason not shown")
412
- assert(md, "Error, no match found for #{pattern}")
413
- assert_equal(md[1], last_reset_reason,
414
- "Error, Last reset reason does not match")
360
+ ref = cmd_ref.lookup('show_version', 'last_reset_reason')
361
+ assert(ref, 'Error, reference not found')
362
+ assert_output_check(command: ref.test_config_get,
363
+ pattern: ref.test_config_get_regex,
364
+ check: node.last_reset_reason,
365
+ msg: 'Error, Last reset reason does not match')
415
366
  end
416
367
 
417
368
  def test_node_get_system_cpu_utilization
418
369
  cpu_utilization = node.system_cpu_utilization
419
- ref = cmd_ref.lookup("system", "resources")
420
- assert(ref, "Error, reference not found")
421
- s = @device.cmd("#{ref.test_config_get}")
422
- pattern = ref.test_config_get_regex
423
- md = pattern.match(s)
424
- assert(md, "Error, md not populated, #{s}")
370
+ ref = cmd_ref.lookup('system', 'resources')
371
+ assert(ref, 'Error, reference not found')
372
+ md = assert_show_match(command: ref.test_config_get,
373
+ pattern: ref.test_config_get_regex)
425
374
  observed_cpu_utilization = md[1].to_f + md[2].to_f
426
375
  delta = cpu_utilization - observed_cpu_utilization
427
376
  assert(delta > -15.0 && delta < 15.0,
@@ -429,22 +378,20 @@ vrf blue",
429
378
  end
430
379
 
431
380
  def test_node_get_boot
432
- boot = node.boot
433
- ref = cmd_ref.lookup("show_version", "boot_image")
434
- assert(ref, "Error, reference not found")
435
- s = @device.cmd("#{ref.test_config_get}")
436
- s =~ ref.test_config_get_regex
437
- assert_equal(Regexp.last_match(1), boot,
438
- "Error, Kickstart Image does not match")
381
+ ref = cmd_ref.lookup('show_version', 'boot_image')
382
+ assert(ref, 'Error, reference not found')
383
+ assert_output_check(command: ref.test_config_get,
384
+ pattern: ref.test_config_get_regex,
385
+ check: node.boot,
386
+ msg: 'Error, Kickstart Image does not match')
439
387
  end
440
388
 
441
389
  def test_node_get_system
442
- system = node.system
443
- ref = cmd_ref.lookup("show_version", "system_image")
444
- assert(ref, "Error, reference not found")
445
- s = @device.cmd("#{ref.test_config_get}")
446
- s =~ ref.test_config_get_regex
447
- assert_equal(Regexp.last_match(1), system,
448
- "Error, System Image does not match")
390
+ ref = cmd_ref.lookup('show_version', 'system_image')
391
+ assert(ref, 'Error, reference not found')
392
+ assert_output_check(command: ref.test_config_get,
393
+ pattern: ref.test_config_get_regex,
394
+ check: node.system,
395
+ msg: 'Error, System Image does not match')
449
396
  end
450
397
  end