Skip to content

Commit

Permalink
Fix infite loop in server name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed May 30, 2014
1 parent 541374d commit 8369413
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Bug Fixes

* PR [#50][] - Fix possible infinite loop when generating server names
* PR [#49][] - Limit server names to 63 characters to get around OpenSSH bug
[2239](https://bugzilla.mindrot.org/show_bug.cgi?id=2239)

Expand Down Expand Up @@ -100,6 +101,7 @@ certain specified NICs; via [@monsterzz][]

* Initial release! Woo!

[#50]: https://github.com/test-kitchen/kitchen-openstack/pull/50
[#49]: https://github.com/test-kitchen/kitchen-openstack/pull/49
[#48]: https://github.com/test-kitchen/kitchen-openstack/pull/48
[#46]: https://github.com/test-kitchen/kitchen-openstack/pull/46
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
@@ -1,6 +1,6 @@
# Encoding: UTF-8

source 'https://rubygems.org'

# Specify your gem's dependencies in kitchen-openstack.gemspec
gemspec

# vim: ai et ts=2 sts=2 sw=2 ft=ruby
9 changes: 3 additions & 6 deletions Rakefile
@@ -1,12 +1,12 @@
require 'bundler/gem_tasks'
# Encoding: UTF-8

require 'bundler/setup'
require 'tailor/rake_task'
require 'cane/rake_task'
require 'rspec/core/rake_task'

desc 'Run Cane to check quality metrics'
Cane::RakeTask.new

desc 'Run Tailor to lint check code'
Tailor::RakeTask.new do |task|
task.file_set '**/*.rb'
end
Expand All @@ -17,9 +17,6 @@ task :loc do
sh 'countloc -r lib/kitchen'
end

desc 'Run RSpec unit tests'
RSpec::Core::RakeTask.new(:spec)

task :default => [ :cane, :tailor, :loc, :spec ]

# vim: ai et ts=2 sts=2 sw=2 ft=ruby fdm=marker
5 changes: 2 additions & 3 deletions kitchen-openstack.gemspec
@@ -1,4 +1,5 @@
# coding: utf-8
# Encoding: UTF-8

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'kitchen/driver/openstack_version'
Expand Down Expand Up @@ -34,5 +35,3 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'simplecov-console'
spec.add_development_dependency 'coveralls'
end

# vim: ai et ts=2 sts=2 sw=2 ft=ruby
21 changes: 13 additions & 8 deletions lib/kitchen/driver/openstack.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# Encoding: UTF-8
#
# Author:: Jonathan Hartman (<j@p4nt5.com>)
#
Expand Down Expand Up @@ -191,20 +191,27 @@ def find_network(network_ref)
end

def generate_name(base)
# Generate what should be a unique server name
# Generate what should be a unique server name up to 63 total chars
# Base name: 15
# Username: 15
# Hostname: 23
# Random string: 7
# Separators: 3
# ================
# Total: 63
sep = '-'
pieces = [
base,
Etc.getlogin,
Socket.gethostname,
Array.new(8) { rand(36).to_s(36) }.join
Array.new(7) { rand(36).to_s(36) }.join
]
until pieces.join(sep).length <= 63 do
if pieces[2].length > 24
if pieces[2].length > 23
pieces[2] = pieces[2][0..-2]
elsif pieces[1].length > 16
elsif pieces[1].length > 15
pieces[1] = pieces[1][0..-2]
elsif pieces[0].length > 16
elsif pieces[0].length > 15
pieces[0] = pieces[0][0..-2]
end
end
Expand Down Expand Up @@ -312,5 +319,3 @@ def find_matching(collection, name)
end
end
end

# vim: ai et ts=2 sts=2 sw=2 ft=ruby
4 changes: 1 addition & 3 deletions lib/kitchen/driver/openstack_version.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# Encoding: UTF-8
#
# Author:: Jonathan Hartman (<j@p4nt5.com>)
#
Expand All @@ -22,5 +22,3 @@ module Driver
OPENSTACK_VERSION = '1.5.1.dev'
end
end

# vim: ai et ts=2 sts=2 sw=2 ft=ruby
20 changes: 16 additions & 4 deletions spec/kitchen/driver/openstack_spec.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# Encoding: UTF-8
#
# Author:: Jonathan Hartman (<j@p4nt5.com>)
#
Expand Down Expand Up @@ -619,10 +619,24 @@
Socket.stub(:gethostname).and_return('ab.c' * 20)
end

it 'limits the generated name to 63-characters' do
it 'limits the generated name to 63 characters' do
expect(driver.send(:generate_name, 'long').length).to eq(63)
end
end

context 'node with a long hostname, username, and base name' do
before(:each) do
Socket.unstub(:gethostname)
Socket.stub(:gethostname).and_return('ab.c' * 20)

Etc.unstub(:getlogin)
Etc.stub(:getlogin).and_return('user' * 20)
end

it 'limits the generated name to 63 characters' do
expect(driver.send(:generate_name, 'long' * 20).length).to eq(63)
end
end
end

describe '#attach_ip_from_pool' do
Expand Down Expand Up @@ -918,5 +932,3 @@
end
end
end

# vim: ai et ts=2 sts=2 sw=2 ft=ruby
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# Encoding: UTF-8
#
# Author:: Jonathan Hartman (<j@p4nt5.com>)
#
Expand Down Expand Up @@ -31,5 +31,3 @@
SimpleCov.start

require_relative '../lib/kitchen/driver/openstack'

# vim: ai et ts=2 sts=2 sw=2 ft=ruby

0 comments on commit 8369413

Please sign in to comment.