def requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return json.loads(self.json_data) def raise_for_status(self): return True ci_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "ci") if args[0] == YARN_APPS_URL: with open(Fixtures.file('apps_metrics', sdk_dir=ci_dir), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_JOBS_URL: with open(Fixtures.file('job_metrics', sdk_dir=ci_dir), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_JOB_COUNTERS_URL: with open(Fixtures.file('job_counter_metrics', sdk_dir=ci_dir), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_TASKS_URL: with open(Fixtures.file('task_metrics', sdk_dir=ci_dir), 'r') as f: body = f.read() return MockResponse(body, 200)
def requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): print self.json_data return json.loads(self.json_data) def raise_for_status(self): return True print 'DEBUG: {0}'.format(args[0]) print NAME_SYSTEM_STATE_URL if args[0] == NAME_SYSTEM_STATE_URL: print 'here' with open(Fixtures.file('hdfs_namesystem_state', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == NAME_SYSTEM_URL: print 'here' with open(Fixtures.file('hdfs_namesystem', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200)
def requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == YARN_CLUSTER_METRICS_URL: with open(Fixtures.file('cluster_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_APPS_URL: with open(Fixtures.file('apps_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_NODES_URL: with open(Fixtures.file('nodes_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200)
def testSpeed(self): # Pretend to be gmetad and serve a large piece of content original_file = Fixtures.file('ganglia.txt') subprocess.Popen("nc -l 8651 < %s" % original_file, shell=True) # Wait for 1 second time.sleep(1) g = Ganglia(logging.getLogger(__file__)) parsed = StringIO(g.check({'ganglia_host': 'localhost', 'ganglia_port': 8651})) original = Fixtures.file('ganglia.txt') x1 = tree.parse(parsed) x2 = tree.parse(original) # Cursory test self.assertEquals([c.tag for c in x1.getroot()], [c.tag for c in x2.getroot()])
def testSpeed(self): # Pretend to be gmetad and serve a large piece of content original_file = Fixtures.file('ganglia.txt') server = subprocess.Popen("nc -l 8651 < %s" % original_file, shell=True) # Wait for 1 second time.sleep(1) pfile = tempfile.NamedTemporaryFile() g = Ganglia(logging.getLogger(__file__)) # Running the profiler # profile.runctx("g.check({'ganglia_host': 'localhost', 'ganglia_port': 8651})", {}, {"g": g}, pfile.name) # p = pstats.Stats(pfile.name) # p.sort_stats('time').print_stats() parsed = StringIO(g.check({'ganglia_host': 'localhost', 'ganglia_port': 8651})) original = Fixtures.file('ganglia.txt') x1 = tree.parse(parsed) x2 = tree.parse(original) # Cursory test self.assertEquals([c.tag for c in x1.getroot()], [c.tag for c in x2.getroot()])
def requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == CLUSTER_INFO_URL: with open(Fixtures.file('cluster_info'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_APPS_URL: with open(Fixtures.file('apps_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_JOBS_URL: with open(Fixtures.file('job_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_JOB_COUNTERS_URL: with open(Fixtures.file('job_counter_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == MR_TASKS_URL: with open(Fixtures.file('task_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200)
def standalone_requests_get_mock(*args, **kwargs): class MockStandaloneResponse: text = '' def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code self.text = json_data def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == STANDALONE_APP_URL: with open(Fixtures.file('spark_standalone_apps'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_APP_HTML_URL: with open(Fixtures.file('spark_standalone_app'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_APP_URL: with open(Fixtures.file('spark_apps'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_JOB_URL: with open(Fixtures.file('job_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_STAGE_URL: with open(Fixtures.file('stage_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_EXECUTOR_URL: with open(Fixtures.file('executor_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_RDD_URL: with open(Fixtures.file('rdd_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200)
def requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): print self.json_data return json.loads(self.json_data) def raise_for_status(self): return True with open(Fixtures.file('hdfs_datanode_jmx'), 'r') as f: body = f.read() return MockResponse(body, 200)
def __init__(self, *args, **kwargs): AgentCheckTest.__init__(self, *args, **kwargs) self._expvar_url = Fixtures.file('expvar_output') self.mock_config = { "instances": [{ "expvar_url": self._expvar_url, "tags": ["optionaltag1", "optionaltag2"], "metrics": [ { # Contains list traversal and default values "path": "memstats/BySize/1/Mallocs", }, { "path": "memstats/PauseTotalNs", "alias": "go_expvar.gc.pause", "type": "rate" }, { "path": "random_walk", "alias": "go_expvar.gauge1", "type": "gauge", "tags": ["metric_tag1:metric_value1", "metric_tag2:metric_value2"] } ] }] } self.mocks = { '_get_data': _get_data_mock, } self.config = { "instances": [{ "expvar_url": 'http://localhost:8079/debug/vars', 'tags': ['my_tag'], 'metrics': [ { 'path': 'last_user' }, { 'path': 'num_calls', "type": "rate" }, ] }] }
def yarn_requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == YARN_APP_URL: with open(Fixtures.file('yarn_apps', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_APP_URL: with open(Fixtures.file('spark_apps', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_JOB_URL: with open(Fixtures.file('job_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_STAGE_URL: with open(Fixtures.file('stage_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_EXECUTOR_URL: with open(Fixtures.file('executor_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_RDD_URL: with open(Fixtures.file('rdd_metrics', sdk_dir=FIXTURE_DIR), 'r') as f: body = f.read() return MockResponse(body, 200)
def yarn_requests_get_mock(*args, **kwargs): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == YARN_APP_URL: with open(Fixtures.file('yarn_apps'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_APP_URL: with open(Fixtures.file('spark_apps'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_JOB_URL: with open(Fixtures.file('job_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_STAGE_URL: with open(Fixtures.file('stage_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_EXECUTOR_URL: with open(Fixtures.file('executor_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200) elif args[0] == YARN_SPARK_RDD_URL: with open(Fixtures.file('rdd_metrics'), 'r') as f: body = f.read() return MockResponse(body, 200)
import unittest # project from tests.checks.common import Fixtures, get_check log = logging.getLogger() CONFIG = """ init_config: instances: - mysql_host: localhost mysql_user: root rrd_path: /tmp/cacti_test/rrds rrd_whitelist: %s """ % Fixtures.file('whitelist.txt') class TestCacti(unittest.TestCase): def setUp(self): self.tmp_dir = '/tmp/cacti_test' self.rrd_dir = os.path.join(os.path.dirname(__file__), "cacti") # Create our temporary RRD path, if needed try: os.mkdir(self.tmp_dir) except Exception: # Ignore, directory already exists pass def tearDown(self):
def test_get_auth_token(self): KubeUtil.AUTH_TOKEN_PATH = '/foo/bar' self.assertIsNone(KubeUtil.get_auth_token()) KubeUtil.AUTH_TOKEN_PATH = Fixtures.file( 'events.json') # any file could do the trick self.assertIsNotNone(KubeUtil.get_auth_token())
def registry_file_path(self, name): return Fixtures.file(name + '_registry.json', sdk_dir=self.FIXTURE_DIR)
def test_get_auth_token(self): KubeUtil.AUTH_TOKEN_PATH = '/foo/bar' self.assertIsNone(KubeUtil.get_auth_token()) KubeUtil.AUTH_TOKEN_PATH = Fixtures.file('events.json') # any file could do the trick self.assertIsNotNone(KubeUtil.get_auth_token())
def standalone_requests_pre20_get_mock(*args, **kwargs): class MockStandaloneResponse: text = '' def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code self.text = json_data def json(self): return json.loads(self.json_data) def raise_for_status(self): return True if args[0] == STANDALONE_APP_URL: with open(Fixtures.file('spark_standalone_apps'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_APP_HTML_URL: with open(Fixtures.file('spark_standalone_app'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_APP_URL: with open(Fixtures.file('spark_apps_pre20'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_JOB_URL: return MockStandaloneResponse("{}", 404) elif args[0] == STANDALONE_SPARK_STAGE_URL: return MockStandaloneResponse("{}", 404) elif args[0] == STANDALONE_SPARK_EXECUTOR_URL: return MockStandaloneResponse("{}", 404) elif args[0] == STANDALONE_SPARK_RDD_URL: return MockStandaloneResponse("{}", 404) elif args[0] == STANDALONE_SPARK_JOB_URL_PRE20: with open(Fixtures.file('job_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_STAGE_URL_PRE20: with open(Fixtures.file('stage_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_EXECUTOR_URL_PRE20: with open(Fixtures.file('executor_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200) elif args[0] == STANDALONE_SPARK_RDD_URL_PRE20: with open(Fixtures.file('rdd_metrics'), 'r') as f: body = f.read() return MockStandaloneResponse(body, 200)