示例#1
0
 def test_get_alert_polices(self, mock_set_clc_creds, mock_clc_sdk):
     mock_clc_sdk.v2.API.Call.side_effect = [{
         'items': [
             {
             'id': '12345',
             'name': 'test1'
             },
             {
             'id': '23456',
             'name': 'test2'
             }
         ]
     }]
     test_params = {
         'name': 'testname'
         , 'alias': 'testalias'
         , 'alert_recipients': ['test']
         , 'metric': 'disk'
         , 'duration': '00:05:00'
         , 'threshold': 5
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.clc = mock_clc_sdk
     res = under_test._get_alert_policies('testalias')
     self.assertEqual(res,
                      {'12345': {'id': '12345', 'name': 'test1'}, '23456': {'id': '23456', 'name': 'test2'}})
示例#2
0
 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 = ClcAlertPolicy(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_delete_alert_policy(self, mock_set_clc_creds, mock_clc_sdk):
     mock_clc_sdk.v2.API.Call.side_effect = ['success']
     test_params = {'alias': 'testalias', 'state': 'absent'}
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.clc = mock_clc_sdk
     res = under_test._delete_alert_policy('testalias', '12345')
     self.assertEqual(res, 'success')
 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 = ClcAlertPolicy(self.module)
         under_test._set_clc_credentials_from_env()
         mock_clc_sdk.v2.SetCredentials.assert_called_once_with(
             api_username='******', api_passwd='dummypwd')
示例#5
0
 def test_alert_policy_exists_false(self, mock_set_clc_creds):
     test_params = {
         'alias': 'testalias'
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.policy_dict = {'12345': {'id': '12345', 'name': 'test1'},
                               '23456': {'id': '23456', 'name': 'test2'}}
     res = under_test._alert_policy_exists('notfound')
     self.assertEqual(res, False)
示例#6
0
 def test_get_alert_policy_id_fail_duplicate_names(self, mock_set_clc_creds):
     test_params = {
         'alias': 'testalias'
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.policy_dict = {'12345': {'id': '12345', 'name': 'test1'},
                               '23456': {'id': '23456', 'name': 'test1'}}
     policy_id = under_test._get_alert_policy_id(self.module, 'test1')
     self.module.fail_json.assert_called_once_with(msg='multiple alert policies were found with policy name : test1')
示例#7
0
 def test_get_alert_policy_id(self, mock_set_clc_creds):
     test_params = {
         'alias': 'testalias'
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.policy_dict = {'12345': {'id': '12345', 'name': 'test1'},
                               '23456': {'id': '23456', 'name': 'test2'}}
     policy_id = under_test._get_alert_policy_id(self.module, 'test2')
     self.assertEqual(policy_id, '23456')
 def test_ensure_alert_policy_is_absent_no_id_no_name(
         self, mock_set_clc_creds, mock_get_id, mock_delete):
     test_params = {'alias': 'testalias', 'state': 'absent'}
     mock_get_id.return_value = '12345'
     mock_delete.return_value = 'success'
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.policy_dict = {'12345', '23456'}
     changed, policy = under_test._ensure_alert_policy_is_absent()
     self.module.fail_json.assert_called_once_with(
         msg='Either alert policy id or policy name is required')
 def test_ensure_alert_policy_is_absent_no_id_present(
         self, mock_set_clc_creds, mock_get_id, mock_delete):
     test_params = {'id': 'testid', 'alias': 'testalias', 'state': 'absent'}
     mock_get_id.return_value = '12345'
     mock_delete.return_value = 'success'
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.policy_dict = {'12345', '23456'}
     changed, policy = under_test._ensure_alert_policy_is_absent()
     self.assertEqual(changed, False)
     self.assertEqual(policy, None)
     self.assertFalse(self.module.fail_json.called)
 def test_delete_alert_policy_exception(self, mock_set_clc_creds,
                                        mock_clc_sdk):
     test_params = {'alias': 'testalias', 'state': 'absent'}
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.clc = mock_clc_sdk
     error = APIFailedResponse('Failed')
     error.response_text = 'Sorry'
     mock_clc_sdk.v2.API.Call.side_effect = error
     under_test._delete_alert_policy('testalias', '12345')
     self.module.fail_json.assert_called_once_with(
         msg='Unable to delete alert policy id "12345". Sorry')
示例#11
0
 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_alert_policy)
         ClcAlertPolicy(self.module)
     # Assert Expected Behavior
     self.module.fail_json.assert_called_with(msg='clc-python-sdk required for this module')
     reload(clc_alert_policy)
示例#12
0
 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_alert_policy)
         ClcAlertPolicy(self.module)
     # Assert Expected Behavior
     self.module.fail_json.assert_called_with(msg='requests library  version should be >= 2.5.0')
     reload(clc_alert_policy)
示例#13
0
    def test_ensure_alert_policy_is_present_no_name(self, mock_set_clc_creds, mock_alert_policy_exists, mock_create):
        test_params = {
            'alias': 'testalias'
            , 'alert_recipients': ['test']
            , 'metric': 'cpu'
            , 'duration': 'duration'
            , 'threshold': 'threashold'
            , 'state': 'absent'
        }
        mock_alert_policy_exists.return_value = False
        mock_create.return_value = 'success'
        self.module.params = test_params
        self.module.check_mode = False

        under_test = ClcAlertPolicy(self.module)
        under_test._ensure_alert_policy_is_present()
        self.module.fail_json.assert_called_once_with(msg='Policy name is a required')
    def test_ensure_alert_policy_is_updated_diff_recipients(
            self, mock_set_clc_creds, mock_update):
        test_params = {
            'name': 'testname',
            'alias': 'testalias',
            'alert_recipients': ['test'],
            'metric': 'disk',
            'duration': '00:05:00',
            'threshold': 5,
            'state': 'absent'
        }
        mock_update.return_value = 'success'
        self.module.params = test_params
        self.module.check_mode = False

        policy = {
            "id":
            "51db33be37b040f6a135abbaf989e36a",
            "name":
            "alert1",
            "actions": [{
                "action": "email",
                "settings": {
                    "recipients":
                    ["*****@*****.**", "*****@*****.**"]
                }
            }],
            "links": [{
                "rel": "self",
                "href":
                "/v2/alertPolicies/wfad/51db33be37b040f6a135abbaf989e36a",
                "verbs": ["GET", "DELETE", "PUT"]
            }],
            "triggers": [{
                "metric": "disk",
                "duration": "00:05:00",
                "threshold": 5.0
            }]
        }

        under_test = ClcAlertPolicy(self.module)
        changed, policy_res = under_test._ensure_alert_policy_is_updated(
            policy)
        self.assertEqual(changed, True)
        self.assertEqual(policy_res, 'success')
        self.assertFalse(self.module.fail_json.called)
示例#15
0
 def test_create_alert_policy(self, mock_set_clc_creds, mock_clc_sdk):
     mock_clc_sdk.v2.API.Call.side_effect = ['success']
     test_params = {
         'name': 'testname'
         , 'alias': 'testalias'
         , 'alert_recipients': ['test']
         , 'metric': 'disk'
         , 'duration': '00:05:00'
         , 'threshold': 5
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.clc = mock_clc_sdk
     res = under_test._create_alert_policy()
     self.assertEqual(res, 'success')
    def test_requests_module_not_found(self):
        # Setup Mock Import Function
        real_import = __import__

        def mock_import(name, *args):
            if name == 'requests':
                args[0]['requests'].__version__ = '2.7.0'
                raise ImportError
            return real_import(name, *args)

        # Under Test
        with mock.patch('__builtin__.__import__', side_effect=mock_import):
            reload(clc_alert_policy)
            ClcAlertPolicy(self.module)
        # Assert Expected Behavior
        self.module.fail_json.assert_called_with(
            msg='requests library is required for this module')
        reload(clc_alert_policy)
示例#17
0
    def test_process_request_state_absent(self, mock_set_clc_creds, mock_ensure_alert_policy, mock_get_alert_policies):
        test_params = {
            'name': 'testname'
            , 'alias': 'testalias'
            , 'alert_recipients': ['test']
            , 'metric': 'cpu'
            , 'duration': 'duration'
            , 'threshold': 'threashold'
            , 'state': 'absent'
        }
        mock_ensure_alert_policy.return_value = True, None
        self.module.params = test_params
        self.module.check_mode = False

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

        self.module.exit_json.assert_called_once_with(changed=True, policy=None)
        self.assertFalse(self.module.fail_json.called)
示例#18
0
    def test_ensure_alert_policy_is_present_existing(self, mock_set_clc_creds, mock_alert_policy_exists, mock_update):
        test_params = {
            'name': 'testname'
            , 'alias': 'testalias'
            , 'alert_recipients': ['test']
            , 'metric': 'cpu'
            , 'duration': 'duration'
            , 'threshold': 'threashold'
            , 'state': 'absent'
        }
        mock_alert_policy_exists.return_value = mock.MagicMock()
        mock_update.return_value = True, 'success'
        self.module.params = test_params
        self.module.check_mode = False

        under_test = ClcAlertPolicy(self.module)
        changed, policy = under_test._ensure_alert_policy_is_present()
        self.assertEqual(changed, True)
        self.assertEqual(policy,'success')
示例#19
0
 def test_create_alert_policy_exception(self, mock_set_clc_creds, mock_clc_sdk):
     test_params = {
         'name': 'testname'
         , 'alias': 'testalias'
         , 'alert_recipients': ['test']
         , 'metric': 'disk'
         , 'duration': '00:05:00'
         , 'threshold': 5
         , 'state': 'absent'
     }
     self.module.params = test_params
     self.module.check_mode = False
     under_test = ClcAlertPolicy(self.module)
     under_test.clc = mock_clc_sdk
     error = APIFailedResponse('Failed')
     error.response_text = 'Sorry'
     mock_clc_sdk.v2.API.Call.side_effect = error
     under_test._create_alert_policy()
     self.module.fail_json.assert_called_once_with(msg='Unable to create alert policy "testname". Sorry')
示例#20
0
 def setUp(self):
     self.clc = mock.MagicMock()
     self.module = FakeAnsibleModule()
     self.policy = ClcAlertPolicy(self.module)
     self.policy.module.exit_json = mock.MagicMock()
示例#21
0
 def test_set_clc_credentials_w_no_creds(self):
     with patch.dict('os.environ', {}, clear=True):
         under_test = ClcAlertPolicy(self.module)
         under_test._set_clc_credentials_from_env()
     self.assertEqual(self.module.fail_json.called, True)
示例#22
0
 def test_set_clc_credentials_w_api_url(self, mock_clc_sdk):
     with patch.dict('os.environ', {'CLC_V2_API_URL': 'dummyapiurl'}):
         under_test = ClcAlertPolicy(self.module)
         under_test._set_clc_credentials_from_env()
         self.assertEqual(under_test.clc.defaults.ENDPOINT_URL_V2, 'dummyapiurl')