def create(self, **params): """ params - dictionary """ c = CDS(**params) c.save(force_insert=True) return c
def test_info_cds(self): hostname = "cds1.example.com" cluster_id = "unit_test_cluster" cds1 = CDS(hostname=hostname, cluster_id=cluster_id) cds1.save(force_insert=True) resp = self.app.get("/%s/" % (hostname)) self.assertEquals(resp.status_code, 200) cds = json.loads(resp.data) self.assertEquals(cds["hostname"], hostname)
def test_delete(self): hostname = "cds1.example.com" cluster_id = "unit_test_cluster" cds1 = CDS(hostname=hostname, cluster_id=cluster_id) cds1.save(force_insert=True) resp = self.app.delete("/%s/" % (hostname)) self.assertEquals(resp.status_code, 200) resp = self.app.get("/%s/" % (hostname)) self.assertEquals(resp.status_code, 404)
def test_delete(self): hostname = "cds1.example.com" cluster_id = "unit_test_cluster" c = self.cds_manager.create(hostname=hostname, cluster_id=cluster_id) from pulp_cds.api.models.cds import CDS found = CDS.objects(hostname=hostname) self.assertEquals(found[0], c) self.cds_manager.delete(hostname) found = CDS.objects(hostname=hostname) self.assertEquals(len(found), 0)
def test_create_already_exists(self): hostname = "cds1.example.com" cluster_id = "unit_test_cluster" cds1 = CDS(hostname=hostname, cluster_id=cluster_id) cds1.save(force_insert=True) data = {"hostname": hostname, "cluster_id": cluster_id} json_data = json.dumps(data) resp = self.app.post("/", data=json_data, content_type="application/json") self.assertEquals(resp.status_code, 409)
def update(self, hostname, **params): c = self.get(hostname) if not c: raise MissingResource("No CDS with hostname: '%s'" % hostname) for k in params.keys(): c[k] = params[k] c.save() cdses = CDS.objects(hostname=hostname) return cdses[0]
def test_update(self): hostname = "cds1.example.com" cluster_id = "unit_test_cluster" cds1 = CDS(hostname=hostname, cluster_id=cluster_id) cds1.save(force_insert=True) cds1.reload() data = {"hostname": hostname, "cluster_id": cluster_id} json_data = json.dumps(data) resp = self.app.put("/%s/" % (hostname), data=json_data, content_type="application/json") self.assertEquals(resp.status_code, 200) # Using json_util.loads so it can parse the {"$date":value} formats for created_at/updated_at cds_updated = json_util.loads(resp.data) self.assertEquals(cds_updated["hostname"], cds1["hostname"]) orig_created_at = pytz.utc.localize(cds1["created_at"]) modified_created_at = cds_updated["created_at"] self.assertEquals(orig_created_at, modified_created_at)
def test_list_cdses(self): resp = self.app.get("/") items = json.loads(resp.data) self.assertEquals(len(items), 0) hostname1 = "cds1.example.com" hostname2 = "cds2.example.com" cluster_id = "unit_test_cluster" cds1 = CDS(hostname=hostname1, cluster_id=cluster_id) cds2 = CDS(hostname=hostname2, cluster_id=cluster_id) cds1.save(force_insert=True) cds2.save(force_insert=True) resp = self.app.get("/") self.assertEquals(resp.status_code, 200) cdses = json.loads(resp.data) self.assertEquals(len(cdses), 2) for c in cdses: self.assertIn(c["hostname"], [hostname1, hostname2])
def drop_collections(self): # Remember to drop the collections instead of the entire DB # Dropping the entire DB can mess up indexes # Removing the unique index leading to odd behavior in unit tests CDS.drop_collection() Cluster.drop_collection()
def get_all(self): return CDS.objects()
def get(self, hostname): c = CDS.objects(hostname=hostname) if len(c) < 1: return None return c[0]