From 251f0183275409905e5bf7865914504e300ca79d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fl=C3=A1vio=20Ramalho?= <framalho@suse.com>
Date: Wed, 22 Jul 2020 16:52:36 +0200
Subject: [PATCH] Enable tests to run on a SUSE container

Those changes are required to be able to run the harbor tests on a
container running SLES or openSUSE 15.

Note that the tests assumes that a docker daemon is serving on localhost,
to achieve that on a container another container should be running with
the docker-dind image [1].

1. https://hub.docker.com/_/docker
---
 tests/apitests/python/library/cnab.py       |  2 +-
 tests/apitests/python/library/containerd.py |  4 ++--
 tests/apitests/python/library/docker_api.py | 11 +++++++----
 tests/apitests/python/update_docker_cfg.sh  |  6 +++---
 tests/robot-cases/Group0-BAT/API_DB.robot   |  2 +-
 5 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/tests/apitests/python/library/cnab.py b/tests/apitests/python/library/cnab.py
index 835d002a8..0be5a7c5b 100644
--- a/tests/apitests/python/library/cnab.py
+++ b/tests/apitests/python/library/cnab.py
@@ -22,7 +22,7 @@ def load_bundle(service_image, invocation_image):
 
 def cnab_fixup_bundle(bundle_file, target, auto_update_bundle = True):
     fixed_bundle_file = "./tests/apitests/python/bundle_data/fixed-bundle.json"
-    command = ["sudo", "cnab-to-oci", "--log-level", "debug", "fixup", bundle_file, "--target", target, "--bundle", fixed_bundle_file]
+    command = ["cnab-to-oci", "--log-level", "debug", "fixup", bundle_file, "--target", target, "--bundle", fixed_bundle_file]
     if auto_update_bundle == True:
          command.append("--auto-update-bundle")
          #fixed_bundle_file = bundle_file
diff --git a/tests/apitests/python/library/containerd.py b/tests/apitests/python/library/containerd.py
index 1517f169f..5833bbaa8 100644
--- a/tests/apitests/python/library/containerd.py
+++ b/tests/apitests/python/library/containerd.py
@@ -5,13 +5,13 @@ import json
 import docker_api
 
 def ctr_images_pull(username, password, oci):
-    command = ["sudo", "ctr", "images", "pull", "-u", username+":"+password, oci]
+    command = ["containerd-ctr", "-a", "/run/containerd/docker-containerd.sock", "images", "pull", "-u", username+":"+password, oci]
     print "Command: ", command
     ret = base.run_command(command)
     print "Command return: ", ret
 
 def ctr_images_list(oci_ref = None):
-    command = ["sudo", "ctr", "images", "list", "--q"]
+    command = ["containerd-ctr", "-a", "/run/containerd/docker-containerd.sock", "images", "list", "--q"]
     print "Command: ", command
     ret = base.run_command(command)
     print "Command return: ", ret
diff --git a/tests/apitests/python/library/docker_api.py b/tests/apitests/python/library/docker_api.py
index 252791375..68094db37 100644
--- a/tests/apitests/python/library/docker_api.py
+++ b/tests/apitests/python/library/docker_api.py
@@ -18,7 +18,7 @@ def docker_info_display():
     print "Command return: ", ret
 
 def docker_login_cmd(harbor_host, user, password, enable_manifest = True):
-    command = ["sudo", "docker", "login", harbor_host, "-u", user, "-p", password]
+    command = ["docker", "login", harbor_host, "-u", user, "-p", password]
     print "Docker Login Command: ", command
     base.run_command(command)
     if enable_manifest == True:
@@ -28,13 +28,13 @@ def docker_login_cmd(harbor_host, user, password, enable_manifest = True):
             raise Exception("Failed to update docker config, error is {} {}.".format(exc.returncode, exc.output))
 
 def docker_manifest_create(index, manifests):
-    command = ["sudo", "docker","manifest","create",index]
+    command = ["docker","manifest","create",index]
     command.extend(manifests)
     print "Docker Manifest Command: ", command
     base.run_command(command)
 
 def docker_manifest_push(index):
-    command = ["sudo", "docker","manifest","push",index]
+    command = ["docker","manifest","push",index]
     print "Docker Manifest Command: ", command
     ret = base.run_command(command)
     index_sha256=""
@@ -77,7 +77,10 @@ def list_image_tags(harbor_host, repository, user, password, n = None, last = No
 
 class DockerAPI(object):
     def __init__(self):
-        self.DCLIENT = docker.APIClient(base_url='unix://var/run/docker.sock',version='auto',timeout=30)
+        tls_config = docker.tls.TLSConfig(
+            client_cert=('/certs/client/cert.pem', '/certs/client/key.pem')
+        )
+        self.DCLIENT = docker.APIClient(base_url='tcp://localhost:2376',version='auto',timeout=30,tls=tls_config)
         self.DCLIENT2 = docker.from_env()
 
     def docker_login(self, registry, username, password, expected_error_message = None):
diff --git a/tests/apitests/python/update_docker_cfg.sh b/tests/apitests/python/update_docker_cfg.sh
index 0ab4968ed..efd138bcf 100755
--- a/tests/apitests/python/update_docker_cfg.sh
+++ b/tests/apitests/python/update_docker_cfg.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
 
-sudo sed -i '$d' /$HOME/.docker/config.json
-sudo sed -i '$d' /$HOME/.docker/config.json
-sudo echo -e "\n        },\n        \"experimental\": \"enabled\"\n}" >> /$HOME/.docker/config.json
+sed -i '$d' /$HOME/.docker/config.json
+sed -i '$d' /$HOME/.docker/config.json
+echo -e "\n        },\n        \"experimental\": \"enabled\"\n}" >> /$HOME/.docker/config.json
diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot
index e0f2548e7..5880084a2 100644
--- a/tests/robot-cases/Group0-BAT/API_DB.robot
+++ b/tests/robot-cases/Group0-BAT/API_DB.robot
@@ -2,7 +2,7 @@
 Documentation  Harbor BATs
 Resource  ../../resources/APITest-Util.robot
 Resource  ../../resources/Docker-Util.robot
-Library  ../../apitests/python/library/Harbor.py  ${SERVER_CONFIG}
+Library  ../../apitests/python/library/Harbor.py
 Library  OperatingSystem
 Library  String
 Library  Collections
-- 
2.16.4

