diff --git a/compass/db/api/machine.py b/compass/db/api/machine.py index 563505ee..741e2120 100644 --- a/compass/db/api/machine.py +++ b/compass/db/api/machine.py @@ -116,5 +116,5 @@ def patch_machine(session, updater, machine_id, **kwargs): @utils.wrap_to_dict(RESP_FIELDS) def del_machine(session, deleter, machine_id, **kwargs): """Delete a machine.""" - machine = utils.get_db_object(session, models.Switch, id=machine_id) + machine = utils.get_db_object(session, models.Machine, id=machine_id) return utils.del_db_object(session, machine) diff --git a/compass/db/api/utils.py b/compass/db/api/utils.py index 5efc0560..779a2582 100644 --- a/compass/db/api/utils.py +++ b/compass/db/api/utils.py @@ -302,7 +302,9 @@ def filter_output(filter_callbacks, filters, obj, missing_ok=False): if missing_ok: continue else: - return False + raise exception.InvalidResponse( + '%s is not in %s' % (callback_key, obj) + ) if not callback_value( filters[callback_key], obj[callback_key] ): @@ -378,6 +380,7 @@ def output_validates(**kwargs_validators): _output_validates(kwargs_validators, obj_item) else: _output_validates(kwargs_validators, obj) + return obj return wrapper return decorator diff --git a/compass/db/models.py b/compass/db/models.py index 0adf7812..22b40993 100644 --- a/compass/db/models.py +++ b/compass/db/models.py @@ -26,7 +26,6 @@ from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.hybrid import hybrid_property -from sqlalchemy.ext.mutable import MutableDict from sqlalchemy import Float from sqlalchemy import ForeignKey from sqlalchemy import Integer @@ -254,7 +253,7 @@ class FieldMixin(HelperMixin): class InstallerMixin(HelperMixin): name = Column(String(80)) instance_name = Column(String(80), unique=True) - settings = Column(MutableDict.as_mutable(JSONEncoded), default={}) + settings = Column(JSONEncoded, default={}) def validate(self): if not self.name: diff --git a/compass/tests/db/api/test_machine.py b/compass/tests/db/api/test_machine.py new file mode 100644 index 00000000..62a4ba71 --- /dev/null +++ b/compass/tests/db/api/test_machine.py @@ -0,0 +1,182 @@ +# Copyright 2014 Huawei Technologies Co. Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime +import logging +import os +import unittest2 + +from compass.db.api import database +from compass.db.api import machine +from compass.db.api import switch +from compass.db.api import user as user_api +from compass.db import exception +from compass.utils import flags +from compass.utils import logsetting +from compass.utils import setting_wrapper as setting +reload(setting) + +os.environ['COMPASS_IGNORE_SETTING'] = 'true' + + +class BaseTest(unittest2.TestCase): + """Base Class for unit test.""" + + def setUp(self): + super(BaseTest, self).setUp() + database.init('sqlite://') + database.create_db() + self.user_object = ( + user_api.get_user_object( + setting.COMPASS_ADMIN_EMAIL + ) + ) + + def tearDown(self): + super(BaseTest, self).setUp() + database.drop_db() + + +class TestGetMachine(BaseTest): + """Test get machine.""" + + def setUp(self): + super(TestGetMachine, self).setUp() + logsetting.init() + + def tearDown(self): + super(TestGetMachine, self).tearDown() + + def test_get_machine(self): + switch.add_switch_machine( + self.user_object, + 1, + mac='28:6e:d4:46:c4:25', + port=1 + ) + get_machine = machine.get_machine( + self.user_object, + 1 + ) + self.assertIsNotNone(get_machine) + + +class TestListMachines(BaseTest): + """Test list machines.""" + + def setUp(self): + super(TestListMachines, self).setUp() + logsetting.init() + + def tearDown(self): + super(TestListMachines, self).tearDown() + + def test_list_machines(self): + switch.add_switch_machine( + self.user_object, + 1, + mac='28:6e:d4:46:c4:25', + port=1 + ) + list_machine = machine.list_machines(self.user_object) + self.assertIsNotNone(list_machine) + + +class TestUpdateMachine(BaseTest): + """Test update machine.""" + + def setUp(self): + super(TestUpdateMachine, self).setUp() + logsetting.init() + + def tearDown(self): + super(TestUpdateMachine, self).tearDown() + + def test_update_machine(self): + switch.add_switch_machine( + self.user_object, + 1, + mac='28:6e:d4:46:c4:25', + port=1 + ) + machine.update_machine( + self.user_object, + 1, + tag='test' + ) + update_machine = machine.list_machines(self.user_object) + expected = {'tag': 'test'} + self.assertTrue( + item in update_machine[0].items() for item in expected.items() + ) + + +class TestPatchMachine(BaseTest): + """Test patch machine.""" + + def setUp(self): + super(TestPatchMachine, self).setUp() + logsetting.init() + + def tearDown(self): + super(TestPatchMachine, self).tearDown() + + def test_patch_machine(self): + switch.add_switch_machine( + self.user_object, + 1, + mac='28:6e:d4:46:c4:25', + port=1 + ) + machine.patch_machine( + self.user_object, + 1, + tag={'patched_tag': 'test'} + ) + patch_machine = machine.list_machines(self.user_object) + expected = {'patched_tag': 'test'} + self.assertTrue( + item in patch_machine[0].items() for item in expected.items() + ) + + +class TestDelMachine(BaseTest): + """Test delete machine.""" + + def setUp(self): + super(TestDelMachine, self).setUp() + logsetting.init() + + def tearDown(self): + super(TestDelMachine, self).tearDown() + + def test_del_machine(self): + switch.add_switch_machine( + self.user_object, + 1, + mac='28:6e:d4:46:c4:25', + port=1 + ) + machine.del_machine( + self.user_object, + 1 + ) + del_machine = machine.list_machines(self.user_object) + self.assertEqual([], del_machine) + + +if __name__ == '__main__': + flags.init() + logsetting.init() + unittest2.main() diff --git a/compass/tests/db/api/test_permission.py b/compass/tests/db/api/test_permission.py index 441da88f..64347328 100644 --- a/compass/tests/db/api/test_permission.py +++ b/compass/tests/db/api/test_permission.py @@ -17,20 +17,15 @@ import logging import os import unittest2 - -os.environ['COMPASS_IGNORE_SETTING'] = 'true' - - -from compass.utils import setting_wrapper as setting -reload(setting) - - from compass.db.api import database from compass.db.api import permission from compass.db.api import user as user_api from compass.db import exception from compass.utils import flags from compass.utils import logsetting +from compass.utils import setting_wrapper as setting + +os.environ['COMPASS_IGNORE_SETTING'] = 'true' class BaseTest(unittest2.TestCase): @@ -82,8 +77,5 @@ class TestGetPermission(BaseTest): get_permission = permission.get_permission(self.user_object, 1) self.assertIsNotNone(get_permission) - if __name__ == '__main__': - flags.init() - logsetting.init() unittest2.main() diff --git a/compass/tests/db/api/test_user.py b/compass/tests/db/api/test_user.py index a1120195..ca54a8c8 100644 --- a/compass/tests/db/api/test_user.py +++ b/compass/tests/db/api/test_user.py @@ -17,19 +17,13 @@ import logging import os import unittest2 - -os.environ['COMPASS_IGNORE_SETTING'] = 'true' - - -from compass.utils import setting_wrapper as setting -reload(setting) - - from compass.db.api import database from compass.db.api import user as user_api from compass.db import exception from compass.utils import flags from compass.utils import logsetting +from compass.utils import setting_wrapper as setting +os.environ['COMPASS_IGNORE_SETTING'] = 'true' class BaseTest(unittest2.TestCase): @@ -331,8 +325,5 @@ class TestUpdatePermissions(BaseTest): item in add_permission[0].items() for item in expected.items() ) - if __name__ == '__main__': - flags.init() - logsetting.init() unittest2.main()