Srikanth Vavilapalli | 1dfc9c8 | 2016-12-20 02:58:32 +0000 | [diff] [blame^] | 1 | import urllib2 |
| 2 | import requests |
| 3 | import time |
| 4 | import json |
| 5 | import sys |
| 6 | import getopt |
| 7 | |
| 8 | monitoring_channel = None |
| 9 | |
| 10 | def acquire_xos_monitoring_channel(): |
| 11 | admin_auth=("padmin@vicci.org", "letmein") |
| 12 | monitoring_channel = None |
| 13 | ceilometer_service = None |
| 14 | start_time = time.time() |
| 15 | ceilometerservice_wait_start_time=start_time |
| 16 | monitoringchannel_wait_start_time=start_time |
| 17 | cur_attempts = 1 |
| 18 | print "Attempt %s" % cur_attempts |
| 19 | while True: |
| 20 | try: |
| 21 | if (not ceilometer_service): |
| 22 | url = "http://localhost:8888/api/core/services/" |
| 23 | services = requests.get(url, auth=admin_auth).json() |
| 24 | if not services: |
| 25 | print 'No services are found....weird....exiting' |
| 26 | return None |
| 27 | else: |
| 28 | for service in services: |
| 29 | if 'ceilometer' in service['name']: |
| 30 | ceilometer_service = service |
| 31 | break |
| 32 | if (not ceilometer_service): |
| 33 | print 'Waiting for ceilometer_service object to be created, elapsed-time=%s' % (time.time()-ceilometerservice_wait_start_time) |
| 34 | else: |
| 35 | if ("OK" not in ceilometer_service['backend_status']): |
| 36 | cur_status = "other" |
| 37 | if "Unreachable" in ceilometer_service['backend_status']: |
| 38 | cur_status = "Unreachable" |
| 39 | elif "defer" in ceilometer_service['backend_status']: |
| 40 | cur_status = "Deferred" |
| 41 | print 'Waiting for ceilometer_service object to be ready, current_status:%s elapsed-time:%s' % (cur_status, time.time()-ceilometerservice_wait_start_time) |
| 42 | ceilometer_service = None |
| 43 | else: |
| 44 | print 'ceilometer_service is ready, elapsed-time:%s' % (time.time()-ceilometerservice_wait_start_time) |
| 45 | else: |
| 46 | print 'ceilometer_service is ready' |
| 47 | except Exception, e: |
| 48 | print 'Waiting for ceilometer_service object to be created, elapsed-time=%s' % (time.time()-ceilometerservice_wait_start_time) |
| 49 | |
| 50 | try: |
| 51 | if (not monitoring_channel): |
| 52 | url = "http://localhost:8888/api/tenant/monitoring/monitoringchannel/" |
| 53 | monitoring_channels = requests.get(url, auth=admin_auth).json() |
| 54 | if not monitoring_channels: |
| 55 | print 'Waiting for monitoring_channel object to be created, elapsed-time=%s' % (time.time()-monitoringchannel_wait_start_time) |
| 56 | else: |
| 57 | monitoring_channel = monitoring_channels[0] |
| 58 | url = "http://localhost:8888/api/core/tenants/"+str(monitoring_channel['id']) |
| 59 | monitoring_channel = None |
| 60 | monitoring_channel = requests.get(url, auth=admin_auth).json() |
| 61 | if (not monitoring_channel) or ("OK" not in monitoring_channel['backend_status']): |
| 62 | cur_status = "other" |
| 63 | if "Unreachable" in monitoring_channel['backend_status']: |
| 64 | cur_status = "Unreachable" |
| 65 | elif "defer" in monitoring_channel['backend_status']: |
| 66 | cur_status = "Deferred" |
| 67 | print 'Waiting for Monitoring_channel to be ready, current_status:%s, elapsed-time=%s' % (cur_status, time.time()-monitoringchannel_wait_start_time) |
| 68 | monitoring_channel = None |
| 69 | else: |
| 70 | print 'Monitoring_channel is ready, elapsed-time:%s' % (time.time()-monitoringchannel_wait_start_time) |
| 71 | else: |
| 72 | print 'Monitoring_channel is ready' |
| 73 | except Exception, e: |
| 74 | print 'Exception....Waiting for monitoring_channel object to be created, elapsed-time=%s' % (time.time()-monitoringchannel_wait_start_time) |
| 75 | |
| 76 | if (not ceilometer_service) or (not monitoring_channel): |
| 77 | #print "Sleeping for 60 seconds...." |
| 78 | cur_attempts += 1 |
| 79 | if cur_attempts > 15: |
| 80 | print "Maximum number of retrys reached....Exiting" |
| 81 | return None |
| 82 | time.sleep(60) |
| 83 | print "Attempt %s" % cur_attempts |
| 84 | else: |
| 85 | print "Both ceilometer_service and monitoring_channel are ready" |
| 86 | break |
| 87 | |
| 88 | #Wait until URL is completely UP |
| 89 | while True: |
| 90 | try: |
| 91 | url = "http://localhost:8888/api/tenant/monitoring/monitoringchannel/" |
| 92 | monitoring_channel = requests.get(url, auth=admin_auth).json()[0] |
| 93 | if not monitoring_channel['ceilometer_url']: |
| 94 | print 'Waiting for monitoring channel URL to be available, elapsed-time=%s' % (e.reason,time.time()-start_time) |
| 95 | time.sleep(5) |
| 96 | pass |
| 97 | else: |
| 98 | response = urllib2.urlopen(monitoring_channel['ceilometer_url'],timeout=5) |
| 99 | break |
| 100 | except urllib2.HTTPError, e: |
| 101 | print 'HTTP error %s ...Means monitoring channel URL is reachable, elapsed-time=%s' % (e.reason,time.time()-start_time) |
| 102 | return monitoring_channel |
| 103 | except urllib2.URLError, e: |
| 104 | print 'URL error...Waiting for monitoring channle URL %s is reachable, elapsed-time=%s' % (monitoring_channel['ceilometer_url'],time.time()-start_time) |
| 105 | time.sleep(5) |
| 106 | pass |
| 107 | |
| 108 | #Test to verify the onboarding of monitoring service and monitoring channel |
| 109 | #Test to verify there is no telemetry data available in the monitoring service initially |
| 110 | def test_1(): |
| 111 | global monitoring_channel |
| 112 | monitoring_channel = acquire_xos_monitoring_channel() |
| 113 | assert monitoring_channel != None |
| 114 | try: |
| 115 | url = monitoring_channel['ceilometer_url']+"v2/meters" |
| 116 | response = urllib2.urlopen(url) |
| 117 | data = json.load(response) |
| 118 | assert len(data) == 0, "Meters list is non empty for the first time" |
| 119 | print 'CURL on ceilometer URL succeeded %s' % data |
| 120 | except Exception, e: |
| 121 | print 'CURL on ceilometer URL failed %s' % e |
| 122 | |
| 123 | #Test to verify telemetry data from openstack and onos services is available in the monitoring service |
| 124 | def test_2(): |
| 125 | global monitoring_channel |
| 126 | if not monitoring_channel: |
| 127 | monitoring_channel = acquire_xos_monitoring_channel() |
| 128 | assert monitoring_channel != None |
| 129 | cur_attempts = 1 |
| 130 | while True: |
| 131 | try: |
| 132 | url = monitoring_channel['ceilometer_url']+"v2/meters" |
| 133 | response = urllib2.urlopen(url,timeout=20) |
| 134 | data = json.load(response) |
| 135 | if (len(data) == 0): |
| 136 | assert (cur_attempts < 5), "Meters list can not be empty after infra monitoring is enabled....Max retries reached" |
| 137 | print 'Waiting for monitoring channle URL %s to return metrics' % (url) |
| 138 | time.sleep(10) |
| 139 | cur_attempts += 1 |
| 140 | continue |
| 141 | assert any(d['name'] == 'disk.write.requests' for d in data), "Metrics does not contains disk related statistics" |
| 142 | assert any(d['name'] == 'cpu' for d in data), "Metrics does not contains cpu related statistics" |
| 143 | assert any(d['name'] == 'memory' for d in data), "Metrics does not contains memory related statistics" |
| 144 | print 'CURL on ceilometer URL succeeded Number of meters: %s' % (str(len(data))) |
| 145 | break |
| 146 | except Exception, e: |
| 147 | print 'CURL on ceilometer URL failed...%s' % e |
| 148 | break |
| 149 | |
| 150 | #Test to verify telemetry data from vSG services is available in the monitoring service |
| 151 | def test_3(): |
| 152 | global monitoring_channel |
| 153 | if not monitoring_channel: |
| 154 | monitoring_channel = acquire_xos_monitoring_channel() |
| 155 | assert monitoring_channel != None |
| 156 | cur_attempts = 1 |
| 157 | while True: |
| 158 | try: |
| 159 | url = monitoring_channel['ceilometer_url']+"v2/meters" |
| 160 | response = urllib2.urlopen(url,timeout=20) |
| 161 | data = json.load(response) |
| 162 | if (len(data) == 0): |
| 163 | assert (cur_attempts < 5), "Meters list can not be empty after infra monitoring is enabled....Max retries reached" |
| 164 | print 'Waiting for monitoring channle URL %s to return metrics' % (url) |
| 165 | time.sleep(10) |
| 166 | cur_attempts += 1 |
| 167 | continue |
| 168 | assert any(d['name'] == 'vsg.dns.cache.size' for d in data), "Metrics does not contains vsg.dns.cache.size related statistics" |
| 169 | assert any(d['name'] == 'vsg.dns.replaced_unexpired_entries' for d in data), "Metrics does not contains vsg.dns.replaced_unexpired_entries related statistics" |
| 170 | assert any(d['name'] == 'vsg.dns.queries_answered_locally' for d in data), "Metrics does not contains vsg.dns.queries_answered_locally related statistics" |
| 171 | print 'CURL on ceilometer URL succeeded Number of meters: %s' % (str(len(data))) |
| 172 | break |
| 173 | except Exception, e: |
| 174 | print 'CURL on ceilometer URL failed...%s' % e |
| 175 | break |
| 176 | |
| 177 | def usage(): |
| 178 | print 'monitoringservice_test.py --test=<num>' |
| 179 | |
| 180 | def main(argv): |
| 181 | try: |
| 182 | opts, args = getopt.getopt(argv,"ht:",["help","test="]) |
| 183 | except getopt.GetoptError: |
| 184 | usage() |
| 185 | sys.exit(2) |
| 186 | |
| 187 | for opt, arg in opts: |
| 188 | if opt in ("-h", "--help"): |
| 189 | usage() |
| 190 | sys.exit() |
| 191 | elif opt in ("-t", "--test"): |
| 192 | fq = "test_"+str(arg) |
| 193 | globals()[fq]() |
| 194 | |
| 195 | if __name__ == "__main__": |
| 196 | main(sys.argv[1:]) |