Example #1
0
    def test_delete_group_exception(self):
        # Setup Test
        mock_group = mock.MagicMock()
        mock_group.name = "test"

        error = CLCException('Failed')
        error.response_text = 'group failed'
        mock_group.Delete.side_effect = error

        mock_parent = mock.MagicMock()
        mock_parent.name = "MockParent"

        mock_grandparent = mock.MagicMock()
        mock_grandparent.name = "MockGrandparent"

        mock_rootgroup = mock.MagicMock()
        mock_rootgroup.name = "MockRootGroup"

        mock_group_dict = {
            mock_parent.name: (mock_parent, mock_grandparent),
            mock_group.name: (mock_group, mock_parent)
        }

        under_test = ClcGroup(self.module)
        under_test.group_dict = mock_group_dict
        under_test.root_group = mock_rootgroup
        ret = under_test._delete_group('test')
        self.assertIsNone(ret, 'The return value should be None')
        self.module.fail_json.assert_called_once_with(
            msg='Failed to delete group :test. group failed')
    def test_delete_group_exception(self):
        # Setup Test
        mock_group = mock.MagicMock()
        mock_group.name = "test"

        error = CLCException('Failed')
        error.response_text = 'group failed'
        mock_group.Delete.side_effect = error

        mock_parent = mock.MagicMock()
        mock_parent.name = "MockParent"

        mock_grandparent = mock.MagicMock()
        mock_grandparent.name = "MockGrandparent"

        mock_rootgroup = mock.MagicMock()
        mock_rootgroup.name = "MockRootGroup"

        mock_group_dict = {mock_parent.name: (mock_parent, mock_grandparent),
                           mock_group.name: (mock_group, mock_parent)}

        under_test = ClcGroup(self.module)
        under_test.group_dict = mock_group_dict
        under_test.root_group = mock_rootgroup
        ret = under_test._delete_group('test')
        self.assertIsNone(ret, 'The return value should be None')
        self.module.fail_json.assert_called_once_with(msg='Failed to delete group :test. group failed')
 def test_get_servers_from_clc_exception(self, mock_clc_sdk):
     error = CLCException()
     error.message = 'Mock failure message'
     mock_clc_sdk.v2.Servers.side_effect = error
     under_test = ClcModifyServer(self.module)
     under_test._get_servers_from_clc(['dummy_server1', 'dummy_server2'], 'Unable to fetch test servers')
     self.module.fail_json.assert_called_once_with(msg='Unable to fetch test servers: Mock failure message')
 def test_get_servers_from_clc_exception(self, mock_clc_sdk):
     error = CLCException()
     error.message = 'Mock failure message'
     mock_clc_sdk.v2.Servers.side_effect = error
     under_test = ClcModifyServer(self.module)
     under_test._get_servers_from_clc(['dummy_server1', 'dummy_server2'], 'Unable to fetch test servers')
     self.module.fail_json.assert_called_once_with(msg='Unable to fetch test servers: Mock failure message')
 def test_restore_server_snapshot_exception(self):
     mock_server = mock.MagicMock()
     mock_server.id = 'test_server'
     error = CLCException('Failed')
     error.response_text = 'Mock failure message'
     mock_server.RestoreSnapshot.side_effect = error
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test._restore_server_snapshot(mock_server)
     self.module.fail_json.assert_called_once_with(msg='Failed to restore snapshot for server : test_server. Mock failure message')
 def test_restore_server_snapshot_exception_ignore_failure(self):
     mock_server = mock.MagicMock()
     mock_server.id = 'test_server'
     error = CLCException('Failed')
     error.message = 'Mock failure message'
     mock_server.RestoreSnapshot.side_effect = error
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test._restore_server_snapshot(mock_server, True)
     self.assertFalse(self.module.fail_json.called)
 def test_clc_install_package_exception(self):
     self.module.check_mode = False
     error = CLCException()
     error.message = 'Mock failure message'
     mock_server = mock.MagicMock()
     mock_server.id = 'server1'
     mock_server.ExecutePackage.side_effect = error
     under_test = ClcBlueprintPackage(self.module)
     under_test.clc_install_package(mock_server, 'package_id', {})
     self.module.fail_json.assert_called_once_with(msg='Failed to install package : package_id to server server1. Mock failure message')
 def test_refresh_servers_fail(self):
     error = CLCException()
     error.message = 'Mock fail message'
     under_test = ClcModifyServer(self.module)
     mock_server = mock.MagicMock()
     mock_server.id = 'mock_server_id'
     mock_server.Refresh.side_effect = error
     mock_servers = [mock_server]
     under_test._refresh_servers(self.module, mock_servers)
     self.module.fail_json.assert_called_with(msg='Unable to refresh the server mock_server_id. Mock fail message')
 def test_restore_server_snapshot_exception(self):
     mock_server = mock.MagicMock()
     mock_server.id = 'test_server'
     error = CLCException('Failed')
     error.response_text = 'Mock failure message'
     mock_server.RestoreSnapshot.side_effect = error
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test._restore_server_snapshot(mock_server)
     self.module.fail_json.assert_called_once_with(msg='Failed to restore snapshot for server : test_server. Mock failure message')
 def test_refresh_servers_fail(self):
     error = CLCException()
     error.message = 'Mock fail message'
     under_test = ClcModifyServer(self.module)
     mock_server = mock.MagicMock()
     mock_server.id = 'mock_server_id'
     mock_server.Refresh.side_effect = error
     mock_servers = [mock_server]
     under_test._refresh_servers(self.module, mock_servers)
     self.module.fail_json.assert_called_with(msg='Unable to refresh the server mock_server_id. Mock fail message')
 def test_add_publicip_to_server_exception(self, mock_clc_sdk):
     error = CLCException("Failed")
     error.response_text = 'Mock failure message'
     mock_server = mock.MagicMock()
     mock_server.id = 'TESTSVR1'
     mock_server.data = {'details': {'ipAddresses': [{'internal': '1.2.3.4'}]}}
     mock_server.PublicIPs().Add.side_effect = error
     under_test = ClcPublicIp(self.module)
     under_test._add_publicip_to_server(mock_server, 'ports')
     self.module.fail_json.assert_called_once_with(
         msg='Failed to add public ip to the server : TESTSVR1. Mock failure message')
 def test_create_aa_policy_error(self, mock_clc_sdk):
     under_test = ClcAntiAffinityPolicy(self.module)
     policy = {'name': 'dummyname', 'location': 'dummylocation'}
     error = CLCException('Failed')
     error.response_text = 'I am failed'
     mock_clc_sdk.v2.AntiAffinity.Create.side_effect = error
     under_test.clc = mock_clc_sdk
     ret = under_test._create_policy(policy)
     self.module.fail_json.assert_called_with(
         msg='Failed to create anti affinity policy : dummyname. I am failed'
     )
    def test_modify_remove_nic_handles_exception(self, mock_sdk):
        mock_sdk.v2.Account.GetAlias.return_value = 'alias'
        exception = CLCException()
        exception.message = "get wrekd"
        mock_sdk.v2.Server.side_effect = exception

        under_test = ClcModifyServer(self.module)
        result = under_test._modify_remove_nic(mock_sdk, self.module, 'test_id')

        self.assertFalse(result)
        self.module.fail_json.assert_called_with(msg='Unable to remove NIC from server : "test_id". get wrekd')
 def test_find_datacenter_error(self):
     self.module.params = {'location': 'test'}
     mock_account = mock.MagicMock()
     self.clc.v2.Account.return_value = mock_account
     error = CLCException()
     error.message = 'Mock failure message'
     self.clc.v2.Datacenter.side_effect = error
     ret = ClcModifyServer._find_datacenter(self.clc, self.module)
     self.module.fail_json.assert_called_with(
         msg='Unable to find location: test. Mock failure message')
     self.assertEqual(ret, None)
 def test_create_aa_policy_error(self, mock_clc_sdk):
     under_test = ClcAntiAffinityPolicy(self.module)
     policy = {
         'name': 'dummyname',
         'location': 'dummylocation'
     }
     error = CLCException('Failed')
     error.response_text = 'I am failed'
     mock_clc_sdk.v2.AntiAffinity.Create.side_effect = error
     under_test.clc = mock_clc_sdk
     ret = under_test._create_policy(policy)
     self.module.fail_json.assert_called_with(msg='Failed to create anti affinity policy : dummyname. I am failed')
 def test_find_datacenter_error(self):
     self.module.params = {
         'location': 'test'
     }
     mock_account = mock.MagicMock()
     self.clc.v2.Account.return_value = mock_account
     error = CLCException()
     error.message = 'Mock failure message'
     self.clc.v2.Datacenter.side_effect = error
     ret = ClcModifyServer._find_datacenter(self.clc, self.module)
     self.module.fail_json.assert_called_with(msg='Unable to find location: test. Mock failure message')
     self.assertEqual(ret, None)
 def test_delete_server_snapshot_exception(self):
     mock_server = mock.MagicMock()
     mock_server.id = "test_server"
     error = CLCException("Failed")
     error.response_text = "Mock failure message"
     mock_server.DeleteSnapshot.side_effect = error
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test._delete_server_snapshot(mock_server)
     self.module.fail_json.assert_called_once_with(
         msg="Failed to delete snapshot for server : test_server. Mock failure message"
     )
 def test_delete_aa_policy_error(self, mock_clc_sdk):
     under_test = ClcAntiAffinityPolicy(self.module)
     error = CLCException('Failed')
     error.response_text = 'I am failed'
     policy_mock = mock.MagicMock()
     policy_mock.Delete.side_effect = error
     under_test.policy_dict['dummyname'] = policy_mock
     under_test.clc = mock_clc_sdk
     policy = {'name': 'dummyname', 'location': 'dummylocation'}
     ret = under_test._delete_policy(policy)
     self.module.fail_json.assert_called_with(
         msg='Failed to delete anti affinity policy : dummyname. I am failed'
     )
 def test_delete_aa_policy_error(self, mock_clc_sdk):
     under_test = ClcAntiAffinityPolicy(self.module)
     error = CLCException('Failed')
     error.response_text = 'I am failed'
     policy_mock = mock.MagicMock()
     policy_mock.Delete.side_effect = error
     under_test.policy_dict['dummyname'] = policy_mock
     under_test.clc = mock_clc_sdk
     policy = {
         'name': 'dummyname',
         'location': 'dummylocation'
     }
     ret = under_test._delete_policy(policy)
     self.module.fail_json.assert_called_with(msg='Failed to delete anti affinity policy : dummyname. I am failed')
 def test_remove_publicip_from_server_exception(self, mock_clc_sdk):
     error = CLCException("Failed")
     error.response_text = 'Mock failure message'
     mock_server = mock.MagicMock()
     mock_server.id = 'TESTSVR1'
     ip = mock.MagicMock()
     ip.ipAddresses = [{'internal': '1.2.3.4'}]
     ip.Delete.side_effect = error
     mock_server.data = {'details': ip}
     mock_server.PublicIPs().public_ips = [ip]
     under_test = ClcPublicIp(self.module)
     under_test._remove_publicip_from_server(mock_server)
     self.module.fail_json.assert_called_once_with(
         msg='Failed to remove public ip from the server : TESTSVR1. Mock failure message')
Example #21
0
 def test_get_servers_from_clc_api(self, mock_clc_sdk):
     mock_clc_sdk.v2.Servers.side_effect = CLCException("Server Not Found")
     under_test = ClcBlueprintPackage(self.module)
     under_test._get_servers_from_clc(['TESTSVR1', 'TESTSVR2'],
                                      'FAILED TO OBTAIN LIST')
     self.module.fail_json.assert_called_once_with(
         msg='FAILED TO OBTAIN LIST: Server Not Found')
 def test_remove_publicip_from_server_exception(self, mock_clc_sdk):
     error = CLCException("Failed")
     error.response_text = 'Mock failure message'
     mock_server = mock.MagicMock()
     mock_server.id = 'TESTSVR1'
     ip = mock.MagicMock()
     ip.ipAddresses = [{'internal': '1.2.3.4'}]
     ip.Delete.side_effect = error
     mock_server.data = {'details': ip}
     mock_server.PublicIPs().public_ips = [ip]
     under_test = ClcPublicIp(self.module)
     under_test._remove_publicip_from_server(mock_server)
     self.module.fail_json.assert_called_once_with(
         msg=
         'Failed to remove public ip from the server : TESTSVR1. Mock failure message'
     )
 def test_add_publicip_to_server_exception(self, mock_clc_sdk):
     error = CLCException("Failed")
     error.response_text = 'Mock failure message'
     mock_server = mock.MagicMock()
     mock_server.id = 'TESTSVR1'
     mock_server.data = {
         'details': {
             'ipAddresses': [{
                 'internal': '1.2.3.4'
             }]
         }
     }
     mock_server.PublicIPs().Add.side_effect = error
     under_test = ClcPublicIp(self.module)
     under_test._add_publicip_to_server(mock_server, 'ports')
     self.module.fail_json.assert_called_once_with(
         msg=
         'Failed to add public ip to the server : TESTSVR1. Mock failure message'
     )
Example #24
0
 def _add_alert_policy_to_server(clc, alias, server_id, alert_policy_id):
     """
     Associate an alert policy to a clc server
     :param clc: the clc-sdk instance to use
     :param alias: the clc account alias
     :param server_id: The clc server id
     :param alert_policy_id: the alert policy id to be associated to the server
     :return: none
     """
     try:
         clc.v2.API.Call(method='POST',
                         url='servers/%s/%s/alertPolicies' %
                         (alias, server_id),
                         payload=json.dumps({'id': alert_policy_id}))
     except APIFailedResponse as e:
         raise CLCException(
             'Failed to associate alert policy to the server : {0} with Error {1}'
             .format(server_id, str(e.response_text)))