Skip to content
Permalink
Browse files

Merge pull request #1143 from sensu/tests

Replace unit test instance variables with let
  • Loading branch information...
ghoneycutt committed Sep 9, 2019
2 parents 517f0ac + 75dd8e5 commit 7856063a45a7ed285a5b95be325eb838d8c9197b
@@ -1,10 +1,10 @@
require 'spec_helper'

describe Puppet::Type.type(:sensu_ad_auth).provider(:sensuctl) do
before(:each) do
@provider = described_class
@type = Puppet::Type.type(:sensu_ad_auth)
@resource = @type.new({
let(:provider) { described_class }
let(:type) { Puppet::Type.type(:sensu_ad_auth) }
let(:resource) do
type.new({
:name => 'test',
:servers => [{'host' => 'test', 'port' => 389}],
:server_binding => {'test' => {'user_dn' => 'cn=foo','password' => 'foo'}},
@@ -15,15 +15,15 @@

describe 'self.instances' do
it 'should create instances' do
allow(@provider).to receive(:sensuctl_auth_types).and_return({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
allow(@provider).to receive(:sensuctl_list).with('auth', false).and_return(JSON.parse(my_fixture_read('list.json')))
expect(@provider.instances.length).to eq(2)
allow(provider).to receive(:sensuctl_auth_types).and_return({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
allow(provider).to receive(:sensuctl_list).with('auth', false).and_return(JSON.parse(my_fixture_read('list.json')))
expect(provider.instances.length).to eq(2)
end

it 'should return the resource for a auth' do
allow(@provider).to receive(:sensuctl_auth_types).and_return({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
allow(@provider).to receive(:sensuctl_list).with('auth', false).and_return(JSON.parse(my_fixture_read('list.json')))
property_hash = @provider.instances[0].instance_variable_get("@property_hash")
allow(provider).to receive(:sensuctl_auth_types).and_return({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
allow(provider).to receive(:sensuctl_list).with('auth', false).and_return(JSON.parse(my_fixture_read('list.json')))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('activedirectory')
end
end
@@ -49,9 +49,9 @@
'user_search' => {'base_dn' => 'ou=People','attribute' => 'sAMAccountName','name_attribute' => 'displayName','object_class' => 'person'},
}]
}
expect(@resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
@resource.provider.create
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash[:ensure]).to eq(:present)
end
it 'should create an auth without binding' do
@@ -73,15 +73,15 @@
'user_search' => {'base_dn' => 'ou=People','attribute' => 'sAMAccountName','name_attribute' => 'displayName','object_class' => 'person'},
}]
}
@resource = @type.new({
resource = type.new({
:name => 'test',
:servers => [{'host' => 'test', 'port' => 389}],
:server_group_search => {'test' => {'base_dn' => 'ou=Groups'}},
:server_user_search => {'test' => {'base_dn' => 'ou=People'}},
})
expect(@resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
@resource.provider.create
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash[:ensure]).to eq(:present)
end
end
@@ -109,17 +109,17 @@
:groups_prefix => nil,
:username_prefix => nil,
}
expect(@resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
@resource.provider.server_binding = {'test' => {'user_dn' => 'cn=foo', 'password' => 'bar'}}
@resource.provider.flush
expect(resource.provider).to receive(:sensuctl_create).with('ad', expected_metadata, expected_spec, 'authentication/v2')
resource.provider.server_binding = {'test' => {'user_dn' => 'cn=foo', 'password' => 'bar'}}
resource.provider.flush
end
end

describe 'destroy' do
it 'should delete an auth' do
expect(@resource.provider).to receive(:sensuctl_delete).with('auth', 'test')
@resource.provider.destroy
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_delete).with('auth', 'test')
resource.provider.destroy
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash).to eq({})
end
end
@@ -1,10 +1,10 @@
require 'spec_helper'

describe Puppet::Type.type(:sensu_asset).provider(:sensuctl) do
before(:each) do
@provider = described_class
@type = Puppet::Type.type(:sensu_asset)
@resource = @type.new({
let(:provider) { described_class }
let(:type) { Puppet::Type.type(:sensu_asset) }
let(:resource) do
type.new({
:name => 'test',
:url => 'http://127.0.0.1',
:sha512 => '4f926bf4328fbad2b9cac873d117f771914f4b837c9c85584c38ccf55a3ef3c2e8d154812246e5dda4a87450576b2c58ad9ab40c9e2edc31b288d066b195b21b'
@@ -13,20 +13,20 @@

describe 'self.instances' do
it 'should create instances' do
allow(@provider).to receive(:sensuctl_list).with('asset').and_return(JSON.parse(my_fixture_read('asset_list.json')))
expect(@provider.instances.length).to eq(1)
allow(provider).to receive(:sensuctl_list).with('asset').and_return(JSON.parse(my_fixture_read('asset_list.json')))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a asset' do
allow(@provider).to receive(:sensuctl_list).with('asset').and_return(JSON.parse(my_fixture_read('asset_list.json')))
property_hash = @provider.instances[0].instance_variable_get("@property_hash")
allow(provider).to receive(:sensuctl_list).with('asset').and_return(JSON.parse(my_fixture_read('asset_list.json')))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('check-cpu.sh in default')
end
end

describe 'create' do
it 'should create a asset' do
@resource[:filters] = ["entity.system.os == 'linux'"]
resource[:filters] = ["entity.system.os == 'linux'"]
expected_metadata = {
:name => 'test',
:namespace => 'default',
@@ -36,16 +36,16 @@
:sha512 => '4f926bf4328fbad2b9cac873d117f771914f4b837c9c85584c38ccf55a3ef3c2e8d154812246e5dda4a87450576b2c58ad9ab40c9e2edc31b288d066b195b21b',
:filters => ["entity.system.os == 'linux'"],
}
expect(@resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
@resource.provider.create
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash[:ensure]).to eq(:present)
end
end

describe 'flush' do
it 'should update a asset filters' do
@resource[:filters] = ["entity.system.os == 'linux'"]
resource[:filters] = ["entity.system.os == 'linux'"]
expected_metadata = {
:name => 'test',
:namespace => 'default',
@@ -55,12 +55,12 @@
:sha512 => '4f926bf4328fbad2b9cac873d117f771914f4b837c9c85584c38ccf55a3ef3c2e8d154812246e5dda4a87450576b2c58ad9ab40c9e2edc31b288d066b195b21b',
:filters => ["entity.system.os == 'windows'"],
}
expect(@resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
@resource.provider.filters = ["entity.system.os == 'windows'"]
@resource.provider.flush
expect(resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
resource.provider.filters = ["entity.system.os == 'windows'"]
resource.provider.flush
end
it 'should remove filters' do
@resource[:filters] = ["entity.system.os == 'linux'"]
resource[:filters] = ["entity.system.os == 'linux'"]
expected_metadata = {
:name => 'test',
:namespace => 'default',
@@ -70,17 +70,17 @@
:sha512 => '4f926bf4328fbad2b9cac873d117f771914f4b837c9c85584c38ccf55a3ef3c2e8d154812246e5dda4a87450576b2c58ad9ab40c9e2edc31b288d066b195b21b',
:filters => nil,
}
expect(@resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
@resource.provider.filters = :absent
@resource.provider.flush
expect(resource.provider).to receive(:sensuctl_create).with('Asset', expected_metadata, expected_spec)
resource.provider.filters = :absent
resource.provider.flush
end
end

describe 'destroy' do
it 'should delete a asset' do
expect(@resource.provider).to receive(:sensuctl_delete).with('asset', 'test', 'default')
@resource.provider.destroy
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_delete).with('asset', 'test', 'default')
resource.provider.destroy
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash).to eq({})
end
end
@@ -1,10 +1,10 @@
require 'spec_helper'

describe Puppet::Type.type(:sensu_check).provider(:sensuctl) do
before(:each) do
@provider = described_class
@type = Puppet::Type.type(:sensu_check)
@resource = @type.new({
let(:provider) { described_class }
let(:type) { Puppet::Type.type(:sensu_check) }
let(:resource) do
type.new({
:name => 'test',
:command => 'foobar',
:subscriptions => ['demo'],
@@ -15,24 +15,24 @@

describe 'self.instances' do
it 'should create instances' do
allow(@provider).to receive(:sensuctl_list).with('check').and_return(JSON.parse(my_fixture_read('check_list.json')))
expect(@provider.instances.length).to eq(1)
allow(provider).to receive(:sensuctl_list).with('check').and_return(JSON.parse(my_fixture_read('check_list.json')))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a check' do
allow(@provider).to receive(:sensuctl_list).with('check').and_return(JSON.parse(my_fixture_read('check_list.json')))
property_hash = @provider.instances[0].instance_variable_get("@property_hash")
allow(provider).to receive(:sensuctl_list).with('check').and_return(JSON.parse(my_fixture_read('check_list.json')))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('check-cpu in default')
end
end

describe 'create' do
it 'should create a check' do
@resource[:command] = 'check_ntp'
@resource[:handlers] = ['email', 'slack']
@resource[:stdin] = true
@resource[:publish] = false
@resource[:proxy_requests_entity_attributes] = ["entity.Class == 'proxy'"]
resource[:command] = 'check_ntp'
resource[:handlers] = ['email', 'slack']
resource[:stdin] = true
resource[:publish] = false
resource[:proxy_requests_entity_attributes] = ["entity.Class == 'proxy'"]
expected_metadata = {
:name => 'test',
:namespace => 'default',
@@ -46,16 +46,16 @@
:publish => false,
:proxy_requests => { :entity_attributes => ["entity.Class == 'proxy'"] }
}
expect(@resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
@resource.provider.create
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash[:ensure]).to eq(:present)
end
end

describe 'flush' do
it 'should update a check proxy_requests' do
@resource[:proxy_requests_splay] = true
resource[:proxy_requests_splay] = true
expected_metadata = {
:name => 'test',
:namespace => 'default',
@@ -69,9 +69,9 @@
:handlers => ['slack'],
:proxy_requests => { :splay => true, :entity_attributes => ["entity.Class == 'proxy'"] }
}
expect(@resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
@resource.provider.proxy_requests_entity_attributes = ["entity.Class == 'proxy'"]
@resource.provider.flush
expect(resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
resource.provider.proxy_requests_entity_attributes = ["entity.Class == 'proxy'"]
resource.provider.flush
end
it 'should update a check' do
expected_metadata = {
@@ -86,9 +86,9 @@
:handlers => ['slack'],
:interval => 20
}
expect(@resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
@resource.provider.interval = 20
@resource.provider.flush
expect(resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
resource.provider.interval = 20
resource.provider.flush
end
it 'should remove ttl' do
expected_metadata = {
@@ -104,18 +104,18 @@
:handlers => ['slack'],
:ttl => nil,
}
@resource[:ttl] = 120
expect(@resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
@resource.provider.ttl = :absent
@resource.provider.flush
resource[:ttl] = 120
expect(resource.provider).to receive(:sensuctl_create).with('CheckConfig', expected_metadata, expected_spec)
resource.provider.ttl = :absent
resource.provider.flush
end
end

describe 'destroy' do
it 'should delete a check' do
expect(@resource.provider).to receive(:sensuctl_delete).with('check', 'test', 'default')
@resource.provider.destroy
property_hash = @resource.provider.instance_variable_get("@property_hash")
expect(resource.provider).to receive(:sensuctl_delete).with('check', 'test', 'default')
resource.provider.destroy
property_hash = resource.provider.instance_variable_get("@property_hash")
expect(property_hash).to eq({})
end
end

0 comments on commit 7856063

Please sign in to comment.
You can’t perform that action at this time.