Skip to content

Commit

Permalink
Merge pull request #198 from vmoravec/update-tests
Browse files Browse the repository at this point in the history
Add tests for Yast::Service
  • Loading branch information
Vladimir Moravec committed Mar 26, 2014
2 parents f47ed2c + be0a1ca commit fc27ade
Show file tree
Hide file tree
Showing 30 changed files with 198 additions and 656 deletions.
12 changes: 6 additions & 6 deletions library/runlevel/src/modules/Service.rb
Expand Up @@ -78,7 +78,7 @@ def Enabled name
# @param [String] service service to be enabled
# @return true if operation is successful
def Enable service_name
log.info "Enabling service %1", service_name
log.info "Enabling service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:enable, service_name, service.error) unless service.enable
Expand All @@ -91,7 +91,7 @@ def Enable service_name
# @param [String] service service to be disabled
# @return true if operation is successful
def Disable service_name
log.info "Disabling service %1", service_name
log.info "Disabling service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:disable, service_name, service.error) unless service.disable
Expand All @@ -104,7 +104,7 @@ def Disable service_name
# @param [String] service service to be started
# @return true if operation is successful
def Start service_name
log.info "Starting service %1", service_name
log.info "Starting service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:start, service_name, service.error) unless service.start
Expand All @@ -117,7 +117,7 @@ def Start service_name
# @param [String] service service to be restarted
# @return true if operation is successful
def Restart service_name
log.info "Restarting service %1", service_name
log.info "Restarting service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:restart, service_name, service.error) unless service.restart
Expand All @@ -130,7 +130,7 @@ def Restart service_name
# @param [String] service service to be reloaded
# @return true if operation is successful
def Reload service_name
log.info "Reloading service %1", service_name
log.info "Reloading service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:reload, service_name, service.error) unless service.reload
Expand All @@ -143,7 +143,7 @@ def Reload service_name
# @param [String] service service to be stopped
# @return true if operation is successful
def Stop service_name
log.info "Stopping service %1", service_name
log.info "Stopping service '#{service_name}'"
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
return failure(:stop, service_name, service.error) unless service.stop
Expand Down
169 changes: 169 additions & 0 deletions library/runlevel/test/service_test.rb
@@ -0,0 +1,169 @@
#!/usr/bin/env rspec
#
require_relative 'test_helper'

module Yast
describe Service do
include SystemdServiceStubs

def stub_service_with method, result
allow_any_instance_of(SystemdServiceClass::Service).to receive(method)
.and_return(result)
end

before do
# Defined in systemd/test/test_helper.rb
# The service 'sshd' is mocked for active/loaded service
# The service 'unknown' is mocked for non-existing service
stub_services
end

describe ".Active" do
it "returns true if a service is active" do
expect(Service.Active('sshd')).to be_true
end

it "returns false if a service is inactive" do
stub_service_with(:active?, false)
expect(Service.Active('sshd')).to be_false
end

it "returns false if a service does not exist" do
stub_services(:service=>'unknown')
expect(Service.Active('unknown')).to be_false
end
end

describe ".Enabled" do
it "returns true if a service is enabled" do
expect(Service.Enabled('sshd')).to be_true
end

it "returns false if a service in not enabled" do
stub_service_with(:enabled?, false)
expect(Service.Enabled('sshd')).to be_false
end

it "returns false if a service does not exists" do
stub_services(:service=>'unknown')
expect(Service.Enabled('unknown')).to be_false
end
end

describe ".Enable" do
it "returns true if a service has been enabled successfully" do
expect(Service.Enable('sshd')).to be_true
end

it "returns false if a service has not been enabled" do
stub_service_with(:enable, false)
stub_service_with(:error, 'error')
expect(Service.Enable('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service has not been found" do
stub_services(:service=>'unknown')
expect(Service.Enable('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end

describe ".Disable" do
it "returns true if a service has been disabled" do
expect(Service.Disable('sshd')).to be_true
end

it "returns false if a service has not been disabled" do
stub_service_with(:disable, false)
stub_service_with(:error, 'error')
expect(Service.Disable('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service does not exist" do
stub_services(:service=>'unknown')
expect(Service.Disable('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end

describe ".Start" do
it "returns true if a service has been started successfully" do
expect(Service.Start('sshd')).to be_true
end

it "returns false if a service has not been started" do
stub_service_with(:start, false)
stub_service_with(:error, 'error')
expect(Service.Start('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service has not been found" do
stub_services(:service=>'unknown')
expect(Service.Start('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end

describe ".Restart" do
it "returns true if a service has been restarted successfully" do
expect(Service.Restart('sshd')).to be_true
end

it "returns false if a service has not been restarted" do
stub_service_with(:restart, false)
stub_service_with(:error, 'error')
expect(Service.Restart('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service has not been found" do
stub_services(:service=>'unknown')
expect(Service.Restart('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end

describe ".Reload" do
it "returns true if a service has been reloaded successfully" do
expect(Service.Reload('sshd')).to be_true
end

it "returns false if a service has not been reloaded" do
stub_service_with(:reload, false)
stub_service_with(:error, 'error')
expect(Service.Reload('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service has not been found" do
stub_services(:service=>'unknown')
expect(Service.Reload('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end

describe ".Stop" do
it "returns true if a service has been stopped successfully" do
expect(Service.Stop('sshd')).to be_true
end

it "returns false if a service has not been stopped" do
stub_service_with(:stop, false)
stub_service_with(:error, 'error')
expect(Service.Stop('sshd')).to be_false
expect(Service.Error).not_to be_empty
end

it "returns false if a service has not been found" do
stub_services(:service=>'unknown')
expect(Service.Stop('unknown')).to be_false
expect(Service.Error).not_to be_empty
end
end
end
end


11 changes: 11 additions & 0 deletions library/runlevel/test/test_helper.rb
@@ -0,0 +1,11 @@
require 'rspec'

ENV["Y2DIR"] = File.expand_path("../../src", __FILE__)

require "yast"

Yast.import 'Service'

# We need the stubs used for SystemdService tests
require_relative "../../systemd/test/test_helper"

Empty file.
7 changes: 0 additions & 7 deletions library/runlevel/testsuite/tests/GetSetServiceStatus.out

This file was deleted.

61 changes: 0 additions & 61 deletions library/runlevel/testsuite/tests/GetSetServiceStatus.rb

This file was deleted.

Empty file.
3 changes: 0 additions & 3 deletions library/runlevel/testsuite/tests/RunInitScript.out

This file was deleted.

48 changes: 0 additions & 48 deletions library/runlevel/testsuite/tests/RunInitScript.rb

This file was deleted.

Empty file.
7 changes: 0 additions & 7 deletions library/runlevel/testsuite/tests/Service.out

This file was deleted.

0 comments on commit fc27ade

Please sign in to comment.