Fix Python linting issues in Python scripts
[integration.git] / test / mocks / pnfsimulator / simulator-cli / tests / test_netconf_simulator.py
1 ###
2 # ============LICENSE_START=======================================================
3 # Simulator
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
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
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=========================================================
19 ###
20 import logging
21 import unittest
22 import os
23 from mock import patch
24
25 from cli.netconf_simulator import create_argument_parser, NetconfSimulatorClient
26
27
28 class TestArgumentParser(unittest.TestCase):
29
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',
34              "--verbose"]
35         )
36
37         self.assertEqual(args.address, '127.0.0.1')
38         self.assertEqual(args.config, 'sample_path')
39         self.assertTrue(args.verbose)
40
41     def test_should_properly_parse_load_yang_model(self):
42         parser = create_argument_parser()
43
44         args = parser.parse_args(
45             ['load-model', '--address', '127.0.0.1', '--module-name',
46              'sample_name', '--yang-model', 'sample_model', '--config',
47              'sample_config',
48              "--verbose"]
49         )
50
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)
56
57     def test_should_properly_parse_delete_yang_model(self):
58         parser = create_argument_parser()
59
60         args = parser.parse_args(
61             ['delete-model', '--address', '127.0.0.1', '--model-name',
62              'sample_name', "--verbose"]
63         )
64
65         self.assertEqual(args.address, '127.0.0.1')
66         self.assertEqual(args.model_name, 'sample_name')
67         self.assertTrue(args.verbose)
68
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']
73         )
74
75         self.assertEqual(args.address, '127.0.0.1')
76         self.assertTrue(args.verbose)
77
78
79 class TestNetconfSimulatorClient(unittest.TestCase):
80
81     @classmethod
82     def setUpClass(cls):
83         with open("example", "w+") as f:
84             f.write("sampleContent")
85
86     @classmethod
87     def tearDownClass(cls):
88         os.remove("example")
89
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()
95
96         client.get_config()
97
98         requests.get.assert_called_with('http://localhost:8080/netconf/get')
99
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()
105
106         client.get_config("module", "container")
107
108         requests.get.assert_called_with('http://localhost:8080/netconf/get/module/container')
109
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()
114
115         with self.assertRaises(AttributeError) as context: # pylint: disable=W0612
116             client.get_config(module_name="test")
117
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()
122
123         with self.assertRaises(AttributeError) as context: # pylint: disable=W0612
124             client.get_config(container="test")
125
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()
131
132         client.load_yang_model('sample_module_name', 'example', 'example')
133
134         requests.post.assert_called()
135
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()
141
142         client.delete_yang_model('sample_model_name')
143
144         requests.delete.assert_called()
145
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()
151
152         client.edit_config('example')
153
154         requests.post.assert_called()
155
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()
161
162         client.less_like_func(100)
163
164         requests.get.assert_called_with(
165             params={"offset": 100}, url="http://localhost:8080/store/less")