vmfloaty 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,224 +1,224 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require_relative '../../lib/vmfloaty/pooler'
3
5
 
4
6
  describe Pooler do
5
7
  before :each do
6
- @vmpooler_url = "https://vmpooler.example.com"
8
+ @vmpooler_url = 'https://vmpooler.example.com'
7
9
  end
8
10
 
9
- describe "#list" do
11
+ describe '#list' do
10
12
  before :each do
11
- @list_response_body = "[\"debian-7-i386\",\"debian-7-x86_64\",\"centos-7-x86_64\"]"
13
+ @list_response_body = '["debian-7-i386","debian-7-x86_64","centos-7-x86_64"]'
12
14
  end
13
15
 
14
- it "returns a hash with operating systems from the pooler" do
15
- stub_request(:get, "#{@vmpooler_url}/vm").
16
- to_return(:status => 200, :body => @list_response_body, :headers => {})
16
+ it 'returns a hash with operating systems from the pooler' do
17
+ stub_request(:get, "#{@vmpooler_url}/vm")
18
+ .to_return(:status => 200, :body => @list_response_body, :headers => {})
17
19
 
18
20
  list = Pooler.list(false, @vmpooler_url, nil)
19
21
  expect(list).to be_an_instance_of Array
20
22
  end
21
23
 
22
- it "filters operating systems based on the filter param" do
23
- stub_request(:get, "#{@vmpooler_url}/vm").
24
- to_return(:status => 200, :body => @list_response_body, :headers => {})
24
+ it 'filters operating systems based on the filter param' do
25
+ stub_request(:get, "#{@vmpooler_url}/vm")
26
+ .to_return(:status => 200, :body => @list_response_body, :headers => {})
25
27
 
26
- list = Pooler.list(false, @vmpooler_url, "deb")
28
+ list = Pooler.list(false, @vmpooler_url, 'deb')
27
29
  expect(list).to be_an_instance_of Array
28
30
  expect(list.size).to equal 2
29
31
  end
30
32
 
31
- it "returns nothing if the filter does not match" do
32
- stub_request(:get, "#{@vmpooler_url}/vm").
33
- to_return(:status => 200, :body => @list_response_body, :headers => {})
33
+ it 'returns nothing if the filter does not match' do
34
+ stub_request(:get, "#{@vmpooler_url}/vm")
35
+ .to_return(:status => 200, :body => @list_response_body, :headers => {})
34
36
 
35
- list = Pooler.list(false, @vmpooler_url, "windows")
37
+ list = Pooler.list(false, @vmpooler_url, 'windows')
36
38
  expect(list).to be_an_instance_of Array
37
39
  expect(list.size).to equal 0
38
40
  end
39
41
  end
40
42
 
41
- describe "#retrieve" do
43
+ describe '#retrieve' do
42
44
  before :each do
43
- @retrieve_response_body_single = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":\"fq6qlpjlsskycq6\"}}"
44
- @retrieve_response_body_double = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"centos-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"}}"
45
+ @retrieve_response_body_single = '{"ok":true,"debian-7-i386":{"hostname":"fq6qlpjlsskycq6"}}'
46
+ @retrieve_response_body_double = '{"ok":true,"debian-7-i386":{"hostname":["sc0o4xqtodlul5w","4m4dkhqiufnjmxy"]},"centos-7-x86_64":{"hostname":"zb91y9qbrbf6d3q"}}'
45
47
  end
46
48
 
47
- it "raises an AuthError if the token is invalid" do
48
- stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
49
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
50
- to_return(:status => 401, :body => "{\"ok\":false}", :headers => {})
49
+ it 'raises an AuthError if the token is invalid' do
50
+ stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386")
51
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
52
+ .to_return(:status => 401, :body => '{"ok":false}', :headers => {})
51
53
 
52
54
  vm_hash = {}
53
55
  vm_hash['debian-7-i386'] = 1
54
- expect{ Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError)
56
+ expect { Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user', {}) }.to raise_error(AuthError)
55
57
  end
56
58
 
57
- it "retrieves a single vm with a token" do
58
- stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
59
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
60
- to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
59
+ it 'retrieves a single vm with a token' do
60
+ stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386")
61
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
62
+ .to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
61
63
 
62
64
  vm_hash = {}
63
65
  vm_hash['debian-7-i386'] = 1
64
- vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
66
+ vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user', {})
65
67
  expect(vm_req).to be_an_instance_of Hash
66
- expect(vm_req["ok"]).to equal true
67
- expect(vm_req["debian-7-i386"]["hostname"]).to eq "fq6qlpjlsskycq6"
68
+ expect(vm_req['ok']).to equal true
69
+ expect(vm_req['debian-7-i386']['hostname']).to eq 'fq6qlpjlsskycq6'
68
70
  end
69
71
 
70
- it "retrieves a multiple vms with a token" do
71
- stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386+debian-7-i386+centos-7-x86_64").
72
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
73
- to_return(:status => 200, :body => @retrieve_response_body_double, :headers => {})
72
+ it 'retrieves a multiple vms with a token' do
73
+ stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386+debian-7-i386+centos-7-x86_64")
74
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
75
+ .to_return(:status => 200, :body => @retrieve_response_body_double, :headers => {})
74
76
 
75
77
  vm_hash = {}
76
78
  vm_hash['debian-7-i386'] = 2
77
79
  vm_hash['centos-7-x86_64'] = 1
78
- vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
80
+ vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user', {})
79
81
  expect(vm_req).to be_an_instance_of Hash
80
- expect(vm_req["ok"]).to equal true
81
- expect(vm_req["debian-7-i386"]["hostname"]).to be_an_instance_of Array
82
- expect(vm_req["debian-7-i386"]["hostname"]).to eq ["sc0o4xqtodlul5w", "4m4dkhqiufnjmxy"]
83
- expect(vm_req["centos-7-x86_64"]["hostname"]).to eq "zb91y9qbrbf6d3q"
82
+ expect(vm_req['ok']).to equal true
83
+ expect(vm_req['debian-7-i386']['hostname']).to be_an_instance_of Array
84
+ expect(vm_req['debian-7-i386']['hostname']).to eq %w[sc0o4xqtodlul5w 4m4dkhqiufnjmxy]
85
+ expect(vm_req['centos-7-x86_64']['hostname']).to eq 'zb91y9qbrbf6d3q'
84
86
  end
85
87
  end
86
88
 
87
- describe "#modify" do
89
+ describe '#modify' do
88
90
  before :each do
89
- @modify_response_body_success = "{\"ok\":true}"
90
- @modify_response_body_fail = "{\"ok\":false}"
91
+ @modify_response_body_success = '{"ok":true}'
92
+ @modify_response_body_fail = '{"ok":false}'
91
93
  end
92
94
 
93
- it "raises a TokenError if token provided is nil" do
94
- expect{ Pooler.modify(false, @vmpooler_url, 'myfakehost', nil, {}) }.to raise_error(TokenError)
95
+ it 'raises a TokenError if token provided is nil' do
96
+ expect { Pooler.modify(false, @vmpooler_url, 'myfakehost', nil, {}) }.to raise_error(TokenError)
95
97
  end
96
98
 
97
- it "modifies the TTL of a vm" do
99
+ it 'modifies the TTL of a vm' do
98
100
  modify_hash = { :lifetime => 12 }
99
- stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
100
- with(:body => {'{"lifetime":12}'=>true},
101
- :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
102
- to_return(:status => 200, :body => @modify_response_body_success, :headers => {})
101
+ stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6")
102
+ .with(:body => { '{"lifetime":12}' => true },
103
+ :headers => { 'Content-Type' => 'application/x-www-form-urlencoded', 'X-Auth-Token' => 'mytokenfile' })
104
+ .to_return(:status => 200, :body => @modify_response_body_success, :headers => {})
103
105
 
104
106
  modify_req = Pooler.modify(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', modify_hash)
105
- expect(modify_req["ok"]).to be true
107
+ expect(modify_req['ok']).to be true
106
108
  end
107
109
  end
108
110
 
109
- describe "#delete" do
111
+ describe '#delete' do
110
112
  before :each do
111
- @delete_response_body_success = "{\"ok\":true}"
112
- @delete_response = {"fq6qlpjlsskycq6"=>{"ok"=>true}}
113
+ @delete_response_body_success = '{"ok":true}'
114
+ @delete_response = { 'fq6qlpjlsskycq6' => { 'ok' => true } }
113
115
  end
114
116
 
115
- it "deletes a specified vm" do
116
- stub_request(:delete, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
117
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
118
- to_return(:status => 200, :body => @delete_response_body_success, :headers => {})
117
+ it 'deletes a specified vm' do
118
+ stub_request(:delete, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6")
119
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
120
+ .to_return(:status => 200, :body => @delete_response_body_success, :headers => {})
119
121
 
120
122
  expect(Pooler.delete(false, @vmpooler_url, ['fq6qlpjlsskycq6'], 'mytokenfile')).to eq @delete_response
121
123
  end
122
124
 
123
- it "raises a token error if no token provided" do
124
- expect{ Pooler.delete(false, @vmpooler_url, ['myfakehost'], nil) }.to raise_error(TokenError)
125
+ it 'raises a token error if no token provided' do
126
+ expect { Pooler.delete(false, @vmpooler_url, ['myfakehost'], nil) }.to raise_error(TokenError)
125
127
  end
126
128
  end
127
129
 
128
- describe "#status" do
130
+ describe '#status' do
129
131
  before :each do
130
- #smaller version
131
- @status_response_body = "{\"capacity\":{\"current\":716,\"total\":717,\"percent\": 99.9},\"status\":{\"ok\":true,\"message\":\"Battle station fully armed and operational.\"}}"
132
+ # smaller version
133
+ @status_response_body = '{"capacity":{"current":716,"total":717,"percent": 99.9},"status":{"ok":true,"message":"Battle station fully armed and operational."}}'
132
134
  end
133
135
 
134
- it "prints the status" do
135
- stub_request(:get, "#{@vmpooler_url}/status").
136
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
137
- to_return(:status => 200, :body => @status_response_body, :headers => {})
136
+ it 'prints the status' do
137
+ stub_request(:get, "#{@vmpooler_url}/status")
138
+ .to_return(:status => 200, :body => @status_response_body, :headers => {})
138
139
 
139
140
  status = Pooler.status(false, @vmpooler_url)
140
141
  expect(status).to be_an_instance_of Hash
141
142
  end
142
143
  end
143
144
 
144
- describe "#summary" do
145
+ describe '#summary' do
145
146
  before :each do
146
- @status_response_body = ""
147
+ @status_response_body = ''
147
148
 
148
- it "prints the summary" do
149
+ it 'prints the summary' do
149
150
  end
150
151
  end
151
152
  end
152
153
 
153
- describe "#query" do
154
+ describe '#query' do
154
155
  before :each do
155
- @query_response_body = "{\"ok\": true,\"fq6qlpjlsskycq6\":{\"template\":\"debian-7-x86_64\",\"lifetime\": 2,\"running\": 0.08,\"state\":\"running\",\"snapshots\":[\"n4eb4kdtp7rwv4x158366vd9jhac8btq\" ],\"domain\": \"delivery.puppetlabs.net\"}}"
156
+ @query_response_body = '{"ok": true,"fq6qlpjlsskycq6":{"template":"debian-7-x86_64","lifetime": 2,"running": 0.08,"state":"running","snapshots":["n4eb4kdtp7rwv4x158366vd9jhac8btq" ],"domain": "delivery.puppetlabs.net"}}'
156
157
  end
157
158
 
158
- it "makes a query about a vm" do
159
- stub_request(:get, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
160
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
161
- to_return(:status => 200, :body => @query_response_body, :headers => {})
159
+ it 'makes a query about a vm' do
160
+ stub_request(:get, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6")
161
+ .to_return(:status => 200, :body => @query_response_body, :headers => {})
162
162
 
163
163
  query_req = Pooler.query(false, @vmpooler_url, 'fq6qlpjlsskycq6')
164
164
  expect(query_req).to be_an_instance_of Hash
165
165
  end
166
166
  end
167
167
 
168
- describe "#snapshot" do
168
+ describe '#snapshot' do
169
169
  before :each do
170
- @snapshot_response_body = "{\"ok\":true}"
170
+ @snapshot_response_body = '{"ok":true}'
171
171
  end
172
172
 
173
- it "makes a snapshot for a single vm" do
174
- stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot").
175
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
176
- to_return(:status => 200, :body => @snapshot_response_body, :headers => {})
173
+ it 'makes a snapshot for a single vm' do
174
+ stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot")
175
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
176
+ .to_return(:status => 200, :body => @snapshot_response_body, :headers => {})
177
177
 
178
178
  snapshot_req = Pooler.snapshot(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile')
179
- expect(snapshot_req["ok"]).to be true
179
+ expect(snapshot_req['ok']).to be true
180
180
  end
181
181
  end
182
182
 
183
- describe "#revert" do
183
+ describe '#revert' do
184
184
  before :each do
185
- @revert_response_body = "{\"ok\":true}"
185
+ @revert_response_body = '{"ok":true}'
186
186
  end
187
187
 
188
- it "makes a request to revert a vm from a snapshot" do
189
- stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot/dAfewKNfaweLKNve").
190
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
191
- to_return(:status => 200, :body => @revert_response_body, :headers => {})
188
+ it 'makes a request to revert a vm from a snapshot' do
189
+ stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot/dAfewKNfaweLKNve")
190
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
191
+ .to_return(:status => 200, :body => @revert_response_body, :headers => {})
192
192
 
193
193
  revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve')
194
- expect(revert_req["ok"]).to be true
194
+ expect(revert_req['ok']).to be true
195
195
  end
196
196
 
197
197
  it "doesn't make a request to revert a vm if snapshot is not provided" do
198
- expect{ Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, "Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6")
198
+ expect { Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, 'Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6')
199
199
  end
200
200
 
201
- it "raises a TokenError if no token was provided" do
202
- expect{ Pooler.revert(false, @vmpooler_url, 'myfakehost', nil, 'shaaaaaaa') }.to raise_error(TokenError)
201
+ it 'raises a TokenError if no token was provided' do
202
+ expect { Pooler.revert(false, @vmpooler_url, 'myfakehost', nil, 'shaaaaaaa') }.to raise_error(TokenError)
203
203
  end
204
204
  end
205
205
 
206
- describe "#disk" do
206
+ describe '#disk' do
207
207
  before :each do
208
- @disk_response_body_success = "{\"ok\":true}"
209
- @disk_response_body_fail = "{\"ok\":false}"
208
+ @disk_response_body_success = '{"ok":true}'
209
+ @disk_response_body_fail = '{"ok":false}'
210
210
  end
211
211
 
212
- it "makes a request to extend disk space of a vm" do
213
- stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/disk/12").
214
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}). to_return(:status => 200, :body => @disk_response_body_success, :headers => {})
212
+ it 'makes a request to extend disk space of a vm' do
213
+ stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/disk/12")
214
+ .with(:headers => { 'X-Auth-Token' => 'mytokenfile' }). to_return(:status => 200, :body => @disk_response_body_success, :headers => {})
215
215
 
216
216
  disk_req = Pooler.disk(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 12)
217
- expect(disk_req["ok"]).to be true
217
+ expect(disk_req['ok']).to be true
218
218
  end
219
219
 
220
- it "raises a TokenError if no token was provided" do
221
- expect{ Pooler.disk(false, @vmpooler_url, 'myfakehost', nil, 12) }.to raise_error(TokenError)
220
+ it 'raises a TokenError if no token was provided' do
221
+ expect { Pooler.disk(false, @vmpooler_url, 'myfakehost', nil, 12) }.to raise_error(TokenError)
222
222
  end
223
223
  end
224
224
  end
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../lib/vmfloaty/service'
2
4
 
3
5
  describe Service do
4
-
5
6
  describe '#initialize' do
6
7
  it 'store configuration options' do
7
8
  options = MockOptions.new({})
8
- config = {'url' => 'http://example.url'}
9
+ config = { 'url' => 'http://example.url' }
9
10
  service = Service.new(options, config)
10
11
  expect(service.config).to include config
11
12
  end
@@ -15,9 +16,9 @@ describe Service do
15
16
  it 'prompts the user for their password and retrieves a token' do
16
17
  config = { 'user' => 'first.last', 'url' => 'http://default.url' }
17
18
  service = Service.new(MockOptions.new, config)
18
- allow(STDOUT).to receive(:puts).with('Enter your pooler service password:')
19
+ allow(STDOUT).to receive(:puts).with('Enter your http://default.url service password:')
19
20
  allow(Commander::UI).to(receive(:password)
20
- .with('Enter your pooler service password:', '*')
21
+ .with('Enter your http://default.url service password:', '*')
21
22
  .and_return('hunter2'))
22
23
  allow(Auth).to(receive(:get_token)
23
24
  .with(nil, config['url'], config['user'], 'hunter2')
@@ -28,11 +29,11 @@ describe Service do
28
29
  it 'prompts the user for their username and password if the username is unknown' do
29
30
  config = { 'url' => 'http://default.url' }
30
31
  service = Service.new(MockOptions.new({}), config)
31
- allow(STDOUT).to receive(:puts).with 'Enter your pooler service username:'
32
+ allow(STDOUT).to receive(:puts).with 'Enter your http://default.url service username:'
32
33
  allow(STDOUT).to receive(:puts).with "\n"
33
34
  allow(STDIN).to receive(:gets).and_return('first.last')
34
35
  allow(Commander::UI).to(receive(:password)
35
- .with('Enter your pooler service password:', '*')
36
+ .with('Enter your http://default.url service password:', '*')
36
37
  .and_return('hunter2'))
37
38
  allow(Auth).to(receive(:get_token)
38
39
  .with(nil, config['url'], 'first.last', 'hunter2')
@@ -43,31 +44,31 @@ describe Service do
43
44
 
44
45
  describe '#delete_token' do
45
46
  it 'deletes a token' do
46
- service = Service.new(MockOptions.new,{'user' => 'first.last', 'url' => 'http://default.url'})
47
+ service = Service.new(MockOptions.new, 'user' => 'first.last', 'url' => 'http://default.url')
47
48
  allow(Commander::UI).to(receive(:password)
48
- .with('Enter your pooler service password:', '*')
49
+ .with('Enter your http://default.url service password:', '*')
49
50
  .and_return('hunter2'))
50
51
  allow(Auth).to(receive(:delete_token)
51
52
  .with(nil, 'http://default.url', 'first.last', 'hunter2', 'token-value')
52
53
  .and_return('ok' => true))
53
- expect(service.delete_token(nil, 'token-value')).to eql({'ok' => true})
54
+ expect(service.delete_token(nil, 'token-value')).to eql('ok' => true)
54
55
  end
55
56
  end
56
57
 
57
58
  describe '#token_status' do
58
59
  it 'reports the status of a token' do
59
60
  config = {
60
- 'user' => 'first.last',
61
- 'url' => 'http://default.url'
61
+ 'user' => 'first.last',
62
+ 'url' => 'http://default.url',
62
63
  }
63
64
  options = MockOptions.new('token' => 'token-value')
64
65
  service = Service.new(options, config)
65
66
  status = {
66
- 'ok' => true,
67
- 'user' => config['user'],
68
- 'created' => '2017-09-22 02:04:18 +0000',
69
- 'last_accessed' => '2017-09-22 02:04:28 +0000',
70
- 'reserved_hosts' => []
67
+ 'ok' => true,
68
+ 'user' => config['user'],
69
+ 'created' => '2017-09-22 02:04:18 +0000',
70
+ 'last_accessed' => '2017-09-22 02:04:28 +0000',
71
+ 'reserved_hosts' => [],
71
72
  }
72
73
  allow(Auth).to(receive(:token_status)
73
74
  .with(nil, config['url'], 'token-value')
@@ -75,5 +76,4 @@ describe Service do
75
76
  expect(service.token_status(nil, 'token-value')).to eql(status)
76
77
  end
77
78
  end
78
-
79
79
  end
@@ -1,13 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'json'
3
5
  require 'commander/command'
4
6
  require_relative '../../lib/vmfloaty/utils'
5
7
 
6
8
  describe Utils do
7
-
8
- describe "#standardize_hostnames" do
9
+ describe '#standardize_hostnames' do
9
10
  before :each do
10
- @vmpooler_response_body ='{
11
+ @vmpooler_response_body = '{
11
12
  "ok": true,
12
13
  "domain": "delivery.mycompany.net",
13
14
  "ubuntu-1610-x86_64": {
@@ -28,79 +29,79 @@ describe Utils do
28
29
  }'
29
30
  end
30
31
 
31
- it "formats a result from vmpooler into a hash of os to hostnames" do
32
+ it 'formats a result from vmpooler into a hash of os to hostnames' do
32
33
  result = Utils.standardize_hostnames(JSON.parse(@vmpooler_response_body))
33
- expect(result).to eq('centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"],
34
- 'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"])
34
+ expect(result).to eq('centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'],
35
+ 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', 'ctnktsd0u11p9tm.delivery.mycompany.net'])
35
36
  end
36
37
 
37
- it "formats a result from the nonstandard pooler into a hash of os to hostnames" do
38
+ it 'formats a result from the nonstandard pooler into a hash of os to hostnames' do
38
39
  result = Utils.standardize_hostnames(JSON.parse(@nonstandard_response_body))
39
- expect(result).to eq('solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
40
+ expect(result).to eq('solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
40
41
  'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net'])
41
42
  end
42
43
  end
43
44
 
44
- describe "#format_host_output" do
45
+ describe '#format_host_output' do
45
46
  before :each do
46
47
  @vmpooler_results = {
47
- 'centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"],
48
- 'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"]
48
+ 'centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'],
49
+ 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', 'ctnktsd0u11p9tm.delivery.mycompany.net'],
49
50
  }
50
51
  @nonstandard_results = {
51
- 'solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
52
- 'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net']
52
+ 'solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
53
+ 'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net'],
53
54
  }
54
- @vmpooler_output = <<-OUT.chomp
55
- - dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64)
56
- - gdoy8q3nckuob0i.delivery.mycompany.net (ubuntu-1610-x86_64)
57
- - ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64)
55
+ @vmpooler_output = <<~OUT.chomp
56
+ - dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64)
57
+ - gdoy8q3nckuob0i.delivery.mycompany.net (ubuntu-1610-x86_64)
58
+ - ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64)
58
59
  OUT
59
- @nonstandard_output = <<-OUT.chomp
60
- - sol10-10.delivery.mycompany.net (solaris-10-sparc)
61
- - sol10-11.delivery.mycompany.net (solaris-10-sparc)
62
- - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8)
60
+ @nonstandard_output = <<~OUT.chomp
61
+ - sol10-10.delivery.mycompany.net (solaris-10-sparc)
62
+ - sol10-11.delivery.mycompany.net (solaris-10-sparc)
63
+ - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8)
63
64
  OUT
64
65
  end
65
- it "formats a hostname hash from vmpooler into a list that includes the os" do
66
+ it 'formats a hostname hash from vmpooler into a list that includes the os' do
66
67
  expect(Utils.format_host_output(@vmpooler_results)).to eq(@vmpooler_output)
67
68
  end
68
69
 
69
- it "formats a hostname hash from the nonstandard pooler into a list that includes the os" do
70
+ it 'formats a hostname hash from the nonstandard pooler into a list that includes the os' do
70
71
  expect(Utils.format_host_output(@nonstandard_results)).to eq(@nonstandard_output)
71
72
  end
72
73
  end
73
74
 
74
- describe "#get_service_object" do
75
- it "assumes vmpooler by default" do
75
+ describe '#get_service_object' do
76
+ it 'assumes vmpooler by default' do
76
77
  expect(Utils.get_service_object).to be Pooler
77
78
  end
78
79
 
79
- it "uses nspooler when told explicitly" do
80
- expect(Utils.get_service_object "nspooler").to be NonstandardPooler
80
+ it 'uses nspooler when told explicitly' do
81
+ expect(Utils.get_service_object('nspooler')).to be NonstandardPooler
81
82
  end
82
83
  end
83
84
 
84
- describe "#get_service_config" do
85
+ describe '#get_service_config' do
85
86
  before :each do
86
87
  @default_config = {
87
- "url" => "http://default.url",
88
- "user" => "first.last.default",
89
- "token" => "default-token",
88
+ 'url' => 'http://default.url',
89
+ 'user' => 'first.last.default',
90
+ 'token' => 'default-token',
90
91
  }
91
92
  @services_config = {
92
- "services" => {
93
- "vm" => {
94
- "url" => "http://vmpooler.url",
95
- "user" => "first.last.vmpooler",
96
- "token" => "vmpooler-token"
97
- },
98
- "ns" => {
99
- "url" => "http://nspooler.url",
100
- "user" => "first.last.nspooler",
101
- "token" => "nspooler-token"
102
- }
103
- }
93
+ 'services' => {
94
+ 'vm' => {
95
+ 'url' => 'http://vmpooler.url',
96
+ 'user' => 'first.last.vmpooler',
97
+ 'token' => 'vmpooler-token',
98
+ },
99
+ 'ns' => {
100
+ 'url' => 'http://nspooler.url',
101
+ 'user' => 'first.last.nspooler',
102
+ 'token' => 'nspooler-token',
103
+ },
104
+ },
104
105
  }
105
106
  end
106
107
 
@@ -110,70 +111,70 @@ describe Utils do
110
111
  expect(Utils.get_service_config(config, options)).to include @services_config['services']['vm']
111
112
  end
112
113
 
113
- it "allows selection by configured service key" do
114
+ it 'allows selection by configured service key' do
114
115
  config = @default_config.merge @services_config
115
- options = MockOptions.new({:service => "ns"})
116
+ options = MockOptions.new(:service => 'ns')
116
117
  expect(Utils.get_service_config(config, options)).to include @services_config['services']['ns']
117
118
  end
118
119
 
119
- it "uses top-level service config values as defaults when configured service values are missing" do
120
+ it 'uses top-level service config values as defaults when configured service values are missing' do
120
121
  config = @default_config.merge @services_config
121
- config["services"]['vm'].delete 'url'
122
- options = MockOptions.new({:service => "vm"})
122
+ config['services']['vm'].delete 'url'
123
+ options = MockOptions.new(:service => 'vm')
123
124
  expect(Utils.get_service_config(config, options)['url']).to eq 'http://default.url'
124
125
  end
125
126
 
126
127
  it "raises an error if passed a service name that hasn't been configured" do
127
128
  config = @default_config.merge @services_config
128
- options = MockOptions.new({:service => "none"})
129
+ options = MockOptions.new(:service => 'none')
129
130
  expect { Utils.get_service_config(config, options) }.to raise_error ArgumentError
130
131
  end
131
132
 
132
- it "prioritizes values passed as command line options over configuration options" do
133
+ it 'prioritizes values passed as command line options over configuration options' do
133
134
  config = @default_config
134
- options = MockOptions.new({:url => "http://alternate.url", :token => "alternate-token"})
135
- expected = config.merge({"url" => "http://alternate.url", "token" => "alternate-token"})
135
+ options = MockOptions.new(:url => 'http://alternate.url', :token => 'alternate-token')
136
+ expected = config.merge('url' => 'http://alternate.url', 'token' => 'alternate-token')
136
137
  expect(Utils.get_service_config(config, options)).to include expected
137
138
  end
138
139
  end
139
140
 
140
- describe "#generate_os_hash" do
141
+ describe '#generate_os_hash' do
141
142
  before :each do
142
- @host_hash = {"centos"=>1, "debian"=>5, "windows"=>1}
143
+ @host_hash = { 'centos' => 1, 'debian' => 5, 'windows' => 1 }
143
144
  end
144
145
 
145
- it "takes an array of os arguments and returns a formatted hash" do
146
- host_arg = ["centos", "debian=5", "windows=1"]
146
+ it 'takes an array of os arguments and returns a formatted hash' do
147
+ host_arg = ['centos', 'debian=5', 'windows=1']
147
148
  expect(Utils.generate_os_hash(host_arg)).to eq @host_hash
148
149
  end
149
150
 
150
- it "returns an empty hash if there are no arguments provided" do
151
+ it 'returns an empty hash if there are no arguments provided' do
151
152
  host_arg = []
152
153
  expect(Utils.generate_os_hash(host_arg)).to be_empty
153
154
  end
154
155
  end
155
156
 
156
157
  describe '#pretty_print_hosts' do
157
- let(:url) { 'http://pooler.example.com' }
158
+ let(:url) { 'http://pooler.example.com' }
158
159
 
159
160
  it 'prints a vmpooler output with host fqdn, template and duration info' do
160
161
  hostname = 'mcpy42eqjxli9g2'
161
162
  response_body = { hostname => {
162
- 'template' => 'ubuntu-1604-x86_64',
163
- 'lifetime' => 12,
164
- 'running' => 9.66,
165
- 'state' => 'running',
166
- 'ip' => '127.0.0.1',
167
- 'domain' => 'delivery.mycompany.net'
168
- }}
169
- output = "- mcpy42eqjxli9g2.delivery.mycompany.net (ubuntu-1604-x86_64, 9.66/12 hours)"
163
+ 'template' => 'ubuntu-1604-x86_64',
164
+ 'lifetime' => 12,
165
+ 'running' => 9.66,
166
+ 'state' => 'running',
167
+ 'ip' => '127.0.0.1',
168
+ 'domain' => 'delivery.mycompany.net',
169
+ } }
170
+ output = '- mcpy42eqjxli9g2.delivery.mycompany.net (ubuntu-1604-x86_64, 9.66/12 hours)'
170
171
 
171
172
  expect(Utils).to receive(:puts).with(output)
172
173
 
173
- service = Service.new(MockOptions.new, {'url' => url})
174
+ service = Service.new(MockOptions.new, 'url' => url)
174
175
  allow(service).to receive(:query)
175
- .with(nil, hostname)
176
- .and_return(response_body)
176
+ .with(nil, hostname)
177
+ .and_return(response_body)
177
178
 
178
179
  Utils.pretty_print_hosts(nil, service, hostname)
179
180
  end
@@ -181,46 +182,46 @@ describe Utils do
181
182
  it 'prints a vmpooler output with host fqdn, template, duration info, and tags when supplied' do
182
183
  hostname = 'aiydvzpg23r415q'
183
184
  response_body = { hostname => {
184
- 'template' => 'redhat-7-x86_64',
185
- 'lifetime' => 48,
186
- 'running' => 7.67,
187
- 'state' => 'running',
188
- 'tags' => {
189
- 'user' => 'bob',
190
- 'role' => 'agent'
191
- },
192
- 'ip' => '127.0.0.1',
193
- 'domain' => 'delivery.mycompany.net'
194
- }}
195
- output = "- aiydvzpg23r415q.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)"
185
+ 'template' => 'redhat-7-x86_64',
186
+ 'lifetime' => 48,
187
+ 'running' => 7.67,
188
+ 'state' => 'running',
189
+ 'tags' => {
190
+ 'user' => 'bob',
191
+ 'role' => 'agent',
192
+ },
193
+ 'ip' => '127.0.0.1',
194
+ 'domain' => 'delivery.mycompany.net',
195
+ } }
196
+ output = '- aiydvzpg23r415q.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)'
196
197
 
197
198
  expect(Utils).to receive(:puts).with(output)
198
199
 
199
- service = Service.new(MockOptions.new, {'url' => url})
200
+ service = Service.new(MockOptions.new, 'url' => url)
200
201
  allow(service).to receive(:query)
201
- .with(nil, hostname)
202
- .and_return(response_body)
202
+ .with(nil, hostname)
203
+ .and_return(response_body)
203
204
 
204
205
  Utils.pretty_print_hosts(nil, service, hostname)
205
206
  end
206
207
 
207
208
  it 'prints a nonstandard pooler output with host, template, and time remaining' do
208
- hostname = "sol11-9.delivery.mycompany.net"
209
+ hostname = 'sol11-9.delivery.mycompany.net'
209
210
  response_body = { hostname => {
210
- 'fqdn' => hostname,
211
- 'os_triple' => 'solaris-11-sparc',
212
- 'reserved_by_user' => 'first.last',
213
- 'reserved_for_reason' => '',
214
- 'hours_left_on_reservation' => 35.89
215
- }}
216
- output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining)"
211
+ 'fqdn' => hostname,
212
+ 'os_triple' => 'solaris-11-sparc',
213
+ 'reserved_by_user' => 'first.last',
214
+ 'reserved_for_reason' => '',
215
+ 'hours_left_on_reservation' => 35.89,
216
+ } }
217
+ output = '- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining)'
217
218
 
218
219
  expect(Utils).to receive(:puts).with(output)
219
220
 
220
- service = Service.new(MockOptions.new, {'url' => url, 'type' => 'ns'})
221
+ service = Service.new(MockOptions.new, 'url' => url, 'type' => 'ns')
221
222
  allow(service).to receive(:query)
222
- .with(nil, hostname)
223
- .and_return(response_body)
223
+ .with(nil, hostname)
224
+ .and_return(response_body)
224
225
 
225
226
  Utils.pretty_print_hosts(nil, service, hostname)
226
227
  end
@@ -228,20 +229,20 @@ describe Utils do
228
229
  it 'prints a nonstandard pooler output with host, template, time remaining, and reason' do
229
230
  hostname = 'sol11-9.delivery.mycompany.net'
230
231
  response_body = { hostname => {
231
- 'fqdn' => hostname,
232
- 'os_triple' => 'solaris-11-sparc',
233
- 'reserved_by_user' => 'first.last',
234
- 'reserved_for_reason' => 'testing',
235
- 'hours_left_on_reservation' => 35.89
236
- }}
237
- output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining, reason: testing)"
232
+ 'fqdn' => hostname,
233
+ 'os_triple' => 'solaris-11-sparc',
234
+ 'reserved_by_user' => 'first.last',
235
+ 'reserved_for_reason' => 'testing',
236
+ 'hours_left_on_reservation' => 35.89,
237
+ } }
238
+ output = '- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining, reason: testing)'
238
239
 
239
240
  expect(Utils).to receive(:puts).with(output)
240
241
 
241
- service = Service.new(MockOptions.new, {'url' => url, 'type' => 'ns'})
242
+ service = Service.new(MockOptions.new, 'url' => url, 'type' => 'ns')
242
243
  allow(service).to receive(:query)
243
- .with(nil, hostname)
244
- .and_return(response_body)
244
+ .with(nil, hostname)
245
+ .and_return(response_body)
245
246
 
246
247
  Utils.pretty_print_hosts(nil, service, hostname)
247
248
  end