| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright 2017 Objectif Libre |
| 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 |
from tempest.lib.common.utils import data_utils |
| 17 |
from tempest.lib import decorators |
| 18 |
from tempest.lib import exceptions as lib_exc |
| 19 |
|
| 20 |
from cloudkitty_tempest_plugin.tests.api import base |
| 21 |
|
| 22 |
SCRIPT_DATA_ONE = """ |
| 23 |
def dumbfunc(): |
| 24 |
return 0 |
| 25 |
|
| 26 |
data = dumbfunc() |
| 27 |
""" |
| 28 |
|
| 29 |
SCRIPT_DATA_TWO = """ |
| 30 |
def dumbfunc(): |
| 31 |
return 0 |
| 32 |
|
| 33 |
data = dumbfunc() + 2 |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
class CloudkittyPyscriptAPITest(base.BaseRatingTest): |
| 38 |
api_version = 'v1' |
| 39 |
credentials = ['admin'] |
| 40 |
|
| 41 |
@decorators.idempotent_id('2015c966-b707-40f7-b84d-9aa6550b9e41') |
| 42 |
def test_get_pyscripts(self): |
| 43 |
self.rating_client.get_pyscripts() |
| 44 |
|
| 45 |
@decorators.idempotent_id('9e78cccc-ca85-42ce-8648-4cd9682375df') |
| 46 |
def test_create_update_delete_pyscript(self): |
| 47 |
pyscript = self.rating_client.create_pyscript( |
| 48 |
data_utils.rand_name('dummy_script'), |
| 49 |
SCRIPT_DATA_ONE, |
| 50 |
) |
| 51 |
self._created_resources['pyscript'].append(pyscript['script_id']) |
| 52 |
self.assertEqual(pyscript['data'], SCRIPT_DATA_ONE) |
| 53 |
# We are creating this flag to allow the test to work for |
| 54 |
# both old and new API version. We can remove this validation |
| 55 |
# in the end of life of version 2024.1. |
| 56 |
old_version = True |
| 57 |
try: |
| 58 |
self.rating_client.update_pyscript(pyscript['script_id'], |
| 59 |
data=SCRIPT_DATA_TWO, |
| 60 |
name=pyscript['name']) |
| 61 |
except lib_exc.BadRequest as e: |
| 62 |
old_version = False |
| 63 |
self.assertTrue("You are allowed to update only the attribute " |
| 64 |
"[end] as this rule is already running as it " |
| 65 |
"started on " in e.resp_body['faultstring']) |
| 66 |
|
| 67 |
if old_version: |
| 68 |
pyscript = self.rating_client.get_pyscript(pyscript['script_id']) |
| 69 |
self.assertEqual(pyscript['data'], SCRIPT_DATA_TWO) |
| 70 |
self.rating_client.delete_pyscript(pyscript['script_id']) |
| 71 |
return |
| 72 |
|
| 73 |
end_date = '3000-01-01T23:59:59' |
| 74 |
|
| 75 |
self.rating_client.update_pyscript(pyscript['script_id'], |
| 76 |
name=pyscript['name'], |
| 77 |
end=end_date) |
| 78 |
try: |
| 79 |
self.rating_client.update_pyscript(pyscript['script_id'], |
| 80 |
name=pyscript['name'], |
| 81 |
end=end_date) |
| 82 |
except lib_exc.BadRequest as e: |
| 83 |
self.assertTrue("Cannot update a rule that was already " |
| 84 |
"processed and has a defined end date." |
| 85 |
in e.resp_body['faultstring']) |
| 86 |
|
| 87 |
pyscript = self.rating_client.get_pyscript(pyscript['script_id']) |
| 88 |
self.assertEqual(pyscript['data'], SCRIPT_DATA_ONE) |
| 89 |
self.assertEqual(pyscript['end'], end_date) |
| 90 |
self.rating_client.delete_pyscript(pyscript['script_id']) |
| 91 |
|
| 92 |
@decorators.idempotent_id('3fbaf8b4-c472-4509-8d73-55dc4a87a442') |
| 93 |
def test_get_pyscript(self): |
| 94 |
pyscript = self.rating_client.create_pyscript( |
| 95 |
data_utils.rand_name('dummy_script'), |
| 96 |
SCRIPT_DATA_ONE, |
| 97 |
) |
| 98 |
self.assertEqual(pyscript['data'], SCRIPT_DATA_ONE) |
| 99 |
self._created_resources['pyscript'].append(pyscript['script_id']) |
| 100 |
pyscript = self.rating_client.get_pyscript(pyscript['script_id']) |
| 101 |
|
| 102 |
|
| 103 |
class CloudkittyPyscriptAPITestNegative(base.BaseRatingTest): |
| 104 |
api_version = 'v1' |
| 105 |
credentials = ['admin'] |
| 106 |
|
| 107 |
@decorators.idempotent_id('999c97cc-1d71-43b8-988f-d89b8fac4040') |
| 108 |
@decorators.attr(type=['negative']) |
| 109 |
def test_update_script_negative(self): |
| 110 |
pyscript = self.rating_client.create_pyscript( |
| 111 |
data_utils.rand_name('dummy_script'), |
| 112 |
SCRIPT_DATA_ONE, |
| 113 |
) |
| 114 |
self._created_resources['pyscript'].append(pyscript['script_id']) |
| 115 |
fake_id = data_utils.rand_uuid() |
| 116 |
self.assertRaises(lib_exc.NotFound, |
| 117 |
self.rating_client.get_pyscript, |
| 118 |
fake_id) |