<commit_before>#!/usr/bin/env python2.7
"""
Integration test of the jmx4prometheus webserver.

Runs a sample Java app and jmx4prometheus webserver then tests some sample beans
in the resulting JSON.
"""

import json
import os
import signal
import subprocess
import time
import unittest
import urllib


def setUpModule():
    dtry = os.path.abspath(os.path.dirname(__file__) + '/../')
    global MBEAN_SERVER
    MBEAN_SERVER = subprocess.Popen(
            ['bash', dtry + '/run_sample_server.sh'], preexec_fn=os.setsid)
    global JMX_4_PROM_SERVER
    JMX_4_PROM_SERVER = subprocess.Popen(
            ['bash', dtry + '/run_jmx4prometheus_server.sh'], preexec_fn=os.setsid)
    time.sleep(1)


def tearDownModule():
    os.killpg(MBEAN_SERVER.pid, signal.SIGTERM)
    os.killpg(JMX_4_PROM_SERVER.pid, signal.SIGTERM)
    MBEAN_SERVER.wait()
    JMX_4_PROM_SERVER.wait()


class Jmx4PrometheusE2ETest(unittest.TestCase):

    def test_basic_json(self):
        self.assertTrue(self.make_json_request())

    def test_jvm_bean(self):
        beans = self.make_json_request()

        self.assertIn('java_lang_MemoryPool_PeakUsage_init', beans.keys())
        bean = beans['java_lang_MemoryPool_PeakUsage_init']
        self.assertBeanEquals(bean,
                        'java_lang_MemoryPool_PeakUsage_init',
                        'java.lang.management.MemoryUsage',
                        {'nom': 'PS Survivor Space'})
        self.assertTrue(self.beanValue(bean) > 0, bean)

    def test_cassandra_bean(self):
        beans = self.make_json_request()
        self.assertIn('org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount', beans.keys())
        bean = beans['org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount']
        self.assertBeanEquals(bean,
                        'org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount',
                        'Attribute exposed for management'
                        )
        self.assertEquals(100, self.beanValue(bean))

    def test_cassandra_metrics(self):
        beans = self.make_json_request()
        self.assertIn('org_apache_cassandra_metrics_Compaction_Value', beans.keys())
        bean = beans['org_apache_cassandra_metrics_Compaction_Value']
        self.assertBeanEquals(bean,
                        'org_apache_cassandra_metrics_Compaction_Value',
                        'Attribute exposed for management',
                        {u'nom': u'CompletedTasks'}
                        )
        self.assertEquals(0.2, self.beanValue(bean))

    def test_hadoop_bean(self):
        beans = self.make_json_request()
        bean = beans['hadoop_DataNode_replaceBlockOpMinTime']
        self.assertBeanEquals(bean,
                        'hadoop_DataNode_replaceBlockOpMinTime',
                        'Attribute exposed for management',
                        {'nom': 'DataNodeActivity-ams-hdd001-50010', 'service': 'DataNode'}
                        )
        self.assertEquals(200, self.beanValue(bean))

    def test_tabular_bean(self):
        beans = self.make_json_request()
        self.assertIn('java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed', beans.keys())
        bean = beans['java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed']
        bean = beans['java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed']
        self.assertBeanEquals(bean,
                        'java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed',
                        'java.lang.management.MemoryUsage',
                        {'key': 'PS Survivor Space', 'nom': 'PS Scavenge'}
                        )
        self.assertTrue(self.beanValue(bean) > 0)

    def test_error_message(self):
        f = urllib.urlopen('http://localhost:5556?target=localhost:10101')
        t = f.read()
        self.assertTrue(len(t) > 0)<commit_msg>Fix integration tests: remove tests that rely on sort order.<commit_after>#!/usr/bin/env python2.7
"""
Integration test of the jmx4prometheus webserver.

Runs a sample Java app and jmx4prometheus webserver then tests some sample beans
in the resulting JSON.
"""

import json
import os
import signal
import subprocess
import time
import unittest
import urllib


def setUpModule():
    dtry = os.path.abspath(os.path.dirname(__file__) + '/../')
    global MBEAN_SERVER
    MBEAN_SERVER = subprocess.Popen(
            ['bash', dtry + '/run_sample_server.sh'], preexec_fn=os.setsid)
    global JMX_4_PROM_SERVER
    JMX_4_PROM_SERVER = subprocess.Popen(
            ['bash', dtry + '/run_jmx4prometheus_server.sh'], preexec_fn=os.setsid)
    time.sleep(1)


def tearDownModule():
    os.killpg(MBEAN_SERVER.pid, signal.SIGTERM)
    os.killpg(JMX_4_PROM_SERVER.pid, signal.SIGTERM)
    MBEAN_SERVER.wait()
    JMX_4_PROM_SERVER.wait()


class Jmx4PrometheusE2ETest(unittest.TestCase):

    def test_basic_json(self):
        self.assertTrue(self.make_json_request())

    def test_jvm_bean(self):
        beans = self.make_json_request()

        self.assertIn('java_lang_MemoryPool_PeakUsage_init', beans.keys())
        bean = beans['java_lang_MemoryPool_PeakUsage_init']
        self.assertTrue(self.beanValue(bean) > 0, bean)

    def test_cassandra_bean(self):
        beans = self.make_json_request()
        self.assertIn('org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount', beans.keys())
        bean = beans['org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount']
        self.assertBeanEquals(bean,
                        'org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount',
                        'Attribute exposed for management'
                        )
        self.assertEquals(100, self.beanValue(bean))

    def test_cassandra_metrics(self):
        beans = self.make_json_request()
        self.assertIn('org_apache_cassandra_metrics_Compaction_Value', beans.keys())
        bean = beans['org_apache_cassandra_metrics_Compaction_Value']
        self.assertBeanEquals(bean,
                        'org_apache_cassandra_metrics_Compaction_Value',
                        'Attribute exposed for management',
                        {u'nom': u'CompletedTasks'}
                        )
        self.assertEquals(0.2, self.beanValue(bean))

    def test_hadoop_bean(self):
        beans = self.make_json_request()
        bean = beans['hadoop_DataNode_replaceBlockOpMinTime']
        self.assertBeanEquals(bean,
                        'hadoop_DataNode_replaceBlockOpMinTime',
                        'Attribute exposed for management',
                        {'nom': 'DataNodeActivity-ams-hdd001-50010', 'service': 'DataNode'}
                        )
        self.assertEquals(200, self.beanValue(bean))

    def test_tabular_bean(self):
        beans = self.make_json_request()
        self.assertIn('java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed', beans.keys())
        bean = beans['java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_committed']
        self.assertTrue(self.beanValue(bean) > 0)

    def test_error_message(self):
        f = urllib.urlopen('http://localhost:5556?target=localhost:10101')
        t = f.read()
        self.assertTrue(len(t) > 0)
{'fraction_non_alphanumeric': 0.095060706401766, 'fraction_numerical': 0.011037527593818985, 'mean_word_length': 2.8354497354497354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '632184', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2181, 'n_words': 335}