Class: TestKitchen::Environment::Openstack

Inherits:
TestKitchen::Environment show all
Defined in:
lib/test-kitchen/runner/openstack/environment.rb

Constant Summary

Constants inherited from TestKitchen::Environment

KITCHEN_SUBDIRS

Instance Attribute Summary collapse

Attributes inherited from TestKitchen::Environment

#cache_path, #default_runner, #kitchenfile_name, #project, #root_path, #tmp_path, #ui

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TestKitchen::Environment

#all_platforms, #cookbook_paths, #create_tmp_file, #load!, #loaded?, #platform_names, #platforms, #setup_tmp_path

Constructor Details

#initialize(conf = {}) ⇒ Openstack

Returns a new instance of Openstack.



11
12
13
14
15
16
17
18
19
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 11

def initialize(conf={})
  super
  @username = conf[:username] || config.username
  @password = conf[:password] || config.password
  @tenant = conf[:tenant] || config.tenant
  @auth_url = conf[:auth_url] || config.auth_url
  @servers = {}
  load
end

Instance Attribute Details

#auth_urlObject (readonly)

Returns the value of attribute auth_url.



8
9
10
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 8

def auth_url
  @auth_url
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 8

def password
  @password
end

#serversObject (readonly)

Returns the value of attribute servers.



9
10
11
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 9

def servers
  @servers
end

#tenantObject (readonly)

Returns the value of attribute tenant.



8
9
10
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 8

def tenant
  @tenant
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 8

def username
  @username
end

Class Method Details

.configObject



106
107
108
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 106

def self.config
  @@config
end

.config=(config) ⇒ Object



110
111
112
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 110

def self.config=(config)
  @@config = config
end

Instance Method Details

#configObject

GROSS: Global config as a class variable



102
103
104
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 102

def config
  @@config
end

#connectionObject



53
54
55
56
57
58
59
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 53

def connection
  @connection ||= Fog::Compute.new(:provider => 'OpenStack',
                                   :openstack_username => username,
                                   :openstack_api_key => password,
                                   :openstack_auth_url => auth_url,
                                   :openstack_tenant => tenant)
end

#create_server(platform_name, server_def) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 21

def create_server(platform_name, server_def)
  @servers[platform_name] ||=
    begin
      server = connection.servers.create({ :name => server_def[:instance_name],
                                           :image_ref => server_def[:image_id],
                                           :flavor_ref => server_def[:flavor_id],
                                           :key_name => server_def[:keyname]})
      server.wait_for { ready? }
      sleep(2) until tcp_test_ssh(server.public_ip_address['addr'])
      save
      server
    end

  # These won't persist on the fog objectso we have to set them every
  # time. :(
  @servers[platform_name].username = server_def[:ssh_user]
  if server_def[:ssh_key]
    @servers[platform_name].private_key_path = File.expand_path(server_def[:ssh_key])
  end
  @servers[platform_name]
end

#destroy(name) ⇒ Object



73
74
75
76
77
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 73

def destroy(name)
  @servers[name].destroy if @servers.has_key?(name)
  @servers.delete(name)
  save
end

#scp(name) ⇒ Object



96
97
98
99
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 96

def scp(name)
  server = @servers[name]
  Fog::SCP.new(server.public_ip_address['addr'], server.username, ssh_options(name))
end

#ssh(name) ⇒ Object



91
92
93
94
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 91

def ssh(name)
  server = @servers[name]
  Fog::SSH.new(server.public_ip_address['addr'], server.username, ssh_options(name))
end

#ssh_options(name) ⇒ Object

Ideally we could use the #ssh and #scp functions on Fog's server ' object. But these seem to be broken in the case of Openstack



83
84
85
86
87
88
89
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 83

def ssh_options(name)
  if key = @servers[name].private_key_path
    {:keys => [key]}
  else
    {}
  end
end

#status(name) ⇒ Object

The following functions all take a name of of a VM in our enironment



65
66
67
68
69
70
71
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 65

def status(name)
  if @servers.has_key?(name)
    @servers[name].state.to_s.downcase
  else
    "not created"
  end
end

#tcp_test_ssh(hostname) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/test-kitchen/runner/openstack/environment.rb', line 43

def tcp_test_ssh(hostname)
  tcp_socket = TCPSocket.new(hostname, '22')
  IO.select([tcp_socket], nil, nil, 5)
rescue SocketError, Errno::ETIMEDOUT, Errno::EPERM,
  Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
  false
ensure
  tcp_socket && tcp_socket.close
end