<s> import random <EOL> from nose . plugins . skip import SkipTest <EOL> from tests . checks . common import AgentCheckTest , load_check <EOL> MOCK_CONFIG = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ] <EOL> } <EOL> MOCK_CONFIG_SERVICE_WHITELIST = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ '<STR_LIT>' . format ( k ) for k in range ( <NUM_LIT> ) ] <EOL> } ] <EOL> } <EOL> MOCK_CONFIG_LEADER_CHECK = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } ] <EOL> } <EOL> MOCK_BAD_CONFIG = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } , { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } ] <EOL> } <EOL> class TestCheckConsul ( AgentCheckTest ) : <EOL> CHECK_NAME = '<STR_LIT>' <EOL> def mock_get_peers_in_cluster ( self , instance ) : <EOL> return [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> def mock_get_services_in_cluster ( self , instance ) : <EOL> return { <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] <EOL> } <EOL> def mock_get_n_services_in_cluster ( self , n ) : <EOL> dct = { } <EOL> for i in range ( n ) : <EOL> k = "<STR_LIT>" . format ( i ) <EOL> dct [ k ] = [ ] <EOL> return dct <EOL> def mock_get_local_config ( self , instance ) : <EOL> return { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : - <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } , <EOL> } <EOL> } <EOL> def mock_get_nodes_in_cluster ( self , instance ) : <EOL> return [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> ] <EOL> def mock_get_nodes_with_service ( self , instance , service ) : <EOL> def _get_random_ip ( ) : <EOL> rand_int = int ( <NUM_LIT:15> * random . random ( ) ) + <NUM_LIT:10> <EOL> return "<STR_LIT>" . format ( rand_int ) <EOL> return [ <EOL> { <EOL> "<STR_LIT>" : _get_random_ip ( ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : service , <EOL> "<STR_LIT>" : service , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] <EOL> } <EOL> ] <EOL> def mock_get_cluster_leader_A ( self , instance ) : <EOL> return '<STR_LIT>' <EOL> def mock_get_cluster_leader_B ( self , instance ) : <EOL> return '<STR_LIT>' <EOL> def _get_consul_mocks ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . mock_get_services_in_cluster , <EOL> '<STR_LIT>' : self . mock_get_nodes_with_service , <EOL> '<STR_LIT>' : self . mock_get_peers_in_cluster , <EOL> '<STR_LIT>' : self . mock_get_local_config , <EOL> '<STR_LIT>' : self . mock_get_cluster_leader_A <EOL> } <EOL> def test_bad_config ( self ) : <EOL> self . assertRaises ( Exception , self . run_check , MOCK_BAD_CONFIG ) <EOL> def test_get_nodes_with_service ( self ) : <EOL> self . run_check ( MOCK_CONFIG , mocks = self . _get_consul_mocks ( ) ) <EOL> self . assertMetric ( '<STR_LIT>' , value = <NUM_LIT:1> , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_get_peers_in_cluster ( self ) : <EOL> mocks = self . _get_consul_mocks ( ) <EOL> self . run_check ( MOCK_CONFIG , mocks = mocks ) <EOL> self . assertMetric ( '<STR_LIT>' , value = <NUM_LIT:3> , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> mocks [ '<STR_LIT>' ] = self . mock_get_cluster_leader_B <EOL> self . run_check ( MOCK_CONFIG , mocks = mocks ) <EOL> self . assertMetric ( '<STR_LIT>' , value = <NUM_LIT:3> , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_get_services_on_node ( self ) : <EOL> self . run_check ( MOCK_CONFIG , mocks = self . _get_consul_mocks ( ) ) <EOL> self . assertMetric ( '<STR_LIT>' , value = <NUM_LIT:6> , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_cull_services_list ( self ) : <EOL> self . check = load_check ( self . CHECK_NAME , MOCK_CONFIG_LEADER_CHECK , self . DEFAULT_AGENT_CONFIG ) <EOL> num_services = self . check . MAX_SERVICES + <NUM_LIT:20> <EOL> services = self . mock_get_n_services_in_cluster ( num_services ) <EOL> whitelist = [ '<STR_LIT>' . format ( k ) for k in range ( num_services ) ] <EOL> self . assertEqual ( len ( self . check . _cull_services_list ( services , whitelist ) ) , self . check . MAX_SERVICES ) <EOL> services = self . mock_get_n_services_in_cluster ( num_services ) <EOL> whitelist = [ '<STR_LIT>' . format ( k ) for k in range ( self . check . MAX_SERVICES - <NUM_LIT:1> ) ] <EOL> self . assertEqual ( set ( self . check . _cull_services_list ( services , whitelist ) ) , set ( whitelist ) ) <EOL> whitelist = [ ] <EOL> self . assertEqual ( len ( self . check . _cull_services_list ( services , whitelist ) ) , self . check . MAX_SERVICES ) <EOL> num_services = self . check . MAX_SERVICES - <NUM_LIT:1> <EOL> services = self . mock_get_n_services_in_cluster ( num_services ) <EOL> self . assertEqual ( len ( self . check . _cull_services_list ( services , whitelist ) ) , num_services ) <EOL> num_services = self . check . MAX_SERVICES - <NUM_LIT:1> <EOL> whitelist = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> services = self . mock_get_n_services_in_cluster ( num_services ) <EOL> self . assertEqual ( set ( self . check . _cull_services_list ( services , whitelist ) ) , set ( whitelist ) ) <EOL> def test_new_leader_event ( self ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> self . check = load_check ( self . CHECK_NAME , MOCK_CONFIG_LEADER_CHECK , self . DEFAULT_AGENT_CONFIG ) <EOL> self . check . _last_known_leader = '<STR_LIT>' <EOL> mocks = self . _get_consul_mocks ( ) <EOL> mocks [ '<STR_LIT>' ] = self . mock_get_cluster_leader_B <EOL> self . run_check ( MOCK_CONFIG_LEADER_CHECK , mocks = mocks ) <EOL> self . assertEqual ( len ( self . events ) , <NUM_LIT:1> ) <EOL> event = self . events [ <NUM_LIT:0> ] <EOL> self . assertEqual ( event [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , event [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , event [ '<STR_LIT>' ] ) </s>
