Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Use cyberflow's unit testing logic from realityforge#101
Browse files Browse the repository at this point in the history
  • Loading branch information
mburns committed Mar 17, 2018
1 parent 095ba3a commit 7f33b02
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 161 deletions.
1 change: 1 addition & 0 deletions chefignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test/*
features/*
Guardfile
Procfile
test

# SCM #
#######
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/environments/chefspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "chefspec",
"description": "Stubbed Environment for Chefspec",
"cookbook_versions": {
},
"json_class": "Chef::Environment",
"chef_type": "environment",
"default_attributes": {
},
"override_attributes": {
}
}
27 changes: 27 additions & 0 deletions test/fixtures/nodes/node01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "node01",
"chef_environment": "_default",
"json_class": "Chef::Node",
"automatic": {
"hostname": "vagrant.vm",
"recipes": [
"kibana::default"
],
"roles": [
],
"ipaddress": "192.168.0.1"
},
"tags": [
"app"
],
"normal": {
},
"chef_type": "node",
"default": {
},
"override": {
},
"run_list": [
"recipe[kibana::default]"
]
}
116 changes: 60 additions & 56 deletions test/unit/spec/_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
# frozen_string_literal: true

require 'chefspec'
require_relative 'spec_helper'

# for ubuntu 14.04 upstart
describe 'kibana::_service' do
before { stub_resources }
let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_OPTS).converge(described_recipe) }
let(:template) { chef_run.template('/etc/init/kibana.conf') }
it 'creates an upstart template at /etc/init/kibana.conf' do
expect(chef_run).to create_template('/etc/init/kibana.conf')
end
it 'enables a kibana service' do
expect(chef_run).to enable_service('kibana')
end
it 'sends a restart notification to the service' do
expect(template).to notify('service[kibana]').to(:restart)
end
end
# describe 'kibana::_service' do
# before { stub_resources }
# let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_OPTS).converge(described_recipe) }
# let(:template) { chef_run.template('/etc/init/kibana.conf') }
# it 'creates an upstart template at /etc/init/kibana.conf' do
# expect(chef_run).to create_template('/etc/init/kibana.conf')
# end
# end
#
# # for ubuntu 16.04 systemd
# describe 'kibana::_service' do
# before { stub_resources }
# let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_1604_OPTS).converge(described_recipe) }
# let(:template) { chef_run.template('/lib/systemd/system/kibana.service') }
# it 'creates an upstart template at /lib/systemd/system/kibana.service' do
# expect(chef_run).to create_template('/lib/systemd/system/kibana.service')
# end
# end
#
# # for centos 6.x init
# describe 'kibana::_service' do
# before { stub_resources }
# let(:chef_run) { ChefSpec::SoloRunner.new(CENTOS_OPTS).converge(described_recipe) }
# let(:template) { chef_run.template('/usr/lib/systemd/system/kibana.service') }
#
# it 'creates an init.d template at /usr/lib/systemd/system/kibana.service' do
# expect(chef_run).to create_template('/usr/lib/systemd/system/kibana.service')
# end
# end
#
# # for centos 7.x systemd
# describe 'kibana::_service' do
# before { stub_resources }
# let(:chef_run) { ChefSpec::SoloRunner.new(CENTOS7_OPTS).converge(described_recipe) }
# let(:template) { chef_run.template('/usr/lib/systemd/system/kibana.service') }
# it 'creates an init.d template at /usr/lib/systemd/system/kibana.service' do
# expect(chef_run).to create_template('/usr/lib/systemd/system/kibana.service')
# end
# end

# for ubuntu 16.04 systemd
describe 'kibana::_service' do
before { stub_resources }
let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_1604_OPTS).converge(described_recipe) }
let(:template) { chef_run.template('/lib/systemd/system/kibana.service') }
it 'creates an upstart template at /lib/systemd/system/kibana.service' do
expect(chef_run).to create_template('/lib/systemd/system/kibana.service')
end
it 'enables a kibana service' do
expect(chef_run).to enable_service('kibana')
end
it 'sends a restart notification to the service' do
expect(template).to notify('service[kibana]').to(:restart)
end
end
supported_platforms.each do |platform, versions|
versions.each do |version|
context "on #{platform.capitalize} #{version}" do
let(:chef_run) do
ChefSpec::ServerRunner.new(platform: platform, version: version) do |node, server|
node_resources(node) # data for this node
stub_chef_zero(platform, version, server) # stub other nodes in chef-zero
end.converge(described_recipe)
end

# for centos 6.x init
describe 'kibana::_service' do
before { stub_resources }
let(:chef_run) { ChefSpec::SoloRunner.new(CENTOS_OPTS).converge(described_recipe) }
let(:template) { chef_run.template('/usr/lib/systemd/system/kibana.service') }
# any platform specific data you want available to your test can be loaded here
_property = load_platform_properties(platform: platform, platform_version: version)

it 'creates an init.d template at /usr/lib/systemd/system/kibana.service' do
expect(chef_run).to create_template('/usr/lib/systemd/system/kibana.service')
end
it 'enables a kibana service' do
expect(chef_run).to enable_service('kibana')
end
it 'sends a restart notification to the service' do
expect(template).to notify('service[kibana]').to(:restart)
end
end
# tempalte kibana service

# for centos 7.x systemd
describe 'kibana::_service' do
before { stub_resources }
let(:chef_run) { ChefSpec::SoloRunner.new(CENTOS7_OPTS).converge(described_recipe) }
let(:template) { chef_run.template('/usr/lib/systemd/system/kibana.service') }
it 'creates an init.d template at /usr/lib/systemd/system/kibana.service' do
expect(chef_run).to create_template('/usr/lib/systemd/system/kibana.service')
end
it 'enables a kibana service' do
expect(chef_run).to enable_service('kibana')
end
it 'sends a restart notification to the service' do
expect(template).to notify('service[kibana]').to(:restart)
it 'enables a kibana service' do
expect(chef_run).to enable_service('kibana')
end

# it 'sends a restart notification to the service' do
# expect(template).to notify('service[kibana]').to(:restart)
# end
end
end
end
end
25 changes: 19 additions & 6 deletions test/unit/spec/apache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@

describe 'kibana::apache' do
before { stub_resources }
supported_platforms.each do |platform, versions|
versions.each do |version|
context "on #{platform.capitalize} #{version}" do
let(:chef_run) do
ChefSpec::ServerRunner.new(platform: platform, version: version) do |node, server|
node_resources(node)
stub_chef_zero(platform, version, server)
end.converge(described_recipe)
end

let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_OPTS).converge(described_recipe) }
# any platform specific data you want available to your test can be loaded here
_property = load_platform_properties(platform: platform, platform_version: version)

# TODO: creates a template /etc/apache2/htpasswd
# TODO: creates a template /etc/apache2/sites-available/kibana.conf

it 'converges successfully' do
expect { :chef_run }.to_not raise_error
# TODO: creates a template /etc/apache2/htpasswd
# TODO: creates a template /etc/apache2/sites-available/kibana.conf

it 'converges successfully' do
expect { :chef_run }.to_not raise_error
end
end
end
end
end
48 changes: 31 additions & 17 deletions test/unit/spec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
# frozen_string_literal: true

require 'chefspec'
require_relative 'spec_helper'

describe 'kibana::default' do
before { stub_resources }
supported_platforms.each do |platform, versions|
versions.each do |version|
context "on #{platform.capitalize} #{version}" do
let(:chef_run) do
ChefSpec::ServerRunner.new(platform: platform, version: version) do |node, server|
node_resources(node)
stub_chef_zero(platform, version, server)
end.converge(described_recipe)
end

let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_OPTS).converge(described_recipe) }
# any platform specific data you want available to your test can be loaded here
_property = load_platform_properties(platform: platform, platform_version: version)

it 'creates kibana group' do
expect(chef_run).to create_group('kibana')
end
it 'creates kibana user' do
expect(chef_run).to create_user('kibana').with(
gid: 'kibana',
home: '/opt/kibana',
shell: '/bin/bash'
)
end
it 'creates kibana base directory' do
expect(chef_run).to create_directory('/opt/kibana').with(
owner: 'kibana',
group: 'kibana'
)
it 'creates kibana group' do
expect(chef_run).to create_group('kibana')
end

it 'creates kibana user' do
expect(chef_run).to create_user('kibana').with(
gid: 'kibana',
home: '/opt/kibana',
shell: '/bin/bash'
)
end

it 'creates kibana base directory' do
expect(chef_run).to create_directory('/opt/kibana').with(
owner: 'kibana',
group: 'kibana'
)
end
end
end
end
end
81 changes: 50 additions & 31 deletions test/unit/spec/kibana3_spec.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
# frozen_string_literal: true

require 'chefspec'
require_relative 'spec_helper'

describe 'kibana::kibana3' do
before { stub_resources }
supported_platforms.each do |platform, versions|
versions.each do |version|
context "on #{platform.capitalize} #{version}" do
let(:chef_run) do
ChefSpec::ServerRunner.new(platform: platform, version: version) do |node, server|
node_resources(node)
stub_chef_zero(platform, version, server)
end.converge(described_recipe)
end

let(:chef_run) { ChefSpec::SoloRunner.new(UBUNTU_OPTS).converge(described_recipe) }
_property = load_platform_properties(platform: platform, platform_version: version)

it 'includes kibana recipe' do
expect(chef_run).to include_recipe('kibana::default')
end
it 'includes kibana recipe' do
expect(chef_run).to include_recipe('kibana::default')
end

it 'installs kibana3 with ark' do
expect(chef_run).to install_ark('kibana')
end
it 'installs kibana3 with ark' do
expect(chef_run).to install_ark('kibana')
end

it 'creates a template for kibana' do
expect(chef_run).to create_template('/opt/kibana/current/config.js').with(
owner: 'kibana',
group: 'kibana',
mode: '0644'
)
it 'creates a template for kibana' do
expect(chef_run).to create_template('/opt/kibana/current/config.js').with(
owner: 'kibana',
group: 'kibana',
mode: '0644'
)
end
end
end
end
end

# if source install is requested test this path
describe 'kibana::kibana3' do
before { stub_resources }
supported_platforms.each do |platform, versions|
versions.each do |version|
context "on #{platform.capitalize} #{version}" do
let(:chef_run) do
ChefSpec::ServerRunner.new(platform: platform, version: version) do |node, server|
node_resources(node)
stub_chef_zero(platform, version, server)
end.converge(described_recipe)
end

let(:chef_run) do
ChefSpec::SoloRunner.new(UBUNTU_OPTS) do |node|
node.override['kibana']['install_method'] = 'source'
end.converge(described_recipe)
end
_property = load_platform_properties(platform: platform, platform_version: version)

it 'installs package git' do
expect(chef_run).to install_package('git')
end
it 'checks out /opt/kibana' do
expect(chef_run).to checkout_git('/opt/kibana/current').with(repository: 'https://github.com/elasticsearch/kibana.git')
end
it 'installs package git' do
expect(chef_run).to install_package('git')
end
it 'checks out /opt/kibana' do
expect(chef_run).to checkout_git('/opt/kibana/current').with(repository: 'https://github.com/elasticsearch/kibana.git')
end

it 'creates a template for kibana' do
expect(chef_run).to create_template('/opt/kibana/current/src/config.js').with(
owner: 'kibana',
group: 'kibana',
mode: '0644'
)
it 'creates a template for kibana' do
expect(chef_run).to create_template('/opt/kibana/current/src/config.js').with(
owner: 'kibana',
group: 'kibana',
mode: '0644'
)
end
end
end
end
end
Loading

0 comments on commit 7f33b02

Please sign in to comment.