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

Commit

Permalink
Get new Cobbler token if old is unvalid
Browse files Browse the repository at this point in the history
Get token for every call to prevent token expiration

Closes-Bug: #1256006

Change-Id: Ibde35849837d8c724ee73379a6fd5f7084dde0bf
  • Loading branch information
Vladimir committed Dec 6, 2013
1 parent 624d841 commit 3055cb1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
13 changes: 8 additions & 5 deletions lib/astute/cobbler.rb
Expand Up @@ -21,7 +21,7 @@ class CobblerError < RuntimeError; end

class Cobbler

attr_reader :remote, :token
attr_reader :remote

def initialize(o={})
Astute.logger.debug("Cobbler options: #{o.inspect}")
Expand All @@ -35,13 +35,16 @@ def initialize(o={})
port = o['port'] || '80'
path = o['path'] || '/cobbler_api'
end
username = o['username'] || 'cobbler'
password = o['password'] || 'cobbler'
@username = o['username'] || 'cobbler'
@password = o['password'] || 'cobbler'

Astute.logger.debug("Connecting to cobbler with: host: #{host} port: #{port} path: #{path}")
@remote = XMLRPC::Client.new(host, path, port)
Astute.logger.debug("Trying to log in to cobbler with username: #{username}, password: #{password}")
@token = remote.call('login', username, password)
Astute.logger.debug("Cobbler initialize with username: #{@username}, password: #{@password}")
end

def token
remote.call('login', @username, @password)
end

def item_from_hash(what, name, data, opts = {})
Expand Down
20 changes: 14 additions & 6 deletions spec/unit/cobbler_spec.rb
Expand Up @@ -29,9 +29,7 @@
username = 'user'
password = 'pass'

remote = mock() do
expects(:call).with('login', username, password)
end
remote = mock()
tmp = XMLRPC::Client
XMLRPC::Client = mock() do
expects(:new).with(host, path, port).returns(remote)
Expand All @@ -50,9 +48,7 @@
host = "host.domain.tld"
path = "/api"
port = "1234"
remote = mock() do
expects(:call).with('login', username, password)
end
remote = mock()
tmp = XMLRPC::Client
XMLRPC::Client = mock() do
expects(:new).with(host, path, port).returns(remote)
Expand Down Expand Up @@ -177,6 +173,18 @@
cobbler.item_from_hash('system', 'name', data, :item_preremove => true)
end

it 'should generate token in every cobbler call where token need' do
remote = mock() do
stubs(:call).twice.with('sync', 'remotetoken')
expects(:call).twice.with('login', 'cobbler', 'cobbler').returns('remotetoken')
end
XMLRPC::Client = mock() do
stubs(:new).returns(remote)
end
cobbler = Astute::Provision::Cobbler.new
cobbler.sync
cobbler.sync
end
end
end

Expand Down

0 comments on commit 3055cb1

Please sign in to comment.