def test_uploading_duplicate(self): found = SpliceServer.objects() self.assertEquals(len(found), 0) server = SpliceServer() server.uuid = "Splice Server Test UUID-1" server.description = "Description data" server.hostname = "server.example.com" server.environment = "environment info" # Note this tests save()'s the instance to the DB server.save() self.assertIsNotNone(server.updated.tzinfo) example = {"objects":[server]} post_data = utils.obj_to_json(example) LOG.info("Calling api for spliceserver import with post data: '%s'" % (post_data)) resp = self.raw_api_client.post('/api/v1/spliceserver/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) LOG.info("Response for spliceserver import: Status Code: %s, Response: %s" % (resp.status_code, resp)) self.assertEquals(resp.status_code, 204) # Now check that the server api saved the object as expected found = SpliceServer.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, server.uuid) self.assertEquals(found[0].description, server.description) self.assertEquals(found[0].hostname, server.hostname) self.assertEquals(found[0].environment, server.environment) self.assertIsNotNone(found[0].created) self.assertIsNotNone(found[0].updated)
def get_this_server(self): server_uuid = certs.get_splice_server_identity() environment = SPLICE_SERVER_INFO["environment"] description = SPLICE_SERVER_INFO["description"] hostname = SPLICE_SERVER_INFO["hostname"] server = SpliceServer.objects(uuid=server_uuid).first() if not server: server = SpliceServer( uuid=server_uuid, description=description, environment=environment, hostname=hostname ) try: server.save() except Exception, e: _LOG.exception(e)
def _get_splice_server_metadata(addr, since=None): """ Returns splice server metadata which has not yet been uploaded to 'addr' @param addr: remote server to upload data to @param since: Optional, date we want to send data from, intended for unit tests only @type since: datetime.datetime @return: list of splice server objects ordered by date """ last_timestamp = since data_transfer = SpliceServerTransferInfo.objects(server_hostname=addr).first() # Get the last timestamp we sent to 'addr' if not last_timestamp and data_transfer: last_timestamp = data_transfer.last_timestamp if last_timestamp: data = SpliceServer.objects(updated__gt=last_timestamp) else: data = SpliceServer.objects() data = data.order_by("updated") _LOG.info("Retrieved %s items to send to %s, since last timestamp of %s" % (len(data), addr, last_timestamp)) return data
def create_splice_server_metadata(self, num, save=True): retval = [] for index in range(0, num): s = SpliceServer() s.uuid = str(uuid4()) s.description = "Description for %s" % (s.uuid) s.hostname = "hostname.example.com" s.environment = "environment example" # s.created & s.modified will be set on pre_save() hook if save: s.save() retval.append(s) return retval
def test_get_splice_server_metadata_collection(self): a = SpliceServer(uuid="uuid a", description="descr a", hostname="a.example.com", environment="environment a") a.save() b = SpliceServer(uuid="uuid b", description="descr b", hostname="b.example.com", environment="environment b") b.save() resp = self.api_client.get('/api/v1/spliceserver/', format='json', SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) self.assertEquals(resp.status_code, 200) LOG.info("Response for GET spliceserver: Status Code: %s, Response: %s" % (resp.status_code, resp))
def test_example_with_raw_string_data(self): example = {"objects": [ {"created": "2012-12-07T15:35:54.448000", "description": "descr a", "environment": "environment a", "hostname": "a.example.com", "id": "50c20cda5c99cb55d9000001", "updated": "2012-12-07T15:35:54.448000", "resource_uri": "/api/v1/spliceserver/50c20cda5c99cb55d9000001/", "uuid": "uuid a"}, {"created": "2012-12-07T15:35:54.686000", "description": "descr b", "environment": "environment b", "hostname": "b.example.com", "id": "50c20cda5c99cb55d9000002", "updated": "2012-12-07T15:35:54.686000", "resource_uri": "/api/v1/spliceserver/50c20cda5c99cb55d9000002/", "uuid": "uuid b" }]} post_data = json.dumps(example) resp = self.raw_api_client.post('/api/v1/spliceserver/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) self.assertEquals(resp.status_code, 204) found = SpliceServer.objects() self.assertEquals(len(found), 2) self.assertIn(found[0].uuid, ("uuid a", "uuid b"))
def test_date_as_string_is_converted_on_save(self): found = SpliceServer.objects() self.assertEquals(len(found), 0) server = SpliceServer() server.uuid = "Splice Server Test UUID-1" server.description = "Description data" server.hostname = "server.example.com" server.environment = "environment info" server.created = "2012-12-06T11:13:06.432367" server.updated = "2012-12-06T11:13:06.432367" server.save() found = SpliceServer.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, server.uuid) self.assertEquals(found[0].description, server.description) self.assertEquals(found[0].hostname, server.hostname) self.assertEquals(found[0].environment, server.environment) self.assertIsNotNone(found[0].created) self.assertEquals(type(found[0].created), datetime.datetime) self.assertIsNotNone(found[0].updated) self.assertEquals(type(found[0].updated), datetime.datetime)
def test_upload_newer_spliceserver(self): found = SpliceServer.objects() self.assertEquals(len(found), 0) # Create 'newer' server and save to DB orig_uuid = "Splice Server UUID" orig_description = "Original Description" orig_hostname = "Original.hostname.com" orig_environment = "Original environment" older = SpliceServer() older.uuid = orig_uuid older.description = orig_description older.hostname = orig_hostname older.environment = orig_environment older.created = "2011-01-01T11:13:06.432367" older.updated = "2012-12-01T11:13:06.432367" older.save() found = SpliceServer.objects() self.assertEquals(len(found), 1) # Create 'older' which is one month older than newer newer = SpliceServer() newer.uuid = orig_uuid newer.description = "Updated description" newer.hostname = "updated.server.example.com" newer.environment = "Updated environment info" newer.created = "2011-01-01T11:13:06.432367" newer.updated = "2012-12-31T11:13:06.432367+00:00" example = {"objects": [newer]} post_data = utils.obj_to_json(example) LOG.info("Calling api for spliceserver import with post data: '%s'" % (post_data)) resp = self.raw_api_client.post('/api/v1/spliceserver/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) LOG.info("Response for spliceserver import: Status Code: %s, Response: %s" % (resp.status_code, resp)) self.assertEquals(resp.status_code, 204) # Now check that the server api kept the 'newer' as is and ignored the older found = SpliceServer.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, orig_uuid) self.assertEquals(found[0].description, newer.description) self.assertEquals(found[0].hostname, newer.hostname) self.assertEquals(found[0].environment, newer.environment) self.assertEquals(str(found[0].updated), "2012-12-31 11:13:06.432000+00:00")
def get_existing(self, obj): return SpliceServer.objects(uuid=obj.uuid).first()