1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #2722 from abenari/docker

[Docker] attribute aliases fixed
This commit is contained in:
Ohad Levy 2014-03-07 21:55:57 +02:00
commit b33e1f05ad
12 changed files with 91 additions and 31 deletions

View file

@ -21,6 +21,7 @@ module Fog
request :container_delete
request :container_get
request :container_action
request :container_commit
request :image_all
request :image_create
request :image_delete
@ -49,6 +50,10 @@ module Fog
hash.inject({}){ |h, v| h.merge! downcase_hash_keys(v[-1], k + [v[0]]) }
end
def camelize_hash_keys(hash)
Hash[ hash.map {|k, v| [k.to_s.split('_').map {|w| w.capitalize}.join, v] }]
end
end
end
end

View file

@ -8,11 +8,15 @@ module Fog
attr_accessor :info
attribute :repotags
attribute :repo_tags
attribute :created
attribute :size
attribute :virtual_size
def name
repo_tags.empty? ? id : repo_tags.first
end
def ready?
!(status =~ /down/i)
end

View file

@ -12,16 +12,21 @@ module Fog
attribute :name
attribute :created
attribute :network_settings_ipaddress, :aliases => 'ipaddress'
attribute :network_settings_bridge, :aliases => 'bridge'
attribute :path
attribute :args
attribute :hostname
attribute :ipaddress, :aliases => 'network_settings_ipaddress'
attribute :bridge, :aliases => 'network_settings_bridge'
attribute :state_running
attribute :state_pid
attribute :config_cpu_shares, :aliases => 'cpus'
attribute :config_memory, :aliases => 'memory'
attribute :config_hostname, :aliases => 'hostname'
attribute :cores, :aliases => 'config_cpu_shares'
attribute :memory, :aliases => 'config_memory'
attribute :hostname, :aliases => 'config_hostname'
attribute :cmd, :aliases => 'config_cmd'
attribute :entrypoint, :aliases => 'config_entrypoint'
attribute :host
attribute :image
attribute :config_exposed_ports, :aliases => 'exposed_ports'
attribute :exposed_ports, :aliases => 'config_exposed_ports'
attribute :volumes
#raw = {"ID"=>"2ce79789656e4f7474624be6496dc6d988899af30d556574389a19aade2f9650",
@ -74,11 +79,12 @@ module Fog
# }
def ready?
state_running == true
reload if state_running.nil?
state_running
end
def stopped?
state_running == false
!ready?
end
def mac
@ -86,7 +92,7 @@ module Fog
end
def start(options = {})
service.container_action(:id =>id, :action => :start)
service.container_action(:id =>id, :action => :start!)
reload
end
@ -97,10 +103,14 @@ module Fog
end
def restart(options = {})
service.container_action(:id =>id, :action => :restart)
service.container_action(:id =>id, :action => :restart!)
reload
end
def commit(options = {})
service.container_commit({:id=>id}.merge(options))
end
def destroy(options = {})
service.container_action(:id =>id, :action => :kill)
service.container_delete(:id => id)

View file

@ -7,7 +7,7 @@ module Fog
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
container = Docker::Container.get(options[:id])
container.send(options[:action], options[:id]).info
downcase_hash_keys container.send(options[:action]).info
end
end
@ -17,7 +17,7 @@ module Fog
def container_action(options = {})
raise ArgumentError, "id is a required parameter" unless options.has_key? :id
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
{'id' => 'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3','State' => {'Running' => false}}
{'id' => 'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3','state_running' => false}
end
end

View file

@ -0,0 +1,23 @@
module Fog
module Compute
class Fogdocker
class Real
def container_commit(options)
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
container = Docker::Container.get(options[:id])
downcase_hash_keys container.commit(camelize_hash_keys(options)).info
end
end
class Mock
def container_commit(options)
{'id'=>'a6b02c7ca29a22619f7d0e59062323247739bc0cd375d619f305f0b519af4ef3',
'repotags' => ['repo/other'],
'created' => 1389877693,
'size' => 3265536}
end
end
end
end
end

View file

@ -27,7 +27,7 @@ module Fog
#}
class Real
def container_create(attrs)
downcase_hash_keys Docker::Container.create(attrs).info
downcase_hash_keys Docker::Container.create(camelize_hash_keys(attrs)).info
end
end

View file

@ -14,7 +14,7 @@ module Fog
class Mock
def image_delete(options = {})
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
true
"[{'Deleted':'b15c1423ba157d0f7ac83cba178390c421bb8d536e7e7857580fc10f2d53e1b9'}]"
end
end

View file

@ -10,7 +10,7 @@ Shindo.tests('Fog::Compute[:fogdocker] | image model', ['fogdocker']) do
tests('have attributes') do
model_attribute_hash = image.attributes
attributes = [ :id,
:repotags,
:repo_tags,
:created,
:size
]
@ -20,7 +20,7 @@ Shindo.tests('Fog::Compute[:fogdocker] | image model', ['fogdocker']) do
end
end
tests("The attributes hash should have key") do
(attributes-[:repotags]).each do |attribute|
(attributes-[:repo_tags]).each do |attribute|
test("#{attribute}") { model_attribute_hash.has_key? attribute }
end
end

View file

@ -6,10 +6,10 @@ Shindo.tests('Fog::Compute[:fogdocker] | server model', ['fogdocker']) do
tests('The server model should') do
tests('have the action') do
test('reload') { server.respond_to? 'reload' }
%w{ start restart stop destroy }.each do |action|
%w{ start restart stop commit destroy}.each do |action|
test(action) { server.respond_to? action }
end
%w{ start restart stop destroy}.each do |action|
%w{ start restart stop commit destroy}.each do |action|
test("#{action} returns successfully") {
server.send(action.to_sym) ? true : false
}
@ -20,14 +20,14 @@ Shindo.tests('Fog::Compute[:fogdocker] | server model', ['fogdocker']) do
attributes = [ :id,
:name,
:created,
:network_settings_ipaddress,
:ipaddress,
:state_running,
:config_cpu_shares,
:config_memory,
:config_hostname,
:cores,
:memory,
:hostname,
:image,
#:config => exposed_ports,
#:config => volumes
#:exposed_ports,
#:volumes
]
tests("The server model should respond to") do
attributes.each do |attribute|

View file

@ -6,20 +6,20 @@ Shindo.tests("Fog::Compute[:fogdocker] | container_action request", 'fogdocker')
id = response['id']
tests("Run Container") do
response = compute.container_action(:id => id, :action => 'run' )
tests("Start Container") do
response = compute.container_action(:id => id, :action => 'start' )
test("should be a kind of Hash") { response.kind_of? Hash}
end
tests("Stop Container") do
response = compute.container_action(:id => id, :action => 'stop' )
test("should be a kind of Hash") { response.kind_of? Hash}
test("should be stopped") { response['State']['Running'] == false}
end
tests("Kill Container") do
response = compute.container_action(:id => id, :action => 'kill' )
test("should be a kind of Hash") { response.kind_of? Hash}
test("should be stopped") { response['state_running'] == false}
end
end

View file

@ -0,0 +1,19 @@
Shindo.tests("Fog::Compute[:fogdocker] | container_create request", 'fogdocker') do
compute = Fog::Compute[:fogdocker]
name_base = Time.now.to_i
hash = compute.container_create(:name => 'fog-'+name_base.to_s, 'image' => 'mattdm/fedora:f19','Cmd' => ['date'] )
response = {}
tests("Commit Container") do
response = compute.container_commit(:id=>hash['id'])
test("should have Image id") { response.include? 'id'}
end
test("Delete Commited Image") do
result = compute.image_delete(:id=>response['id'])
test("should have a deleted message") {result.include?('Deleted')}
end
end

View file

@ -1,9 +1,8 @@
Shindo.tests('Fog::Compute[:fogdocker] | container_delete request', ['fogdocker']) do
compute = Fog::Compute[:fogdocker]
container = compute.servers.create('name' => 'fog-'+Time.now.to_i.to_s,
'Image' => 'mattdm/fedora:f19',
'Cmd' => ['/bin/bash'])
container = compute.servers.create({'Image' => 'mattdm/fedora:f19',
'Cmd' => ['/bin/bash']})
tests('The response should') do
response = compute.container_delete(:id => container.id)