Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(POOLER-138) Support multiple pools per alias #314

Merged
merged 1 commit into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
cache: bundler
sudo: false
language: ruby
services:
- redis-server

matrix:
include:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ git logs & PR history.

### Fixed
- Improve support for configuration via environment variables (POOLER-137)
- Support multiple pool backends per alias (POOLER-138)
- Remove redis server testing requirement

# [0.3.0](/~https://github.com/puppetlabs/vmpooler/compare/0.2.2...0.3.0)

Expand Down
7 changes: 4 additions & 3 deletions lib/vmpooler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def self.config(filepath = 'vmpooler.yaml')
parsed_config[:pool_names] << pool['name']
if pool['alias']
if pool['alias'].is_a?(Array)
pool['alias'].each do |a|
pool['alias'].each do |pool_alias|
parsed_config[:alias] ||= {}
parsed_config[:alias][a] = pool['name']
parsed_config[:pool_names] << a
parsed_config[:alias][pool_alias] = [pool['name']] unless parsed_config[:alias].key? pool_alias
parsed_config[:alias][pool_alias] << pool['name'] unless parsed_config[:alias][pool_alias].include? pool['name']
parsed_config[:pool_names] << pool_alias
end
elsif pool['alias'].is_a?(String)
parsed_config[:alias][pool['alias']] = pool['name']
Expand Down
8 changes: 5 additions & 3 deletions lib/vmpooler/api/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def fetch_single_vm(template)
template_backends = [template]
aliases = Vmpooler::API.settings.config[:alias]
if aliases
template_backends << aliases[template] if aliases[template]

template_backends = template_backends + aliases[template] if aliases[template].is_a?(Array)
template_backends << aliases[template] if aliases[template].is_a?(String)
pool_index = pool_index(pools)
weighted_pools = {}
template_backends.each do |t|
Expand All @@ -61,7 +61,9 @@ def fetch_single_vm(template)
template_backends.delete(selection)
template_backends.unshift(selection)
else
template_backends = template_backends.sample(template_backends.count)
first = template_backends.sample
template_backends.delete(first)
template_backends.unshift(first)
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'mock_redis'

def redis
unless @redis
@redis = Redis.new
@redis.select(15) # let's use the highest numbered database available in a default install
@redis = MockRedis.new
end
@redis
end
Expand Down
2 changes: 0 additions & 2 deletions spec/integration/api/v1/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def app()
let(:current_time) { Time.now }

before(:each) do
redis.flushdb

app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :metrics, metrics
Expand Down
2 changes: 0 additions & 2 deletions spec/integration/api/v1/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def app()
let(:current_time) { Time.now }

before(:each) do
redis.flushdb

app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :config, auth: false
Expand Down
2 changes: 0 additions & 2 deletions spec/integration/api/v1/vm_hostname_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def app()
let(:current_time) { Time.now }

before(:each) do
redis.flushdb

app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :config, auth: false
Expand Down
33 changes: 27 additions & 6 deletions spec/integration/api/v1/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ def app()
},
pools: [
{'name' => 'pool1', 'size' => 5},
{'name' => 'pool2', 'size' => 10}
{'name' => 'pool2', 'size' => 10},
{'name' => 'pool3', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
alias: { 'poolone' => ['pool1'] },
pool_names: [ 'pool1', 'pool2', 'pool3', 'poolone', 'genericpool' ]
}
}

let(:current_time) { Time.now }

before(:each) do
redis.flushdb

app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :metrics, metrics
Expand Down Expand Up @@ -192,6 +190,29 @@ def app()
expect_json(ok = true, http = 200)
end

it 'returns VMs from multiple backend pools requested by an alias' do
Vmpooler::API.settings.config[:alias]['genericpool'] = ['pool1', 'pool2', 'pool3']

create_ready_vm 'pool1', '1abcdefghijklmnop'
create_ready_vm 'pool2', '2abcdefghijklmnop'
create_ready_vm 'pool3', '1qrstuvwxyz012345'

post "#{prefix}/vm", '{"genericpool":"3"}'

expected = {
ok: true,
genericpool: {
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop', '1qrstuvwxyz012345' ]
}
}

result = JSON.parse(last_response.body)
expect(result['ok']).to eq(true)
expect(result['genericpool']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop', '1qrstuvwxyz012345')

expect_json(ok = true, http = 200)
end

it 'fails when not all requested vms can be allocated' do
create_ready_vm 'pool1', '1abcdefghijklmnop'

Expand Down
2 changes: 0 additions & 2 deletions spec/integration/api/v1/vm_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def app()
let(:current_time) { Time.now }

before(:each) do
redis.flushdb

app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :metrics, metrics
Expand Down
4 changes: 0 additions & 4 deletions spec/integration/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ def app()

describe 'Dashboard' do

before(:each) do
redis.flushdb
end

context '/' do
before { get '/' }

Expand Down