Skip to content

Commit

Permalink
Address issues with sensuctl configure and errors
Browse files Browse the repository at this point in the history
Ensure 'sensuctl configure' is re-run if no access or refresh tokens
Ensure that any errors running sensuctl commands like 'sensuctl create' will be reported by Puppet
  • Loading branch information
treydock committed Mar 23, 2023
1 parent f94d482 commit de62f55
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 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 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
}
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
14 changes: 14 additions & 0 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

0 comments on commit de62f55

Please sign in to comment.