def test_create_capsule_wrong_kind_set(self, mock_check_template, mock_capsule_create): params = ('{"template": {"kind": "test",' '"spec": {"containers":' '[{"environment": {"ROOT_PASSWORD": "******"}, ' '"image": "test1", "resources": ' '{"requests": {"cpu": 1, "memory": 1024}}}]}, ' '"metadata": {"labels": {"foo0": "bar0"}, ' '"name": "capsule-example"}}}') mock_check_template.side_effect = exception.InvalidCapsuleTemplate( "kind fields need to be set as capsule or Capsule") response = self.post_json('/capsules/', params, expect_errors=True) self.assertEqual(400, response.status_int) self.assertFalse(mock_capsule_create.called)
def test_create_capsule_no_container_field(self, mock_check_template, mock_capsule_create): params = ('{"template": {"kind": "capsule",' '"spec": {}, ' '"metadata": {"labels": {"foo0": "bar0"}, ' '"name": "capsule-example"}}}') mock_check_template.side_effect = exception.InvalidCapsuleTemplate( "Capsule need to have one container at least") self.assertRaises(AppError, self.post, '/capsules/', params=params, content_type='application/json') self.assertFalse(mock_capsule_create.called)
def check_capsule_template(tpl): # TODO(kevinz): add volume spec check kind_field = tpl.get('kind', None) if kind_field not in ['capsule', 'Capsule']: raise exception.InvalidCapsuleTemplate("kind fields need to be " "set as capsule or Capsule") spec_field = tpl.get('spec', None) if spec_field is None: raise exception.InvalidCapsuleTemplate("No Spec found") if spec_field.get('containers', None) is None: raise exception.InvalidCapsuleTemplate("No valid containers field") containers_spec = spec_field.get('containers', None) containers_num = len(containers_spec) if containers_num == 0: raise exception.InvalidCapsuleTemplate("Capsule need to have one " "container at least") for i in range(0, containers_num): container_image = containers_spec[i].get('image', None) if container_image is None: raise exception.InvalidCapsuleTemplate("Container " "image is needed") return containers_spec
def test_create_capsule_no_container_image(self, mock_check_template, mock_container_create): params = ('{"template": {"kind": "capsule",' '"spec": {container:[{"env": ' '{"ROOT_PASSWORD": "******"}]}, ' '"metadata": {"labels": {"foo0": "bar0"}, ' '"name": "capsule-example"}}}') mock_check_template.side_effect = exception.InvalidCapsuleTemplate( "Container image is needed") self.assertRaises(AppError, self.post, '/v1/capsules/', params=params, content_type='application/json') self.assertFalse(mock_container_create.called)