fix(core): Docker detection 67/38767/2
authorpmikus <peter.mikus@protonmail.ch>
Fri, 5 May 2023 09:26:12 +0000 (09:26 +0000)
committerPeter Mikus <peter.mikus@protonmail.ch>
Fri, 5 May 2023 09:32:26 +0000 (09:32 +0000)
Signed-off-by: pmikus <peter.mikus@protonmail.ch>
Change-Id: I976f96410b06b4f4d7500b03b1f5dd54486bbf42

resources/libraries/python/DUTSetup.py
resources/libraries/robot/shared/container.robot

index 300248c..363bb72 100644 (file)
@@ -713,18 +713,15 @@ class DUTSetup:
             to detect.
         :rtype: bool
         """
-        command = u"fgrep docker /proc/1/cgroup"
-        message = u"Failed to get cgroup settings."
+        command = "cat /.dockerenv"
         try:
-            exec_cmd_no_error(
-                node, command, timeout=30, sudo=False, message=message
-            )
+            exec_cmd_no_error(node, command, timeout=30)
         except RuntimeError:
             return False
         return True
 
     @staticmethod
-    def get_docker_mergeddir(node, uuid):
+    def get_docker_mergeddir(node, uuid=None):
         """Get Docker overlay for MergedDir diff.
 
         :param node: DUT node.
@@ -735,8 +732,15 @@ class DUTSetup:
         :rtype: str
         :raises RuntimeError: If getting output failed.
         """
-        command = f"docker inspect " \
+        if not uuid:
+            command = 'fgrep "hostname" /proc/self/mountinfo | cut -f 4 -d" "'
+            message = "Failed to get UUID!"
+            stdout, _ = exec_cmd_no_error(node, command, message=message)
+            uuid = stdout.split(sep="/")[-2]
+        command = (
+            f"docker inspect "
             f"--format='{{{{.GraphDriver.Data.MergedDir}}}}' {uuid}"
+        )
         message = f"Failed to get directory of {uuid} on host {node[u'host']}"
 
         stdout, _ = exec_cmd_no_error(node, command, sudo=True, message=message)
index 931508b..ed695af 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Cisco and/or its affiliates.
+# Copyright (c) 2023 Cisco and/or its affiliates.
 # 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:
@@ -85,8 +85,9 @@
 | | ${nf_id}= | Evaluate | (${nf_chain} - ${1}) * ${nf_nodes} + ${nf_node}
 | | ${env}= | Create List | DEBIAN_FRONTEND=noninteractive
 | | ${dut1_uuid_length} = | Get Length | ${DUT1_UUID}
-| | ${root}= | Run Keyword If | ${dut1_uuid_length}
-| | ... | Get Docker Mergeddir | ${nodes['DUT1']} | ${DUT1_UUID}
+| | ${in_container}= | Running in Container | ${nodes['${dut}']}
+| | ${root}= | Run Keyword If | ${in_container}
+| | ... | Get Docker Mergeddir | ${nodes['DUT1']}
 | | ... | ELSE | Set Variable | ${EMPTY}
 | | ${node_arch}= | Get Node Arch | ${nodes['${dut}']}
 | | ${name}= | Set Variable | ${dut}_${container_group}${nf_id}${DUT1_UUID}