diff --git a/lib/puppet/type/grafana_ldap_config.rb b/lib/puppet/type/grafana_ldap_config.rb index f007d32..4b54287 100644 --- a/lib/puppet/type/grafana_ldap_config.rb +++ b/lib/puppet/type/grafana_ldap_config.rb @@ -1,184 +1,184 @@ require 'toml' Puppet::Type.newtype(:grafana_ldap_config) do @doc = 'Manage Grafana LDAP configuration' @toml_header = <<-EOF # # Grafana LDAP configuration # # generated by Puppet module puppet-grafana # https://github.com/voxpupuli/puppet-grafana # # *** Edit at your own peril *** # # ############################################# # EOF # currently not ensurable as we are not parsing the LDAP toml config. # ensurable newparam(:title, namevar: true) do desc 'Path to ldap.toml' validate do |value| raise ArgumentError, _('name/title must be a String') unless value.is_a?(String) end end newparam(:owner) do desc 'Owner of the LDAP configuration-file either as String or Integer (default: root)' defaultto 'root' validate do |value| raise ArgumentError, _('owner must be a String or Integer') unless value.is_a?(String) || value.is_a?(Integer) end end newparam(:group) do desc 'Group of the LDAP configuration file either as String or Integer (default: grafana)' defaultto 'grafana' validate do |value| raise ArgumentError, _('group must be a String or Integer') unless value.is_a?(String) || value.is_a?(Integer) end end newparam(:mode) do desc 'File-permissions mode of the LDAP configuration file as String' defaultto '0440' validate do |value| raise ArgumentError, _('file-permissions must be a String') unless value.is_a?(String) raise ArgumentError, _('file-permissions must be a String') if value.empty? # regex-pattern stolen from here - all credis to them! # https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/types/filemode.pp # currently disabled, as it fails when implicitly called. # # raise ArgumentError, _('file-permissions is not valid') unless value.to_s.match(%r{/\A(([0-7]{1,4})|(([ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+)(,([ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+))*))\z/}) end end newparam(:replace, boolean: true, parent: Puppet::Parameter::Boolean) do desc 'Replace existing files' defaultto true end newparam(:backup, boolean: true, parent: Puppet::Parameter::Boolean) do desc 'Backup existing files before replacing them into the file-bucket' defaultto false end newparam(:validate_cmd) do desc 'A command to validate the new Grafana LDAP configuration before actually replacing it' validate do |value| raise ArgumentError, _('validate_cmd must be a String or undef') unless value.nil? || value.is_a?(String) end end def ldap_servers catalog.resources.each_with_object({}) do |resource, memo| next unless resource.is_a?(Puppet::Type.type(:grafana_ldap_server)) next unless resource[:name].is_a?(String) memo[resource[:name]] = resource memo end end def should_content return @generated_config if @generated_config @generated_config = {} ldap_servers.each do |server_k, server_v| # convert symbols to strings server_params = Hash[server_v.original_parameters.map { |k, v| [k.to_s, v] }] server_attributes = server_params['attributes'] server_params.delete('attributes') # grafana-syntax for multiple hosts is a space-separate list. server_params['host'] = server_params['hosts'].join(' ') server_params.delete('hosts') server_group_mappings = server_v.group_mappings server_block = { 'servers' => [server_params], 'servers.attributes' => server_attributes, 'servers.group_mappings' => server_group_mappings }.compact @generated_config[server_k] = server_block end @generated_config.compact end def generate file_opts = {} # currently not ensurable # file_opts = { # ensure: (self[:ensure] == :absent) ? :absent : :file, # } [:name, :owner, :group, :mode, :replace, :backup, # this we have currently not implemented # :selinux_ignore_defaults, # :selrange, # :selrole, # :seltype, # :seluser, # :show_diff, :validate_cmd].each do |param| file_opts[param] = self[param] unless self[param].nil? end metaparams = Puppet::Type.metaparams - excluded_metaparams = ['before', 'notify', 'require', 'subscribe', 'tag'] + excluded_metaparams = %w[before notify require subscribe tag] metaparams.reject! { |param| excluded_metaparams.include? param } metaparams.each do |metaparam| file_opts[metaparam] = self[metaparam] unless self[metaparam].nil? end [Puppet::Type.type(:file).new(file_opts)] end def eval_generate ldap_servers = should_content if !ldap_servers.nil? && !ldap_servers.empty? toml_contents = [] toml_contents << @toml_header toml_contents << ldap_servers.map do |k, v| str = [] str << "\n\n" str << <<-EOF # # #{k} # EOF str << TOML::Generator.new(v).body str.join end catalog.resource("File[#{self[:name]}]")[:content] = toml_contents.join end [catalog.resource("File[#{self[:name]}]")] end autonotify(:class) do 'grafana::service' end end diff --git a/spec/grafana_dashboard_permission_type_spec.rb b/spec/grafana_dashboard_permission_type_spec.rb index 0341a88..5e45622 100644 --- a/spec/grafana_dashboard_permission_type_spec.rb +++ b/spec/grafana_dashboard_permission_type_spec.rb @@ -1,62 +1,60 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard_permission) do let(:gpermission) do described_class.new( title: 'foo_title', grafana_url: 'http://example.com/', grafana_api_path: '/api', user: 'foo_user', dashboard: 'foo_dashboard', permission: 'View', ensure: :present ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new title: 'foo_title', name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new title: 'foo_title', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end it 'fails if both user and team set' do expect do described_class.new title: 'foo title', user: 'foo_user', team: 'foo_team' end.to raise_error(Puppet::Error, %r{Only user or team can be set, not both}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gpermission[:user]).to eq('foo_user') expect(gpermission[:grafana_api_path]).to eq('/api') expect(gpermission[:grafana_url]).to eq('http://example.com/') expect(gpermission[:dashboard]).to eq('foo_dashboard') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gpermission relationship = gpermission.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gpermission.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gpermission expect(gpermission.autorequire).to be_empty end end end diff --git a/spec/grafana_membership_type_spec.rb b/spec/grafana_membership_type_spec.rb index 31ded64..b0ef316 100644 --- a/spec/grafana_membership_type_spec.rb +++ b/spec/grafana_membership_type_spec.rb @@ -1,64 +1,62 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_membership) do let(:gmembership) do described_class.new( title: 'foo_title', user_name: 'foo_user', target_name: 'foo_target', grafana_url: 'http://example.com/', grafana_api_path: '/api', membership_type: 'organization', role: 'Viewer', ensure: :present ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new title: 'foo_title', name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new title: 'foo_title', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end it 'fails if membership type not valid' do expect do described_class.new title: 'foo title', membership_type: 'foo' end.to raise_error(Puppet::Error, %r{Invalid value "foo"}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gmembership[:user_name]).to eq('foo_user') expect(gmembership[:target_name]).to eq('foo_target') expect(gmembership[:grafana_api_path]).to eq('/api') expect(gmembership[:grafana_url]).to eq('http://example.com/') expect(gmembership[:membership_type]).to eq(:organization) end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gmembership relationship = gmembership.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gmembership.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gmembership expect(gmembership.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb b/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb index a2a59fb..68c52a9 100644 --- a/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb +++ b/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb @@ -1,74 +1,73 @@ require 'spec_helper' provider_class = Puppet::Type.type(:grafana_plugin).provider(:grafana_cli) describe provider_class do let(:resource) do Puppet::Type::Grafana_plugin.new( name: 'grafana-wizzle' ) end let(:provider) { provider_class.new(resource) } describe '#instances' do let(:plugins_ls_two) do # rubocop:disable Layout/TrailingWhitespace <<-PLUGINS installed plugins: grafana-simple-json-datasource @ 1.3.4 jdbranham-diagram-panel @ 1.4.0 Restart grafana after installing plugins . PLUGINS # rubocop:enable Layout/TrailingWhitespace end let(:plugins_ls_none) do <<-PLUGINS Restart grafana after installing plugins . PLUGINS end - # rubocop:disable RSpec/MultipleExpectations it 'has the correct names' do allow(provider_class).to receive(:grafana_cli).with('plugins', 'ls').and_return(plugins_ls_two) expect(provider_class.instances.map(&:name)).to match_array(['grafana-simple-json-datasource', 'jdbranham-diagram-panel']) expect(provider_class).to have_received(:grafana_cli) end it 'does not match if there are no plugins' do allow(provider_class).to receive(:grafana_cli).with('plugins', 'ls').and_return(plugins_ls_none) expect(provider_class.instances.size).to eq(0) expect(provider.exists?).to eq(false) expect(provider_class).to have_received(:grafana_cli) end # rubocop:enable RSpec/MultipleExpectations end it '#create' do allow(provider).to receive(:grafana_cli) provider.create expect(provider).to have_received(:grafana_cli).with('plugins', 'install', 'grafana-wizzle') end it '#destroy' do allow(provider).to receive(:grafana_cli) provider.destroy expect(provider).to have_received(:grafana_cli).with('plugins', 'uninstall', 'grafana-wizzle') end describe 'create with repo' do let(:resource) do Puppet::Type::Grafana_plugin.new( name: 'grafana-plugin', repo: 'https://nexus.company.com/grafana/plugins' ) end it '#create with repo' do allow(provider).to receive(:grafana_cli) provider.create expect(provider).to have_received(:grafana_cli).with('--repo https://nexus.company.com/grafana/plugins', 'plugins', 'install', 'grafana-plugin') end end end diff --git a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb index 18f9a88..a59d53e 100644 --- a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb +++ b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb @@ -1,63 +1,61 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard) do let(:gdashboard) do described_class.new name: 'foo', grafana_url: 'http://example.com/', content: '{}', ensure: :present end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if content isn't provided" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', ensure: :present end.to raise_error(Puppet::Error, %r{content is required}) end it "fails if content isn't JSON" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com/', content: '{invalid', ensure: :present end.to raise_error(Puppet::Error, %r{Invalid JSON}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gdashboard[:name]).to eq('foo') expect(gdashboard[:grafana_url]).to eq('http://example.com/') expect(gdashboard[:content]).to eq({}) end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gdashboard relationship = gdashboard.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gdashboard.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gdashboard expect(gdashboard.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_datasource_type_spec.rb b/spec/unit/puppet/type/grafana_datasource_type_spec.rb index 1bad7e8..80f75e3 100644 --- a/spec/unit/puppet/type/grafana_datasource_type_spec.rb +++ b/spec/unit/puppet/type/grafana_datasource_type_spec.rb @@ -1,95 +1,94 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_datasource) do let(:gdatasource) do described_class.new( name: 'foo', grafana_url: 'http://example.com', url: 'http://es.example.com', type: 'elasticsearch', organization: 'test_org', access_mode: 'proxy', is_default: true, basic_auth: true, basic_auth_user: 'user', basic_auth_password: 'password', with_credentials: true, database: 'test_db', user: 'db_user', password: 'db_password', json_data: { esVersion: 5, timeField: '@timestamp', timeInterval: '1m' }, secure_json_data: { password: '5ecretPassw0rd' } ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if json_data isn't valid" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', json_data: 'invalid', ensure: :present end.to raise_error(Puppet::Error, %r{json_data should be a Hash}) end it "fails if secure_json_data isn't valid" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', secure_json_data: 'invalid', ensure: :present end.to raise_error(Puppet::Error, %r{json_data should be a Hash}) end - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gdatasource[:name]).to eq('foo') expect(gdatasource[:grafana_url]).to eq('http://example.com') expect(gdatasource[:url]).to eq('http://es.example.com') expect(gdatasource[:type]).to eq('elasticsearch') expect(gdatasource[:organization]).to eq('test_org') expect(gdatasource[:access_mode]).to eq(:proxy) expect(gdatasource[:is_default]).to eq(:true) expect(gdatasource[:basic_auth]).to eq(:true) expect(gdatasource[:basic_auth_user]).to eq('user') expect(gdatasource[:basic_auth_password]).to eq('password') expect(gdatasource[:with_credentials]).to eq(:true) expect(gdatasource[:database]).to eq('test_db') expect(gdatasource[:user]).to eq('db_user') expect(gdatasource[:password]).to eq('db_password') expect(gdatasource[:json_data]).to eq(esVersion: 5, timeField: '@timestamp', timeInterval: '1m') expect(gdatasource[:secure_json_data]).to eq(password: '5ecretPassw0rd') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gdatasource relationship = gdatasource.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gdatasource.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gdatasource expect(gdatasource.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_folder_type_spec.rb b/spec/unit/puppet/type/grafana_folder_type_spec.rb index bb26999..7d01ac1 100644 --- a/spec/unit/puppet/type/grafana_folder_type_spec.rb +++ b/spec/unit/puppet/type/grafana_folder_type_spec.rb @@ -1,56 +1,54 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_folder) do let(:gfolder) do described_class.new name: 'foo', grafana_url: 'http://example.com/', ensure: :present end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if grafana_api_path isn't properly formed" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gfolder[:name]).to eq('foo') expect(gfolder[:grafana_url]).to eq('http://example.com/') end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gfolder relationship = gfolder.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gfolder.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gfolder expect(gfolder.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_ldap_config_spec.rb b/spec/unit/puppet/type/grafana_ldap_config_spec.rb index e8a32ed..c917c28 100644 --- a/spec/unit/puppet/type/grafana_ldap_config_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_config_spec.rb @@ -1,164 +1,164 @@ # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # require 'spec_helper' - +# rubocop:disable RSpec/VoidExpect describe Puppet::Type.type(:grafana_ldap_config) do # resource title context 'validate resource title' do it 'fails if title is not set' do expect do described_class.new name: nil end.to raise_error(Puppet::Error, %r{Title or name must be provided}) end it 'fails if title is not a string' do expect do described_class.new name: 123 end.to raise_error(Puppet::ResourceError, %r{must be a String}) end end # owner context 'validate owner' do it 'fails if owner is wrong type' do expect do described_class.new name: 'foo_bar', owner: true end.to raise_error(Puppet::ResourceError, %r{must be a String or Integer}) end it 'succeeds if owner is string' do expect do described_class.new name: 'foo_bar', owner: 'foo' end end it 'succeeds if owner is numeric' do expect do described_class.new name: 'foo_bar', owner: 111 end end end # group context 'validate group' do it 'fails if group is wrong type' do expect do described_class.new name: 'foo_bar', group: true end.to raise_error(Puppet::ResourceError, %r{must be a String or Integer}) end it 'succeeds if group is string' do expect do described_class.new name: 'foo_bar', group: 'foo' end end it 'succeeds if group is numeric' do expect do described_class.new name: 'foo_bar', owner: 111 end end end # mode context 'validate mode' do it 'fails if mode is wrong type' do expect do described_class.new name: 'foo_bar', mode: 123 end.to raise_error(Puppet::ResourceError, %r{must be a String}) end it 'fails if mode is empty' do expect do described_class.new name: 'foo_bar', mode: '' end.to raise_error(Puppet::ResourceError, %r{must be a String}) end # currently disabled # it 'fails if mode is invalid' do # expect do # described_class.new name: 'foo_bar', mode: 'abcd' # end.to raise_error(Puppet::ResourceError, %r{is not valid}) # end it 'succeeds if mode is string' do expect do described_class.new name: 'foo_bar', mode: '0755' end end end # replace context 'validate replace' do it 'fails if replace is not a boolean' do expect do described_class.new name: 'foo_bar', replace: 'bla' end.to raise_error(Puppet::ResourceError, %r{Valid values are}) end it 'succeeds if replace' do expect do described_class.new name: 'foo_bar', replace: true end end end # backup context 'validate backup' do it 'fails if backup is not a boolean' do expect do described_class.new name: 'foo_bar', backup: 'bla' end.to raise_error(Puppet::ResourceError, %r{Valid values are}) end it 'succeeds if backup' do expect do described_class.new name: 'foo_bar', backup: true end end end # validate_cmd context 'validate validate_cmd' do it 'fails if validate_cmd is wrong type' do expect do described_class.new name: 'foo_bar', validate_cmd: 123 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if group is string' do expect do described_class.new name: 'foo_bar', validate_cmd: '0755' end end end # ldap_servers context 'validate ldap_servers' do it 'correctly returns the declared LDAP servers' do catalog = Puppet::Resource::Catalog.new server = Puppet::Type.type(:grafana_ldap_server).new( name: 'ldap.example.com', hosts: ['ldap.example.com'], search_base_dns: ['ou=auth'] ) config = Puppet::Type.type(:grafana_ldap_config).new name: 'ldap1' catalog.add_resource server catalog.add_resource config expect(config.ldap_servers.keys).to include('ldap.example.com') end end end diff --git a/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb b/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb index 410be52..81d33b4 100644 --- a/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb @@ -1,151 +1,151 @@ # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # require 'spec_helper' - +# rubocop:disable RSpec/VoidExpect describe Puppet::Type.type(:grafana_ldap_group_mapping) do # resource title context 'validate resource title' do it 'fails if title is not set' do expect do described_class.new name: nil end.to raise_error(Puppet::Error, %r{Title or name must be provided}) end it 'fails if title is empty' do expect do described_class.new name: '' end.to raise_error(RuntimeError, %r{needs to be a non-empty string}) end it 'fails if title is not a string' do expect do described_class.new name: 123 end.to raise_error(Puppet::ResourceError, %r{must be a String}) end end # ldap_server_name context 'validate ldap_server_name' do it 'fails if ldap_server_name is not set' do expect do described_class.new name: 'foo_bar', ldap_server_name: nil, group_dn: 'bar' end.to raise_error(Puppet::Error, %r{Got nil value for}) end it 'fails if ldap_server_name is empty' do expect do described_class.new name: 'foo_bar', ldap_server_name: '', group_dn: 'bar' end.to raise_error(RuntimeError, %r{needs to be a non-empty string}) end it 'fails if ldap_server_name is not a string' do expect do described_class.new name: '123_bar', ldap_server_name: 123, group_dn: 'bar' end.to raise_error(Puppet::ResourceError, %r{must be a String}) end end # group_dn context 'validate group_dn' do it 'fails if group_dn is not set' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: nil end.to raise_error(Puppet::Error, %r{Got nil value for}) end it 'fails if group_dn is empty' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: '' end.to raise_error(RuntimeError, %r{needs to be a non-empty string}) end it 'fails if group_dn is not a string' do expect do described_class.new name: 'foo_123', ldap_server_name: 'foo', group_dn: 123 end.to raise_error(Puppet::ResourceError, %r{must be a String}) end end # org_role context 'validate org_role' do it 'fails if org_role is not set' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: nil end.to raise_error(Puppet::Error, %r{Got nil value for}) end it 'fails if org_role is not a string' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 123 end.to raise_error(Puppet::ResourceError, %r{Valid values are}) end it 'fails if org_role is an unknown role' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'bla' end.to raise_error(Puppet::Error, %r{Valid values are}) end it 'succeeds if all is correct' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Editor' end end end # grafana_admin context 'validate grafana_admin' do it 'fails if org_role is not a boolean' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Admin', grafana_admin: 'bla' end.to raise_error(Puppet::ResourceError, %r{Valid values are}) end it 'succeeds if grafana_admin' do expect do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Admin', grafana_admin: true end end end context 'valid viewer' do let(:group_mapping) do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Viewer', grafana_admin: false end it 'given all parameters' do expect(group_mapping[:org_role]).to eq(:Viewer) end end context 'valid editor' do let(:group_mapping) do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Editor', grafana_admin: false end it 'given all parameters' do expect(group_mapping[:org_role]).to eq(:Editor) end end context 'valid admin' do let(:group_mapping) do described_class.new name: 'foo_bar', ldap_server_name: 'foo', group_dn: 'bar', org_role: 'Admin', grafana_admin: true end it 'given all parameters' do expect(group_mapping[:org_role]).to eq(:Admin) end end end diff --git a/spec/unit/puppet/type/grafana_ldap_server_spec.rb b/spec/unit/puppet/type/grafana_ldap_server_spec.rb index 0706c0d..8cff019 100644 --- a/spec/unit/puppet/type/grafana_ldap_server_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_server_spec.rb @@ -1,337 +1,337 @@ # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # require 'spec_helper' - +# rubocop:disable RSpec/VoidExpect describe Puppet::Type.type(:grafana_ldap_server) do # resource title context 'validate resource title' do it 'fails if title is not set' do expect do described_class.new name: nil end.to raise_error(Puppet::Error, %r{Title or name must be provided}) end it 'fails if title is empty' do expect do described_class.new name: '' end.to raise_error(RuntimeError, %r{must not be empty}) end it 'fails if title is not a string' do expect do described_class.new name: 123 end.to raise_error(Puppet::ResourceError, %r{must be a String}) end end # hosts context 'validate hosts' do it 'fails if hosts is not set' do expect do described_class.new name: 'server1', hosts: nil end.to raise_error(Puppet::Error, %r{Got nil value for}) end it 'fails if hosts is not an array' do expect do described_class.new name: 'server1', hosts: '' end.to raise_error(RuntimeError, %r{must be an Array}) end it 'fails if hosts is empty' do expect do described_class.new name: 'server1', hosts: [] end.to raise_error(RuntimeError, %r{must not be empty}) end end # port context 'validate port' do it 'fails if port is empty' do expect do described_class.new name: 'server1', hosts: ['server1'], port: 0 end.to raise_error(RuntimeError, %r{must be an Integer within}) end it 'fails if port is a string' do expect do described_class.new name: 'server1', hosts: ['server1'], port: '123' end.to raise_error(Puppet::ResourceError, %r{must be an Integer within}) end it 'fails if port is greater than 65535' do expect do described_class.new name: 'server1', hosts: ['server1'], port: 123_456 end.to raise_error(Puppet::ResourceError, %r{must be an Integer within}) end end # use_ssl context 'validate use_ssl' do it 'fails if use_ssl is not boolean' do expect do described_class.new name: 'server1', hosts: ['server1'], use_ssl: 'foobar' end.to raise_error(Puppet::Error, %r{Valid values are true}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], use_ssl: true end end end # start_tls context 'validate start_tls' do it 'fails if start_tls is not boolean' do expect do described_class.new name: 'server1', hosts: ['server1'], start_tls: 'foobar' end.to raise_error(Puppet::Error, %r{Valid values are true}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], start_tls: true end end end # ssl_skip_verify context 'validate ssl_skip_verify' do it 'fails if ssl_skip_verify is not boolean' do expect do described_class.new name: 'server1', hosts: ['server1'], ssl_skip_verify: 'foobar' end.to raise_error(Puppet::Error, %r{Valid values are true}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], ssl_skip_verify: true end end end # root_ca_cert context 'validate root_ca_cert' do it 'fails if root_ca_cert is empty' do expect do described_class.new name: 'server1', hosts: ['server1'], root_ca_cert: '' end.to raise_error(RuntimeError, %r{must be set when SSL}) end it 'fails if root_ca_cert is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], root_ca_cert: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], root_ca_cert: '/etc/ssl/certs/ca-certificate.crt' end end end # client_cert context 'validate client_cert' do it 'fails if client_cert is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], client_cert: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], client_cert: '/etc/ssl/host.crt' end end end # client_key context 'validate client_key' do it 'fails if client_key is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], client_key: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], client_key: '/etc/ssl/certs/ca-certificate.crt' end end end # bind_dn context 'validate bind_dn' do it 'fails if bind_dn is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], bind_dn: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], bind_dn: 'cn=Admin', search_base_dns: ['ou=users'] end end end # bind_password context 'validate bind_password' do it 'fails if bind_password is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], bind_password: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], bind_password: 'foobar', search_base_dns: ['ou=users'] end end end # search_filter context 'validate search_filter' do it 'fails if search_filter is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], search_filter: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], search_filter: 'uid=%u', search_base_dns: ['ou=users'] end end end # search_base_dns context 'validate search_base_dns' do it 'fails if search_base_dns is not an array' do expect do described_class.new name: 'server1', hosts: ['server1'], search_base_dns: 12_345 end.to raise_error(Puppet::Error, %r{must be an Array}) end it 'fails if search_base_dns array members are not strings' do expect do described_class.new name: 'server1', hosts: ['server1'], search_base_dns: [12_345] end.to raise_error(Puppet::Error, %r{must be a String}) end it 'fails if search_base_dns array is empty' do expect do described_class.new name: 'server1', hosts: ['server1'], search_base_dns: [] end.to raise_error(RuntimeError, %r{needs to contain at least one LDAP base-dn}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], search_base_dns: ['ou=users'] end end end # group_search_filter context 'validate group_search_filter' do it 'fails if group_search_filter is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_filter: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_filter: 'cn=adminsgroup', search_base_dns: ['ou=users'] end end end # group_search_filter_user_attribute context 'validate group_search_filter_user_attribute' do it 'fails if group_search_filter_user_attribute is not a string' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_filter_user_attribute: 12_345 end.to raise_error(Puppet::Error, %r{must be a String}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_filter_user_attribute: 'dn', search_base_dns: ['ou=users'] end end end # group_search_base_dns context 'validate group_search_base_dns' do it 'fails if group_search_base_dns is not an array' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_base_dns: 12_345 end.to raise_error(Puppet::Error, %r{must be an Array}) end it 'fails if group_search_base_dns array members are not strings' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_base_dns: [12_345] end.to raise_error(Puppet::Error, %r{must be a String}) end it 'fails if group_search_base_dns array is empty' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_base_dns: [], search_base_dns: ['ou=auth'] end.to raise_error(RuntimeError, %r{needs to contain at least one LDAP base-dn}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], group_search_base_dns: ['ou=users'] end end end # attributes context 'validate attributes' do it 'fails if attributes is not a hash' do expect do described_class.new name: 'server1', hosts: ['server1'], attributes: [12_345] end.to raise_error(Puppet::Error, %r{must be a Hash}) end it 'fails if unknown attribute' do expect do described_class.new name: 'server1', hosts: ['server1'], attributes: { 'foo' => 'bar' } end.to raise_error(Puppet::Error, %r{contains an unknown key}) end it 'fails if wrong key type' do expect do described_class.new name: 'server1', hosts: ['server1'], attributes: { 12_345 => 'bar' } end.to raise_error(Puppet::Error, %r{must be Strings}) end it 'fails if wrong value type' do expect do described_class.new name: 'server1', hosts: ['server1'], attributes: { 'surname' => {} } end.to raise_error(Puppet::Error, %r{must be Strings}) end it 'succeeds if all is correct' do expect do described_class.new name: 'server1', hosts: ['server1'], attributes: { 'username' => 'uid' } end end end end diff --git a/spec/unit/puppet/type/grafana_notification_type_spec.rb b/spec/unit/puppet/type/grafana_notification_type_spec.rb index d85489b..e1b9953 100644 --- a/spec/unit/puppet/type/grafana_notification_type_spec.rb +++ b/spec/unit/puppet/type/grafana_notification_type_spec.rb @@ -1,72 +1,70 @@ # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_notification) do let(:gnotification) do described_class.new( name: 'foo', grafana_url: 'http://example.com', type: 'email', is_default: true, send_reminder: true, frequency: '20m', settings: { adresses: 'test@example.com' } ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}' end.to raise_error(Puppet::Error, %r{not a valid URL}) end it "fails if settings isn't valid" do expect do described_class.new name: 'foo', grafana_url: 'http://example.com', settings: 'invalid' end.to raise_error(Puppet::Error, %r{settings should be a Hash}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gnotification[:name]).to eq('foo') expect(gnotification[:grafana_url]).to eq('http://example.com') expect(gnotification[:type]).to eq('email') expect(gnotification[:is_default]).to eq(:true) expect(gnotification[:send_reminder]).to eq(:true) expect(gnotification[:frequency]).to eq('20m') expect(gnotification[:settings]).to eq(adresses: 'test@example.com') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gnotification relationship = gnotification.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gnotification.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gnotification expect(gnotification.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_organization_type_spec.rb b/spec/unit/puppet/type/grafana_organization_type_spec.rb index 522ec0e..9fb7dd6 100644 --- a/spec/unit/puppet/type/grafana_organization_type_spec.rb +++ b/spec/unit/puppet/type/grafana_organization_type_spec.rb @@ -1,55 +1,53 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_organization) do let(:gorganization) do described_class.new( name: 'foo', grafana_url: 'http://example.com', grafana_user: 'admin', grafana_password: 'admin', address: { address1: 'test address1', address2: 'test address2', city: 'CityName', state: 'NewState', zipcode: '12345', country: 'USA' } ) end context 'when setting parameters' do it "fails if json_data isn't valid" do expect do described_class.new name: 'foo', address: 'invalid address' end.to raise_error(Puppet::Error, %r{address should be a Hash!}) end it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gorganization[:name]).to eq('foo') expect(gorganization[:grafana_user]).to eq('admin') expect(gorganization[:grafana_password]).to eq('admin') expect(gorganization[:grafana_url]).to eq('http://example.com') expect(gorganization[:address]).to eq(address1: 'test address1', address2: 'test address2', city: 'CityName', state: 'NewState', zipcode: '12345', country: 'USA') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gorganization relationship = gorganization.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gorganization.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gorganization expect(gorganization.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_team_type_spec.rb b/spec/unit/puppet/type/grafana_team_type_spec.rb index 0f01c1b..f2e2f77 100644 --- a/spec/unit/puppet/type/grafana_team_type_spec.rb +++ b/spec/unit/puppet/type/grafana_team_type_spec.rb @@ -1,51 +1,49 @@ require 'spec_helper' describe Puppet::Type.type(:grafana_team) do let(:gteam) do described_class.new( name: 'foo', grafana_url: 'http://example.com', grafana_user: 'admin', grafana_password: 'admin', home_dashboard: 'foo_dashboard', organization: 'foo_organization' ) end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(gteam[:name]).to eq('foo') expect(gteam[:grafana_user]).to eq('admin') expect(gteam[:grafana_password]).to eq('admin') expect(gteam[:grafana_url]).to eq('http://example.com') expect(gteam[:home_dashboard]).to eq('foo_dashboard') expect(gteam[:organization]).to eq('foo_organization') end # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource gteam relationship = gteam.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == gteam.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gteam expect(gteam.autorequire).to be_empty end end end diff --git a/spec/unit/puppet/type/grafana_user_type_spec.rb b/spec/unit/puppet/type/grafana_user_type_spec.rb index f8f63b9..0e0f0a7 100644 --- a/spec/unit/puppet/type/grafana_user_type_spec.rb +++ b/spec/unit/puppet/type/grafana_user_type_spec.rb @@ -1,50 +1,48 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'spec_helper' describe Puppet::Type.type(:grafana_user) do let(:guser) do described_class.new name: 'test', full_name: 'Mr tester', password: 't3st', grafana_url: 'http://example.com/' end context 'when setting parameters' do it "fails if grafana_url isn't HTTP-based" do expect do described_class.new name: 'test', grafana_url: 'example.com' end.to raise_error(Puppet::Error, %r{not a valid URL}) end - - # rubocop:disable RSpec/MultipleExpectations it 'accepts valid parameters' do expect(guser[:name]).to eq('test') expect(guser[:full_name]).to eq('Mr tester') expect(guser[:password]).to eq('t3st') expect(guser[:grafana_url]).to eq('http://example.com/') end it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') catalog.add_resource service catalog.add_resource guser relationship = guser.autorequire.find do |rel| (rel.source.to_s == 'Service[grafana-server]') && (rel.target.to_s == guser.to_s) end expect(relationship).to be_a Puppet::Relationship end it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource guser expect(guser.autorequire).to be_empty end end end