From 6f6ce01973b949197daa2ca32c930d629b351163 Mon Sep 17 00:00:00 2001
From: asmorodskyi <asmorodskyi@suse.com>
Date: Fri, 29 Apr 2022 16:20:07 +0200
Subject: [PATCH] containers: remove double init of clients

---
 lib/publiccloud/ecr.pm                        |  1 +
 lib/publiccloud/k8sbasetest.pm                | 33 ++++---------------
 .../containers/push_container_image_to_acr.pm |  7 ++--
 .../containers/push_container_image_to_ecr.pm | 11 +++----
 .../containers/push_container_image_to_gcr.pm |  6 ++--
 tests/containers/run_container_in_k8s.pm      |  9 ++---
 6 files changed, 20 insertions(+), 47 deletions(-)

diff --git a/lib/publiccloud/ecr.pm b/lib/publiccloud/ecr.pm
index ee450a3df..c2c118d18 100644
--- a/lib/publiccloud/ecr.pm
+++ b/lib/publiccloud/ecr.pm
@@ -31,6 +31,7 @@ Delete a ECR image
 =cut
 sub delete_image {
     my ($self, $tag) = @_;
+    record_info('INFO', "Deleting image $tag");
     assert_script_run(
         "aws ecr batch-delete-image --repository-name " . $self->container_registry . " --image-ids imageTag=" . $tag);
     return;
diff --git a/lib/publiccloud/k8sbasetest.pm b/lib/publiccloud/k8sbasetest.pm
index 29f0bcd8b..0d708f6a9 100644
--- a/lib/publiccloud/k8sbasetest.pm
+++ b/lib/publiccloud/k8sbasetest.pm
@@ -20,12 +20,12 @@ use utils qw(random_string);
 Prepare the provider and install kubectl
 =cut
 sub init {
-    my ($self) = @_;
+    my ($self, $args) = @_;
 
-    my $service = $self->get_k8s_service_name();
-    $self->provider_factory(service => $service);
+    # we need to copy this to $self so it will be available for post_fail_hook / cleanup calls
+    $self->{provider} = $args->{provider};
+    $self->{image_tag} = $args->{image_tag};
 
-    $self->select_serial_terminal;
     $self->install_kubectl();
 }
 
@@ -34,7 +34,9 @@ sub init {
 Install kubectl from the k8s page
 =cut
 sub install_kubectl {
+    my ($self) = @_;
     my $version = get_var('PUBLICCLOUD_KUBECTL_VERSION', 'v1.22.2');
+    $self->select_serial_terminal();
     assert_script_run("curl -LO https://dl.k8s.io/release/$version/bin/linux/amd64/kubectl");
     assert_script_run("chmod a+x kubectl");
     assert_script_run("rm /usr/bin/kubectl");
@@ -84,27 +86,4 @@ sub validate_log {
 }
 
 
-=head2 get_k8s_service_name
-
-Returns the name for the kubernetes service
-=cut
-sub get_k8s_service_name {
-    my ($self) = @_;
-
-    my $provider = get_required_var('PUBLIC_CLOUD_PROVIDER');
-
-    if ($provider eq 'EC2') {
-        return "EKS";
-    }
-    elsif ($provider eq 'GCE') {
-        return "GKE";
-    }
-    elsif ($provider eq 'AZURE') {
-        return "AKS";
-    }
-    else {
-        die('Unknown PUBLIC_CLOUD_PROVIDER given');
-    }
-}
-
 1;
diff --git a/tests/containers/push_container_image_to_acr.pm b/tests/containers/push_container_image_to_acr.pm
index 9c93c9004..fff76fc0b 100644
--- a/tests/containers/push_container_image_to_acr.pm
+++ b/tests/containers/push_container_image_to_acr.pm
@@ -16,10 +16,10 @@ sub run {
 
     $self->select_serial_terminal;
 
-    my $provider = $self->provider_factory(service => 'ACR');
+    my $provider = $self->{provider} = $args->{provider} = $self->provider_factory(service => 'ACR');
 
     my $image = get_image_uri();
-    my $tag = $provider->get_default_tag();
+    my $tag = $args->{image_tag} = $provider->get_default_tag();
 
     record_info('Pull', "Pulling $image");
     assert_script_run("podman pull $image", 360);
@@ -28,14 +28,13 @@ sub run {
     my $image_build = script_output("podman image inspect $image --format='$image_build_format'");
     record_info('Img version', $image_build);
 
-    my $image_name = $provider->push_container_image($image, $tag);
+    my $image_name = $args->{image_name} = $provider->push_container_image($image, $tag);
     record_info('Registry',
         "Image successfully uploaded to ACR:\n$image_name\n" . script_output("podman inspect $image_name"));
 }
 
 sub post_fail_hook {
     my ($self) = @_;
-    record_info('INFO', "Deleting image $self->tag");
     $self->{provider}->delete_image();
 }
 
diff --git a/tests/containers/push_container_image_to_ecr.pm b/tests/containers/push_container_image_to_ecr.pm
index ef9e16b7b..d593eaa53 100644
--- a/tests/containers/push_container_image_to_ecr.pm
+++ b/tests/containers/push_container_image_to_ecr.pm
@@ -16,12 +16,10 @@ sub run {
 
     $self->select_serial_terminal;
 
-    my $provider = $self->provider_factory(service => 'ECR');
-    $self->{provider} = $provider;
+    my $provider = $self->{provider} = $args->{provider} = $self->provider_factory(service => 'ECR');
 
     my $image = get_image_uri();
-    my $tag = $provider->get_default_tag();
-    $self->{tag} = $tag;
+    my $tag = $self->{image_tag} = $args->{image_tag} = $provider->get_default_tag();
 
     record_info('Pull', "Pulling $image");
     assert_script_run("podman pull $image", 360);
@@ -29,14 +27,13 @@ sub run {
     my $image_build = script_output("podman image inspect $image --format='{{ index .Config.Labels \"org.opencontainers.image.version\"}}'");
     record_info('Img version', $image_build);
 
-    my $image_name = $provider->push_container_image($image, $tag);
+    my $image_name = $args->{image_name} = $provider->push_container_image($image, $tag);
     record_info('Registry', "Image successfully uploaded to ECR:\n$image_name\n" . script_output("podman inspect $image_name"));
 }
 
 sub post_fail_hook {
     my ($self) = @_;
-    record_info('INFO', "Deleting image $self->tag");
-    $self->{provider}->delete_image($self->tag);
+    $self->{provider}->delete_image($self->{image_tag});
 }
 
 sub test_flags {
diff --git a/tests/containers/push_container_image_to_gcr.pm b/tests/containers/push_container_image_to_gcr.pm
index 2d205f910..b7a207c7b 100644
--- a/tests/containers/push_container_image_to_gcr.pm
+++ b/tests/containers/push_container_image_to_gcr.pm
@@ -16,10 +16,10 @@ sub run {
 
     $self->select_serial_terminal;
 
-    my $provider = $self->provider_factory(service => 'GCR');
+    my $provider = $self->{provider} = $args->{provider} = $self->provider_factory(service => 'GCR');
 
     my $image = get_image_uri();
-    my $tag = $provider->get_default_tag();
+    my $tag = $args->{image_tag} =  $provider->get_default_tag();
 
     record_info('Pull', "Pulling $image");
     assert_script_run("podman pull $image", 360);
@@ -28,7 +28,7 @@ sub run {
     my $image_build = script_output("podman image inspect $image --format='$image_build_format'");
     record_info('Img version', $image_build);
 
-    my $image_name = $provider->push_container_image($image, $tag);
+    my $image_name = $args->{image_name} = $provider->push_container_image($image, $tag);
     record_info('Registry',
         "Image successfully uploaded to GCR:\n$image_name\n" . script_output("podman inspect $image_name"));
 }
diff --git a/tests/containers/run_container_in_k8s.pm b/tests/containers/run_container_in_k8s.pm
index be41a82cf..2babe3451 100644
--- a/tests/containers/run_container_in_k8s.pm
+++ b/tests/containers/run_container_in_k8s.pm
@@ -15,16 +15,13 @@ use Mojo::Base 'publiccloud::k8sbasetest';
 use testapi;
 
 sub run {
-    my ($self) = @_;
+    my ($self, $args) = @_;
     $self->SUPER::init();
 
     my $cmd = '"cat", "/etc/os-release"';
 
-    my $image_tag = $self->{provider}->get_default_tag();
-    $self->{image_tag} = $image_tag;
-
-    my $image = $self->{provider}->get_container_image_full_name($image_tag);
-    my $job_name = $image_tag =~ s/_/-/gr;
+    my $image = $args->{image_name};
+    my $job_name = $args->{image_tag} =~ s/_/-/gr;
     $self->{job_name} = $job_name;
 
     my $manifest = <<EOT;
-- 
2.26.2

