def test_isotime(self): dt1 = datetime.datetime(2001, 11, 10, 1, 2, 3) self.assertEqual('2001-11-10T01:02:03Z', utils.isotime(dt1)) iso_re = re.compile(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z') now_iso = utils.isotime() self.assertTrue(iso_re.match(now_iso) is not None)
def test_results_filtering(self): self.cleanup() self.start_servers(**self.__dict__.copy()) api_port = self.api_port registry_port = self.registry_port # 1. Add some images _add_cmd = "bin/glance --port=%d add is_public=True" % api_port _add_args = [ "name=Name1 disk_format=vhd container_format=ovf foo=bar", "name=Name2 disk_format=ami container_format=ami foo=bar", "name=Name3 disk_format=vhd container_format=ovf foo=baz " "min_disk=7 min_ram=256", ] image_ids = [] for i, args in enumerate(_add_args): cmd = "%s %s" % (_add_cmd, args) exitcode, out, err = execute(cmd) self.assertEqual(0, exitcode) self.assertTrue(out.strip().find('Added new image with ID:') > -1) image_ids.append(out.strip().split(':')[1].strip()) _base_cmd = "bin/glance --port=%d" % api_port _index_cmd = "%s index -f" % (_base_cmd,) # 2. Check name filter cmd = "name=Name2" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) image_lines = out.split("\n")[2:-1] self.assertEqual(0, exitcode) self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 3. Check disk_format filter cmd = "disk_format=vhd" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(2, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[2]) self.assertEqual(image_lines[1].split()[0], image_ids[0]) # 4. Check container_format filter cmd = "container_format=ami" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 5. Check container_format filter cmd = "container_format=ami" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 6. Check status filter cmd = "status=killed" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(0, len(image_lines)) # 7. Check property filter cmd = "foo=bar" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(2, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) self.assertEqual(image_lines[1].split()[0], image_ids[0]) # 8. Check multiple filters cmd = "name=Name2 foo=bar" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 9. Check past changes-since dt1 = datetime.datetime.utcnow() - datetime.timedelta(1) iso1 = utils.isotime(dt1) cmd = "changes-since=%s" % iso1 exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(3, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[2]) self.assertEqual(image_lines[1].split()[0], image_ids[1]) self.assertEqual(image_lines[2].split()[0], image_ids[0]) # 10. Check future changes-since dt2 = datetime.datetime.utcnow() + datetime.timedelta(1) iso2 = utils.isotime(dt2) cmd = "changes-since=%s" % iso2 exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(0, len(image_lines)) # 11. Ensure details call also respects filters _details_cmd = "%s details" % (_base_cmd,) cmd = "foo=bar" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[1:-1] self.assertEqual(26, len(image_lines)) self.assertEqual(image_lines[1].split()[1], image_ids[1]) self.assertEqual(image_lines[14].split()[1], image_ids[0]) # 12. Check min_ram filter cmd = "min_ram=256" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(12, len(image_lines)) self.assertEqual(image_lines[0].split()[1], image_ids[2]) # 13. Check min_disk filter cmd = "min_disk=7" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(12, len(image_lines)) self.assertEqual(image_lines[0].split()[1], image_ids[2]) self.stop_servers()
def test_results_filtering(self): self.cleanup() self.start_servers(**self.__dict__.copy()) api_port = self.api_port registry_port = self.registry_port # 1. Add some images _add_cmd = "bin/glance --port=%d add is_public=True" % api_port _add_args = [ "name=Name1 disk_format=vhd container_format=ovf foo=bar", "name=Name2 disk_format=ami container_format=ami foo=bar", "name=Name3 disk_format=vhd container_format=ovf foo=baz " "min_disk=7 min_ram=256", ] image_ids = [] for i, args in enumerate(_add_args): cmd = "%s %s" % (_add_cmd, args) exitcode, out, err = execute(cmd) self.assertEqual(0, exitcode) self.assertTrue(out.strip().find('Added new image with ID:') > -1) image_ids.append(out.strip().split(':')[1].strip()) _base_cmd = "bin/glance --port=%d" % api_port _index_cmd = "%s index -f" % (_base_cmd, ) # 2. Check name filter cmd = "name=Name2" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) image_lines = out.split("\n")[2:-1] self.assertEqual(0, exitcode) self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 3. Check disk_format filter cmd = "disk_format=vhd" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(2, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[2]) self.assertEqual(image_lines[1].split()[0], image_ids[0]) # 4. Check container_format filter cmd = "container_format=ami" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 5. Check container_format filter cmd = "container_format=ami" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 6. Check status filter cmd = "status=killed" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(0, len(image_lines)) # 7. Check property filter cmd = "foo=bar" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(2, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) self.assertEqual(image_lines[1].split()[0], image_ids[0]) # 8. Check multiple filters cmd = "name=Name2 foo=bar" exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(1, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[1]) # 9. Check past changes-since dt1 = datetime.datetime.utcnow() - datetime.timedelta(1) iso1 = utils.isotime(dt1) cmd = "changes-since=%s" % iso1 exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(3, len(image_lines)) self.assertEqual(image_lines[0].split()[0], image_ids[2]) self.assertEqual(image_lines[1].split()[0], image_ids[1]) self.assertEqual(image_lines[2].split()[0], image_ids[0]) # 10. Check future changes-since dt2 = datetime.datetime.utcnow() + datetime.timedelta(1) iso2 = utils.isotime(dt2) cmd = "changes-since=%s" % iso2 exitcode, out, err = execute("%s %s" % (_index_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(0, len(image_lines)) # 11. Ensure details call also respects filters _details_cmd = "%s details" % (_base_cmd, ) cmd = "foo=bar" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[1:-1] self.assertEqual(26, len(image_lines)) self.assertEqual(image_lines[1].split()[1], image_ids[1]) self.assertEqual(image_lines[14].split()[1], image_ids[0]) # 12. Check min_ram filter cmd = "min_ram=256" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(12, len(image_lines)) self.assertEqual(image_lines[0].split()[1], image_ids[2]) # 13. Check min_disk filter cmd = "min_disk=7" exitcode, out, err = execute("%s %s" % (_details_cmd, cmd)) self.assertEqual(0, exitcode) image_lines = out.split("\n")[2:-1] self.assertEqual(12, len(image_lines)) self.assertEqual(image_lines[0].split()[1], image_ids[2]) self.stop_servers()
def _serialize_datetimes(image): for (key, value) in image.iteritems(): if isinstance(value, datetime.datetime): image[key] = utils.isotime(value)
def test_filtered_images(self): """ Set up four test images and ensure each query param filter works """ self.cleanup() self.start_servers(**self.__dict__.copy()) # 0. GET /images # Verify no public images path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) self.assertEqual(content, '{"images": []}') # 1. POST /images with three public images, and one private image # with various attributes headers = {'Content-Type': 'application/octet-stream', 'X-Image-Meta-Name': 'Image1', 'X-Image-Meta-Status': 'active', 'X-Image-Meta-Container-Format': 'ovf', 'X-Image-Meta-Disk-Format': 'vdi', 'X-Image-Meta-Size': '19', 'X-Image-Meta-Is-Public': 'True', 'X-Image-Meta-Property-pants': 'are on'} path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() response, content = http.request(path, 'POST', headers=headers) self.assertEqual(response.status, 201) data = json.loads(content) self.assertEqual(data['image']['properties']['pants'], "are on") self.assertEqual(data['image']['is_public'], True) headers = {'Content-Type': 'application/octet-stream', 'X-Image-Meta-Name': 'My Image!', 'X-Image-Meta-Status': 'active', 'X-Image-Meta-Container-Format': 'ovf', 'X-Image-Meta-Disk-Format': 'vhd', 'X-Image-Meta-Size': '20', 'X-Image-Meta-Is-Public': 'True', 'X-Image-Meta-Property-pants': 'are on'} path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() response, content = http.request(path, 'POST', headers=headers) self.assertEqual(response.status, 201) data = json.loads(content) self.assertEqual(data['image']['properties']['pants'], "are on") self.assertEqual(data['image']['is_public'], True) headers = {'Content-Type': 'application/octet-stream', 'X-Image-Meta-Name': 'My Image!', 'X-Image-Meta-Status': 'saving', 'X-Image-Meta-Container-Format': 'ami', 'X-Image-Meta-Disk-Format': 'ami', 'X-Image-Meta-Size': '21', 'X-Image-Meta-Is-Public': 'True', 'X-Image-Meta-Property-pants': 'are off'} path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() response, content = http.request(path, 'POST', headers=headers) self.assertEqual(response.status, 201) data = json.loads(content) self.assertEqual(data['image']['properties']['pants'], "are off") self.assertEqual(data['image']['is_public'], True) headers = {'Content-Type': 'application/octet-stream', 'X-Image-Meta-Name': 'My Private Image', 'X-Image-Meta-Status': 'active', 'X-Image-Meta-Container-Format': 'ami', 'X-Image-Meta-Disk-Format': 'ami', 'X-Image-Meta-Size': '22', 'X-Image-Meta-Is-Public': 'False'} path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() response, content = http.request(path, 'POST', headers=headers) self.assertEqual(response.status, 201) data = json.loads(content) self.assertEqual(data['image']['is_public'], False) # 2. GET /images # Verify three public images path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 3) # 3. GET /images with name filter # Verify correct images returned with name params = "name=My%20Image!" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 2) for image in data['images']: self.assertEqual(image['name'], "My Image!") # 4. GET /images with status filter # Verify correct images returned with status params = "status=queued" path = "http://%s:%d/v1/images/detail?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 3) for image in data['images']: self.assertEqual(image['status'], "queued") params = "status=active" path = "http://%s:%d/v1/images/detail?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 0) # 5. GET /images with container_format filter # Verify correct images returned with container_format params = "container_format=ovf" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 2) for image in data['images']: self.assertEqual(image['container_format'], "ovf") # 6. GET /images with disk_format filter # Verify correct images returned with disk_format params = "disk_format=vdi" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 1) for image in data['images']: self.assertEqual(image['disk_format'], "vdi") # 7. GET /images with size_max filter # Verify correct images returned with size <= expected params = "size_max=20" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 2) for image in data['images']: self.assertTrue(image['size'] <= 20) # 8. GET /images with size_min filter # Verify correct images returned with size >= expected params = "size_min=20" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 2) for image in data['images']: self.assertTrue(image['size'] >= 20) # 9. Get /images with is_public=None filter # Verify correct images returned with property # Bug lp:803656 Support is_public in filtering params = "is_public=None" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 4) # 10. Get /images with is_public=False filter # Verify correct images returned with property # Bug lp:803656 Support is_public in filtering params = "is_public=False" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 1) for image in data['images']: self.assertEqual(image['name'], "My Private Image") # 11. Get /images with is_public=True filter # Verify correct images returned with property # Bug lp:803656 Support is_public in filtering params = "is_public=True" path = "http://%s:%d/v1/images?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 3) for image in data['images']: self.assertNotEqual(image['name'], "My Private Image") # 12. GET /images with property filter # Verify correct images returned with property params = "property-pants=are%20on" path = "http://%s:%d/v1/images/detail?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 2) for image in data['images']: self.assertEqual(image['properties']['pants'], "are on") # 13. GET /images with property filter and name filter # Verify correct images returned with property and name # Make sure you quote the url when using more than one param! params = "name=My%20Image!&property-pants=are%20on" path = "http://%s:%d/v1/images/detail?%s" % ( "0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 1) for image in data['images']: self.assertEqual(image['properties']['pants'], "are on") self.assertEqual(image['name'], "My Image!") # 14. GET /images with past changes-since filter dt1 = datetime.datetime.utcnow() - datetime.timedelta(1) iso1 = utils.isotime(dt1) params = "changes-since=%s" % iso1 path = "http://%s:%d/v1/images?%s" % ("0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 3) # 15. GET /images with future changes-since filter dt2 = datetime.datetime.utcnow() + datetime.timedelta(1) iso2 = utils.isotime(dt2) params = "changes-since=%s" % iso2 path = "http://%s:%d/v1/images?%s" % ("0.0.0.0", self.api_port, params) response, content = http.request(path, 'GET') self.assertEqual(response.status, 200) data = json.loads(content) self.assertEqual(len(data['images']), 0) self.stop_servers()
def test_west_roundtrip(self): str = '2012-02-14T20:53:07+11:30' west = utils.parse_isotime(str) self.assertEquals(west.tzinfo.tzname(None), '+11:30') self.assertEquals(utils.isotime(west), str)
def test_now_roundtrip(self): str = utils.isotime() now = utils.parse_isotime(str) self.assertEquals(now.tzinfo, iso8601.iso8601.UTC) self.assertEquals(utils.isotime(now), str)
def test_zulu_roundtrip(self): str = '2012-02-14T20:53:07Z' zulu = utils.parse_isotime(str) self.assertEquals(zulu.tzinfo, iso8601.iso8601.UTC) self.assertEquals(utils.isotime(zulu), str)
def test_east_roundtrip(self): str = '2012-02-14T20:53:07-07:00' east = utils.parse_isotime(str) self.assertEquals(east.tzinfo.tzname(None), '-07:00') self.assertEquals(utils.isotime(east), str)