fleet-ruby 0.1.0

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.
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'fleet/configuration'
3
+
4
+ describe Fleet::Configuration do
5
+
6
+ subject { Class.new { extend Fleet::Configuration } }
7
+
8
+ describe 'exposed attribes' do
9
+ Fleet::Configuration::VALID_OPTIONS_KEYS.each do |key|
10
+ it { should respond_to key.to_sym }
11
+ end
12
+ end
13
+
14
+ describe 'default values' do
15
+
16
+ describe 'fleet_api_url' do
17
+ it 'is matches DEFAULT_FLEET_API_URL' do
18
+ expect(subject.fleet_api_url).to eq Fleet::Configuration::DEFAULT_FLEET_API_URL
19
+ end
20
+ end
21
+
22
+ describe 'fleet_api_version' do
23
+ it 'is matches DEFAULT_FLEET_API_VERSION' do
24
+ expect(subject.fleet_api_version).to eq Fleet::Configuration::DEFAULT_FLEET_API_VERSION
25
+ end
26
+ end
27
+
28
+ describe 'open_timeout' do
29
+ it 'is matches DEFAULT_OPEN_TIMEOUT' do
30
+ expect(subject.open_timeout).to eq Fleet::Configuration::DEFAULT_OPEN_TIMEOUT
31
+ end
32
+ end
33
+
34
+ describe 'read_timeout' do
35
+ it 'is matches DEFAULT_READ_TIMEOUT' do
36
+ expect(subject.read_timeout).to eq Fleet::Configuration::DEFAULT_READ_TIMEOUT
37
+ end
38
+ end
39
+
40
+ describe 'logger' do
41
+ it 'is matches DEFAULT_LOGGER' do
42
+ expect(subject.logger).to eq Fleet::Configuration::DEFAULT_LOGGER
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fleet::Connection do
4
+
5
+ describe 'connection options' do
6
+
7
+ let(:open_timeout) { 30 }
8
+ let(:read_timeout) { 40 }
9
+
10
+ subject do
11
+ Fleet::Client.new(
12
+ open_timeout: open_timeout,
13
+ read_timeout: read_timeout).connection
14
+ end
15
+
16
+ describe 'open_timeout' do
17
+ it 'matches the the specified timeout value' do
18
+ expect(subject.data[:connect_timeout]).to eq open_timeout
19
+ end
20
+ end
21
+
22
+ describe 'read_timeout' do
23
+ it 'matches the the specified timeout value' do
24
+ expect(subject.data[:read_timeout]).to eq read_timeout
25
+ end
26
+ end
27
+
28
+ context 'when URL is HTTP' do
29
+ let(:url) { 'http://foo.com/bar' }
30
+
31
+ subject do
32
+ Fleet::Client.new(fleet_api_url: url).connection
33
+ end
34
+ describe 'scheme' do
35
+ it 'matches the scheme of the URL' do
36
+ expect(subject.data[:scheme]).to eq 'http'
37
+ end
38
+ end
39
+
40
+ describe 'host' do
41
+ it 'matches the host of the URL' do
42
+ expect(subject.data[:host]).to eq 'foo.com'
43
+ end
44
+ end
45
+
46
+ describe 'port' do
47
+ it 'matches the port of the URL' do
48
+ expect(subject.data[:port]).to eq 80
49
+ end
50
+ end
51
+
52
+ describe 'prefix' do
53
+ it 'matches the path of the URL' do
54
+ expect(subject.data[:path]).to eq '/bar'
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'when URL is UNIX' do
60
+ let(:url) { 'unix:///foo/bar.socket' }
61
+
62
+ subject do
63
+ Fleet::Client.new(fleet_api_url: url).connection
64
+ end
65
+
66
+ describe 'scheme' do
67
+ it 'matches the scheme of the URL' do
68
+ expect(subject.data[:scheme]).to eq 'unix'
69
+ end
70
+ end
71
+
72
+ describe 'socket' do
73
+ it 'matches the port of the URL' do
74
+ expect(subject.data[:socket]).to eq '/foo/bar.socket'
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fleet::Error do
4
+
5
+ let(:message) { 'some message' }
6
+ let(:error_code) { 12345 }
7
+
8
+ subject { Fleet::Error.new(message, error_code) }
9
+
10
+ it { should respond_to(:message) }
11
+ it { should respond_to(:error_code) }
12
+
13
+ describe '#initialize' do
14
+
15
+ it 'saves the passed-in message' do
16
+ expect(subject.message).to eq message
17
+ end
18
+
19
+ it 'saves the passed-in error code' do
20
+ expect(subject.error_code).to eq error_code
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fleet::Request do
4
+
5
+ subject { Fleet::Client.new }
6
+
7
+ let(:path) { '/foo bar@' }
8
+
9
+ let(:response) do
10
+ double(:response, body: '{"name":"foo"}', status: 200)
11
+ end
12
+
13
+ let(:connection) { double(:connection) }
14
+
15
+ before do
16
+ allow(connection).to receive(:send).and_return(response)
17
+ allow(subject).to receive(:connection).and_return(connection)
18
+ end
19
+
20
+ describe '#get' do
21
+
22
+ let(:options) do
23
+ { foo: 'bar' }
24
+ end
25
+
26
+ it 'invokes #get on the connection with the correct params' do
27
+ opts = { path: '/foo%20bar%40', query: options }
28
+ expect(connection).to receive(:send).with(:get, opts)
29
+
30
+ subject.send(:get, path, options)
31
+ end
32
+
33
+ it 'returns the parsed response body' do
34
+ expect(subject.send(:get, path, options)).to eq('name' => 'foo')
35
+ end
36
+
37
+ context 'when there is pagination' do
38
+ let(:first_response) do
39
+ double(:first_response, body: '{"things":[{"name":"foo"}], "nextPageToken":"123"}', status: 200)
40
+ end
41
+ let(:second_response) do
42
+ double(:second_response, body: '{"things":[{"name":"bah"}], "nextPageToken":"456"}', status: 200)
43
+ end
44
+ let(:third_response) do
45
+ double(:second_response, body: '{"things":[{"name":"tah"}]}', status: 200)
46
+ end
47
+
48
+ it 'merges the responses' do
49
+ expect(connection).to receive(:send).with(:get, anything).and_return(first_response)
50
+ expect(connection).to receive(:send).with(:get, hash_including(query: { 'nextPageToken' => '123' })).and_return(second_response)
51
+ expect(connection).to receive(:send).with(:get, hash_including(query: {'nextPageToken' => '456'})).and_return(third_response)
52
+
53
+ expect(subject.send(:get, path)).to eql(
54
+ 'things' => [{ 'name' => 'foo' }, { 'name' => 'bah' }, { 'name' => 'tah' }]
55
+ )
56
+ end
57
+ end
58
+
59
+ context 'when there is a SocketError' do
60
+ before do
61
+ allow(connection).to receive(:send)
62
+ .and_raise(Excon::Errors::SocketError, Excon::Errors::Error.new('oops'))
63
+ end
64
+
65
+ it 'raises a Fleet::ConnectionError' do
66
+ expect { subject.send(:get, path, options) }.to raise_error(Fleet::ConnectionError)
67
+ end
68
+ end
69
+
70
+ context 'when a non-200 status code is returned' do
71
+ let(:response) do
72
+ double(:response, body: '{"error": {"message": "oops", "code": "400"}}', status: 400)
73
+ end
74
+
75
+ it 'raises a Fleet::ConnectionError' do
76
+ expect { subject.send(:get, path, options) }.to raise_error(Fleet::BadRequest, 'oops')
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '#put' do
82
+
83
+ let(:options) do
84
+ { foo: 'bar' }
85
+ end
86
+
87
+ it 'invokes #put on the connection with the correct params' do
88
+ opts = {
89
+ path: '/foo%20bar%40',
90
+ headers: { 'Content-Type' => 'application/json' },
91
+ body: JSON.dump(options)
92
+ }
93
+ expect(connection).to receive(:send).with(:put, opts)
94
+
95
+ subject.send(:put, path, options)
96
+ end
97
+
98
+ it 'returns true' do
99
+ expect(subject.send(:put, path, options)).to eq(true)
100
+ end
101
+ end
102
+
103
+ describe '#delete' do
104
+
105
+ it 'invokes #get on the connection with the correct params' do
106
+ opts = { path: '/foo%20bar%40' }
107
+ expect(connection).to receive(:send).with(:delete, opts)
108
+
109
+ subject.send(:delete, path, nil)
110
+ end
111
+
112
+ it 'returns true' do
113
+ expect(subject.send(:delete, path, nil)).to eq(true)
114
+ end
115
+ end
116
+
117
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fleet::ServiceDefinition do
4
+
5
+ let(:name) { 'foo.service' }
6
+
7
+ let(:service_hash) do
8
+ {
9
+ 'Unit' => {
10
+ 'Description' => 'infinite loop'
11
+ },
12
+ 'Service' => {
13
+ 'ExecStartPre' => ['foo', 'bar'],
14
+ 'ExecStart' => "/bin/bash -c \"while true; do sleep 1; done\""
15
+ }
16
+ }
17
+ end
18
+
19
+ describe '#to_unit' do
20
+
21
+ subject { described_class.new(service_hash) }
22
+
23
+ it 'provides a fleet formatted unit definition' do
24
+
25
+ expected = {
26
+ "name" => name,
27
+ "options"=> [
28
+ { "section" => "Unit", "name" => "Description", "value" => "infinite loop"},
29
+ { "section" => "Service", "name" => "ExecStartPre", "value" => "foo" },
30
+ { "section" => "Service", "name" => "ExecStartPre", "value" => "bar" },
31
+ { "section" => "Service", "name" => "ExecStart", "value" => "/bin/bash -c \"while true; do sleep 1; done\"" }
32
+ ]
33
+ }
34
+
35
+ expect(subject.to_unit(name)).to eq expected
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fleet do
4
+
5
+ after do
6
+ Fleet.reset
7
+ end
8
+
9
+ describe '.new' do
10
+
11
+ it 'returns a Fleet::Client' do
12
+ expect(Fleet.new).to be_a Fleet::Client
13
+ end
14
+
15
+ context 'when no options specified' do
16
+
17
+ Fleet::Configuration::VALID_OPTIONS_KEYS.each do |option|
18
+
19
+ it "new Fleet::Client inherits :#{option} default from Fleet" do
20
+ expect(Fleet.new.send(option)).to eq Fleet.send(option)
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'when options are specified' do
26
+
27
+ Fleet::Configuration::VALID_OPTIONS_KEYS.each do |option|
28
+
29
+ it "new Fleet::Client receives specified :#{option} value" do
30
+ expect(Fleet.new({option => 'foo'}).send(option)).to eq 'foo'
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.fleet_api_url' do
37
+
38
+ let(:url) { 'http://foo.com/bar' }
39
+
40
+ before do
41
+ stub_const('Fleet::Configuration::DEFAULT_FLEET_API_URL', url)
42
+ Fleet.reset
43
+ end
44
+
45
+ it 'defaults to the value of DEFAULT_FLEET_API_URL' do
46
+ expect(Fleet.fleet_api_url).to eq url
47
+ end
48
+ end
49
+
50
+ describe '.fleet_api_version' do
51
+ it 'defaults to v1' do
52
+ expect(Fleet.fleet_api_version).to eq 'v1'
53
+ end
54
+ end
55
+
56
+ describe '.open_timeout' do
57
+ it 'defaults to 2' do
58
+ expect(Fleet.open_timeout).to eq 2
59
+ end
60
+ end
61
+
62
+ describe '.read_timeout' do
63
+ it 'defaults to 5' do
64
+ expect(Fleet.read_timeout).to eq 5
65
+ end
66
+ end
67
+
68
+ describe '.configure' do
69
+ it "accepts a block" do
70
+ expect { Fleet.configure {} }.to_not raise_error
71
+ end
72
+
73
+ it "yields self" do
74
+ Fleet.configure { |conf| expect(conf).to be(Fleet) }
75
+ end
76
+
77
+ it "returns true" do
78
+ expect(Fleet.configure {}).to eq true
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,9 @@
1
+ require 'simplecov'
2
+ require 'simplecov-rcov'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
5
+ SimpleCov.start do
6
+ add_filter '/spec'
7
+ end
8
+
9
+ require 'fleet'
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fleet-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Barry Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.27.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.27.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov-rcov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.2.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.2.3
83
+ description: A simple REST client for the CoreOS Fleet API
84
+ email: nyxcharon@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".travis.yml"
91
+ - CHANGELOG.md
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - circle.yml
98
+ - fleet-ruby.gemspec
99
+ - lib/fleet.rb
100
+ - lib/fleet/client.rb
101
+ - lib/fleet/client/machines.rb
102
+ - lib/fleet/client/state.rb
103
+ - lib/fleet/client/unit.rb
104
+ - lib/fleet/configuration.rb
105
+ - lib/fleet/connection.rb
106
+ - lib/fleet/error.rb
107
+ - lib/fleet/request.rb
108
+ - lib/fleet/service_definition.rb
109
+ - lib/fleet/version.rb
110
+ - spec/fleet/client/machines_spec.rb
111
+ - spec/fleet/client/state_spec.rb
112
+ - spec/fleet/client/unit_spec.rb
113
+ - spec/fleet/client_spec.rb
114
+ - spec/fleet/configuration_spec.rb
115
+ - spec/fleet/connection_spec.rb
116
+ - spec/fleet/error_spec.rb
117
+ - spec/fleet/request_spec.rb
118
+ - spec/fleet/service_definition_spec.rb
119
+ - spec/fleet_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: https://github.com/nyxcharon/fleet-ruby.git
122
+ licenses:
123
+ - Apache 2
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 1.9.3
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.5.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A simple REST client for the CoreOS Fleet API
145
+ test_files:
146
+ - spec/fleet/client/machines_spec.rb
147
+ - spec/fleet/client/state_spec.rb
148
+ - spec/fleet/client/unit_spec.rb
149
+ - spec/fleet/client_spec.rb
150
+ - spec/fleet/configuration_spec.rb
151
+ - spec/fleet/connection_spec.rb
152
+ - spec/fleet/error_spec.rb
153
+ - spec/fleet/request_spec.rb
154
+ - spec/fleet/service_definition_spec.rb
155
+ - spec/fleet_spec.rb
156
+ - spec/spec_helper.rb
157
+ has_rdoc: