Add PaaS service creation UTs
[osm/osmclient.git] / osmclient / scripts / tests / test_ns_operations.py
1 # Copyright 2022 Canonical Ltd.
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
16 import unittest
17 from unittest.mock import Mock, patch
18 from click.testing import CliRunner
19 from osmclient.common.exceptions import ClientException
20 from osmclient.scripts import osm
21
22
23 @patch("osmclient.scripts.osm.client.Client")
24 class TestNS(unittest.TestCase):
25 def setUp(self):
26 self.runner = CliRunner()
27 self.ctx_obj = Mock()
28
29 def test_ns_create_with_vim_account(self, mock_client):
30 mock_client.return_value = self.ctx_obj
31 result = self.runner.invoke(
32 osm.cli_osm,
33 [
34 "ns-create",
35 "--ns_name",
36 "ns_name",
37 "--nsd_name",
38 "nsd_name",
39 "--vim_account",
40 "vim_account",
41 ],
42 )
43 self.ctx_obj.ns.create.assert_called_with(
44 "nsd_name",
45 "ns_name",
46 config=None,
47 ssh_keys=None,
48 vim_account="vim_account",
49 paas_account=None,
50 admin_status="ENABLED",
51 wait=False,
52 timeout=None,
53 )
54 assert not result.exception
55
56 def test_ns_create_with_paas_account(self, mock_client):
57 mock_client.return_value = self.ctx_obj
58 result = self.runner.invoke(
59 osm.cli_osm,
60 [
61 "ns-create",
62 "--ns_name",
63 "ns_name",
64 "--nsd_name",
65 "nsd_name",
66 "--paas_account",
67 "paas_account",
68 ],
69 )
70 self.ctx_obj.ns.create.assert_called_with(
71 "nsd_name",
72 "ns_name",
73 config=None,
74 ssh_keys=None,
75 vim_account=None,
76 paas_account="paas_account",
77 admin_status="ENABLED",
78 wait=False,
79 timeout=None,
80 )
81 assert not result.exception
82
83 def test_ns_create_with_paas_and_vim_account_raises_exception(self, mock_client):
84 mock_client.return_value = self.ctx_obj
85 result = self.runner.invoke(
86 osm.cli_osm,
87 [
88 "ns-create",
89 "--ns_name",
90 "ns_name",
91 "--nsd_name",
92 "nsd_name",
93 "--vim_account",
94 "vim_account",
95 "--paas_account",
96 "paas_account",
97 ],
98 )
99 self.ctx_obj.ns.create.assert_not_called()
100 expected_msg = '"vim_account" and "paas_account" can not be used together, use only one of them'
101 assert result.exception
102 assert isinstance(result.exception, ClientException)
103 assert expected_msg in str(result.exception)
104
105 def test_ns_creat_without_paas_or_vim_account_raises_exception(self, mock_client):
106 mock_client.return_value = self.ctx_obj
107 result = self.runner.invoke(
108 osm.cli_osm, ["ns-create", "--ns_name", "ns_name", "--nsd_name", "nsd_name"]
109 )
110 self.ctx_obj.ns.create.assert_not_called()
111 expected_msg = (
112 'specify "vim_account" or "paas_account", both options can not be empty'
113 )
114 assert result.exception
115 assert isinstance(result.exception, ClientException)
116 assert expected_msg in str(result.exception)