From e82af602ea8f70054b37274c10559e25910448ac Mon Sep 17 00:00:00 2001
From: Takashi Kajinami <kajinamit@oss.nttdata.com>
Date: Fri, 10 Nov 2023 23:15:37 +0900
Subject: [PATCH] Use package to install pip

The easy_install command is no longer available in CentOS 9 (and even
CentOS 8), and we should use package installation. Because RHEL has no
pip package available now, the package installation is skipped with
a warning message.

Closes-Bug: #2043206
Change-Id: Id70b4610f5e016ae69fc8ef2b81e2176f199fe87
---
 manifests/init.pp                             | 24 +++++++------------
 manifests/params.pp                           |  6 ++++-
 .../use-pip-package-15400e8fe8cbb275.yaml     |  8 +++++++
 spec/classes/tempest_init_spec.rb             | 18 +++++++-------
 4 files changed, 29 insertions(+), 27 deletions(-)
 create mode 100644 releasenotes/notes/use-pip-package-15400e8fe8cbb275.yaml

diff --git a/manifests/init.pp b/manifests/init.pp
index 209351eb..fde20633 100644
--- a/manifests/init.pp
+++ b/manifests/init.pp
@@ -500,30 +500,22 @@ class tempest(
   include openstacklib::openstackclient
 
   if $install_from_source {
-    $setuptools_pkg = 'python3-setuptools'
-    ensure_packages([
-      'git',
-      $setuptools_pkg,
-    ])
-
+    ensure_packages(['git'])
     ensure_packages($tempest::params::dev_packages)
 
-    # NOTE(aschultz): Ubuntu setup tools has dropped easy_install since 18.04
-    # so we install via package now. Though if we hit this, we can only
-    # install "pip". This likely should just be removed though I'm not sure
-    # about pip availability for RHEL systems.
-    exec { 'install-pip':
-      command => 'easy_install pip',
-      unless  => "which ${tempest::params::pip_command}",
-      path    => ['/bin','/usr/bin','/usr/local/bin'],
-      require => Package[$setuptools_pkg],
+    if $::tempest::params::pip_package_name {
+      ensure_packages('pip', {
+        name => $::tempest::params::pip_package_name,
+      })
+      Package['pip'] -> Exec['install-tox']
+    } else {
+      warning('pip package is not available in this distribution.')
     }
 
     exec { 'install-tox':
       command => "${tempest::params::pip_command} install -U tox",
       unless  => 'which tox',
       path    => ['/bin','/usr/bin','/usr/local/bin'],
-      require => Exec['install-pip'],
     }
 
     if $git_clone {
diff --git a/manifests/params.pp b/manifests/params.pp
index 7426bb26..7d12beec 100644
--- a/manifests/params.pp
+++ b/manifests/params.pp
@@ -36,6 +36,10 @@ class tempest::params {
       $python_barbican_tests   = 'python3-barbican-tests-tempest'
       $python_manila_tests     = 'python3-manila-tests-tempest'
       $package_name            = 'openstack-tempest'
+      $pip_package_name        = $facts['os']['name'] ? {
+        'RedHat' => undef,
+        default  => 'python3-pip'
+      }
     }
     'Debian': {
       $dev_packages = [
@@ -47,7 +51,6 @@ class tempest::params {
         'patch',
         'gcc',
         'python3-virtualenv',
-        'python3-pip',
       ]
       if $facts['os']['name'] == 'Debian' {
         $python_telemetry_tests  = 'telemetry-tempest-plugin'
@@ -96,6 +99,7 @@ class tempest::params {
         $python_manila_tests     = false
         $package_name            = 'tempest'
       }
+      $pip_package_name          = 'python3-pip'
     }
     default: {
       fail("Unsupported osfamily: ${facts['os']['family']}")
diff --git a/releasenotes/notes/use-pip-package-15400e8fe8cbb275.yaml b/releasenotes/notes/use-pip-package-15400e8fe8cbb275.yaml
new file mode 100644
index 00000000..8e5d31c7
--- /dev/null
+++ b/releasenotes/notes/use-pip-package-15400e8fe8cbb275.yaml
@@ -0,0 +1,8 @@
+---
+fixes:
+  - |
+    The ``tempest`` class now installs the pip command using the distribution
+    package instead of the ``easy_install`` command for CentOS, because
+    the command is no longer provided by the setuptool package now. Note that
+    RHEL does not have the pip package so the package installation is skipped.
+    Make sure the pip command is installed before the class is executed.
diff --git a/spec/classes/tempest_init_spec.rb b/spec/classes/tempest_init_spec.rb
index 58c9cafc..f9322a05 100644
--- a/spec/classes/tempest_init_spec.rb
+++ b/spec/classes/tempest_init_spec.rb
@@ -204,7 +204,6 @@ describe 'tempest' do
         it 'installs packages' do
 
           is_expected.to contain_package('git')
-          is_expected.to contain_package("python3-setuptools")
 
           platform_params[:dev_packages].each do |package|
             is_expected.to contain_package("#{package}")
@@ -212,18 +211,16 @@ describe 'tempest' do
 
           is_expected.to contain_class('tempest::params')
 
-          is_expected.to contain_exec('install-pip').with(
-            :command => 'easy_install pip',
-            :unless  => "which #{platform_params[:pip_command]}",
-            :path    => ['/bin', '/usr/bin', '/usr/local/bin'],
-            :require => "Package[python3-setuptools]"
-          )
+          if platform_params[:pip_package_name]
+            is_expected.to contain_package('pip').with(
+              :name => platform_params[:pip_package_name],
+            )
+          end
 
           is_expected.to contain_exec('install-tox').with(
             :command => "#{platform_params[:pip_command]} install -U tox",
             :unless  => 'which tox',
             :path    => ['/bin', '/usr/bin', '/usr/local/bin'],
-            :require => 'Exec[install-pip]'
           )
 
           is_expected.to contain_vcsrepo('/var/lib/tempest').with(
@@ -592,10 +589,10 @@ describe 'tempest' do
                                        'libffi-dev',
                                        'patch',
                                        'gcc',
-                                       'python3-virtualenv',
-                                       'python3-pip' ],
+                                       'python3-virtualenv'],
             :package_name          => 'tempest',
             :pip_command           => 'pip3',
+            :pip_package_name      => 'python3-pip',
             :python_keystone_tests => 'keystone-tempest-plugin',
             :python_neutron_tests  => 'neutron-tempest-plugin',
             :python_sahara_tests   => false }
@@ -609,6 +606,7 @@ describe 'tempest' do
                                        'gcc'],
             :package_name          => 'openstack-tempest',
             :pip_command           => 'pip3',
+            :pip_package_name      => 'python3-pip',
             :python_keystone_tests => 'python3-keystone-tests-tempest',
             :python_neutron_tests  => 'python3-neutron-tests-tempest',
             :python_sahara_tests   => 'python3-sahara-tests-tempest' }