diff --git a/.github/workflows/basic-ci.yaml b/.github/workflows/basic-ci.yaml index efacbf1f..49db856f 100644 --- a/.github/workflows/basic-ci.yaml +++ b/.github/workflows/basic-ci.yaml @@ -9,20 +9,18 @@ jobs: matrix: python-version: [3.8, '3.10'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install Dependencies And Self run: | python -m pip install --upgrade pip setuptools wheel - # Workaround for https://github.com/docker/docker-py/issues/2807 - python -m pip install six python -m pip install -e .[test] codecov pytest-cov - name: Run headless tests - uses: GabrielBB/xvfb-action@v1 + uses: coactions/setup-xvfb@v1 with: run: python -m pytest -s -v --cov=rocker -m "not nvidia" - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 diff --git a/setup.py b/setup.py index 3ba780ef..d10b0d09 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ 'empy', 'pexpect', 'packaging', + 'urllib3<2', # Workaround for https://github.com/docker/docker-py/issues/3113 ] # docker API used to be in a package called `docker-py` before the 2.0 release @@ -49,6 +50,7 @@ 'git = rocker.git_extension:Git', 'group_add = rocker.extensions:GroupAdd', 'home = rocker.extensions:HomeDir', + 'hostname = rocker.extensions:Hostname', 'name = rocker.extensions:Name', 'network = rocker.extensions:Network', 'nvidia = rocker.nvidia_extension:Nvidia', diff --git a/src/rocker/extensions.py b/src/rocker/extensions.py index 75030dbf..43bc3420 100644 --- a/src/rocker/extensions.py +++ b/src/rocker/extensions.py @@ -91,6 +91,29 @@ def register_arguments(parser, defaults={}): help="add development tools emacs and byobu to your environment") +class Hostname(RockerExtension): + @staticmethod + def get_name(): + return 'hostname' + + def __init__(self): + self.name = Hostname.get_name() + + def get_preamble(self, cliargs): + return '' + + def get_docker_args(self, cliargs): + args = '' + hostname = cliargs.get('hostname', None) + if hostname: + args += ' --hostname %s ' % hostname + return args + + @staticmethod + def register_arguments(parser, defaults={}): + parser.add_argument('--hostname', default=defaults.get('hostname', ''), + help='Hostname of the container.') + class Name(RockerExtension): @staticmethod def get_name(): diff --git a/test/test_extension.py b/test/test_extension.py index 6e748bc3..a2c41a46 100644 --- a/test/test_extension.py +++ b/test/test_extension.py @@ -243,6 +243,37 @@ def test_name_extension(self): args = p.get_docker_args(mock_cliargs) self.assertTrue('--name docker_name' in args) +class HostnameExtensionTest(unittest.TestCase): + + def setUp(self): + # Work around interference between empy Interpreter + # stdout proxy and test runner. empy installs a proxy on stdout + # to be able to capture the information. + # And the test runner creates a new stdout object for each test. + # This breaks empy as it assumes that the proxy has persistent + # between instances of the Interpreter class + # empy will error with the exception + # "em.Error: interpreter stdout proxy lost" + em.Interpreter._wasProxyInstalled = False + + def test_name_extension(self): + plugins = list_plugins() + name_plugin = plugins['hostname'] + self.assertEqual(name_plugin.get_name(), 'hostname') + + p = name_plugin() + self.assertTrue(plugin_load_parser_correctly(name_plugin)) + + mock_cliargs = {'hostname': 'none'} + self.assertEqual(p.get_snippet(mock_cliargs), '') + self.assertEqual(p.get_preamble(mock_cliargs), '') + args = p.get_docker_args(mock_cliargs) + self.assertTrue('--hostname none' in args) + + mock_cliargs = {'hostname': 'docker-hostname'} + args = p.get_docker_args(mock_cliargs) + self.assertTrue('--hostname docker-hostname' in args) + class PrivilegedExtensionTest(unittest.TestCase):