Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
containers: add support for host_config
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Derich committed Aug 30, 2017
1 parent c5d5382 commit 8b22456
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,6 @@

# rspec failure tracking
.rspec_status

# annoying mac stuff
.DS_Store
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -39,3 +39,6 @@ Metrics/LineLength:

Metrics/ParameterLists:
Max: 6

Metrics/AbcSize:
Max: 15.80
26 changes: 22 additions & 4 deletions lib/hyperb/containers/containers.rb
@@ -1,5 +1,6 @@
require 'hyperb/request'
require 'hyperb/containers/container'
require 'hyperb/containers/host_config'
require 'hyperb/utils'
require 'json'
require 'uri'
Expand Down Expand Up @@ -103,21 +104,28 @@ def remove_container(params = {})
# @option params [String] :networkmode network mode, ie 'bridge'.
# @option params [Hash] :exposedports ports to expose.
#
# @option params [Hash] :exposedports ports to expose.
#
# @option params [Hash] :labels hash containing key: value
# @option params labels [String] :sh_hyper_instancetype container size: s1, s2, s3 ...
def create_container(params = {})
raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'image')
path = '/containers/create'
query = {}

# set a default size, otherwise container can't be started
# set default size, otherwise container can't be started
body = { labels: { sh_hyper_instancetype: 's1' } }

# parse host_config options
if params.key?(:host_config)
body[camelize(:host_config)] = setup_host_config(params)
params.delete(:host_config)
end

query[:name] = params[:name] if params.key?(:name)
params.delete(:name)
body.merge!(params)

response = JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform)
downcase_symbolize(response)
downcase_symbolize(JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform))
end

# inspect a container
Expand Down Expand Up @@ -247,5 +255,15 @@ def rename_container(params = {})
query[:name] = params[:name] if params.key?(:name)
Hyperb::Request.new(self, path, query, 'post').perform
end

private

def setup_host_config(params)
if params[:host_config].is_a?(Hash)
HostConfig.new(params[:host_config]).fmt
elsif params[:host_config].is_a?(HostConfig)
params[:host_config].fmt
end
end
end
end
6 changes: 6 additions & 0 deletions lib/hyperb/utils.rb
@@ -1,6 +1,12 @@
module Hyperb
# utils functions
module Utils
# converts from Symbol or String to CamelCase
# @return String
def camelize(value)
value.to_s.split('_').collect(&:capitalize).join
end

# checks if all args are keys into the hash
#
# @return [Boolean]
Expand Down
112 changes: 112 additions & 0 deletions spec/containers_spec.rb
Expand Up @@ -263,6 +263,118 @@
expect(a_request(:post, path)
.with(body: { image: 'image', workingdir: '/path/', labels: { sh_hyper_instancetype: 's1' } })).to have_been_made
end

it 'correct request should be made with Hyperb::HostConfig class' do
path = @create_container_path

hc = Hyperb::HostConfig.new(binds: ['/path/to/mount'])

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: hc)
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:binds' do
path = @create_container_path

hc = Hyperb::HostConfig.new(binds: ['/path/to/mount'])

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { binds: ['/path/to/mount'] })
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:links' do
path = @create_container_path

hc = Hyperb::HostConfig.new(links: ['container2:alias'])

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { links: ['container2:alias'] })
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:publish_all_ports' do
path = @create_container_path

hc = Hyperb::HostConfig.new(publish_all_ports: false)

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { publish_all_ports: false })
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:readonly_rootfs' do
path = @create_container_path

hc = Hyperb::HostConfig.new(readonly_rootfs: false)

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { readonly_rootfs: false })
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:restart_policy' do
path = @create_container_path

hc = Hyperb::HostConfig.new(restart_policy: { name: 'unless-stopped' })

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { restart_policy: { name: 'unless-stopped' }})
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end

it 'correct request should be made with host_configs:network_mode' do
path = @create_container_path

hc = Hyperb::HostConfig.new(network_mode: 'host')

stub_request(:post, path)
.with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
.to_return(body: fixture('create_container.json'))

@client.create_container(image: 'image', host_config: { network_mode: 'host' })
expect(a_request(:post, path)
.with(body: { image: 'image',
labels: { sh_hyper_instancetype:'s1' },
'HostConfig': hc.fmt })).to have_been_made
end
end

describe '#start_container' do
Expand Down

0 comments on commit 8b22456

Please sign in to comment.