Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor unit test #80

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 39 additions & 280 deletions spec/classes/openvmtools_init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,292 +2,51 @@

require 'spec_helper'

describe 'openvmtools', type: 'class' do
context 'on a non-supported os, non-vmware platform' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'foo',
operatingsystemmajrelease: '1',
operatingsystemrelease: '1',
os: {
family: 'foo',
name: 'foo',
release: {
full: '1.1',
major: '1',
minor: '1'
}
},
osfamily: 'foo',
virtual: 'foo'
}
end

it { is_expected.not_to contain_package('open-vm-tools') }
it { is_expected.not_to contain_package('open-vm-tools-desktop') }
it { is_expected.not_to contain_service('vgauthd') }
it { is_expected.not_to contain_service('vmtoolsd') }
end

context 'on a supported os, non-vmware platform' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'RedHat',
operatingsystemmajrelease: '7',
operatingsystemrelease: '7.0',
os: {
family: 'RedHat',
name: 'RedHat',
release: {
full: '7.0',
major: '7',
minor: '0'
}
},
osfamily: 'RedHat',
virtual: 'foo'
}
end

it { is_expected.not_to contain_package('open-vm-tools') }
it { is_expected.not_to contain_package('open-vm-tools-desktop') }
it { is_expected.not_to contain_service('vgauthd') }
it { is_expected.not_to contain_service('vmtoolsd') }
end

context 'on a supported RedHat 7 os, vmware platform, default parameters' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'RedHat',
operatingsystemmajrelease: '7',
operatingsystemrelease: '7.0',
os: {
family: 'RedHat',
name: 'RedHat',
release: {
full: '7.0',
major: '7',
minor: '0'
}
},
osfamily: 'RedHat',
virtual: 'vmware'
}
end

it { is_expected.to contain_package('open-vm-tools') }
it { is_expected.not_to contain_package('open-vm-tools-desktop') }

it {
is_expected.to contain_service('vgauthd').with(
ensure: 'running',
enable: true,
hasstatus: true,
require: '[Package[open-vm-tools]{:name=>"open-vm-tools"}]'
)
}

it {
is_expected.to contain_service('vmtoolsd').with(
ensure: 'running',
enable: true,
hasstatus: true,
require: '[Package[open-vm-tools]{:name=>"open-vm-tools"}]'
)
}
end

context 'on a supported Ubuntu 20.04 os, vmware platform, default parameters' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'Ubuntu',
operatingsystemmajrelease: '20.04',
operatingsystemrelease: '20.04',
os: {
family: 'Debian',
name: 'Ubuntu',
release: {
full: '20.04',
major: '20.04',
}
},
osfamily: 'Debian',
virtual: 'vmware'
}
end

it { is_expected.to contain_package('open-vm-tools') }
it { is_expected.not_to contain_package('open-vm-tools-desktop') }

it {
is_expected.to contain_service('open-vm-tools').with(
ensure: 'running',
enable: true,
hasstatus: false,
require: '[Package[open-vm-tools]{:name=>"open-vm-tools"}]'
)
}
end

context 'on a supported FreeBSD 10 os, vmware platform, default parameters' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'FreeBSD',
operatingsystemmajrelease: '10',
operatingsystemrelease: '10.4-RELEASE',
os: {
family: 'FreeBSD',
name: 'FreeBSD',
release: {
full: '10.4-RELEASE',
major: '10',
minor: '4'
}
},
osfamily: 'FreeBSD',
virtual: 'vmware'
}
end

it { is_expected.to contain_package('open-vm-tools-nox11') }
it { is_expected.not_to contain_package('open-vm-tools') }

it {
is_expected.to contain_service('vmware_guestd').with(
ensure: 'running',
enable: true,
hasstatus: true,
require: "[Package[open-vm-tools-nox11]\
{:name=>\"open-vm-tools-nox11\"}]"
)
}
end

context 'on a supported FreeBSD 11 os, vmware platform, default parameters' do
let(:params) { {} }
let :facts do
{
operatingsystem: 'FreeBSD',
operatingsystemmajrelease: '11',
operatingsystemrelease: '11.2-RELEASE',
os: {
family: 'FreeBSD',
name: 'FreeBSD',
release: {
full: '11.2-RELEASE',
major: '11',
minor: '2'
}
},
osfamily: 'FreeBSD',
virtual: 'vmware'
}
end

it { is_expected.to contain_package('open-vm-tools-nox11') }
it { is_expected.not_to contain_package('open-vm-tools') }

it {
is_expected.to contain_service('vmware_guestd').with(
ensure: 'running',
enable: true,
hasstatus: true,
require: "[Package[open-vm-tools-nox11]\
{:name=>\"open-vm-tools-nox11\"}]"
)
}
end

context 'on a supported RedHat 7 os, vmware platform, custom parameters' do
let :facts do
{
operatingsystem: 'RedHat',
operatingsystemmajrelease: '7',
operatingsystemrelease: '7.0',
os: {
family: 'RedHat',
name: 'RedHat',
release: {
full: '7.0',
major: '7',
minor: '0'
}
},
osfamily: 'RedHat',
virtual: 'vmware'
}
end

describe 'ensure => absent' do
let(:params) { { ensure: 'absent' } }

it { is_expected.to contain_package('open-vm-tools').with_ensure('absent') }
it { is_expected.to contain_service('vgauthd').with_ensure('stopped') }
it { is_expected.to contain_service('vmtoolsd').with_ensure('stopped') }
end

describe 'with_desktop => true' do
let(:params) { { with_desktop: true } }

it {
is_expected.to contain_package('open-vm-tools-desktop').with_ensure('present')
}
end

describe 'uninstall_vmware_tools => true' do
let(:params) { { uninstall_vmware_tools: true } }
describe 'openvmtools' do
on_supported_os.each do |os, os_facts|
context "os #{os}" do
let(:facts) do
os_facts.merge({ virtual: 'vmware' })
end
let(:service_name) do
case facts[:os]['family']
when 'Debian'
'open-vm-tools'
when 'FreeBSD'
'vmware_guestd'
else
'vmtoolsd'
end
Copy link
Member

@jay7x jay7x Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see missing a end here.. but maybe my builtin ruby interpreter misinterprets something in the end of the day 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end is after the last it {} statement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jay7x is correct that the let block needs to be closed

end
let(:package_name) do
case facts[:os]['family']
when 'FreeBSD'
'open-vm-tools-nox11'
else
'open-vm-tools'
end
end

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_package(package_name) }
it { is_expected.not_to contain_package('open-vm-tools-desktop') }

it {
is_expected.to contain_package('VMwareTools').with_ensure('absent')
}
end
end

context 'on a supported FreeBSD 11 os, vmware platform, custom parameters' do
let :facts do
{
operatingsystem: 'FreeBSD',
operatingsystemmajrelease: '11',
operatingsystemrelease: '11.2-RELEASE',
os: {
family: 'FreeBSD',
name: 'FreeBSD',
release: {
full: '11.2-RELEASE',
major: '11',
minor: '2'
}
},
osfamily: 'FreeBSD',
virtual: 'vmware'
case facts[:os]['family']
when 'RedHat'
is_expected.to contain_service('vgauthd').with(
ensure: 'running',
enable: true
).that_requires("Package[#{package_name}]")
end
}
end

describe 'ensure => absent' do
let(:params) { { ensure: 'absent' } }

it { is_expected.to contain_package('open-vm-tools-nox11').with_ensure('absent') }
it { is_expected.to contain_service('vmware_guestd').with_ensure('stopped') }
end

describe 'with_desktop => true' do
let(:params) { { with_desktop: true } }

it { is_expected.to contain_package('open-vm-tools') }
it { is_expected.not_to contain_package('open-vm-tools-nox11') }

it {
is_expected.to contain_service('vmware_guestd').with(
is_expected.to contain_service(service_name).with(
ensure: 'running',
enable: true,
hasstatus: true,
require: '[Package[open-vm-tools]{:name=>"open-vm-tools"}]'
)
enable: true
).that_requires("Package[#{package_name}]")
}
end
end
Expand Down
36 changes: 6 additions & 30 deletions spec/functions/supported_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,13 @@
require 'spec_helper'

describe 'openvmtools::supported' do
context 'On Debian 10' do
let(:facts) do
{
os: {
name: 'Debian',
release: {
major: '10',
}
}
}
end

it 'returns false' do
is_expected.to run.with_params('openvmtools').and_return(false)
end
end

context 'On Debian 11' do
let(:facts) do
{
os: {
name: 'Debian',
release: {
major: '11',
}
}
}
end
on_supported_os.each do |os, os_facts|
context "On #{os}" do
let(:facts) { os_facts.merge(virtual: 'vmware') }

it 'returns true' do
is_expected.to run.with_params('openvmtools').and_return(true)
it 'returns true' do
is_expected.to run.with_params('openvmtools').and_return(true)
end
end
end
end