From 05c24c658303b86e4020a782a2d4a20d851a3eea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartarisi@suse.cz>
Date: Mon, 25 Feb 2013 18:10:19 +0100
Subject: [PATCH] CHEf-4090: refactor zypper commands and use shell_out

---
 lib/chef/provider/package/zypper.rb       | 88 ++++++++++++------------------
 spec/unit/provider/package/zypper_spec.rb | 89 +++++++++++++++----------------
 2 files changed, 77 insertions(+), 100 deletions(-)

Index: lib/chef/provider/package/zypper.rb
===================================================================
--- lib/chef/provider/package/zypper.rb.orig	2014-10-21 11:25:10.201126932 +0200
+++ lib/chef/provider/package/zypper.rb	2014-10-21 11:25:22.532126910 +0200
@@ -1,14 +1,17 @@
+# -*- coding: utf-8 -*-
 #
-# Author:: Adam Jacob (<adam@opscode.com>)
+# Authors:: Adam Jacob (<adam@opscode.com>)
+#           Ionuț Arțăriși (<iartarisi@suse.cz>)
 # Copyright:: Copyright (c) 2008 Opscode, Inc.
+#             Copyright (c) 2013 SUSE Linux GmbH
 # License:: Apache License, Version 2.0
 #
 # 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
-# 
+#
 #     http://www.apache.org/licenses/LICENSE-2.0
-# 
+#
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,14 +22,16 @@
 require 'chef/provider/package'
 require 'chef/mixin/command'
 require 'chef/resource/package'
+require 'chef/mixin/shell_out'
 require 'singleton'
 
 class Chef
   class Provider
     class Package
-      class Zypper < Chef::Provider::Package  
-      
- 
+      class Zypper < Chef::Provider::Package
+
+        include Chef::Mixin::ShellOut
+
         def load_current_resource
           @current_resource = Chef::Resource::Package.new(@new_resource.name)
           @current_resource.package_name(@new_resource.package_name)
@@ -45,7 +50,7 @@ class Chef
               when /^Installed: Yes$/
                 is_installed=true
                 Chef::Log.debug("#{@new_resource} is installed")
-                
+
               when /^Installed: No$/
                 is_installed=false
                 Chef::Log.debug("#{@new_resource} is not installed")
@@ -61,12 +66,12 @@ class Chef
             @candidate_version=version
             @current_resource.version(nil)
           end
- 
+
           if is_installed==true
             if is_out_of_date==true
               @current_resource.version(oud_version)
               @candidate_version=version
-            else 
+            else
               @current_resource.version(version)
               @candidate_version=version
             end
@@ -144,8 +149,8 @@ class Chef
           when false
             " --no-gpg-checks"
           when nil
-            Chef::Log.warn("Chef::Config[:zypper_check_gpg] was not set. " + 
-              "All packages will be installed without gpg signature checks. " + 
+            Chef::Log.warn("Chef::Config[:zypper_check_gpg] was not set. " +
+              "All packages will be installed without gpg signature checks. " +
               "This is a security hazard.")
             " --no-gpg-checks"
           end
Index: spec/unit/provider/package/zypper_spec.rb
===================================================================
--- spec/unit/provider/package/zypper_spec.rb.orig	2014-10-21 11:25:10.201126932 +0200
+++ spec/unit/provider/package/zypper_spec.rb	2014-10-21 11:25:22.533126910 +0200
@@ -93,24 +93,23 @@ describe Chef::Provider::Package::Zypper
   describe "install_package" do
     it "should run zypper install with the package name and version" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(true)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive install --auto-agree-with-licenses emacs=1.0")
       @provider.install_package("emacs", "1.0")
     end
     it "should run zypper install without gpg checks" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(false)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks install "+
+        "--auto-agree-with-licenses emacs=1.0")
       @provider.install_package("emacs", "1.0")
     end
     it "should warn about gpg checks on zypper install" do
       Chef::Log.should_receive(:warn).with(
         /All packages will be installed without gpg signature checks/)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks install "+
+        "--auto-agree-with-licenses emacs=1.0")
       @provider.install_package("emacs", "1.0")
     end
   end
@@ -118,24 +117,30 @@ describe Chef::Provider::Package::Zypper
   describe "upgrade_package" do
     it "should run zypper update with the package name and version" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(true)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive install --auto-agree-with-licenses emacs=1.0")
       @provider.upgrade_package("emacs", "1.0")
     end
     it "should run zypper update without gpg checks" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(false)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks install "+
+        "--auto-agree-with-licenses emacs=1.0")
       @provider.upgrade_package("emacs", "1.0")
     end
     it "should warn about gpg checks on zypper upgrade" do
       Chef::Log.should_receive(:warn).with(
         /All packages will be installed without gpg signature checks/)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks install -l emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks install "+
+        "--auto-agree-with-licenses emacs=1.0")
+      @provider.upgrade_package("emacs", "1.0")
+    end
+    it "should run zypper upgrade without gpg checks" do
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks install "+
+        "--auto-agree-with-licenses emacs=1.0")
+
       @provider.upgrade_package("emacs", "1.0")
     end
   end
@@ -143,46 +148,43 @@ describe Chef::Provider::Package::Zypper
   describe "remove_package" do
     it "should run zypper remove with the package name" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(true)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n remove emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive remove emacs=1.0")
       @provider.remove_package("emacs", "1.0")
     end
     it "should run zypper remove without gpg checks" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(false)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks remove emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+          "zypper --non-interactive --no-gpg-checks remove emacs=1.0")
       @provider.remove_package("emacs", "1.0")
     end
     it "should warn about gpg checks on zypper remove" do
       Chef::Log.should_receive(:warn).with(
         /All packages will be installed without gpg signature checks/)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks remove emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks remove emacs=1.0")
+
       @provider.remove_package("emacs", "1.0")
     end
   end
 
   describe "purge_package" do
     it "should run remove_package with the name and version" do
-      @provider.should_receive(:remove_package).with("emacs", "1.0")
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks remove --clean-deps emacs=1.0")
       @provider.purge_package("emacs", "1.0")
     end
     it "should run zypper purge without gpg checks" do
       Chef::Config.stub(:[]).with(:zypper_check_gpg).and_return(false)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks remove emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks remove --clean-deps emacs=1.0")
       @provider.purge_package("emacs", "1.0")
     end
     it "should warn about gpg checks on zypper purge" do
       Chef::Log.should_receive(:warn).with(
         /All packages will be installed without gpg signature checks/)
-      @provider.should_receive(:run_command).with({
-          :command => "zypper -n --no-gpg-checks remove emacs=1.0",
-        })
+      @provider.should_receive(:shell_out!).with(
+        "zypper --non-interactive --no-gpg-checks remove --clean-deps emacs=1.0")
       @provider.purge_package("emacs", "1.0")
     end
   end
@@ -194,27 +196,24 @@ describe Chef::Provider::Package::Zypper
 
     describe "install_package" do
       it "should run zypper install with the package name and version" do
-        @provider.should_receive(:run_command).with({
-            :command => "zypper install -y emacs"
-          })
+        @provider.should_receive(:shell_out!).with(
+          "zypper --no-gpg-checks install --auto-agree-with-licenses -y emacs")
         @provider.install_package("emacs", "1.0")
       end
     end
-  
+
     describe "upgrade_package" do
       it "should run zypper update with the package name and version" do
-        @provider.should_receive(:run_command).with({
-            :command => "zypper install -y emacs"
-          })
+        @provider.should_receive(:shell_out!).with(
+          "zypper --no-gpg-checks install --auto-agree-with-licenses -y emacs")
         @provider.upgrade_package("emacs", "1.0")
       end
     end
-  
+
     describe "remove_package" do
       it "should run zypper remove with the package name" do
-        @provider.should_receive(:run_command).with({
-            :command => "zypper remove -y emacs"
-          })
+        @provider.should_receive(:shell_out!).with(
+           "zypper --no-gpg-checks remove -y emacs")
         @provider.remove_package("emacs", "1.0")
       end
     end
