def test_other_connection_error(self, mock_create_sf_connection):
     ''' filter on key, value - no match - force error on empty '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     set_module_args(args)
     # force a connection exception
     mock_create_sf_connection.side_effect = KeyError('testme')
     with pytest.raises(AnsibleFailJson) as exc:
         my_module()
     print(exc.value.args[0])
     msg = 'Failed to connect for hostname:442'
     assert msg in exc.value.args[0]['msg']
 def test_info_all_nodes_not_alone(self, mock_create_sf_connection):
     ''' gather all node scoped subsets but fail as another subset is present '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['all_nodes', 'dummy']
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleFailJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     msg = 'no other subset is allowed'
     assert msg in exc.value.args[0]['msg']
 def test_info_filter_record_not_found(self, mock_create_sf_connection):
     ''' filter on key, value - no match '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['cluster_accounts']
     args['filter'] = dict(bad_key='user1')
     args['fail_on_key_not_found'] = False
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleExitJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     assert exc.value.args[0]['info']['cluster_accounts']['accounts'] == list()
 def test_info_filter_bad_key(self, mock_create_sf_connection):
     ''' filter on key, value - key not found  '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['cluster_accounts']
     args['filter'] = dict(bad_key='user1')
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleFailJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     msg = 'Error: key bad_key not found in'
     assert msg in exc.value.args[0]['msg']
 def test_info_filter_success(self, mock_create_sf_connection):
     ''' filter on key, value - succesful match '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['cluster_accounts']
     args['filter'] = dict(username='******')
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleExitJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     username = exc.value.args[0]['info']['cluster_accounts']['accounts'][0]['username']
     assert username == 'user1'
 def test_info_all_all(self, mock_create_sf_connection):
     ''' gather all explictly '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['all']
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleExitJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     assert not exc.value.args[0]['changed']
     assert 'list_accounts' in my_obj.sfe_node.called
     assert 'get_config' in my_obj.sfe_node.called
 def test_info_filter_record_not_found_error(self, mock_create_sf_connection):
     ''' filter on key, value - no match - force error on empty '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['cluster_accounts']
     args['filter'] = dict(username='******')
     args['fail_on_record_not_found'] = True
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleFailJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     msg = 'Error: no match for'
     assert msg in exc.value.args[0]['msg']
 def test_info_all_default(self, mock_create_sf_connection):
     ''' gather all by default '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleExitJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     assert not exc.value.args[0]['changed']
     assert 'cluster_accounts' in exc.value.args[0]['info']
     assert 'node_config' in exc.value.args[0]['info']
     username = exc.value.args[0]['info']['cluster_accounts']['accounts'][0]['username']
     assert username == 'user1'
     assert 'list_accounts' in my_obj.sfe_node.called
     assert 'get_config' in my_obj.sfe_node.called
 def test_info_all_clusters(self, mock_create_sf_connection):
     ''' gather all cluster scoped subsets '''
     args = dict(self.ARGS)  # deep copy as other tests can modify args
     args['gather_subsets'] = ['all_clusters']
     set_module_args(args)
     # my_obj.sfe will be assigned a MockSFConnection object:
     mock_create_sf_connection.return_value = MockSFConnection()
     my_obj = my_module()
     with pytest.raises(AnsibleExitJson) as exc:
         my_obj.apply()
     print(exc.value.args[0])
     assert not exc.value.args[0]['changed']
     assert 'cluster_accounts' in exc.value.args[0]['info']
     accounts = exc.value.args[0]['info']['cluster_accounts']
     print('accounts: >>%s<<' % accounts, type(accounts))
     print(my_obj.sfe_node.called)
     assert 'list_accounts' in my_obj.sfe_node.called
     assert 'get_config' not in my_obj.sfe_node.called
 def test_module_fail_when_required_args_missing(self):
     ''' required arguments are reported as errors '''
     with pytest.raises(AnsibleFailJson) as exc:
         set_module_args({})
         my_module()
     print('Info: %s' % exc.value.args[0]['msg'])