123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- # -*- coding: utf-8 -*-
- """
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- """
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Salt Libs
- import salt.states.keystone as keystone
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase
- class KeystoneTestCase(TestCase, LoaderModuleMockMixin):
- """
- Test cases for salt.states.keystone
- """
- def setup_loader_modules(self):
- return {keystone: {}}
- # 'user_present' function tests: 1
- def test_user_present(self):
- """
- Test to ensure that the keystone user is present
- with the specified properties.
- """
- name = "nova"
- password = "$up3rn0v4"
- email = "nova@domain.com"
- tenant = "demo"
- ret = {"name": name, "result": False, "changes": {}, "comment": ""}
- mock_f = MagicMock(return_value=False)
- mock_lst = MagicMock(return_value=["Error"])
- with patch.dict(keystone.__salt__, {"keystone.tenant_get": mock_lst}):
- comt = 'Tenant / project "{0}" does not exist'.format(tenant)
- ret.update({"comment": comt})
- self.assertDictEqual(
- keystone.user_present(name, password, email, tenant), ret
- )
- mock_dict = MagicMock(
- side_effect=[
- {name: {"email": "a@a.com"}},
- {name: {"email": email, "enabled": False}},
- {name: {"email": email, "enabled": True}},
- {name: {"email": email, "enabled": True}},
- {"Error": "error"},
- {"Error": "error"},
- ]
- )
- mock_l = MagicMock(return_value={tenant: {"id": "abc"}})
- with patch.dict(
- keystone.__salt__,
- {
- "keystone.user_get": mock_dict,
- "keystone.tenant_get": mock_l,
- "keystone.user_verify_password": mock_f,
- "keystone.user_create": mock_f,
- },
- ):
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'User "{0}" will be updated'.format(name)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {
- "Email": "Will be updated",
- "Enabled": "Will be True",
- "Password": "Will be updated",
- },
- }
- )
- self.assertDictEqual(keystone.user_present(name, password, email), ret)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {
- "Enabled": "Will be True",
- "Password": "Will be updated",
- },
- }
- )
- self.assertDictEqual(keystone.user_present(name, password, email), ret)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {
- "Tenant": 'Will be added to "demo" tenant',
- "Password": "Will be updated",
- },
- }
- )
- self.assertDictEqual(
- keystone.user_present(name, password, email, tenant), ret
- )
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"Password": "Will be updated"},
- }
- )
- self.assertDictEqual(keystone.user_present(name, password, email), ret)
- comt = 'Keystone user "nova" will be added'
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"User": "Will be created"},
- }
- )
- self.assertDictEqual(keystone.user_present(name, password, email), ret)
- with patch.dict(keystone.__opts__, {"test": False}):
- comt = "Keystone user {0} has been added".format(name)
- ret.update(
- {"comment": comt, "result": True, "changes": {"User": "Created"}}
- )
- self.assertDictEqual(keystone.user_present(name, password, email), ret)
- # 'user_absent' function tests: 1
- def test_user_absent(self):
- """
- Test to ensure that the keystone user is absent.
- """
- name = "nova"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'User "{0}" is already absent'.format(name),
- }
- mock_lst = MagicMock(side_effect=[["Error"], []])
- with patch.dict(keystone.__salt__, {"keystone.user_get": mock_lst}):
- self.assertDictEqual(keystone.user_absent(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'User "{0}" will be deleted'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.user_absent(name), ret)
- # 'tenant_present' function tests: 1
- def test_tenant_present(self):
- """
- Test to ensures that the keystone tenant exists
- """
- name = "nova"
- description = "OpenStack Compute Service"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Tenant / project "{0}" already exists'.format(name),
- }
- mock_dict = MagicMock(
- side_effect=[
- {name: {"description": "desc"}},
- {name: {"description": description, "enabled": False}},
- {"Error": "error"},
- {"Error": "error"},
- ]
- )
- mock_t = MagicMock(return_value=True)
- with patch.dict(
- keystone.__salt__,
- {"keystone.tenant_get": mock_dict, "keystone.tenant_create": mock_t},
- ):
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Tenant / project "{0}" will be updated'.format(name)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"Description": "Will be updated"},
- }
- )
- self.assertDictEqual(keystone.tenant_present(name), ret)
- comt = 'Tenant / project "{0}" will be updated'.format(name)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"Enabled": "Will be True"},
- }
- )
- self.assertDictEqual(keystone.tenant_present(name, description), ret)
- comt = 'Tenant / project "{0}" will be added'.format(name)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"Tenant": "Will be created"},
- }
- )
- self.assertDictEqual(keystone.tenant_present(name), ret)
- with patch.dict(keystone.__opts__, {"test": False}):
- comt = 'Tenant / project "{0}" has been added'.format(name)
- ret.update(
- {"comment": comt, "result": True, "changes": {"Tenant": "Created"}}
- )
- self.assertDictEqual(keystone.tenant_present(name), ret)
- # 'tenant_absent' function tests: 1
- def test_tenant_absent(self):
- """
- Test to ensure that the keystone tenant is absent.
- """
- name = "nova"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Tenant / project "{0}" is already absent'.format(name),
- }
- mock_lst = MagicMock(side_effect=[["Error"], []])
- with patch.dict(keystone.__salt__, {"keystone.tenant_get": mock_lst}):
- self.assertDictEqual(keystone.tenant_absent(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Tenant / project "{0}" will be deleted'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.tenant_absent(name), ret)
- # 'role_present' function tests: 1
- def test_role_present(self):
- """
- Test to ensures that the keystone role exists
- """
- name = "nova"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Role "{0}" already exists'.format(name),
- }
- mock_lst = MagicMock(side_effect=[[], ["Error"]])
- with patch.dict(keystone.__salt__, {"keystone.role_get": mock_lst}):
- self.assertDictEqual(keystone.role_present(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Role "{0}" will be added'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.role_present(name), ret)
- # 'role_absent' function tests: 1
- def test_role_absent(self):
- """
- Test to ensure that the keystone role is absent.
- """
- name = "nova"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Role "{0}" is already absent'.format(name),
- }
- mock_lst = MagicMock(side_effect=[["Error"], []])
- with patch.dict(keystone.__salt__, {"keystone.role_get": mock_lst}):
- self.assertDictEqual(keystone.role_absent(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Role "{0}" will be deleted'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.role_absent(name), ret)
- # 'service_present' function tests: 1
- def test_service_present(self):
- """
- Test to ensure service present in Keystone catalog
- """
- name = "nova"
- service_type = "compute"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Service "{0}" already exists'.format(name),
- }
- mock_lst = MagicMock(side_effect=[[], ["Error"]])
- with patch.dict(keystone.__salt__, {"keystone.service_get": mock_lst}):
- self.assertDictEqual(keystone.service_present(name, service_type), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Service "{0}" will be added'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.service_present(name, service_type), ret)
- # 'service_absent' function tests: 1
- def test_service_absent(self):
- """
- Test to ensure that the service doesn't exist in Keystone catalog
- """
- name = "nova"
- ret = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": 'Service "{0}" is already absent'.format(name),
- }
- mock_lst = MagicMock(side_effect=[["Error"], []])
- with patch.dict(keystone.__salt__, {"keystone.service_get": mock_lst}):
- self.assertDictEqual(keystone.service_absent(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Service "{0}" will be deleted'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.service_absent(name), ret)
- # 'endpoint_present' function tests: 1
- def test_endpoint_present(self):
- """
- Test to ensure the specified endpoints exists for service
- """
- name = "nova"
- region = "RegionOne"
- ret = {"name": name, "changes": {}, "result": True, "comment": ""}
- endpoint = {
- "adminurl": None,
- "region": None,
- "internalurl": None,
- "publicurl": None,
- "id": 1,
- "service_id": None,
- }
- mock_lst = MagicMock(
- side_effect=[endpoint, ["Error"], {"id": 1, "service_id": None}, []]
- )
- mock = MagicMock(return_value=True)
- with patch.dict(
- keystone.__salt__,
- {"keystone.endpoint_get": mock_lst, "keystone.endpoint_create": mock},
- ):
- comt = 'Endpoint for service "{0}" already exists'.format(name)
- ret.update({"comment": comt, "result": True, "changes": {}})
- self.assertDictEqual(keystone.endpoint_present(name), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Endpoint for service "{0}" will be added'.format(name)
- ret.update(
- {
- "comment": comt,
- "result": None,
- "changes": {"Endpoint": "Will be created"},
- }
- )
- self.assertDictEqual(keystone.endpoint_present(name), ret)
- comt = 'Endpoint for service "{0}" already exists'.format(name)
- ret.update({"comment": comt, "result": True, "changes": {}})
- self.assertDictEqual(keystone.endpoint_present(name), ret)
- with patch.dict(keystone.__opts__, {"test": False}):
- comt = 'Endpoint for service "{0}" has been added'.format(name)
- ret.update({"comment": comt, "result": True, "changes": True})
- self.assertDictEqual(keystone.endpoint_present(name), ret)
- # 'endpoint_absent' function tests: 1
- def test_endpoint_absent(self):
- """
- Test to ensure that the endpoint for a service doesn't
- exist in Keystone catalog
- """
- name = "nova"
- region = "RegionOne"
- comment = 'Endpoint for service "{0}" is already absent'.format(name)
- ret = {"name": name, "changes": {}, "result": True, "comment": comment}
- mock_lst = MagicMock(side_effect=[[], ["Error"]])
- with patch.dict(keystone.__salt__, {"keystone.endpoint_get": mock_lst}):
- self.assertDictEqual(keystone.endpoint_absent(name, region), ret)
- with patch.dict(keystone.__opts__, {"test": True}):
- comt = 'Endpoint for service "{0}" will be deleted'.format(name)
- ret.update({"comment": comt, "result": None})
- self.assertDictEqual(keystone.endpoint_absent(name, region), ret)
|