Skip to content

Commit

Permalink
Merge pull request #1335 from sensu/fix-configure-errors
Browse files Browse the repository at this point in the history
Address issues with sensuctl configure and errors
  • Loading branch information
treydock authored Mar 24, 2023
2 parents f94d482 + cf7c408 commit 02974a5
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/puppet/provider/sensu_postgres_config/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def self.instances
data.each do |d|
config = {}
config[:ensure] = :present
config[:name] = d['metadata']['name']
config[:name] = d.fetch('metadata', {}).fetch('name', nil)
next if config[:name].nil?
d['spec'].each_pair do |key,value|
if !!value == value
value = value.to_s.to_sym
Expand Down
22 changes: 16 additions & 6 deletions lib/puppet/provider/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def self.sensuctl(args, opts = {})
else
cmd = [sensuctl_cmd] + args
end
opts[:failonfail] = true unless opts.key?(:failonfail)
opts[:combine] = true unless opts.key?(:combine)
execute(cmd, opts)
end
def sensuctl(*args)
Expand All @@ -91,7 +93,7 @@ def self.sensuctl_list(command, namespaces = true)
end
data = []
begin
output = sensuctl(args, {:failonfail => false})
output = sensuctl(args, failonfail: false)
Puppet.debug("sensuctl #{args.join(' ')}: #{output}")
rescue Exception => e
Puppet.notice("Failed to list resources with sensuctl: #{e}")
Expand Down Expand Up @@ -150,14 +152,17 @@ def sensuctl_delete(*args)
end

def self.sensuctl_auth_types()
output = sensuctl(['auth','list','--format','yaml'])
output = sensuctl(['auth','list','--format','yaml'], failonfail: false)
Puppet.debug("YAML auth list: #{output}")
auth_types = {}
auths = output.split('---')
Puppet.debug("auths: #{auths}")
auths.each do |auth|
a = YAML.load(auth)
auth_types[a['metadata']['name']] = a['type']
next if a.nil?
name = a.fetch('metadata', {}).fetch('name', nil)
next if name.nil?
auth_types[name] = a['type']
end
Puppet.debug("auth_types: #{auth_types}")
auth_types
Expand All @@ -166,8 +171,13 @@ def self.sensuctl_auth_types()
def self.dump(resource_type)
# Dump YAML because 'sensuctl dump' does not yet support '--format json'
# https://github.com/sensu/sensu-go/issues/3424
output = sensuctl(['dump',resource_type,'--format','yaml','--all-namespaces'])
Puppet.debug("YAML dump of #{resource_type}:\n#{output}")
begin
output = sensuctl(['dump',resource_type,'--format','yaml','--all-namespaces'], {:failonfail => false})
Puppet.debug("YAML dump of #{resource_type}:\n#{output}")
rescue Exception => e
Puppet.notice("Failed to dump resources with sensuctl: #{e}")
return []
end
resources = []
dumps = output.split('---')
dumps.each do |d|
Expand Down Expand Up @@ -224,7 +234,7 @@ def valid_json?(json)
end

def self.version
output = sensuctl(['version'], {:failonfail => false})
output = sensuctl(['version'], failonfail: false)
version = output[%r{version ([0-9.]+)}, 1]
return version
rescue Exception => e
Expand Down
3 changes: 2 additions & 1 deletion lib/puppet/provider/sensuctl_configure/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
desc "Provider sensuctl_configure using sensuctl"

def exists?
File.file?(config_path)
return false unless File.file?(config_path)
sensuctl_config.key?('access_token') || sensuctl_config.key?('refresh_token')
end

def initialize(value = {})
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/unit/provider/sensuctl_configure/sensuctl/cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"api-url": "https://example.com:8080",
"trusted-ca-file": "/etc/sensu/ssl/ca.crt",
"insecure-skip-tls-verify": false,
"access_token": "baz",
"expires_at": 1679580582,
"refresh_token": "foobar",
"APIKey": "",
"timeout": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"api-url": "https://example.com:8080",
"trusted-ca-file": "/etc/sensu/ssl/ca.crt",
"insecure-skip-tls-verify": false,
"expires_at": 1679580582,
"APIKey": "",
"timeout": 0
}
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_cluster_federation/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('test')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(provider.instances.length).to eq(2)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('https://10.0.0.1:8080 in test')
end
Expand All @@ -30,7 +30,7 @@
expected_spec = {
:api_urls => ['https://10.0.0.1:8080','https://10.0.0.2:8080','https://10.0.0.3:8080'],
}
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(resource.provider).to receive(:sensuctl_create).with('Cluster', expected_metadata, expected_spec, 'federation/v1')
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
Expand All @@ -44,7 +44,7 @@
expected_spec = {
:api_urls => ['https://10.0.0.1:8080','https://10.0.0.2:8080'],
}
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(resource.provider).to receive(:sensuctl_create).with('Cluster', expected_metadata, expected_spec, 'federation/v1')
resource.provider.destroy
property_hash = resource.provider.instance_variable_get("@property_hash")
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_etcd_replicator/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(2)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('role_replicator')
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_postgres_config/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a postres config' do
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('my-postgres')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(3)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('my_vault')
end
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/provider/sensuctl_configure/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@
})
end

describe 'exists?' do
before(:each) do
allow(Dir).to receive(:home).and_return('/root')
end
it 'should exist' do
allow(File).to receive(:file?).with('/root/.config/sensu/sensuctl/cluster').and_return(true)
allow(File).to receive(:read).with('/root/.config/sensu/sensuctl/cluster').and_return(my_fixture_read('cluster'))
expect(resource.provider.exists?).to eq(true)
end
it 'should not exist if no tokens' do
allow(File).to receive(:file?).with('/root/.config/sensu/sensuctl/cluster').and_return(true)
allow(File).to receive(:read).with('/root/.config/sensu/sensuctl/cluster').and_return(my_fixture_read('cluster-dne'))
expect(resource.provider.exists?).to eq(false)
end
it 'should not exist if no file' do
allow(File).to receive(:file?).with('/root/.config/sensu/sensuctl/cluster').and_return(false)
expect(resource.provider.exists?).to eq(false)
end
end

describe 'config_format' do
it 'should have value' do
allow(resource.provider).to receive(:sensuctl).with(['config','view','--format','json']).and_return(my_fixture_read('config_list.json'))
Expand Down
18 changes: 16 additions & 2 deletions spec/unit/provider/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@
end
end

context 'sensuctl' do
it 'should handle a command' do
allow(subject).to receive(:which).with('sensuctl').and_return('/usr/bin/sensuctl')
expect(subject).to receive(:execute).with(['/usr/bin/sensuctl', 'create', '--file', 'foo'], {failonfail: true, combine: true})
subject.sensuctl(['create', '--file', 'foo'])
end

it 'should handle failonfail=false' do
allow(subject).to receive(:which).with('sensuctl').and_return('/usr/bin/sensuctl')
expect(subject).to receive(:execute).with(['/usr/bin/sensuctl', 'create', '--file', 'foo'], {failonfail: false, combine: true})
subject.sensuctl(['create', '--file', 'foo'], failonfail: false)
end
end

context 'sensuctl_create' do
let(:tmp) { Tempfile.new() }
before(:each) do
Expand Down Expand Up @@ -111,14 +125,14 @@

context 'sensuctl_auth_types' do
it 'should return auth and their types' do
allow(subject).to receive(:sensuctl).with(['auth','list','--format','yaml']).and_return(my_fixture_read('auths.txt'))
allow(subject).to receive(:sensuctl).with(['auth','list','--format','yaml'], failonfail: false).and_return(my_fixture_read('auths.txt'))
expect(subject.sensuctl_auth_types).to eq({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
end
end

context 'dump' do
it 'dumps multiple resources' do
allow(subject).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(subject).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
ret = subject.dump('federation/v1.EtcdReplicator')
expect(ret.size).to eq(2)
expect(ret[0]['metadata']['name']).to eq('role_replicator')
Expand Down

0 comments on commit 02974a5

Please sign in to comment.