"Fossies" - the Fresh Open Source Software Archive

Member "senlin-8.0.0/senlin/tests/unit/objects/requests/test_policies.py" (16 Oct 2019, 9328 Bytes) of package /linux/misc/openstack/senlin-8.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 import copy
   13 
   14 from oslo_serialization import jsonutils
   15 from senlin.objects.requests import policies
   16 from senlin.tests.unit.common import base as test_base
   17 
   18 
   19 class TestPolicyList(test_base.SenlinTestCase):
   20 
   21     def test_policy_list_request_body_full(self):
   22         params = {
   23             'name': ['policy1'],
   24             'type': ['senlin.policy.scaling-1.0'],
   25             'limit': 2,
   26             'marker': 'd6901ce0-1403-4b9c-abf5-25c59cf79823',
   27             'sort': 'name:asc',
   28             'project_safe': False
   29         }
   30         sot = policies.PolicyListRequest(**params)
   31         self.assertEqual(['policy1'], sot.name)
   32         self.assertEqual(['senlin.policy.scaling-1.0'], sot.type)
   33         self.assertEqual(2, sot.limit)
   34         self.assertEqual('d6901ce0-1403-4b9c-abf5-25c59cf79823', sot.marker)
   35         self.assertEqual('name:asc', sot.sort)
   36         self.assertFalse(sot.project_safe)
   37 
   38 
   39 class TestPolicyCreate(test_base.SenlinTestCase):
   40 
   41     spec = {
   42         "properties": {
   43             "adjustment": {
   44                 "min_step": 1,
   45                 "number": 1,
   46                 "type": "CHANGE_IN_CAPACITY"
   47             },
   48             "event": "CLUSTER_SCALE_IN"
   49         },
   50         "type": "senlin.policy.scaling",
   51         "version": "1.0"
   52     }
   53 
   54     def test_policy_create_body(self):
   55         spec = copy.deepcopy(self.spec)
   56         sot = policies.PolicyCreateRequestBody(name='foo', spec=spec)
   57         self.assertEqual('foo', sot.name)
   58         self.assertEqual('senlin.policy.scaling', sot.spec['type'])
   59         self.assertEqual('1.0', sot.spec['version'])
   60 
   61     def test_policy_create_request(self):
   62         spec = copy.deepcopy(self.spec)
   63         policy = policies.PolicyCreateRequestBody(name='foo', spec=spec)
   64         sot = policies.PolicyCreateRequest(policy=policy)
   65 
   66         self.assertIsInstance(sot.policy, policies.PolicyCreateRequestBody)
   67 
   68     def test_request_body_to_primitive(self):
   69         spec = copy.deepcopy(self.spec)
   70         sot = policies.PolicyCreateRequestBody(name='foo', spec=spec)
   71         self.assertEqual('foo', sot.name)
   72 
   73         res = sot.obj_to_primitive()
   74         # request body
   75         self.assertEqual('PolicyCreateRequestBody', res['senlin_object.name'])
   76         self.assertEqual('1.0', res['senlin_object.version'])
   77         self.assertEqual('senlin', res['senlin_object.namespace'])
   78         self.assertIn('name', res['senlin_object.changes'])
   79         self.assertIn('spec', res['senlin_object.changes'])
   80         # spec
   81         data = res['senlin_object.data']
   82         self.assertEqual(u'foo', data['name'])
   83         spec_data = jsonutils.loads(data['spec'])
   84         self.assertEqual('senlin.policy.scaling', spec_data['type'])
   85         self.assertEqual('1.0', spec_data['version'])
   86 
   87     def test_request_to_primitive(self):
   88         spec = copy.deepcopy(self.spec)
   89         body = policies.PolicyCreateRequestBody(name='foo', spec=spec)
   90         sot = policies.PolicyCreateRequest(policy=body)
   91 
   92         self.assertIsInstance(sot.policy, policies.PolicyCreateRequestBody)
   93         self.assertEqual('foo', sot.policy.name)
   94 
   95         res = sot.obj_to_primitive()
   96         self.assertIn('policy', res['senlin_object.changes'])
   97         self.assertEqual('PolicyCreateRequest', res['senlin_object.name'])
   98         self.assertEqual('senlin', res['senlin_object.namespace'])
   99         self.assertEqual('1.0', res['senlin_object.version'])
  100 
  101         data = res['senlin_object.data']['policy']
  102         self.assertEqual('PolicyCreateRequestBody', data['senlin_object.name'])
  103         self.assertEqual('senlin', data['senlin_object.namespace'])
  104         self.assertEqual('1.0', data['senlin_object.version'])
  105         self.assertIn('name', data['senlin_object.changes'])
  106         self.assertIn('spec', data['senlin_object.changes'])
  107 
  108         pd = data['senlin_object.data']
  109         self.assertEqual(u'foo', pd['name'])
  110 
  111         spec_data = jsonutils.loads(pd['spec'])
  112         self.assertEqual('senlin.policy.scaling', spec_data['type'])
  113         self.assertEqual('1.0', spec_data['version'])
  114 
  115 
  116 class TestPolicyGet(test_base.SenlinTestCase):
  117 
  118     def test_policy_get(self):
  119         sot = policies.PolicyGetRequest(identity='foo')
  120 
  121         self.assertEqual('foo', sot.identity)
  122 
  123 
  124 class TestPolicyUpdate(test_base.SenlinTestCase):
  125 
  126     def test_policy_update_body(self):
  127         data = {'name': 'foo'}
  128         sot = policies.PolicyUpdateRequestBody(**data)
  129         self.assertEqual('foo', sot.name)
  130 
  131     def test_policy_update(self):
  132         data = {'name': 'foo'}
  133         body = policies.PolicyUpdateRequestBody(**data)
  134 
  135         request = {
  136             'identity': 'pid',
  137             'policy': body
  138         }
  139         sot = policies.PolicyUpdateRequest(**request)
  140         self.assertEqual('pid', sot.identity)
  141         self.assertIsInstance(sot.policy, policies.PolicyUpdateRequestBody)
  142 
  143     def test_policy_data_to_primitive(self):
  144         data = {'name': 'foo'}
  145         sot = policies.PolicyUpdateRequestBody(**data)
  146         res = sot.obj_to_primitive()
  147 
  148         self.assertIn('name', res['senlin_object.changes'])
  149         self.assertEqual(u'foo', res['senlin_object.data']['name'])
  150         self.assertEqual('PolicyUpdateRequestBody', res['senlin_object.name'])
  151         self.assertEqual('senlin', res['senlin_object.namespace'])
  152         self.assertEqual('1.0', res['senlin_object.version'])
  153 
  154     def test_request_to_primitive(self):
  155         data = {'name': 'foo'}
  156         name = policies.PolicyUpdateRequestBody(**data)
  157 
  158         request = {
  159             'identity': 'pid',
  160             'name': name
  161         }
  162         sot = policies.PolicyUpdateRequest(**request)
  163         res = sot.obj_to_primitive()
  164 
  165         self.assertIn('identity', res['senlin_object.changes'])
  166         self.assertEqual(u'pid', res['senlin_object.data']['identity'])
  167         self.assertEqual('PolicyUpdateRequest', res['senlin_object.name'])
  168         self.assertEqual('senlin', res['senlin_object.namespace'])
  169         self.assertEqual('1.0', res['senlin_object.version'])
  170 
  171 
  172 class TestPolicyValidate(test_base.SenlinTestCase):
  173     spec = {
  174         "properties": {
  175             "adjustment": {
  176                 "min_step": 1,
  177                 "number": 1,
  178                 "type": "CHANGE_IN_CAPACITY"
  179             },
  180             "event": "CLUSTER_SCALE_IN"
  181         },
  182         "type": "senlin.policy.scaling",
  183         "version": "1.0"
  184     }
  185 
  186     def test_validate_request_body(self):
  187         spec = copy.deepcopy(self.spec)
  188         body = policies.PolicyValidateRequestBody(spec=spec)
  189 
  190         self.assertEqual(spec['type'], body.spec['type'])
  191         self.assertEqual(spec['version'], body.spec['version'])
  192 
  193     def test_validate_request(self):
  194         spec = copy.deepcopy(self.spec)
  195         body = policies.PolicyValidateRequestBody(spec=spec)
  196         policy = policies.PolicyValidateRequest(policy=body)
  197 
  198         self.assertIsInstance(
  199             policy.policy, policies.PolicyValidateRequestBody)
  200 
  201     def test_request_body_to_primitive(self):
  202         spec = copy.deepcopy(self.spec)
  203 
  204         sot = policies.PolicyValidateRequestBody(spec=spec)
  205         res = sot.obj_to_primitive()
  206 
  207         self.assertIn('spec', res['senlin_object.changes'])
  208         self.assertEqual(
  209             'PolicyValidateRequestBody', res['senlin_object.name'])
  210         self.assertEqual('senlin', res['senlin_object.namespace'])
  211         self.assertEqual('1.0', res['senlin_object.version'])
  212 
  213         pd = res['senlin_object.data']['spec']
  214         data = jsonutils.loads(pd)
  215         self.assertEqual('senlin.policy.scaling', data['type'])
  216         self.assertEqual('1.0', data['version'])
  217 
  218     def test_request_to_primitive(self):
  219         spec = copy.deepcopy(self.spec)
  220         body = policies.PolicyValidateRequestBody(spec=spec)
  221         policy = policies.PolicyValidateRequest(policy=body)
  222 
  223         res = policy.obj_to_primitive()
  224 
  225         self.assertIn('policy', res['senlin_object.changes'])
  226         self.assertEqual('PolicyValidateRequest', res['senlin_object.name'])
  227         self.assertEqual('senlin', res['senlin_object.namespace'])
  228         self.assertEqual('1.0', res['senlin_object.version'])
  229 
  230         body = res['senlin_object.data']['policy']
  231 
  232         self.assertIn('spec', body['senlin_object.changes'])
  233         self.assertEqual(
  234             'PolicyValidateRequestBody', body['senlin_object.name'])
  235         self.assertEqual('senlin', body['senlin_object.namespace'])
  236         self.assertEqual('1.0', body['senlin_object.version'])
  237 
  238         pd = body['senlin_object.data']['spec']
  239         data = jsonutils.loads(pd)
  240         self.assertEqual('senlin.policy.scaling', data['type'])
  241         self.assertEqual('1.0', data['version'])
  242 
  243 
  244 class TestPolicyDelete(test_base.SenlinTestCase):
  245 
  246     def test_policy_delete(self):
  247         sot = policies.PolicyDeleteRequest(identity='foo')
  248 
  249         self.assertEqual('foo', sot.identity)