From 7ee677b03aecee31d00e3391a7626a2d2ff1dbbb Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Mon, 17 Jan 2022 12:19:53 -0700 Subject: [PATCH] Remote: Add is_container property Signed-off-by: Zack Cerza --- teuthology/orchestra/remote.py | 9 +++++++++ teuthology/orchestra/test/test_remote.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index 634ce76004..80c8962078 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -679,6 +679,15 @@ class Remote(RemoteShell): self._is_vm = teuthology.lock.query.is_vm(self.name) return self._is_vm + @property + def is_container(self): + if not hasattr(self, '_is_container'): + self._is_container = not bool(self.run( + args="test -f /run/.containerenv -o -f /.dockerenv", + check_status=False, + ).returncode) + return self._is_container + @property def init_system(self): """ diff --git a/teuthology/orchestra/test/test_remote.py b/teuthology/orchestra/test/test_remote.py index 9a94bfc43c..5fa5797eca 100644 --- a/teuthology/orchestra/test/test_remote.py +++ b/teuthology/orchestra/test/test_remote.py @@ -183,3 +183,23 @@ class TestRemote(object): assert remote.Remote._format_size(1024**5).strip() == '1TB' assert remote.Remote._format_size(1021112).strip() == '997KB' assert remote.Remote._format_size(1021112**2).strip() == '971GB' + + def test_is_container(self): + m_transport = MagicMock() + m_transport.getpeername.return_value = ('name', 22) + self.m_ssh.get_transport.return_value = m_transport + m_run = MagicMock() + args = [] + proc = RemoteProcess( + client=self.m_ssh, + args=args, + ) + proc.returncode = 0 + m_run.return_value = proc + rem = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh) + rem._runner = m_run + assert rem.is_container + proc.returncode = 1 + rem2 = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh) + rem2._runner = m_run + assert not rem2.is_container -- 2.39.5