Skip to content

Commit

Permalink
Added tests + client
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jul 15, 2016
1 parent a6b475e commit de318be
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/clients/inst_instsys_cleanup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "installation/clients/inst_instsys_cleanup"
Yast::InstInstsysCleanupClient.new.main
28 changes: 28 additions & 0 deletions src/lib/installation/clients/inst_instsys_cleanup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: utf-8

# ------------------------------------------------------------------------------
# Copyright (c) 2016 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# ------------------------------------------------------------------------------

require "yast"
require "installation/instsys_cleaner"

# This is a client wrapper around the InstsysCleaner class
module Yast
class InstInstsysCleanupClient < Client
def main
Installation::InstsysCleaner.make_clean

:next
end
end
end
26 changes: 13 additions & 13 deletions src/lib/installation/instsys_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ module Installation
class InstsysCleaner
extend Yast::Logger

Yast.import "Mode"
Yast.import "Stage"

# memory limit for removing the kernel modules from inst-sys (1GB)
KERNEL_MODULES_WATERLINE = 1 << 30
KERNEL_MODULES_MOUNT_POINT = "/parts/mp_0000"
KERNEL_MODULES_MOUNT_POINT = "/parts/mp_0000".freeze

# memory limit for removing the libzypp metadata cache (640MB)
LIBZYPP_WATERLINE = 640 * (1 << 20)
LIBZYPP_WATERLINE = 640 << 20
# the cache in inst-sys, the target system cache is at /mnt/...,
# in upgrade mode the target cache is kept
LIBZYPP_CACHE_PATH = "/var/cache/zypp/raw".freeze

# Remove some files in inst-sys to have more free space if the system
# has too low memory. If the system has enough memory keep everything in place.
def self.make_clean
Yast.import "Mode"
Yast.import "Stage"

# just a sanity check to make sure it's not called in an unexpected situation
if !Stage.initial && (Mode.install || Mode.update || Mode.auto)
log.warning("Skipping inst-sys cleanup (not in installation/update)")
if !Yast::Stage.initial || !(Yast::Mode.installation || Yast::Mode.update || Yast::Mode.auto)
log.warn("Skipping inst-sys cleanup (not in installation/update)")
return :auto
end

# memory size in MB
memory_mb = Yast2::HwDetection.memory << 20
# memory size in bytes
memory = Yast2::HwDetection.memory

# run the cleaning actions depending on the available memory
unmount_kernel_modules if memory_mb < KERNEL_MODULES_WATERLINE
cleanup_zypp_cache if memory_mb < LIBZYPP_WATERLINE
unmount_kernel_modules if memory < KERNEL_MODULES_WATERLINE
cleanup_zypp_cache if memory < LIBZYPP_WATERLINE

:auto
end
Expand Down Expand Up @@ -112,14 +112,14 @@ def self.unmount_kernel_modules

# unmount the loop device
`umount #{KERNEL_MODULES_MOUNT_POINT}`
if !$?.success?
if !$CHILD_STATUS.success?
log.warn("Unmouting #{KERNEL_MODULES_MOUNT_POINT} failed")
return
end

# remove the loop device setup (losetup -d)
`losetup -d #{Shellwords.escape(device)}`
if !$?.success?
if !$CHILD_STATUS.success?
log.warn("Detaching loopback device #{device} failed")
return
end
Expand Down
59 changes: 59 additions & 0 deletions test/instsys_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#! /usr/bin/env rspec

require_relative "./test_helper"

require "installation/instsys_cleaner"

describe Installation::InstsysCleaner do
describe ".make_clean" do
context "in the initial stage" do
before do
expect(Yast::Stage).to receive(:initial).and_return(true)
expect(Yast::Mode).to receive(:installation).and_return(true)
end

it "removes the libzypp cache if the memory less than 640MB" do
# 512MB RAM
expect(Yast2::HwDetection).to receive(:memory).and_return(512 << 20)
allow(subject.class).to receive(:unmount_kernel_modules)

expect(FileUtils).to receive(:rm_rf).with(Installation::InstsysCleaner::LIBZYPP_CACHE_PATH)

subject.class.make_clean
end

it "removes the kernel modules if the memory less than 1GB" do
# 768MB RAM
expect(Yast2::HwDetection).to receive(:memory).and_return(768 << 20)
allow(subject.class).to receive(:cleanup_zypp_cache)

expect(File).to receive(:exist?).with("/parts/mp_0000/lib/modules").and_return(true)

subject.class.make_clean
end

it "does not remove anything if there is enough memory" do
# 2GB RAM
expect(Yast2::HwDetection).to receive(:memory).and_return(2 << 30)

expect(subject.class).to_not receive(:cleanup_zypp_cache)
expect(subject.class).to_not receive(:unmount_kernel_modules)

subject.class.make_clean
end
end

context "outside the initial stage" do
before do
expect(Yast::Stage).to receive(:initial).and_return(false)
end

it "does not do anything" do
expect(subject.class).to_not receive(:cleanup_zypp_cache)
expect(subject.class).to_not receive(:unmount_kernel_modules)

subject.class.make_clean
end
end
end
end

0 comments on commit de318be

Please sign in to comment.