rancher.rb 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,187 @@
1
+ require 'helper'
2
+ require 'json'
3
+
4
+ describe Rancher::Client do
5
+
6
+ before do
7
+ Rancher.reset!
8
+ end
9
+
10
+ after do
11
+ Rancher.reset!
12
+ end
13
+
14
+ describe "module configuration" do
15
+
16
+ before do
17
+ Rancher.reset!
18
+ Rancher.configure do |config|
19
+ Rancher::Configurable.keys.each do |key|
20
+ config.send("#{key}=", "Some #{key}")
21
+ end
22
+ end
23
+ end
24
+
25
+ after do
26
+ Rancher.reset!
27
+ end
28
+
29
+ it "inherits the module configuration" do
30
+ client = Rancher::Client.new
31
+ Rancher::Configurable.keys.each do |key|
32
+ expect(client.instance_variable_get(:"@#{key}")).to eq("Some #{key}")
33
+ end
34
+ end
35
+
36
+ describe "with class level configuration" do
37
+
38
+ before do
39
+ @opts = {
40
+ :connection_options => {:ssl => {:verify => false}},
41
+ }
42
+ end
43
+
44
+ it "overrides module configuration" do
45
+ client = Rancher::Client.new(@opts)
46
+ expect(client.access_key).to eq(Rancher.access_key)
47
+ end
48
+
49
+ it "can set configuration after initialization" do
50
+ client = Rancher::Client.new
51
+ client.configure do |config|
52
+ @opts.each do |key, value|
53
+ config.send("#{key}=", value)
54
+ end
55
+ end
56
+ expect(client.access_key).to eq(Rancher.access_key)
57
+ end
58
+
59
+ it "masks client secrets on inspect" do
60
+ client = Rancher::Client.new(:secret_key => '87614b09dd141c22800f96f11737ade5226d7ba8')
61
+ inspected = client.inspect
62
+ expect(inspected).not_to include("87614b09dd141c22800f96f11737ade5226d7ba8")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "authentication" do
68
+ before do
69
+ Rancher.reset!
70
+ @client = Rancher.client
71
+ end
72
+
73
+ describe "with module level config" do
74
+ before do
75
+ Rancher.reset!
76
+ end
77
+ it "sets oauth application creds with .configure" do
78
+ Rancher.configure do |config|
79
+ config.access_key = '97b4937b385eb63d1f46'
80
+ config.secret_key = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
81
+ end
82
+ expect(Rancher.client).to be_basic_authenticated
83
+ end
84
+ it "sets oauth token with module methods" do
85
+ Rancher.access_key = '97b4937b385eb63d1f46'
86
+ Rancher.secret_key = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
87
+ expect(Rancher.client).to be_basic_authenticated
88
+ end
89
+ end
90
+
91
+ describe "with class level config" do
92
+ it "sets oauth application creds with .configure" do
93
+ @client.configure do |config|
94
+ config.access_key = '97b4937b385eb63d1f46'
95
+ config.secret_key = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
96
+ end
97
+ expect(@client).to be_basic_authenticated
98
+ end
99
+ it "sets oauth token with module methods" do
100
+ @client.access_key = '97b4937b385eb63d1f46'
101
+ @client.secret_key = 'd255197b4937b385eb63d1f4677e3ffee61fbaea'
102
+ expect(@client).to be_basic_authenticated
103
+ end
104
+ end
105
+
106
+
107
+ end
108
+
109
+ describe ".agent" do
110
+ before do
111
+ Rancher.reset!
112
+ end
113
+ it "acts like a Sawyer agent" do
114
+ expect(Rancher.client.agent).to respond_to :start
115
+ end
116
+ it "caches the agent" do
117
+ agent = Rancher.client.agent
118
+ expect(agent.object_id).to eq(Rancher.client.agent.object_id)
119
+ end
120
+ end # .agent
121
+
122
+ describe ".root" do
123
+ it "fetches the API root" do
124
+ Rancher.reset!
125
+ VCR.use_cassette 'root' do
126
+ root = test_client.root
127
+ expect(root.rels[:self].href).to eq(test_rancher_api_endpoint)
128
+ end
129
+ end
130
+ end
131
+
132
+ describe ".last_response", :vcr do
133
+ it "caches the last agent response" do
134
+ Rancher.reset!
135
+ client = test_client
136
+ expect(client.last_response).to be_nil
137
+ client.get "/"
138
+ expect(client.last_response.status).to eq(200)
139
+ end
140
+ end # .last_response
141
+
142
+ describe ".get", :vcr do
143
+ before(:each) do
144
+ Rancher.reset!
145
+ end
146
+ it "handles headers" do
147
+ request = stub_get("/zen").
148
+ with(:query => {:foo => "bar"}, :headers => {:accept => "text/plain"})
149
+ test_client.get "zen", :foo => "bar", :accept => "text/plain"
150
+ assert_requested request
151
+ end
152
+ end # .get
153
+
154
+ describe ".head", :vcr do
155
+ it "handles headers" do
156
+ Rancher.reset!
157
+ request = stub_head("/zen").
158
+ with(:query => {:foo => "bar"}, :headers => {:accept => "text/plain"})
159
+ Rancher.head "zen", :foo => "bar", :accept => "text/plain"
160
+ assert_requested request
161
+ end
162
+ end # .head
163
+
164
+ describe "when making requests" do
165
+ before do
166
+ Rancher.reset!
167
+ @client = test_client
168
+ end
169
+ it "sets a proxy server" do
170
+ Rancher.configure do |config|
171
+ config.proxy = 'http://proxy.example.com:80'
172
+ end
173
+ conn = Rancher.client.send(:agent).instance_variable_get(:"@conn")
174
+ expect(conn.proxy[:uri].to_s).to eq('http://proxy.example.com')
175
+ end
176
+ it "passes along request headers for POST" do
177
+ headers = {"X-Rancher-Foo" => "bar"}
178
+ root_request = stub_post("/").
179
+ with(:headers => headers).
180
+ to_return(:status => 201)
181
+ client = test_client
182
+ client.post "", :headers => headers
183
+ assert_requested root_request
184
+ expect(client.last_response.status).to eq(201)
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,135 @@
1
+ require 'helper'
2
+
3
+ describe Rancher::Type do
4
+ let(:type) { Rancher::Type }
5
+ let(:url) { 'http://test.dev/' }
6
+ context '#list_href' do
7
+
8
+ context 'with existing url' do
9
+ it 'will append query string to url' do
10
+ actual = type.list_href('/test/', {
11
+ :filters => {
12
+ :k => 42
13
+ }
14
+ })
15
+ expect(actual).to eq '/test/?k=42'
16
+ end
17
+
18
+ it 'will keep existin query string' do
19
+ actual = type.list_href('/test/?jon=test', {
20
+ :filters => {
21
+ :k => 42
22
+ }
23
+ })
24
+ expect(actual).to eq '/test/?jon=test&k=42'
25
+ end
26
+ end
27
+
28
+ context 'with filters' do
29
+ it 'will not contain any filters' do
30
+ actual = type.list_href(url, {})
31
+
32
+ expect(actual).to_not include('?')
33
+ end
34
+
35
+ it 'will contain k=42' do
36
+ actual = type.list_href(url, {
37
+ :filters => {
38
+ :k => 42
39
+ }
40
+ })
41
+ expect(actual).to include('k=42')
42
+ end
43
+
44
+ it 'will use modifiers' do
45
+ actual = type.list_href(url, {
46
+ :filters => {
47
+ :k => [
48
+ {:modifier => 'ne', :value => 43},
49
+ {:modifier => 'gt', :value => 44}
50
+ ]
51
+ }
52
+ })
53
+ expect(actual).to include('k_ne=43&k_gt=44')
54
+ end
55
+
56
+ it 'will use modifiers and standard values' do
57
+ actual = type.list_href(url, {
58
+ :filters => {
59
+ :k => [
60
+ {:modifier => 'ne', :value => 43},
61
+ {:modifier => 'gt', :value => 44},
62
+ 45
63
+ ],
64
+ :j => 46
65
+ }
66
+ })
67
+ expect(actual).to include('k_ne=43&k_gt=44&k=45&j=46')
68
+ end
69
+ end
70
+
71
+ context 'with sort' do
72
+ it 'query string contains sort' do
73
+ actual = type.list_href(url, {
74
+ :sort => {
75
+ :name => 'test'
76
+ }
77
+ })
78
+
79
+ expect(actual).to include('sort=test')
80
+ end
81
+
82
+ it 'query string contains sort but not order asc' do
83
+ actual = type.list_href(url, {
84
+ :sort => {
85
+ :name => 'test',
86
+ :order => 'asc'
87
+ }
88
+ })
89
+
90
+ expect(actual).to include('sort=test')
91
+ expect(actual).to_not include('&order=asc')
92
+ end
93
+
94
+ it 'query string contains sort and order=desc' do
95
+ actual = type.list_href(url, {
96
+ :sort => {
97
+ :name => 'test',
98
+ :order => 'desc'
99
+ }
100
+ })
101
+
102
+ expect(actual).to include('sort=test&order=desc')
103
+ end
104
+ end
105
+
106
+ context 'pagination' do
107
+ it 'url will contain limit' do
108
+ actual = type.list_href(url, {
109
+ :pagination => {
110
+ :limit => 100
111
+ }
112
+ })
113
+ expect(actual).to include('limit=100')
114
+ end
115
+ it 'url will contain marker' do
116
+ actual = type.list_href(url, {
117
+ :pagination => {
118
+ :limit => 100,
119
+ :marker => 2
120
+ }
121
+ })
122
+ expect(actual).to include('limit=100&marker=2')
123
+ end
124
+ end
125
+
126
+ context 'include' do
127
+ it 'will include everything' do
128
+ actual = type.list_href(url, {
129
+ :include => %w(one two three)
130
+ })
131
+ expect(actual).to include('include=one&include=two&include=three')
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,46 @@
1
+ require 'helper'
2
+
3
+ describe Rancher do
4
+ before do
5
+ Rancher.reset!
6
+ end
7
+
8
+ after do
9
+ Rancher.reset!
10
+ end
11
+
12
+ it 'sets defaults' do
13
+ Rancher::Configurable.keys.each do |key|
14
+ expect(Rancher.instance_variable_get(:"@#{key}")).to eq(Rancher::Default.send(key))
15
+ end
16
+ end
17
+
18
+ describe ".client" do
19
+ it "creates an Octokit::Client" do
20
+ expect(Rancher.client).to be_kind_of Rancher::Client
21
+ end
22
+ it "caches the client when the same options are passed" do
23
+ expect(Rancher.client).to eq(Rancher.client)
24
+ end
25
+ it "returns a fresh client when options are not the same" do
26
+ client = Rancher.client
27
+ Rancher.access_key = "87614b09dd141c22800f96f11737ade5226d7ba8"
28
+ client_two = Rancher.client
29
+ client_three = Rancher.client
30
+ expect(client).not_to eq(client_two)
31
+ expect(client_three).to eq(client_two)
32
+ end
33
+ end
34
+
35
+ describe ".configure" do
36
+ Rancher::Configurable.keys.each do |key|
37
+ it "sets the #{key.to_s.gsub('_', ' ')}" do
38
+ Rancher.configure do |config|
39
+ config.send("#{key}=", key)
40
+ end
41
+ expect(Rancher.instance_variable_get(:"@#{key}")).to eq(key)
42
+ end
43
+ end
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rancher.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Whitcraft
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sawyer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.3
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 0.6.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.5.3
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.0
47
+ description: Consume the API from Rancher with Ruby!
48
+ email:
49
+ - jwhitcraft@mac.com
50
+ executables:
51
+ - console
52
+ - setup
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - ".codeclimate.yml"
57
+ - ".gitignore"
58
+ - ".ruby-version"
59
+ - ".travis.yml"
60
+ - Gemfile
61
+ - LICENSE.txt
62
+ - README.md
63
+ - Rakefile
64
+ - bin/console
65
+ - bin/setup
66
+ - lib/rancher.rb
67
+ - lib/rancher/arguments.rb
68
+ - lib/rancher/authentication.rb
69
+ - lib/rancher/classify.rb
70
+ - lib/rancher/client.rb
71
+ - lib/rancher/collection.rb
72
+ - lib/rancher/configurable.rb
73
+ - lib/rancher/connection.rb
74
+ - lib/rancher/default.rb
75
+ - lib/rancher/error.rb
76
+ - lib/rancher/middleware/follow_redirects.rb
77
+ - lib/rancher/resource.rb
78
+ - lib/rancher/response/raise_error.rb
79
+ - lib/rancher/type.rb
80
+ - lib/rancher/version.rb
81
+ - rancher.gemspec
82
+ - spec/cassettes/Rancher_Client/_get/handles_headers.json
83
+ - spec/cassettes/Rancher_Client/_head/handles_headers.json
84
+ - spec/cassettes/Rancher_Client/_last_response/caches_the_last_agent_response.json
85
+ - spec/cassettes/root.json
86
+ - spec/helper.rb
87
+ - spec/rancher/client_spec.rb
88
+ - spec/rancher/type_spec.rb
89
+ - spec/rancher_spec.rb
90
+ homepage: https://github.com/jwhitcraft/rancher.rb
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Ruby API Client for Rancher
114
+ test_files:
115
+ - spec/cassettes/Rancher_Client/_get/handles_headers.json
116
+ - spec/cassettes/Rancher_Client/_head/handles_headers.json
117
+ - spec/cassettes/Rancher_Client/_last_response/caches_the_last_agent_response.json
118
+ - spec/cassettes/root.json
119
+ - spec/helper.rb
120
+ - spec/rancher/client_spec.rb
121
+ - spec/rancher/type_spec.rb
122
+ - spec/rancher_spec.rb