def test_wait_for_requests_no_wait(self):
     mock_request = mock.MagicMock()
     mock_request.WaitUntilComplete.return_value = True
     self.module.params = {"wait": False}
     under_test = ClcSnapshot(self.module)
     under_test._wait_for_requests_to_complete([mock_request])
     self.assertFalse(self.module.fail_json.called)
 def test_set_clc_credentials_from_env(self, mock_clc_sdk):
     with patch.dict("os.environ", {"CLC_V2_API_TOKEN": "dummyToken", "CLC_ACCT_ALIAS": "TEST"}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(under_test.clc._LOGIN_TOKEN_V2, "dummyToken")
     self.assertFalse(mock_clc_sdk.v2.SetCredentials.called)
     self.assertEqual(self.module.fail_json.called, False)
Esempio n. 3
0
 def test_get_servers_from_clc(self, mock_clc_sdk):
     mock_clc_sdk.v2.Servers.side_effect = CLCException("Server Not Found")
     under_test = ClcSnapshot(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_ensure_server_snapshot_present_w_mock_server(self,mock_get_servers):
     server_ids = ['TESTSVR1']
     mock_get_servers.return_value=[mock.MagicMock()]
     exp_days = 7
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test.ensure_server_snapshot_present(server_ids, exp_days)
     self.assertFalse(self.module.fail_json.called)
 def test_set_clc_credentials_from_env(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_TOKEN': 'dummyToken',
                                    'CLC_ACCT_ALIAS': 'TEST'}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(under_test.clc._LOGIN_TOKEN_V2, 'dummyToken')
     self.assertFalse(mock_clc_sdk.v2.SetCredentials.called)
     self.assertEqual(self.module.fail_json.called, False)
 def test_ensure_server_snapshot_present_w_mock_server(self,mock_get_servers):
     server_ids = ['TESTSVR1']
     mock_get_servers.return_value=[mock.MagicMock()]
     exp_days = 7
     self.module.check_mode = False
     under_test = ClcSnapshot(self.module)
     under_test.ensure_server_snapshot_present(server_ids, exp_days)
     self.assertFalse(self.module.fail_json.called)
 def test_set_clc_credentials_from_env(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_TOKEN': 'dummyToken',
                                    'CLC_ACCT_ALIAS': 'TEST'}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(under_test.clc._LOGIN_TOKEN_V2, 'dummyToken')
     self.assertFalse(mock_clc_sdk.v2.SetCredentials.called)
     self.assertEqual(self.module.fail_json.called, False)
 def test_set_clc_credentials_w_creds(self, mock_clc_sdk):
     with patch.dict('os.environ', {
             'CLC_V2_API_USERNAME': '******',
             'CLC_V2_API_PASSWD': 'dummypwd'
     }):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
         mock_clc_sdk.v2.SetCredentials.assert_called_once_with(
             api_username='******', api_passwd='dummypwd')
 def test_wait_for_requests_no_wait(self):
     mock_request = mock.MagicMock()
     mock_request.WaitUntilComplete.return_value = True
     self.module.params = {
         'wait': False
     }
     under_test = ClcSnapshot(self.module)
     under_test._wait_for_requests_to_complete([mock_request])
     self.assertFalse(self.module.fail_json.called)
 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_ensure_server_snapshot_restore_w_mock_server(self,mock_get_servers):
    server_ids = ['TESTSVR1']
    mock_server = mock.MagicMock()
    mock_server.id = 'TESTSVR1'
    mock_server.GetSnapshots.return_value = '123'
    mock_get_servers.return_value=[mock_server]
    self.module.check_mode = False
    under_test = ClcSnapshot(self.module)
    under_test.ensure_server_snapshot_restore(server_ids)
    self.assertFalse(self.module.fail_json.called)
 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(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_ensure_server_snapshot_restore_w_mock_server(self,mock_get_servers):
    server_ids = ['TESTSVR1']
    mock_server = mock.MagicMock()
    mock_server.id = 'TESTSVR1'
    mock_server.GetSnapshots.return_value = '123'
    mock_get_servers.return_value=[mock_server]
    self.module.check_mode = False
    under_test = ClcSnapshot(self.module)
    under_test.ensure_server_snapshot_restore(server_ids)
    self.assertFalse(self.module.fail_json.called)
    def test_wait_for_requests_w_mock_request(self):
        mock_r1 = mock.MagicMock()
        mock_r1.WaitUntilComplete.return_value = True
        mock_r2 = mock.MagicMock()
        mock_r2.WaitUntilComplete.return_value = True
        requests = [mock_r1, mock_r2]
        self.module.wait = True

        under_test = ClcSnapshot(self.module)
        under_test._wait_for_requests_to_complete(requests)
        self.assertFalse(self.module.fail_json.called)
    def test_ensure_server_snapshot_absent_w_mock_server(self, mock_get_servers):
        server_ids = ["TESTSVR1"]
        mock_server = mock.MagicMock()
        mock_server.id = "TESTSVR1"
        mock_server.GetSnapshots.return_value = "123"
        mock_get_servers.return_value = [mock_server]
        self.module.check_mode = False

        under_test = ClcSnapshot(self.module)
        under_test.ensure_server_snapshot_absent(server_ids)
        self.assertFalse(self.module.fail_json.called)
    def test_process_request_state_restore(self, mock_set_clc_creds, mock_server_snapshot):
        test_params = {"server_ids": ["TESTSVR1", "TESTSVR2"], "expiration_days": 7, "wait": True, "state": "restore"}
        mock_server_snapshot.return_value = True, mock.MagicMock(), ["TESTSVR1"]
        self.module.params = test_params
        self.module.check_mode = False

        under_test = ClcSnapshot(self.module)
        under_test.process_request()

        self.module.exit_json.assert_called_once_with(changed=True, server_ids=["TESTSVR1"])
        self.assertFalse(self.module.fail_json.called)
    def test_wait_for_requests_w_mock_request(self):
        mock_r1 = mock.MagicMock()
        mock_r1.WaitUntilComplete.return_value = True
        mock_r2 = mock.MagicMock()
        mock_r2.WaitUntilComplete.return_value = True
        requests = [mock_r1, mock_r2]
        self.module.wait = True

        under_test = ClcSnapshot(self.module)
        under_test._wait_for_requests_to_complete(requests)
        self.assertFalse(self.module.fail_json.called)
 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_wait_for_requests_w_mock_request_fail(self):
        mock_request = mock.MagicMock()
        mock_request.WaitUntilComplete.return_value = True
        mock_response = mock.MagicMock()
        mock_response.Status.return_value = 'Failed'
        mock_request.requests = [mock_response]
        requests = [mock_request]
        self.module.wait = True

        under_test = ClcSnapshot(self.module)
        under_test._wait_for_requests_to_complete(requests)
        self.assertTrue(self.module.fail_json.called)
    def test_wait_for_requests_w_mock_request_fail(self):
        mock_request = mock.MagicMock()
        mock_request.WaitUntilComplete.return_value = True
        mock_response = mock.MagicMock()
        mock_response.Status.return_value = 'Failed'
        mock_request.requests = [mock_response]
        requests = [mock_request]
        self.module.wait = True

        under_test = ClcSnapshot(self.module)
        under_test._wait_for_requests_to_complete(requests)
        self.assertTrue(self.module.fail_json.called)
    def test_process_request_state_restore(self, mock_set_clc_creds, mock_server_snapshot):
        test_params = {
            'server_ids': ['TESTSVR1', 'TESTSVR2']
            ,'expiration_days': 7
            ,'wait': True
            , 'state': 'restore'
        }
        mock_server_snapshot.return_value = True, mock.MagicMock(), ['TESTSVR1']
        self.module.params = test_params
        self.module.check_mode = False

        under_test = ClcSnapshot(self.module)
        under_test.process_request()

        self.module.exit_json.assert_called_once_with(changed=True, server_ids=['TESTSVR1'])
        self.assertFalse(self.module.fail_json.called)
    def test_process_request_state_restore(self, mock_set_clc_creds, mock_server_snapshot):
        test_params = {
            'server_ids': ['TESTSVR1', 'TESTSVR2']
            ,'expiration_days': 7
            ,'wait': True
            , 'state': 'restore'
        }
        mock_server_snapshot.return_value = True, mock.MagicMock(), ['TESTSVR1']
        self.module.params = test_params
        self.module.check_mode = False

        under_test = ClcSnapshot(self.module)
        under_test.process_request()

        self.module.exit_json.assert_called_once_with(changed=True, server_ids=['TESTSVR1'])
        self.assertFalse(self.module.fail_json.called)
    def test_clc_module_not_found(self):
        # Setup Mock Import Function
        import __builtin__ as builtins
        real_import = builtins.__import__
        def mock_import(name, *args):
            if name == 'clc': raise ImportError
            return real_import(name, *args)
        # Under Test
        with mock.patch('__builtin__.__import__', side_effect=mock_import):
            reload(clc_server_snapshot)
            ClcSnapshot(self.module)
        # Assert Expected Behavior
        self.module.fail_json.assert_called_with(msg='clc-python-sdk required for this module')

        # Reset
        reload(clc_server_snapshot)
    def test_requests_invalid_version(self):
        # Setup Mock Import Function
        import __builtin__ as builtins
        real_import = builtins.__import__
        def mock_import(name, *args):
            if name == 'requests':
                args[0]['requests'].__version__ = '2.4.0'
            return real_import(name, *args)
        # Under Test
        with mock.patch('__builtin__.__import__', side_effect=mock_import):
            reload(clc_server_snapshot)
            ClcSnapshot(self.module)
        # Assert Expected Behavior
        self.module.fail_json.assert_called_with(msg='requests library  version should be >= 2.5.0')

        # Reset
        reload(clc_server_snapshot)
 def test_set_clc_credentials_w_creds(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_USERNAME': '******', 'CLC_V2_API_PASSWD': 'dummypwd'}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
         mock_clc_sdk.v2.SetCredentials.assert_called_once_with(api_username='******', api_passwd='dummypwd')
    def test_set_user_agent(self, mock_clc_sdk):
        clc_server_snapshot.__version__ = "1"
        ClcSnapshot._set_user_agent(mock_clc_sdk)

        self.assertTrue(mock_clc_sdk.SetRequestsSession.called)
 def test_set_clc_credentials_w_no_creds(self):
     with patch.dict('os.environ', {}, clear=True):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(self.module.fail_json.called, True)
 def test_set_clc_credentials_w_api_url(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_URL': 'dummyapiurl'}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
         self.assertEqual(under_test.clc.defaults.ENDPOINT_URL_V2, 'dummyapiurl')
 def test_define_argument_spec(self):
     result = ClcSnapshot.define_argument_spec()
     self.assertIsInstance(result, dict)
 def test_wait_for_requests_to_complete(self,mock_get_servers):
     server_ids = ['INVALID']
     mock_get_servers.return_value=[mock.MagicMock()]
     under_test = ClcSnapshot(self.module)
     under_test._wait_for_requests_to_complete (mock.MagicMock())
     self.assertFalse(self.module.fail_json.called)
 def test_get_servers_from_clc(self, mock_clc_sdk):
     mock_clc_sdk.v2.Servers.side_effect = CLCException("Server Not Found")
     under_test = ClcSnapshot(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_wait_for_requests_to_complete(self,mock_get_servers):
     server_ids = ['INVALID']
     mock_get_servers.return_value=[mock.MagicMock()]
     under_test = ClcSnapshot(self.module)
     under_test._wait_for_requests_to_complete (mock.MagicMock())
     self.assertFalse(self.module.fail_json.called)
 def test_set_clc_credentials_w_api_url(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_URL': 'dummyapiurl'}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
         self.assertEqual(under_test.clc.defaults.ENDPOINT_URL_V2, 'dummyapiurl')
 def test_set_clc_credentials_w_no_creds(self):
     with patch.dict('os.environ', {}, clear=True):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(self.module.fail_json.called, True)
 def test_define_argument_spec(self):
     result = ClcSnapshot.define_argument_spec()
     self.assertIsInstance(result, dict)
    def test_set_user_agent(self, mock_clc_sdk):
        clc_server_snapshot.__version__ = "1"
        ClcSnapshot._set_user_agent(mock_clc_sdk)

        self.assertTrue(mock_clc_sdk.SetRequestsSession.called)
 def test_set_clc_credentials_w_creds(self, mock_clc_sdk):
     with patch.dict("os.environ", {"CLC_V2_API_USERNAME": "******", "CLC_V2_API_PASSWD": "dummypwd"}):
         under_test = ClcSnapshot(self.module)
         under_test._set_clc_credentials_from_env()
         mock_clc_sdk.v2.SetCredentials.assert_called_once_with(api_username="******", api_passwd="dummypwd")