1# Copyright 2015 Huawei Technologies India Pvt Ltd.
2# All Rights Reserved
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15#
16
17import sys
18
19from neutronclient.neutron.v2_0.qos import policy as policy
20from neutronclient.tests.unit import test_cli20
21
22
23class CLITestV20QoSPolicyJSON(test_cli20.CLITestV20Base):
24
25    non_admin_status_resources = ['policy']
26
27    def setUp(self):
28        super(CLITestV20QoSPolicyJSON, self).setUp()
29        self.res = 'policy'
30        self.cmd_res = 'qos_policy'
31        self.ress = "policies"
32        self.cmd_ress = 'qos_policies'
33
34    def test_create_policy_with_only_keyattributes(self):
35        # Create qos policy abc.
36        cmd = policy.CreateQoSPolicy(test_cli20.MyApp(sys.stdout),
37                                     None)
38        myid = 'myid'
39        name = 'abc'
40        args = [name]
41        position_names = ['name']
42        position_values = [name]
43        self._test_create_resource(self.res, cmd, name, myid, args,
44                                   position_names, position_values,
45                                   cmd_resource=self.cmd_res)
46
47    def test_create_policy_with_description(self):
48        # Create qos policy xyz --description abc.
49        cmd = policy.CreateQoSPolicy(test_cli20.MyApp(sys.stdout),
50                                     None)
51        myid = 'myid'
52        name = 'abc'
53        description = 'policy_abc'
54        args = [name, '--description', description]
55        position_names = ['name', 'description']
56        position_values = [name, description]
57        self._test_create_resource(self.res, cmd, name, myid, args,
58                                   position_names, position_values,
59                                   cmd_resource=self.cmd_res)
60
61    def test_create_policy_with_shared(self):
62        # Create qos policy abc shared across tenants
63        cmd = policy.CreateQoSPolicy(test_cli20.MyApp(sys.stdout),
64                                     None)
65        myid = 'myid'
66        name = 'abc'
67        description = 'policy_abc'
68        args = [name, '--description', description, '--shared']
69        position_names = ['name', 'description', 'shared']
70        position_values = [name, description, True]
71        self._test_create_resource(self.res, cmd, name, myid, args,
72                                   position_names, position_values,
73                                   cmd_resource=self.cmd_res)
74
75    def test_create_policy_with_unicode(self):
76        # Create qos policy u'\u7f51\u7edc'.
77        cmd = policy.CreateQoSPolicy(test_cli20.MyApp(sys.stdout),
78                                     None)
79        myid = 'myid'
80        name = u'\u7f51\u7edc'
81        description = u'\u7f51\u7edc'
82        args = [name, '--description', description]
83        position_names = ['name', 'description']
84        position_values = [name, description]
85        self._test_create_resource(self.res, cmd, name, myid, args,
86                                   position_names, position_values,
87                                   cmd_resource=self.cmd_res)
88
89    def test_update_policy(self):
90        # policy-update myid --name newname.
91        cmd = policy.UpdateQoSPolicy(test_cli20.MyApp(sys.stdout),
92                                     None)
93        self._test_update_resource(self.res, cmd, 'myid',
94                                   ['myid', '--name', 'newname'],
95                                   {'name': 'newname', },
96                                   cmd_resource=self.cmd_res)
97
98    def test_update_policy_description(self):
99        # policy-update myid --name newname --description newdesc
100        cmd = policy.UpdateQoSPolicy(test_cli20.MyApp(sys.stdout),
101                                     None)
102        self._test_update_resource(self.res, cmd, 'myid',
103                                   ['myid', '--description', 'newdesc'],
104                                   {'description': 'newdesc', },
105                                   cmd_resource=self.cmd_res)
106
107    def test_update_policy_to_shared(self):
108        # policy-update myid --shared
109        cmd = policy.UpdateQoSPolicy(test_cli20.MyApp(sys.stdout),
110                                     None)
111        self._test_update_resource(self.res, cmd, 'myid',
112                                   ['myid', '--shared'],
113                                   {'shared': True, },
114                                   cmd_resource=self.cmd_res)
115
116    def test_update_policy_to_no_shared(self):
117        # policy-update myid --no-shared
118        cmd = policy.UpdateQoSPolicy(test_cli20.MyApp(sys.stdout),
119                                     None)
120        self._test_update_resource(self.res, cmd, 'myid',
121                                   ['myid', '--no-shared'],
122                                   {'shared': False, },
123                                   cmd_resource=self.cmd_res)
124
125    def test_update_policy_to_shared_no_shared_together(self):
126        # policy-update myid --shared --no-shared
127        cmd = policy.UpdateQoSPolicy(test_cli20.MyApp(sys.stdout),
128                                     None)
129        self.assertRaises(
130            SystemExit,
131            self._test_update_resource,
132            self.res, cmd, 'myid',
133            ['myid', '--shared', '--no-shared'], {},
134            cmd_resource=self.cmd_res
135        )
136
137    def test_list_policies(self):
138        # qos-policy-list.
139        cmd = policy.ListQoSPolicy(test_cli20.MyApp(sys.stdout),
140                                   None)
141        self._test_list_resources(self.ress, cmd, True,
142                                  cmd_resources=self.cmd_ress)
143
144    def test_list_policies_pagination(self):
145        # qos-policy-list for pagination.
146        cmd = policy.ListQoSPolicy(test_cli20.MyApp(sys.stdout),
147                                   None)
148        self._test_list_resources_with_pagination(self.ress, cmd,
149                                                  cmd_resources=self.cmd_ress)
150
151    def test_list_policies_sort(self):
152        # sorted list: qos-policy-list --sort-key name --sort-key id
153        # --sort-key asc --sort-key desc
154        cmd = policy.ListQoSPolicy(test_cli20.MyApp(sys.stdout),
155                                   None)
156        self._test_list_resources(self.ress, cmd,
157                                  sort_key=["name", "id"],
158                                  sort_dir=["asc", "desc"],
159                                  cmd_resources=self.cmd_ress)
160
161    def test_list_policies_limit(self):
162        # size (1000) limited list: qos-policy-list -P.
163        cmd = policy.ListQoSPolicy(test_cli20.MyApp(sys.stdout),
164                                   None)
165        self._test_list_resources(self.ress, cmd, page_size=1000,
166                                  cmd_resources=self.cmd_ress)
167
168    def test_show_policy_id(self):
169        # qos-policy-show test_id.
170        cmd = policy.ShowQoSPolicy(test_cli20.MyApp(sys.stdout),
171                                   None)
172        args = ['--fields', 'id', self.test_id]
173        self._test_show_resource(self.res, cmd, self.test_id, args,
174                                 ['id'], cmd_resource=self.cmd_res)
175
176    def test_show_policy_name(self):
177        # qos-policy-show.
178        cmd = policy.ShowQoSPolicy(test_cli20.MyApp(sys.stdout),
179                                   None)
180        args = ['--fields', 'id', '--fields', 'name', self.test_id]
181        self._test_show_resource(self.res, cmd, self.test_id,
182                                 args, ['id', 'name'],
183                                 cmd_resource=self.cmd_res)
184
185    def test_delete_policy(self):
186        # qos-policy-delete my-id.
187        cmd = policy.DeleteQoSPolicy(test_cli20.MyApp(sys.stdout),
188                                     None)
189        my_id = 'myid1'
190        args = [my_id]
191        self._test_delete_resource(self.res, cmd, my_id, args,
192                                   cmd_resource=self.cmd_res)
193