tainers 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/tainers/cli.rb CHANGED
@@ -97,6 +97,15 @@ module Tainers
97
97
  end
98
98
  end
99
99
 
100
+ def create_command
101
+ return 0 if specification.create
102
+ 1
103
+ end
104
+
105
+ def self.create_help
106
+ "Creates specified container, if it doesn't already exist; exits with 0 on creation, 1 if already exists."
107
+ end
108
+
100
109
  def ensure_command
101
110
  return 0 if specification.ensure
102
111
  255
@@ -40,6 +40,28 @@ module Tainers
40
40
  return nil
41
41
  end
42
42
 
43
+ # Creates the container named by this specification, if it does
44
+ # not already exist.
45
+ #
46
+ # Returns true (self, actually) if the invocation resulted in the
47
+ # creation of a new container; false otherwise.
48
+ #
49
+ # A false condition could result from:
50
+ # - The container already existing
51
+ # - The container being simultaneously created by another
52
+ # actor, with your invocation losing the race.
53
+ #
54
+ # A failure to create due to operational or semantic issues
55
+ # should result in an exception. Therefore, any non-exceptional
56
+ # case should mean that a container of the expected name exists,
57
+ # though in the false result case there is no firm guarantee
58
+ # that the existing container has the requested configuration e.
59
+ def create
60
+ return false if exists?
61
+ return self if Tainers::API.create(@args)
62
+ false
63
+ end
64
+
43
65
  # The name of the container described by this specification.
44
66
  def name
45
67
  @args['name']
@@ -70,14 +92,18 @@ module Tainers
70
92
  end
71
93
  end
72
94
 
73
- def self.create_or_conflict params
95
+ def self.create params
74
96
  begin
75
97
  Docker::Container.create(params.dup)
76
98
  return true
77
99
  rescue Excon::Errors::Conflict
78
- return true
100
+ return false
79
101
  end
80
- false
102
+ end
103
+
104
+ def self.create_or_conflict params
105
+ create params
106
+ true
81
107
  end
82
108
  end
83
109
 
@@ -0,0 +1,22 @@
1
+ require_relative 'common_examples'
2
+
3
+ shared_examples_for 'create command' do
4
+ before do
5
+ args << 'create'
6
+ end
7
+
8
+ it "creates container and exits with 0 on creation" do
9
+ expect(specification).to receive(:create).with(no_args).and_return(true)
10
+ expect(command_run).to eq(0)
11
+ end
12
+
13
+ it "creates container and exits with 1 on non-creation" do
14
+ expect(specification).to receive(:create).with(no_args).and_return(false)
15
+ expect(command_run).to eq(1)
16
+ end
17
+ end
18
+
19
+ describe 'tainers create' do
20
+ it_behaves_like "a command", "create command"
21
+ end
22
+
@@ -1,5 +1,27 @@
1
1
  require 'rspec_helper'
2
2
 
3
+ shared_examples_for 'container creator' do
4
+ before do
5
+ expect(subject).to receive(:exists?).and_return(false)
6
+ end
7
+
8
+ it 'uses Docker::Container for creation' do
9
+ expect(Docker::Container).to receive(:create).with(specification_args).and_return(container = double)
10
+ expect(creation_operation).to be(subject)
11
+ end
12
+
13
+ it 'is okay with a conflict result' do
14
+ expect(Docker::Container).to receive(:create).with(specification_args).and_raise(Excon::Errors::Conflict, "Pickles")
15
+ expect(creation_operation).to be(expected_conflict_result)
16
+ end
17
+
18
+ it 'does not handle other exceptions' do
19
+ expect(Docker::Container).to receive(:create).with(specification_args).and_raise(Exception, "You suck.")
20
+ expect { creation_operation }.to raise_error("You suck.")
21
+ end
22
+ end
23
+
24
+
3
25
  RSpec.describe Tainers::Specification do
4
26
  it 'requires a name' do
5
27
  expect { Tainers::Specification.new 'Image' => 'foo/image:latest' }.to raise_error(/name is required/)
@@ -26,24 +48,17 @@ RSpec.describe Tainers::Specification do
26
48
  end
27
49
 
28
50
  context '#ensure' do
29
- before do
30
- expect(subject).to receive(:exists?).and_return(false)
31
- end
32
-
33
- it 'uses Docker::Container for creation' do
34
- expect(Docker::Container).to receive(:create).with(specification_args).and_return(container = double)
35
- expect(subject.ensure).to be(subject)
36
- end
37
-
38
- it 'is okay with a conflict result' do
39
- expect(Docker::Container).to receive(:create).with(specification_args).and_raise(Excon::Errors::Conflict, "Pickles")
40
- expect(subject.ensure).to be(subject)
41
- end
42
-
43
- it 'does not handle other exceptions' do
44
- expect(Docker::Container).to receive(:create).with(specification_args).and_raise(Exception, "You suck.")
45
- expect { subject.ensure }.to raise_error("You suck.")
46
- end
51
+ let(:expected_conflict_result) { subject }
52
+ let(:creation_operation) { subject.ensure }
53
+
54
+ it_behaves_like 'container creator'
55
+ end
56
+
57
+ context '#create' do
58
+ let(:expected_conflict_result) { false }
59
+ let(:creation_operation) { subject.create }
60
+
61
+ it_behaves_like 'container creator'
47
62
  end
48
63
  end
49
64
 
@@ -58,6 +73,12 @@ RSpec.describe Tainers::Specification do
58
73
  expect(Docker::Container).to receive(:create).never
59
74
  expect(subject.ensure).to be(subject)
60
75
  end
76
+
77
+ it 'does a no-op for #create' do
78
+ expect(subject).to receive(:exists?).and_return(true)
79
+ expect(Docker::Container).to receive(:create).never
80
+ expect(subject.create).to be(false)
81
+ end
61
82
  end
62
83
  end
63
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tainers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -74,6 +74,7 @@ files:
74
74
  - spec/commands/exists_spec.rb
75
75
  - spec/commands/ensure_spec.rb
76
76
  - spec/commands/common_examples.rb
77
+ - spec/commands/create_spec.rb
77
78
  - spec/commands/name_spec.rb
78
79
  - spec/specification_spec.rb
79
80
  - spec/rspec_helper.rb