2 # ============LICENSE_START=======================================================
4 # ================================================================================
5 # Copyright (C) 2019 Nokia. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
23 from mock import patch
25 from cli.netconf_simulator import create_argument_parser, NetconfSimulatorClient
28 class TestArgumentParser(unittest.TestCase):
30 def test_should_properly_parse_edit_config_with_all_params(self):
31 parser = create_argument_parser()
32 args = parser.parse_args(
33 ['edit-config', '--address', '127.0.0.1', '--config', 'sample_path',
37 self.assertEqual(args.address, '127.0.0.1')
38 self.assertEqual(args.config, 'sample_path')
39 self.assertTrue(args.verbose)
41 def test_should_properly_parse_load_yang_model(self):
42 parser = create_argument_parser()
44 args = parser.parse_args(
45 ['load-model', '--address', '127.0.0.1', '--module-name',
46 'sample_name', '--yang-model', 'sample_model', '--config',
51 self.assertEqual(args.address, '127.0.0.1')
52 self.assertEqual(args.config, 'sample_config')
53 self.assertEqual(args.yang_model, 'sample_model')
54 self.assertEqual(args.module_name, 'sample_name')
55 self.assertTrue(args.verbose)
57 def test_should_properly_parse_delete_yang_model(self):
58 parser = create_argument_parser()
60 args = parser.parse_args(
61 ['delete-model', '--address', '127.0.0.1', '--model-name',
62 'sample_name', "--verbose"]
65 self.assertEqual(args.address, '127.0.0.1')
66 self.assertEqual(args.model_name, 'sample_name')
67 self.assertTrue(args.verbose)
69 def test_should_properly_parse_get_config(self):
70 parser = create_argument_parser()
71 args = parser.parse_args(
72 ['get-config', '--address', '127.0.0.1', '--verbose']
75 self.assertEqual(args.address, '127.0.0.1')
76 self.assertTrue(args.verbose)
79 class TestNetconfSimulatorClient(unittest.TestCase):
83 with open("example", "w+") as f:
84 f.write("sampleContent")
87 def tearDownClass(cls):
90 @patch('cli.netconf_simulator.requests')
91 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
92 def test_should_properly_get_config(self, logger, requests):
93 client = NetconfSimulatorClient('localhost')
94 client.logger = logging.getLogger()
98 requests.get.assert_called_with('http://localhost:8080/netconf/get')
100 @patch('cli.netconf_simulator.requests')
101 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
102 def test_should_properly_get_config_for_given_module(self, logger, requests):
103 client = NetconfSimulatorClient('localhost')
104 client.logger = logging.getLogger()
106 client.get_config("module", "container")
108 requests.get.assert_called_with('http://localhost:8080/netconf/get/module/container')
110 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
111 def test_should_raise_exception_when_module_is_present_and_container_is_absent(self, logger):
112 client = NetconfSimulatorClient('localhost')
113 client.logger = logging.getLogger()
115 with self.assertRaises(AttributeError) as context: # pylint: disable=W0612
116 client.get_config(module_name="test")
118 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
119 def test_should_raise_exception_when_module_is_absent_and_container_is_present(self, logger):
120 client = NetconfSimulatorClient('localhost')
121 client.logger = logging.getLogger()
123 with self.assertRaises(AttributeError) as context: # pylint: disable=W0612
124 client.get_config(container="test")
126 @patch('cli.netconf_simulator.requests')
127 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
128 def test_should_properly_load_yang_model(self, logger, requests):
129 client = NetconfSimulatorClient('localhost')
130 client.logger = logging.getLogger()
132 client.load_yang_model('sample_module_name', 'example', 'example')
134 requests.post.assert_called()
136 @patch('cli.netconf_simulator.requests')
137 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
138 def test_should_properly_delete_yang_model(self, logger, requests):
139 client = NetconfSimulatorClient('localhost')
140 client.logger = logging.getLogger()
142 client.delete_yang_model('sample_model_name')
144 requests.delete.assert_called()
146 @patch('cli.netconf_simulator.requests')
147 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
148 def test_should_properly_edit_config(self, logger, requests):
149 client = NetconfSimulatorClient('localhost')
150 client.logger = logging.getLogger()
152 client.edit_config('example')
154 requests.post.assert_called()
156 @patch('cli.netconf_simulator.requests')
157 @patch('cli.netconf_simulator.NetconfSimulatorClient._configure_logger')
158 def test_should_properly_run_less_like_mode(self, logger, requests):
159 client = NetconfSimulatorClient('localhost')
160 client.logger = logging.getLogger()
162 client.less_like_func(100)
164 requests.get.assert_called_with(
165 params={"offset": 100}, url="http://localhost:8080/store/less")