Exemplo n.º 1
0
def test_server_thread():
    task = {'farm_id':'1', 'farm_role_id':'2', 'index':'3', 'host':'localhost', 'api_port':80,
            'api_key':'api_key', 'snmp_port':'80', 'community':'public',
            'metrics':['cpu', 'la', 'net', 'mem']}

    assert stats_poller.server_thread(task) == {'index': '3', 'farm_id': '1',
            'data': {'mem': {'swapavail': None, 'cached': None, 'free': None, 'avail': None,
            'buffer': None, 'swap': None, 'shared': None, 'total': None},
            'net': {'in': None, 'out': None},
            'cpu': {'idle': None, 'nice': None, 'system': None, 'user': None},
            'la': {'la5': None, 'la15': None, 'la1': None}}, 'farm_role_id': '2'}

    with mock.patch('scalrpy.stats_poller.HttpServiceProxy') as HttpServiceProxy:
        hsp = HttpServiceProxy.return_value
        hsp.sysinfo.cpu_stat.return_value = {'user':0, 'nice':0, 'system':0, 'idle':0}
        hsp.sysinfo.mem_info.return_value = {
                'total_swap':0,
                'avail_swap':0,
                'total_real':0,
                'total_free':0,
                'shared':0,
                'buffer':0,
                'cached':0}
        hsp.sysinfo.net_stats.return_value = {
                'eth0':{'receive':{'bytes':0}, 'transmit':{'bytes':0}}}
        hsp.sysinfo.load_average.return_value = [0.0, 0.0, 0.0]

        assert stats_poller.server_thread(task) == {'index': '3', 'farm_id': '1', 'data': {
                'mem': {'avail': 0.0, 'cached': 0.0, 'total': 0.0, 'swap': 0.0, 'buffer': 0.0,
                'shared': 0.0, 'swapavail': 0.0, 'free': 0.0}, 'net': {'out': 0.0, 'in': 0.0},
                'cpu': {'system': 0.0, 'idle': 0.0, 'user': 0.0, 'nice': 0.0},
                'la': {'la5': 0.0, 'la15': 0.0, 'la1': 0.0}}, 'farm_role_id': '2'}

    with mock.patch('scalrpy.stats_poller.netsnmp.Session') as Session:
        instance = Session.return_value
        instance.get.return_value = (
            '0', '0', '0', '0', '0.0', '0.0', '0.0','0',
            '0', '0', '0', '0', '0', '0', '0', '0', '0')

        assert stats_poller.server_thread(task) == {'index': '3', 'farm_id': '1', 'data': {
                'mem': {'avail': 0.0, 'cached': 0.0, 'total': 0.0, 'swap': 0.0, 'buffer': 0.0,
                'shared': 0.0, 'swapavail': 0.0, 'free': 0.0}, 'net': {'out': 0.0, 'in': 0.0},
                'cpu': {'system': 0.0, 'idle': 0.0, 'user': 0.0, 'nice': 0.0},
                'la': {'la5': 0.0, 'la15': 0.0, 'la1': 0.0}}, 'farm_role_id': '2'}
Exemplo n.º 2
0
def test_server_thread():
    task = (
            {'farm_id':'1', 'farm_role_id':'2', 'index':'3', 'host':'localhost', 'api_port':'80',
            'srz_key':'uyPi0f3quqcQw4N4WXSlcj6FSKgt5pAVItGS91K3/l6MVlOaaHAtGA==', 'snmp_port':'80',
            'community':'public', 'os_type':'linux', 'proxy':None,
            'metrics':['cpu', 'la', 'net', 'mem']},
            mock.MagicMock()
            )
    assert stats_poller.server_thread(task) == None

    with mock.patch('scalrpy.util.rpc.HttpServiceProxy') as HttpServiceProxy:
        hsp = HttpServiceProxy.return_value
        hsp.sysinfo.cpu_stat.return_value = {'user':0, 'nice':0, 'system':0, 'idle':0}
        hsp.sysinfo.mem_info.return_value = {
                'total_swap':0,
                'avail_swap':0,
                'total_real':0,
                'total_free':0,
                'shared':0,
                'buffer':0,
                'cached':0,
                }
        hsp.sysinfo.net_stats.return_value = {
                'eth0':{'receive':{'bytes':0}, 'transmit':{'bytes':0}}}
        hsp.sysinfo.load_average.return_value = [0.0, 0.0, 0.0]
        hsp.sysinfo.disk_stats.return_value = {
            'xvda1':{
                    'write':{
                            'num':0,
                            'bytes':0,
                            'sectors':0,
                            },
                    'read':{
                            'num':0,
                            'bytes':0,
                            'sectors':0,
                            },
                    },
            'loop0':{
                    'write':{
                            'num':0,
                            'bytes':0,
                            'sectors':0,
                            },
                    'read':{
                            'num':0,
                            'bytes':0,
                            'sectors':0,
                            },
                    },
            }

        assert stats_poller.server_thread(task) == {'index': '3', 'farm_id': '1', 'data': {
                'mem': {'avail': None, 'cached': 0.0, 'total': 0.0, 'swap': 0.0, 'buffer': 0.0,
                'shared': 0.0, 'swapavail': 0.0, 'free': 0.0}, 'net': {'out': 0.0, 'in': 0.0},
                'cpu': {'system': 0.0, 'idle': 0.0, 'user': 0.0, 'nice': 0.0},
                'la': {'la5': 0.0, 'la15': 0.0, 'la1': 0.0}}, 'farm_role_id': '2'}

    import netsnmp
    stats_poller.CONFIG['with_snmp'] = True
    with mock.patch('netsnmp.Session') as Session:
        instance = Session.return_value
        instance.get.return_value = (
            '0', '0', '0', '0', '0.0', '0.0', '0.0','0',
            '0', '0', '0', '0', '0', '0', '0', '0', '0')

        assert stats_poller.server_thread(task) == {'index': '3', 'farm_id': '1', 'data': {
                'mem': {'avail': 0.0, 'cached': 0.0, 'total': 0.0, 'swap': 0.0, 'buffer': 0.0,
                'shared': 0.0, 'swapavail': 0.0, 'free': 0.0}, 'net': {'out': 0.0, 'in': 0.0},
                'cpu': {'system': 0.0, 'idle': 0.0, 'user': 0.0, 'nice': 0.0},
                'la': {'la5': 0.0, 'la15': 0.0, 'la1': 0.0}}, 'farm_role_id': '2'}
    stats_poller.CONFIG['with_snmp'] = False
Exemplo n.º 3
0
def test_server_thread():
    task = ({
        'farm_id': '1',
        'farm_role_id': '2',
        'index': '3',
        'host': 'localhost',
        'api_port': '80',
        'srz_key': 'uyPi0f3quqcQw4N4WXSlcj6FSKgt5pAVItGS91K3/l6MVlOaaHAtGA==',
        'snmp_port': '80',
        'community': 'public',
        'os_type': 'linux',
        'proxy': None,
        'metrics': ['cpu', 'la', 'net', 'mem']
    }, mock.MagicMock())
    assert stats_poller.server_thread(task) == None

    with mock.patch('scalrpy.util.rpc.HttpServiceProxy') as HttpServiceProxy:
        hsp = HttpServiceProxy.return_value
        hsp.sysinfo.cpu_stat.return_value = {
            'user': 0,
            'nice': 0,
            'system': 0,
            'idle': 0
        }
        hsp.sysinfo.mem_info.return_value = {
            'total_swap': 0,
            'avail_swap': 0,
            'total_real': 0,
            'total_free': 0,
            'shared': 0,
            'buffer': 0,
            'cached': 0,
        }
        hsp.sysinfo.net_stats.return_value = {
            'eth0': {
                'receive': {
                    'bytes': 0
                },
                'transmit': {
                    'bytes': 0
                }
            }
        }
        hsp.sysinfo.load_average.return_value = [0.0, 0.0, 0.0]
        hsp.sysinfo.disk_stats.return_value = {
            'xvda1': {
                'write': {
                    'num': 0,
                    'bytes': 0,
                    'sectors': 0,
                },
                'read': {
                    'num': 0,
                    'bytes': 0,
                    'sectors': 0,
                },
            },
            'loop0': {
                'write': {
                    'num': 0,
                    'bytes': 0,
                    'sectors': 0,
                },
                'read': {
                    'num': 0,
                    'bytes': 0,
                    'sectors': 0,
                },
            },
        }

        assert stats_poller.server_thread(task) == {
            'index': '3',
            'farm_id': '1',
            'data': {
                'mem': {
                    'avail': None,
                    'cached': 0.0,
                    'total': 0.0,
                    'swap': 0.0,
                    'buffer': 0.0,
                    'shared': 0.0,
                    'swapavail': 0.0,
                    'free': 0.0
                },
                'net': {
                    'out': 0.0,
                    'in': 0.0
                },
                'cpu': {
                    'system': 0.0,
                    'idle': 0.0,
                    'user': 0.0,
                    'nice': 0.0
                },
                'la': {
                    'la5': 0.0,
                    'la15': 0.0,
                    'la1': 0.0
                }
            },
            'farm_role_id': '2'
        }

    import netsnmp
    stats_poller.CONFIG['with_snmp'] = True
    with mock.patch('netsnmp.Session') as Session:
        instance = Session.return_value
        instance.get.return_value = ('0', '0', '0', '0', '0.0', '0.0', '0.0',
                                     '0', '0', '0', '0', '0', '0', '0', '0',
                                     '0', '0')

        assert stats_poller.server_thread(task) == {
            'index': '3',
            'farm_id': '1',
            'data': {
                'mem': {
                    'avail': 0.0,
                    'cached': 0.0,
                    'total': 0.0,
                    'swap': 0.0,
                    'buffer': 0.0,
                    'shared': 0.0,
                    'swapavail': 0.0,
                    'free': 0.0
                },
                'net': {
                    'out': 0.0,
                    'in': 0.0
                },
                'cpu': {
                    'system': 0.0,
                    'idle': 0.0,
                    'user': 0.0,
                    'nice': 0.0
                },
                'la': {
                    'la5': 0.0,
                    'la15': 0.0,
                    'la1': 0.0
                }
            },
            'farm_role_id': '2'
        }
    stats_poller.CONFIG['with_snmp'] = False