""" Test creation of a PCP web daemon via the requests module """ # # Copyright (C) 2013-2014 Red Hat. # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # import requests, argparse, subprocess, os parser = argparse.ArgumentParser(description='test_webapi.py pmwebapi check') parser.add_argument('--version', action='version', version='1') parser.add_argument('--host', required=True) parser.add_argument('--port', default=44323) args = parser.parse_args() url = 'http://' + args.host + ':' + str(args.port) + '/' devnull = os.open(os.devnull, os.O_RDWR) os.unsetenv('http_proxy') os.unsetenv('HTTP_PROXY') # ------------------------------------------------------------------------ # test - create contexts req = requests.get(url=url + 'pmapi/context?local=foo') resp = req.json() ctx_local = resp['context'] print('Received PM_CONTEXT_LOCAL #' + str(ctx_local)) req = requests.get(url=url + 'pmapi/context?hostname=' + args.host) resp = req.json() ctx_host = resp['context'] print('Received PM_CONTEXT_HOST #' + str(ctx_host)) # ------------------------------------------------------------------------ # all these should get an error req = requests.get(url=url + 'pmapi/context?archivefile=/dev/null') print('bad archive /dev/null response code ' + str(req.status_code)) req = requests.get(url=url + 'pmapi/context?archivefile=../etc/passwd') print('bad archive ../etc/passwd response code ' + str(req.status_code)) req = requests.get(url=url + 'pmapi/context?archivefile=../../etc/shadow') print('bad archive ../../etc/shadow response code ' + str(req.status_code)) req = requests.get(url=url + 'pmapi/NOSUCHAPI') print('command NOSUCHAPI response code ' + str(req.status_code)) req = requests.get(url=url + 'pmapi/NOSUCHCONTEXT/_metric') print('context NOSUCHCONTEXT response code ' + str(req.status_code)) req = requests.get(url=url + 'pmapi/0/_metric') print('context 0 response code ' + str(req.status_code)) req = requests.get(url=url + 'random_nonpmwebapi_url') print('non-pmwebapi response code ' + str(req.status_code)) # ------------------------------------------------------------------------ def test_metric_enumeration(ctx, prefix): ctxurl = url + 'pmapi/' + str(ctx) + '/' if (ctx == ctx_local): procargs = ['pminfo', '-L', '-t'] dbg = 'local' else: procargs = ['pminfo', '-h', args.host, '-t'] dbg = 'host' if (prefix != ''): procargs.append(prefix) dbg = dbg + '-' + prefix proc = subprocess.Popen(procargs, stdout=subprocess.PIPE, stderr=devnull) num_metrics = 0 # f = open('/tmp/pmcd-' + dbg, 'w') while True: line = proc.stdout.readline() if (line == ''): break # eof if line.strip() == '': continue # blank line pminfo sometimes does that if line.find('Error: No PMCD agent') == -1 and \ line.find('Not known to the PMDA') == -1: num_metrics = num_metrics + 1 # f.write(line) # f.write('Total ' + str(num_metrics) + ' metrics\n') # f.close() testprefix='test #'+str(ctx)+' metric '+prefix+'.*' print(testprefix + ' enumeration with pminfo #'+str(num_metrics)) req = requests.get(url=ctxurl + '_metric' + \ ('?prefix='+prefix if prefix != '' else '')) resp = req.json() print(testprefix + ' enumeration with pmwebinfo #'+str(len(resp['metrics']))) # web_metrics = 0 # f = open('/tmp/webd-' + dbg, 'w') # for metric in resp['metrics']: # web_metrics = web_metrics + 1 # f.write(metric['name'] + '\n') # f.write('Total ' + str(web_metrics) + ' metrics\n') # f.close() if (abs(len(resp['metrics']) - num_metrics) < 10): # allow some variation print(testprefix + ' enumeration match count PASS') else: print(testprefix + ' enumeration match count FAIL') test_metric_enumeration(ctx_local,'') test_metric_enumeration(ctx_host,'') test_metric_enumeration(ctx_local,'kernel') test_metric_enumeration(ctx_host,'kernel') # ------------------------------------------------------------------------ # empty _fetch should get an error req = requests.get(url=url + 'pmapi/'+str(ctx_host)+'/_fetch') print('context #'+str(ctx_host)+' response code ' + str(req.status_code)) # ------------------------------------------------------------------------