From 3e5fcdd954a17c6e173d0d18c462f3cc2510618c Mon Sep 17 00:00:00 2001 From: lpalovsky Date: Wed, 13 Nov 2024 10:16:36 +0100 Subject: [PATCH] Create PC lib classes --- .../inventory_tools.pm | 41 +++++++++++ .../deploy_hana_sr.yml | 2 +- .../test_hana_sr.yml | 3 +- t/29_sdaf_inventory_tools.t | 72 ++++++++++--------- .../prepare_ssh_config.pm | 15 ++-- .../publiccloud_lib_compatibility_layer.pm | 41 +++++++++++ 6 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 tests/sles4sap/sap_deployment_automation_framework/publiccloud_lib_compatibility_layer.pm diff --git a/lib/sles4sap/sap_deployment_automation_framework/inventory_tools.pm b/lib/sles4sap/sap_deployment_automation_framework/inventory_tools.pm index 24f2dc3da002..b511da856053 100644 --- a/lib/sles4sap/sap_deployment_automation_framework/inventory_tools.pm +++ b/lib/sles4sap/sap_deployment_automation_framework/inventory_tools.pm @@ -14,6 +14,7 @@ use Exporter qw(import); use Carp qw(croak); use sles4sap::sap_deployment_automation_framework::naming_conventions qw($deployer_private_key_path $sut_private_key_path); +use publiccloud::azure; use sles4sap::console_redirection; =head1 SYNOPSIS @@ -61,6 +62,7 @@ our @EXPORT = qw( read_inventory_file prepare_ssh_config verify_ssh_proxy_connection + sdaf_create_instances ); =head2 read_inventory_file @@ -231,3 +233,42 @@ sub verify_ssh_proxy_connection { } } } + +=head2 sdaf_create_instances + + sdaf_create_instances( inventory_content=>HASHREF); + +Creates and returns B<$instances> class which is a main component of F and +general public cloud libraries F. + +=over + +=item * B Referenced content of the inventory yaml file + +=back +=cut + +sub sdaf_create_instances { + my (%args) = @_; + my $provider = publiccloud::azure->new(); + my @instances = (); + + for my $instance_type (keys(%{$args{inventory_content}})) { + my $hosts = $args{inventory_content}->{$instance_type}{hosts}; + for my $physical_host (keys %$hosts) { + my $instance = publiccloud::instance->new( + public_ip => $hosts->{$physical_host}->{ansible_host}, + instance_id => $physical_host, + username => $hosts->{$physical_host}->{ansible_user}, + ssh_key => $args{sut_ssh_key_path}, + provider => $provider, + region => get_required_var('PUBLIC_CLOUD_REGION') + ); + push(@instances, $instance); + } + } + use Data::Dumper; + record_info('Instance', Dumper(\@instances)); + publiccloud::instances::set_instances(@instances); + return \@instances; +} diff --git a/schedule/sles4sap/sap_deployment_automation_framework/deploy_hana_sr.yml b/schedule/sles4sap/sap_deployment_automation_framework/deploy_hana_sr.yml index 343e8143c424..1d593b23b279 100644 --- a/schedule/sles4sap/sap_deployment_automation_framework/deploy_hana_sr.yml +++ b/schedule/sles4sap/sap_deployment_automation_framework/deploy_hana_sr.yml @@ -9,4 +9,4 @@ schedule: - sles4sap/sap_deployment_automation_framework/configure_deployer - sles4sap/sap_deployment_automation_framework/deploy_workload_zone - sles4sap/sap_deployment_automation_framework/deploy_sap_systems - - sles4sap/sap_deployment_automation_framework/deploy_hanasr + #- sles4sap/sap_deployment_automation_framework/deploy_hanasr diff --git a/schedule/sles4sap/sap_deployment_automation_framework/test_hana_sr.yml b/schedule/sles4sap/sap_deployment_automation_framework/test_hana_sr.yml index c2e09bbafb9f..f00f87436aa6 100644 --- a/schedule/sles4sap/sap_deployment_automation_framework/test_hana_sr.yml +++ b/schedule/sles4sap/sap_deployment_automation_framework/test_hana_sr.yml @@ -6,4 +6,5 @@ schedule: - boot/boot_to_desktop - sles4sap/sap_deployment_automation_framework/connect_to_deployer - sles4sap/sap_deployment_automation_framework/prepare_ssh_config - - sles4sap/sap_deployment_automation_framework/cleanup + #- sles4sap/sap_deployment_automation_framework/publiccloud_lib_compatibility_layer + #- sles4sap/sap_deployment_automation_framework/cleanup diff --git a/t/29_sdaf_inventory_tools.t b/t/29_sdaf_inventory_tools.t index 83f5d4543b3a..cbac629def7b 100644 --- a/t/29_sdaf_inventory_tools.t +++ b/t/29_sdaf_inventory_tools.t @@ -9,6 +9,38 @@ use testapi; use sles4sap::sap_deployment_automation_framework::inventory_tools; use Data::Dumper; +my $mock_inventory_data = { + 'QES_PAS' => { + 'hosts' => { + 'Freddie' => { + 'ansible_connection' => 'ssh', + 'connection_type' => 'key', + 'virtual_host' => 'Mercury', + 'ansible_user' => 'freddie', + 'vm_name' => 'FreddieMercury', + 'become_user' => 'root', + 'ansible_host' => '10.10.10.2', + 'os_type' => 'linux' + } + }, + 'vars' => undef + }, + 'QES_DB' => { + 'vars' => undef, + 'hosts' => { + 'John' => { + 'ansible_connection' => 'ssh', + 'connection_type' => 'key', + 'virtual_host' => 'Deacon', + 'ansible_user' => 'john', + 'vm_name' => 'JohnDeacon', + 'become_user' => 'john', + 'ansible_host' => '10.10.10.3', + 'os_type' => 'linux' + } + } + } +}; subtest '[prepare_ssh_config] ' => sub { my $mock = Test::MockModule->new('sles4sap::sap_deployment_automation_framework::inventory_tools', no_auto => 1); @@ -43,39 +75,6 @@ subtest '[prepare_ssh_config] ' => sub { $db_host_B_content = join(' ', @_) if join(' ', @_) =~ /entry_name Freddie/; return; }); - my $mock_inventory_data = { - 'QES_PAS' => { - 'hosts' => { - 'Freddie' => { - 'ansible_connection' => 'ssh', - 'connection_type' => 'key', - 'virtual_host' => 'Mercury', - 'ansible_user' => 'freddie', - 'vm_name' => 'FreddieMercury', - 'become_user' => 'root', - 'ansible_host' => '10.10.10.2', - 'os_type' => 'linux' - } - }, - 'vars' => undef - }, - 'QES_DB' => { - 'vars' => undef, - 'hosts' => { - 'John' => { - 'ansible_connection' => 'ssh', - 'connection_type' => 'key', - 'virtual_host' => 'Deacon', - 'ansible_user' => 'john', - 'vm_name' => 'JohnDeacon', - 'become_user' => 'john', - 'ansible_host' => '10.10.10.3', - 'os_type' => 'linux' - } - } - } - }; - prepare_ssh_config(inventory_data => $mock_inventory_data, jump_host_ip => '127.0.0.1', jump_host_user => 'Freddie'); note("\n --> $jump_config_content"); ok($jump_config_content =~ /entry_name deployer_jump/, 'Jump host: entry_name'); @@ -101,9 +100,14 @@ subtest '[prepare_ssh_config] ' => sub { ok($db_host_B_content =~ /identity_file/, 'DB host A: identity_file'); ok($db_host_B_content =~ /proxy_jump deployer_jump/, 'DB host A: proxy_jump'); ok($db_host_B_content =~ /strict_host_key_checking no/, 'DB host A: strict_host_key_checking'); +}; +subtest '[sdaf_create_instances] ' => sub { + my $mock = Test::MockModule->new('sles4sap::sap_deployment_automation_framework::inventory_tools', no_auto => 1); + $mock->redefine(record_info => sub { note("\n --> " . join("\n --> ", @_)); return; }); + set_var('PUBLIC_CLOUD_REGION', 'swedencentral'); + my $instances = sdaf_create_instances(inventory_content=>$mock_inventory_data); }; - done_testing; diff --git a/tests/sles4sap/sap_deployment_automation_framework/prepare_ssh_config.pm b/tests/sles4sap/sap_deployment_automation_framework/prepare_ssh_config.pm index a12702fb073c..b5b0ff38f97f 100644 --- a/tests/sles4sap/sap_deployment_automation_framework/prepare_ssh_config.pm +++ b/tests/sles4sap/sap_deployment_automation_framework/prepare_ssh_config.pm @@ -23,9 +23,6 @@ use sles4sap::sap_deployment_automation_framework::naming_conventions $sut_private_key_path generate_resource_group_name); -sub test_flags { - return {fatal => 1, publiccloud_multi_module => 1}; -} sub run { my ($self, $run_args) = @_; @@ -64,6 +61,16 @@ sub run { ); # checks SSH connection to each host executing simple command verify_ssh_proxy_connection(inventory_data => $inventory_data); -} + + for my $instance_type (keys(%{$inventory_data})) { + my $hosts = $inventory_data->{$instance_type}{hosts}; + for my $hostname (keys %$hosts) { + my $ip_addr = $hosts->{$hostname}{ansible_host}; + my $user = $hosts->{$hostname}{ansible_user}; + connect_target_to_serial(destination_ip=>$ip_addr, ssh_user=>$user); + record_info(script_output('hostname')); + disconnect_target_from_serial(); + } + }} 1; diff --git a/tests/sles4sap/sap_deployment_automation_framework/publiccloud_lib_compatibility_layer.pm b/tests/sles4sap/sap_deployment_automation_framework/publiccloud_lib_compatibility_layer.pm new file mode 100644 index 000000000000..71b26730e339 --- /dev/null +++ b/tests/sles4sap/sap_deployment_automation_framework/publiccloud_lib_compatibility_layer.pm @@ -0,0 +1,41 @@ +# SUSE's openQA tests +# +# Copyright SUSE LLC +# SPDX-License-Identifier: FSFAP +# Maintainer: QE-SAP +# Summary: Prepares compatibility layer for using `lib/publiccloud/` library with SDAF deployment + +use parent 'sles4sap::sap_deployment_automation_framework::basetest'; + +use warnings; +use strict; +use testapi; +use serial_terminal qw(select_serial_terminal); +use sles4sap::console_redirection qw(connect_target_to_serial disconnect_target_from_serial); +use sles4sap::sap_deployment_automation_framework::inventory_tools qw(read_inventory_file sdaf_create_instances); +use sles4sap::sap_deployment_automation_framework::naming_conventions + qw(get_sdaf_inventory_path convert_region_to_short get_workload_vnet_code); +sub run { + my ($self, $run_args) = @_; + select_serial_terminal; + + my $inventory_path = get_sdaf_inventory_path( + env_code => get_required_var('SDAF_ENV_CODE'), + sdaf_region_code => convert_region_to_short(get_required_var('PUBLIC_CLOUD_REGION')), + vnet_code => get_workload_vnet_code(), + sap_sid => get_required_var('SAP_SID') + ); + + # Connect serial to Deployer VM to get inventory file + connect_target_to_serial(); + my $inventory_data = read_inventory_file($inventory_path); + # From now on all commands will be executed on worker VM + disconnect_target_from_serial(); + + my $instances = $self->{instances} = sdaf_create_instances(inventory_content=>$inventory_data); + #for my $instance (@$instances) { + # $self->{my_instance} = $instance; + #} +} + +1; \ No newline at end of file