Merge "Add container-update API"

This commit is contained in:
Jenkins 2017-01-19 06:19:43 +00:00 committed by Gerrit Code Review
commit e1e83d3ba6
4 changed files with 55 additions and 0 deletions

View File

@ -155,6 +155,11 @@ class ZunClient(rest_client.RestClient):
return self.get(
self.container_uri(container_id, action='logs'), None, **kwargs)
def update_container(self, container_id, model, **kwargs):
resp, body = self.patch(
self.container_uri(container_id), body=model.to_json(), **kwargs)
return self.deserialize(resp, body, container_model.ContainerEntity)
def list_services(self, **kwargs):
resp, body = self.get(self.services_uri(), **kwargs)
return self.deserialize(resp, body,

View File

@ -60,3 +60,15 @@ def container_data(**kwargs):
model = container_model.ContainerEntity.from_dict(data)
return model
def container_patch_data(**kwargs):
data = {
'cpu': 0.2,
'memory': '512',
}
data.update(kwargs)
model = container_model.ContainerPatchEntity.from_dict(data)
return model

View File

@ -28,3 +28,14 @@ class ContainerCollection(base_model.CollectionModel):
"""Collection Model that represents a list of ContainerData objects"""
COLLECTION_NAME = 'containerlists'
MODEL_TYPE = ContainerData
class ContainerPatchData(base_model.BaseModel):
"""Data that encapsulates container update attributes"""
pass
class ContainerPatchEntity(base_model.EntityModel):
"""Entity Model that represents a single instance of ContainerPatchData"""
ENTITY_NAME = 'containerpatch'
MODEL_TYPE = ContainerPatchData

View File

@ -149,6 +149,33 @@ class TestContainer(base.BaseZunTest):
self.assertEqual(200, resp.status)
self.assertTrue('hello' in body)
@decorators.idempotent_id('d383f359-3ebd-40ef-9dc5-d36922790230')
def test_update_container(self):
_, model = self._run_container(cpu=0.1, memory=100)
self.assertEqual('100M', model.memory)
self.assertEqual(0.1, model.cpu)
container = self.docker_client.get_container(model.uuid)
self._assert_resource_constraints(container, cpu=0.1, memory=100)
gen_model = datagen.container_patch_data(cpu=0.2, memory=200)
resp, model = self.container_client.update_container(model.uuid,
gen_model)
self.assertEqual(200, resp.status)
self.assertEqual('200M', model.memory)
self.assertEqual(0.2, model.cpu)
container = self.docker_client.get_container(model.uuid)
self._assert_resource_constraints(container, cpu=0.2, memory=200)
def _assert_resource_constraints(self, container, cpu=None, memory=None):
if cpu is not None:
cpu_quota = container.get('HostConfig').get('CpuQuota')
self.assertEqual(int(cpu * 100000), cpu_quota)
cpu_period = container.get('HostConfig').get('CpuPeriod')
self.assertEqual(100000, cpu_period)
if memory is not None:
docker_memory = container.get('HostConfig').get('Memory')
self.assertEqual(memory * 1024 * 1024, docker_memory)
def _create_container(self, **kwargs):
gen_model = datagen.container_data(**kwargs)
resp, model = self.container_client.post_container(gen_model)