From 17efdb8a14a305456b1d4cf77e968f01813ac17d Mon Sep 17 00:00:00 2001
From: Melissa Stone <melissa@puppetlabs.com>
Date: Thu, 21 May 2015 16:20:01 -0700
Subject: [PATCH] (PUP-4497) Ignore obsoleted packages on update check

Prior to this commit, if there were any packages that had been
obsoleted, the check to see if any updates were available would fail.
This was because of the way the output from `yum check-update` was being
parsed. We take the output, split it into sets of three, and then take
the first two entries for each set, assuming they correspond to the
package name and the package version. When there are packages that have
been obsoleted, this method doesn't hold true. We find ourselves with a
tuplet consisting of something like ["Obsoleting", "Packages",
"NetworkManager.x86_64"], which then threw off all the following package
tuples. From here on out, what we are assuming in the package name is
actually the package version, and what we are assuming is the package
version is actually the repo where the package is located. When we then
go to parse out the details of the package version (by splitting out
elements based on a regex), we don't get the matches we expect and
Puppet then goes and vomits on your nice shoes.

This commit simple breaks off the process of parsing the output of `yum
check-updates` once the line with "Obsoleting" is reached. Everything
below here is a list of packages along with the package that is
obsoleted. i.e.,
```
Obsoleting Packages
NetworkManager.x86_64     1:1.0.0-14.git20150121.b4ea599c.el7     base
    NetworkManager.x86_64 1:0.9.9.1-25.git20140326.4dba720.el7_0  @updates
```

We could parse this out and return the first of the pair (in this case,
`NetworkManager.x86_64     1:1.0.0-14.git20150121.b4ea599c.el7`).
However, we currently are not doing anything with these obsoleted
packages, so this commit simple ignores the packages that obsolete and
are obsoleted.

(cherry picked from commit 1a3bd9b8ce838b204075e3b684c5075fbb5e3b96)
---
 lib/puppet/provider/package/yum.rb                 |   1 +
 .../package/yum/yum-check-update-obsoletes.txt     | 195 +++++++++++++++++++++
 spec/unit/provider/package/yum_spec.rb             |  11 ++
 3 files changed, 207 insertions(+)
 create mode 100644 spec/fixtures/unit/provider/package/yum/yum-check-update-obsoletes.txt

diff --git a/lib/puppet/provider/package/yum.rb b/lib/puppet/provider/package/yum.rb
index 0b8c28a..34fc106 100644
--- a/lib/puppet/provider/package/yum.rb
+++ b/lib/puppet/provider/package/yum.rb
@@ -91,6 +91,7 @@ Puppet::Type.type(:package).provide :yum, :parent => :rpm, :source => :rpm do
 
     updates = Hash.new { |h, k| h[k] = [] }
     body.split.each_slice(3) do |tuple|
+      break if tuple[0] =~ /^Obsoleting/
       hash = update_to_hash(*tuple[0..1])
       # Create entries for both the package name without a version and a
       # version since yum considers those as mostly interchangeable.
diff --git a/spec/fixtures/unit/provider/package/yum/yum-check-update-obsoletes.txt b/spec/fixtures/unit/provider/package/yum/yum-check-update-obsoletes.txt
new file mode 100644
index 0000000..62ae6ee
--- /dev/null
+++ b/spec/fixtures/unit/provider/package/yum/yum-check-update-obsoletes.txt
@@ -0,0 +1,195 @@
+Loaded plugins: fastestmirror
+Loading mirror speeds from cached hostfile
+
+NetworkManager.x86_64              1:1.0.0-14.git20150121.b4ea599c.el7    base
+NetworkManager-glib.x86_64         1:1.0.0-14.git20150121.b4ea599c.el7    base
+NetworkManager-tui.x86_64          1:1.0.0-14.git20150121.b4ea599c.el7    base
+alsa-firmware.noarch               1.0.28-2.el7                           base
+alsa-lib.x86_64                    1.0.28-2.el7                           base
+audit.x86_64                       2.4.1-5.el7                            base
+audit-libs.x86_64                  2.4.1-5.el7                            base
+authconfig.x86_64                  6.2.8-9.el7                            base
+avahi.x86_64                       0.6.31-14.el7                          base
+avahi-autoipd.x86_64               0.6.31-14.el7                          base
+avahi-libs.x86_64                  0.6.31-14.el7                          base
+bash.x86_64                        4.2.46-12.el7                          base
+bind-libs-lite.x86_64              32:9.9.4-18.el7_1.1                    updates
+bind-license.noarch                32:9.9.4-18.el7_1.1                    updates
+binutils.x86_64                    2.23.52.0.1-30.el7_1.2                 updates
+biosdevname.x86_64                 0.6.1-2.el7                            base
+btrfs-progs.x86_64                 3.16.2-1.el7                           base
+ca-certificates.noarch             2015.2.4-70.0.el7_1                    updates
+centos-logos.noarch                70.0.6-2.el7.centos                    updates
+centos-release.x86_64              7-1.1503.el7.centos.2.8                base
+cpp.x86_64                         4.8.3-9.el7                            base
+cronie.x86_64                      1.4.11-13.el7                          base
+cronie-anacron.x86_64              1.4.11-13.el7                          base
+cryptsetup-libs.x86_64             1.6.6-3.el7                            base
+dbus.x86_64                        1:1.6.12-11.el7                        base
+dbus-libs.x86_64                   1:1.6.12-11.el7                        base
+device-mapper.x86_64               7:1.02.93-3.el7                        base
+device-mapper-event.x86_64         7:1.02.93-3.el7                        base
+device-mapper-event-libs.x86_64    7:1.02.93-3.el7                        base
+device-mapper-libs.x86_64          7:1.02.93-3.el7                        base
+device-mapper-persistent-data.x86_64
+                                   0.4.1-2.el7                            base
+dhclient.x86_64                    12:4.2.5-36.el7.centos                 base
+dhcp-common.x86_64                 12:4.2.5-36.el7.centos                 base
+dhcp-libs.x86_64                   12:4.2.5-36.el7.centos                 base
+dnsmasq.x86_64                     2.66-13.el7_1                          updates
+dracut.x86_64                      033-241.el7_1.1                        updates
+dracut-config-rescue.x86_64        033-241.el7_1.1                        updates
+dracut-network.x86_64              033-241.el7_1.1                        updates
+e2fsprogs.x86_64                   1.42.9-7.el7                           base
+e2fsprogs-libs.x86_64              1.42.9-7.el7                           base
+elfutils-libelf.x86_64             0.160-1.el7                            base
+elfutils-libs.x86_64               0.160-1.el7                            base
+ethtool.x86_64                     2:3.15-2.el7                           base
+firewalld.noarch                   0.3.9-11.el7                           base
+freetype.x86_64                    2.4.11-10.el7_1.1                      updates
+gcc.x86_64                         4.8.3-9.el7                            base
+glib-networking.x86_64             2.40.0-1.el7                           base
+glib2.x86_64                       2.40.0-4.el7                           base
+glibc.x86_64                       2.17-78.el7                            base
+glibc-common.x86_64                2.17-78.el7                            base
+glibc-devel.x86_64                 2.17-78.el7                            base
+glibc-headers.x86_64               2.17-78.el7                            base
+gmp.x86_64                         1:6.0.0-11.el7                         base
+gnutls.x86_64                      3.3.8-12.el7                           base
+grep.x86_64                        2.20-1.el7                             base
+grub2.x86_64                       1:2.02-0.16.el7.centos                 base
+grub2-tools.x86_64                 1:2.02-0.16.el7.centos                 base
+grubby.x86_64                      8.28-11.el7                            base
+hwdata.noarch                      0.252-7.5.el7                          base
+hwdata.x86_64                      0.252-7.8.el7_1                        updates
+initscripts.x86_64                 9.49.24-1.el7                          base
+iproute.x86_64                     3.10.0-21.el7                          base
+iprutils.x86_64                    2.4.3-3.el7                            base
+irqbalance.x86_64                  2:1.0.7-1.el7                          base
+iwl100-firmware.noarch             39.31.5.1-36.el7                       base
+iwl1000-firmware.noarch            1:39.31.5.1-36.el7                     base
+iwl105-firmware.noarch             18.168.6.1-36.el7                      base
+iwl135-firmware.noarch             18.168.6.1-36.el7                      base
+iwl2000-firmware.noarch            18.168.6.1-36.el7                      base
+iwl2030-firmware.noarch            18.168.6.1-36.el7                      base
+iwl3160-firmware.noarch            22.0.7.0-36.el7                        base
+iwl3945-firmware.noarch            15.32.2.9-36.el7                       base
+iwl4965-firmware.noarch            228.61.2.24-36.el7                     base
+iwl5000-firmware.noarch            8.83.5.1_1-36.el7                      base
+iwl5150-firmware.noarch            8.24.2.2-36.el7                        base
+iwl6000-firmware.noarch            9.221.4.1-36.el7                       base
+iwl6000g2a-firmware.noarch         17.168.5.3-36.el7                      base
+iwl6000g2b-firmware.noarch         17.168.5.2-36.el7                      base
+iwl6050-firmware.noarch            41.28.5.1-36.el7                       base
+iwl7260-firmware.noarch            22.0.7.0-36.el7                        base
+kbd.x86_64                         1.15.5-11.el7                          base
+kbd-misc.noarch                    1.15.5-11.el7                          base
+kernel.x86_64                      3.10.0-229.4.2.el7                     updates
+kernel-headers.x86_64              3.10.0-229.4.2.el7                     updates
+kernel-tools.x86_64                3.10.0-229.4.2.el7                     updates
+kernel-tools-libs.x86_64           3.10.0-229.4.2.el7                     updates
+kexec-tools.x86_64                 2.0.7-19.el7_1.2                       updates
+kmod.x86_64                        14-10.el7                              base
+kmod-libs.x86_64                   14-10.el7                              base
+kpartx.x86_64                      0.4.9-77.el7                           base
+krb5-libs.x86_64                   1.12.2-14.el7                          base
+libblkid.x86_64                    2.23.2-22.el7_1                        updates
+libcom_err.x86_64                  1.42.9-7.el7                           base
+libdb.x86_64                       5.3.21-17.el7_0.1                      base
+libdb-utils.x86_64                 5.3.21-17.el7_0.1                      base
+libdrm.x86_64                      2.4.56-2.el7                           base
+libgcc.x86_64                      4.8.3-9.el7                            base
+libgcrypt.x86_64                   1.5.3-12.el7_1.1                       updates
+libgcrypt-devel.x86_64             1.5.3-12.el7_1.1                       updates
+libgomp.x86_64                     4.8.3-9.el7                            base
+libgudev1.x86_64                   208-20.el7_1.3                         updates
+libmount.x86_64                    2.23.2-22.el7_1                        updates
+libnl3.x86_64                      3.2.21-8.el7                           base
+libnl3-cli.x86_64                  3.2.21-8.el7                           base
+libpcap.x86_64                     14:1.5.3-4.el7_1.2                     updates
+libsoup.x86_64                     2.46.0-3.el7                           base
+libss.x86_64                       1.42.9-7.el7                           base
+libstdc++.x86_64                   4.8.3-9.el7                            base
+libtasn1.x86_64                    3.8-2.el7                              base
+libteam.x86_64                     1.15-1.el7                             base
+libuuid.x86_64                     2.23.2-22.el7_1                        updates
+libxml2.x86_64                     2.9.1-5.el7_1.2                        updates
+libxml2-devel.x86_64               2.9.1-5.el7_1.2                        updates
+libyaml.x86_64                     0.1.4-11.el7_0                         base
+linux-firmware.noarch              20140911-0.1.git365e80c.el7            base
+lvm2.x86_64                        7:2.02.115-3.el7                       base
+lvm2-libs.x86_64                   7:2.02.115-3.el7                       base
+mariadb-libs.x86_64                1:5.5.41-2.el7_0                       base
+microcode_ctl.x86_64               2:2.1-10.el7                           base
+nettle.x86_64                      2.7.1-4.el7                            base
+nspr.x86_64                        4.10.8-1.el7_1                         updates
+nss.x86_64                         3.18.0-2.2.el7_1                       updates
+nss-softokn.x86_64                 3.16.2.3-9.el7                         base
+nss-softokn-freebl.x86_64          3.16.2.3-9.el7                         base
+nss-sysinit.x86_64                 3.18.0-2.2.el7_1                       updates
+nss-tools.x86_64                   3.18.0-2.2.el7_1                       updates
+nss-util.x86_64                    3.18.0-1.el7_1                         updates
+ntpdate.x86_64                     4.2.6p5-19.el7.centos                  base
+numactl-libs.x86_64                2.0.9-4.el7                            base
+open-vm-tools.x86_64               9.4.0-6.el7                            base
+openldap.x86_64                    2.4.39-6.el7                           base
+openssh.x86_64                     6.6.1p1-12.el7_1                       updates
+openssh-clients.x86_64             6.6.1p1-12.el7_1                       updates
+openssh-server.x86_64              6.6.1p1-12.el7_1                       updates
+openssl.x86_64                     1:1.0.1e-42.el7.4                      updates
+openssl-libs.x86_64                1:1.0.1e-42.el7.4                      updates
+p11-kit.x86_64                     0.20.7-3.el7                           base
+p11-kit-trust.x86_64               0.20.7-3.el7                           base
+pam.x86_64                         1.1.8-12.el7                           base
+parted.x86_64                      3.1-20.el7                             base
+pcre.x86_64                        8.32-14.el7                            base
+plymouth.x86_64                    0.8.9-0.13.20140113.el7.centos         base
+plymouth-core-libs.x86_64          0.8.9-0.13.20140113.el7.centos         base
+plymouth-scripts.x86_64            0.8.9-0.13.20140113.el7.centos         base
+policycoreutils.x86_64             2.2.5-15.el7                           base
+procps-ng.x86_64                   3.3.10-3.el7                           base
+pygobject3-base.x86_64             3.8.2-6.el7                            base
+python-backports.x86_64            1.0-8.el7                              base
+python-urlgrabber.noarch           3.10-6.el7                             base
+rpm.x86_64                         4.11.1-25.el7                          base
+rpm-build-libs.x86_64              4.11.1-25.el7                          base
+rpm-libs.x86_64                    4.11.1-25.el7                          base
+rpm-python.x86_64                  4.11.1-25.el7                          base
+rsyslog.x86_64                     7.4.7-7.el7_0                          base
+rubygem-bigdecimal.x86_64          1.2.0-24.el7                           base
+rubygem-io-console.x86_64          0.4.2-24.el7                           base
+rubygem-json.x86_64                1.7.7-24.el7                           base
+rubygem-psych.x86_64               2.0.0-24.el7                           base
+rubygems.noarch                    2.0.14-24.el7                          base
+selinux-policy.noarch              3.13.1-23.el7_1.7                      updates
+selinux-policy-targeted.noarch     3.13.1-23.el7_1.7                      updates
+setup.noarch                       2.8.71-5.el7                           base
+shadow-utils.x86_64                2:4.1.5.1-18.el7                       base
+sudo.x86_64                        1.8.6p7-13.el7                         base
+systemd.x86_64                     208-20.el7_1.3                         updates
+systemd-libs.x86_64                208-20.el7_1.3                         updates
+systemd-sysv.x86_64                208-20.el7_1.3                         updates
+teamd.x86_64                       1.15-1.el7                             base
+tuned.noarch                       2.4.1-1.el7                            base
+tzdata.noarch                      2015d-1.el7                            updates
+util-linux.x86_64                  2.23.2-22.el7_1                        updates
+wpa_supplicant.x86_64              1:2.0-13.el7_0                         base
+xfsprogs.x86_64                    3.2.1-6.el7                            base
+xz.x86_64                          5.1.2-9alpha.el7                       base
+xz-devel.x86_64                    5.1.2-9alpha.el7                       base
+xz-libs.x86_64                     5.1.2-9alpha.el7                       base
+yum.noarch                         3.4.3-125.el7.centos                   base
+yum-plugin-fastestmirror.noarch    1.1.31-29.el7                          base
+Obsoleting Packages
+NetworkManager.x86_64              1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
+NetworkManager-adsl.x86_64         1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
+NetworkManager-bluetooth.x86_64    1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
+NetworkManager-team.x86_64         1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
+NetworkManager-wifi.x86_64         1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
+NetworkManager-wwan.x86_64         1:1.0.0-14.git20150121.b4ea599c.el7    base
+    NetworkManager.x86_64          1:0.9.9.1-25.git20140326.4dba720.el7_0 @updates
diff --git a/spec/unit/provider/package/yum_spec.rb b/spec/unit/provider/package/yum_spec.rb
index fa3a208..bd89cf4 100755
--- a/spec/unit/provider/package/yum_spec.rb
+++ b/spec/unit/provider/package/yum_spec.rb
@@ -471,6 +471,17 @@ describe provider_class do
         expect(output['libpcap']).to eq([{:name => 'libpcap', :epoch => '14', :version => '1.4.0', :release => '1.20130826git2dbcaa1.el6', :arch => 'x86_64'}])
       end
     end
+
+    describe "with obsoleted packages" do
+      let(:check_update) { File.read(my_fixture('yum-check-update-obsoletes.txt')) }
+      let(:output) { described_class.parse_updates(check_update) }
+
+      it "ignores all entries including and after 'Obsoleting Packages'" do
+        expect(output).not_to include("Obsoleting")
+        expect(output).not_to include("NetworkManager-bluetooth.x86_64")
+        expect(output).not_to include("1:1.0.0-14.git20150121.b4ea599c.el7")
+      end
+    end
   end
 
   describe "parsing a line from yum check-update" do
-- 
2.4.3

