def testPayloadGeneration(self): # check current version of report (should be empty) self.checkForExistingCallHomeData() # call callhome scripting beforeReportGeneration = datetime.utcnow() chd = CallHomeData(self.dmd, True) data = chd.getData() afterReportGeneration = datetime.utcnow() time.sleep(1) # Unfortunately the cycler code can't be disentagled # from itself for testability so we have to mimic it # for this test case. # What happens is that CallHomeCycler # kicks off the callhome script (callhome.py) # and takes the process output as a string # and stores it in the callhome object # in zodb (dmd.callHome). In callhome.py you can # see that the Main class spits out the # json.dumps of the output of CallHomeData.getData. self.dmd.callHome = PersistentCallHomeData() self.dmd.callHome.metrics = json.dumps(data) # create the actual payload that will be sent beforePayloadGeneration = datetime.utcnow() payloadGenerator = CallHome(self.dmd) payload = payloadGenerator.get_payload(doEncrypt=False) afterPayloadGeneration = datetime.utcnow() # decrypt payload and reconstitute object payloadObj = json.loads(payload) # make sure payload has the required fields self.assertTrue('product' in payloadObj) self.assertTrue('uuid' in payloadObj) self.assertTrue('symkey' in payloadObj) self.assertTrue('metrics' in payloadObj) # reconstitute metrics obj & make sure send date is present # and has a valid time metricsObj = json.loads(payloadObj['metrics']) self.assertTrue('Send Date' in metricsObj) sendDateDT = datetime.strptime(metricsObj['Send Date'], DATETIME_ISOFORMAT) reportDateDT = datetime.strptime(metricsObj['Report Date'], DATETIME_ISOFORMAT) self.assertTrue(reportDateDT < sendDateDT) self.assertTrue( beforeReportGeneration <= reportDateDT <= afterReportGeneration) self.assertTrue( beforePayloadGeneration <= sendDateDT <= afterPayloadGeneration)
def testPayloadGeneration(self): # check current version of report (should be empty) self.checkForExistingCallHomeData() # call callhome scripting beforeReportGeneration = datetime.utcnow() chd = CallHomeData(self.dmd, True) data = chd.getData() afterReportGeneration = datetime.utcnow() time.sleep(1) # Unfortunately the cycler code can't be disentagled # from itself for testability so we have to mimic it # for this test case. # What happens is that CallHomeCycler # kicks off the callhome script (callhome.py) # and takes the process output as a string # and stores it in the callhome object # in zodb (dmd.callHome). In callhome.py you can # see that the Main class spits out the # json.dumps of the output of CallHomeData.getData. self.dmd.callHome = PersistentCallHomeData() self.dmd.callHome.metrics = json.dumps(data) # create the actual payload that will be sent beforePayloadGeneration = datetime.utcnow() payloadGenerator = CallHome(self.dmd) payload = payloadGenerator.get_payload(doEncrypt=False) afterPayloadGeneration = datetime.utcnow() # decrypt payload and reconstitute object payloadObj = json.loads(payload) # make sure payload has the required fields self.assertTrue('product' in payloadObj) self.assertTrue('uuid' in payloadObj) self.assertTrue('symkey' in payloadObj) self.assertTrue('metrics' in payloadObj) # reconstitute metrics obj & make sure send date is present # and has a valid time metricsObj = json.loads(payloadObj['metrics']) self.assertTrue('Send Date' in metricsObj) sendDateDT = datetime.strptime(metricsObj['Send Date'], DATETIME_ISOFORMAT) reportDateDT = datetime.strptime(metricsObj['Report Date'], DATETIME_ISOFORMAT) self.assertTrue(reportDateDT < sendDateDT) self.assertTrue(beforeReportGeneration <= reportDateDT <= afterReportGeneration) self.assertTrue(beforePayloadGeneration <= sendDateDT <= afterPayloadGeneration)
def testSendMethod(self): # check current version of report (should be empty) self.checkForExistingCallHomeData() # call callhome scripting chd = CallHomeData(self.dmd, True) data = chd.getData() # Unfortunately the cycler code can't be disentagled # from itself for testability so we have to mimic it # for this test case. # What happens is that CallHomeCycler # kicks off the callhome script (callhome.py) # and takes the process output as a string # and stores it in the callhome object # in zodb (dmd.callHome). In callhome.py you can # see that the Main class spits out the # json.dumps of the output of CallHomeData.getData. self.dmd.callHome = PersistentCallHomeData() self.dmd.callHome.metrics = json.dumps(data) # create the actual payload that will be sent payloadGenerator = CallHome(self.dmd) payload = payloadGenerator.get_payload(doEncrypt=False) # reconstitute object payloadObj = json.loads(payload) # reconstitute metrics obj & make sure send date is present # and has a valid time metricsObj = json.loads(payloadObj['metrics']) self.assertTrue('Send Method' in metricsObj) self.assertEquals('directpost', metricsObj['Send Method']) # Fetch the payload the browserjs way payloadGenerator = CallHome(self.dmd) payload = payloadGenerator.get_payload(method='browserjs', doEncrypt=False) # reconstitute object payloadObj = json.loads(payload) # reconstitute metrics obj & make sure send date is present # and has a valid time metricsObj = json.loads(payloadObj['metrics']) self.assertTrue('Send Method' in metricsObj) self.assertEquals('browserjs', metricsObj['Send Method'])
def __init__(self, dmd): self.dmd = dmd if not safe_hasattr(dmd, 'callHome') or dmd.callHome is None: dmd._p_jar.sync() CallHome(dmd).callHome transaction.commit() self.callhome = dmd.callHome self.gatherProtocol = None
def direct_post(dmd): callhome = CallHome(dmd) chs = CallHomeStatus() if not callhome.attempt('directpost'): return payload = callhome.get_payload() if not payload: logger.warning('Error getting or encrypting payload for direct-post') return payload = base64.urlsafe_b64encode(payload) params = urlencode({'enc': payload}) chs.stage(chs.REQUEST_CALLHOME) try: httpreq = urllib2.urlopen(POST_CHECKIN_URL, params, _URL_TIMEOUT) returnPayload = httpreq.read() except Exception as e: chs.stage(chs.REQUEST_CALLHOME, "FAILED", str(e)) logger.warning('Error retrieving data from callhome server %s', e) else: chs.stage(chs.REQUEST_CALLHOME, "FINISHED") callhome.save_return_payload(returnPayload) return
def render(self): dmd = self.context.dmd # if not logged in, inject nothing if not dmd.ZenUsers.getUserSettings(): return '' callhome = CallHome(dmd) # if we've checked in or attempted to check in recently, inject nothing if not callhome.attempt('browserjs'): return '' payload = callhome.get_payload(method='browserjs') if not payload: logger.warning('Error getting or encrypting payload for browserjs') return '' # Output the checkin data to a js snippet, wait a few seconds in the # browser, and inject script tags to the checkin url to the body tag. # This makes sure that the browser never waits on the checkin url. # Callbacks from the server script invoke the next # Zenoss.Callhome_next() return """<script type="text/javascript"> var packets = %s, currentPacket = 0; Zenoss.Callhome_next = function() { if (currentPacket < packets.length) { var script = document.createElement('script'); script.type= 'text/javascript'; script.src = "%s?enc=" + packets[currentPacket]; document.body.appendChild(script); } currentPacket += 1; }; var task = new Ext.util.DelayedTask(Zenoss.Callhome_next); task.delay(5000); </script>""" % (json.dumps(encode_for_js(payload)), JS_CALLHOME_URL)
def render(self): dmd = self.context.dmd # if not logged in, inject nothing if not dmd.ZenUsers.getUserSettings(): return '' callhome = CallHome(dmd) # if we've checked in or attempted to check in recently, inject nothing if not callhome.attempt('browserjs'): return '' payload = callhome.get_payload(method='browserjs') if not payload: logger.warning('Error getting or encrypting payload for browserjs') return '' # Output the checkin data to a js snippet, wait a few seconds in the # browser, and inject script tags to the checkin url to the body tag. # This makes sure that the browser never waits on the checkin url. # Callbacks from the server script invoke the next # Zenoss.Callhome_next() return """<script type="text/javascript"> var packets = %s, currentPacket = 0; Zenoss.Callhome_next = function() { if (currentPacket < packets.length) { var script = document.createElement('script'); script.type= 'text/javascript'; script.src = "%s?enc=" + packets[currentPacket]; document.body.appendChild(script); } currentPacket += 1; }; var task = new Ext.util.DelayedTask(Zenoss.Callhome_next); task.delay(5000); </script>""" % (json.dumps( encode_for_js(payload)), JS_CALLHOME_URL)
def checkin(self, returnPayload): # record successful check in callhome = CallHome(self.context.dmd) callhome.save_return_payload(returnPayload) return ''
def __init__(self, dmd): self.dmd = dmd self.callhome = CallHome(dmd).callHome self.gatherProtocol = None