Add get_sandbox_key to utils.py

This patch adds get_sandbox_key, which takes the container ID as string
and returns the path to the netns file as string, to utils.py. This
patch also includes the unit test for it.

Change-Id: I931036337d16074b3aa4d5555ba2bf3d178c714f
Signed-off-by: Taku Fukushima <f.tac.mac@gmail.com>
This commit is contained in:
Taku Fukushima 2015-09-15 19:18:05 +09:00
parent 2eaf80259d
commit cbe0734875
2 changed files with 45 additions and 0 deletions

31
kuryr/tests/test_utils.py Normal file
View File

@ -0,0 +1,31 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
import random
import ddt
from kuryr.tests import base
from kuryr import utils
@ddt.ddt
class TestKuryrUtils(base.TestKuryrBase):
"""Unit tests for utilities."""
@ddt.data(hashlib.sha256(str(random.getrandbits(256))).hexdigest(),
'51c75a2515d4' '51c75a')
def test_get_sandbox_key(self, fake_container_id):
sandbox_key = utils.get_sandbox_key(fake_container_id)
expected = '/'.join([utils.DOCKER_NETNS_BASE, fake_container_id[:12]])
self.assertEqual(expected, sandbox_key)

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import flask
import jsonschema
from neutronclient.common import exceptions as n_exceptions
@ -18,6 +20,9 @@ from neutronclient.v2_0 import client as client_v2
from werkzeug import exceptions as w_exceptions
DOCKER_NETNS_BASE = '/var/run/docker/netns'
def get_neutron_client_simple(url, token):
return client.Client('2.0', endpoint_url=url, token=token)
@ -72,3 +77,12 @@ def make_json_app(import_name, **kwargs):
app.error_handler_spec[None][code] = make_json_error
return app
def get_sandbox_key(container_id):
"""Returns a sandbox key constructed with the given container ID.
:param container_id: the ID of the Docker container as string
:returns: the constructed sandbox key as string
"""
return os.path.join(DOCKER_NETNS_BASE, container_id[:12])