vmfloaty 0.7.9 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,115 @@
1
1
  require 'spec_helper'
2
2
  require 'json'
3
+ require 'commander/command'
3
4
  require_relative '../../lib/vmfloaty/utils'
4
5
 
5
6
  describe Utils do
6
7
 
7
- describe "#get_hosts" do
8
+ describe "#format_hosts" do
8
9
  before :each do
9
- @hostname_hash = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"debian-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"},\"domain\":\"company.com\"}"
10
- @format_hash = "{\"debian-7-i386\":[\"sc0o4xqtodlul5w.company.com\",\"4m4dkhqiufnjmxy.company.com\"],\"debian-7-x86_64\":\"zb91y9qbrbf6d3q.company.com\"}"
10
+ @vmpooler_response_body ='{
11
+ "ok": true,
12
+ "domain": "delivery.mycompany.net",
13
+ "ubuntu-1610-x86_64": {
14
+ "hostname": ["gdoy8q3nckuob0i", "ctnktsd0u11p9tm"]
15
+ },
16
+ "centos-7-x86_64": {
17
+ "hostname": "dlgietfmgeegry2"
18
+ }
19
+ }'
20
+ @nonstandard_response_body = '{
21
+ "ok": true,
22
+ "solaris-10-sparc": {
23
+ "hostname": ["sol10-10.delivery.mycompany.net", "sol10-11.delivery.mycompany.net"]
24
+ },
25
+ "ubuntu-16.04-power8": {
26
+ "hostname": "power8-ubuntu16.04-6.delivery.mycompany.net"
27
+ }
28
+ }'
29
+ @vmpooler_output = <<-OUT
30
+ - gdoy8q3nckuob0i.delivery.mycompany.net (ubuntu-1610-x86_64)
31
+ - ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64)
32
+ - dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64)
33
+ OUT
34
+ @nonstandard_output = <<-OUT
35
+ - sol10-10.delivery.mycompany.net (solaris-10-sparc)
36
+ - sol10-11.delivery.mycompany.net (solaris-10-sparc)
37
+ - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8)
38
+ OUT
11
39
  end
12
40
 
13
- it "formats a hostname hash into os, hostnames, and domain name" do
41
+ it "formats a hostname hash from vmpooler into a list that includes the os" do
42
+ expect { Utils.format_hosts(JSON.parse(@vmpooler_response_body)) }.to output( @vmpooler_output).to_stdout_from_any_process
43
+ end
44
+
45
+ it "formats a hostname hash from the nonstandard pooler into a list that includes the os" do
46
+ expect { Utils.format_hosts(JSON.parse(@nonstandard_response_body)) }.to output(@nonstandard_output).to_stdout_from_any_process
47
+ end
48
+ end
49
+
50
+ describe "#get_service_object" do
51
+ it "assumes vmpooler by default" do
52
+ expect(Utils.get_service_object).to be Pooler
53
+ end
54
+
55
+ it "uses nspooler when told explicitly" do
56
+ expect(Utils.get_service_object "nspooler").to be NonstandardPooler
57
+ end
58
+ end
59
+
60
+ describe "#get_service_config" do
61
+ before :each do
62
+ @default_config = {
63
+ "url" => "http://default.url",
64
+ "user" => "first.last.default",
65
+ "token" => "default-token",
66
+ }
67
+ @services_config = {
68
+ "services" => {
69
+ "vm" => {
70
+ "url" => "http://vmpooler.url",
71
+ "user" => "first.last.vmpooler",
72
+ "token" => "vmpooler-token"
73
+ },
74
+ "ns" => {
75
+ "url" => "http://nspooler.url",
76
+ "user" => "first.last.nspooler",
77
+ "token" => "nspooler-token"
78
+ }
79
+ }
80
+ }
81
+ end
82
+
83
+ it "returns the first service configured under 'services' as the default if available" do
84
+ config = @default_config.merge @services_config
85
+ options = MockOptions.new({})
86
+ expect(Utils.get_service_config(config, options)).to include @services_config['services']['vm']
87
+ end
88
+
89
+ it "allows selection by configured service key" do
90
+ config = @default_config.merge @services_config
91
+ options = MockOptions.new({:service => "ns"})
92
+ expect(Utils.get_service_config(config, options)).to include @services_config['services']['ns']
93
+ end
94
+
95
+ it "uses top-level service config values as defaults when configured service values are missing" do
96
+ config = @default_config.merge @services_config
97
+ config["services"]['vm'].delete 'url'
98
+ options = MockOptions.new({:service => "vm"})
99
+ expect(Utils.get_service_config(config, options)['url']).to eq 'http://default.url'
100
+ end
101
+
102
+ it "raises an error if passed a service name that hasn't been configured" do
103
+ config = @default_config.merge @services_config
104
+ options = MockOptions.new({:service => "none"})
105
+ expect { Utils.get_service_config(config, options) }.to raise_error ArgumentError
106
+ end
14
107
 
15
- expect(Utils.format_hosts(JSON.parse(@hostname_hash))).to eq @format_hash
108
+ it "prioritizes values passed as command line options over configuration options" do
109
+ config = @default_config
110
+ options = MockOptions.new({:url => "http://alternate.url", :token => "alternate-token"})
111
+ expected = config.merge({"url" => "http://alternate.url", "token" => "alternate-token"})
112
+ expect(Utils.get_service_config(config, options)).to include expected
16
113
  end
17
114
  end
18
115
 
@@ -32,60 +129,97 @@ describe Utils do
32
129
  end
33
130
  end
34
131
 
35
- describe '#prettyprint_hosts' do
36
- let(:host_without_tags) { 'mcpy42eqjxli9g2' }
37
- let(:host_with_tags) { 'aiydvzpg23r415q' }
132
+ describe '#pretty_print_hosts' do
38
133
  let(:url) { 'http://pooler.example.com' }
39
134
 
40
- let(:host_info_with_tags) do
41
- {
42
- host_with_tags => {
43
- "template" => "redhat-7-x86_64",
44
- "lifetime" => 48,
45
- "running" => 7.67,
46
- "tags" => {
47
- "user" => "bob",
48
- "role" => "agent"
49
- },
50
- "domain" => "delivery.puppetlabs.net"
51
- }
52
- }
53
- end
54
-
55
- let(:host_info_without_tags) do
56
- {
57
- host_without_tags => {
58
- "template" => "ubuntu-1604-x86_64",
59
- "lifetime" => 12,
60
- "running" => 9.66,
61
- "domain" => "delivery.puppetlabs.net"
62
- }
63
- }
135
+ it 'prints a vmpooler output with host fqdn, template and duration info' do
136
+ hostname = 'mcpy42eqjxli9g2'
137
+ response_body = { hostname => {
138
+ 'template' => 'ubuntu-1604-x86_64',
139
+ 'lifetime' => 12,
140
+ 'running' => 9.66,
141
+ 'state' => 'running',
142
+ 'ip' => '127.0.0.1',
143
+ 'domain' => 'delivery.mycompany.net'
144
+ }}
145
+ output = "- mcpy42eqjxli9g2.delivery.mycompany.net (ubuntu-1604-x86_64, 9.66/12 hours)"
146
+
147
+ expect(Utils).to receive(:puts).with(output)
148
+
149
+ service = Service.new(MockOptions.new, {'url' => url})
150
+ allow(service).to receive(:query)
151
+ .with(nil, hostname)
152
+ .and_return(response_body)
153
+
154
+ Utils.pretty_print_hosts(nil, service, hostname)
64
155
  end
65
156
 
66
- let(:output_with_tags) { "- #{host_with_tags}.delivery.puppetlabs.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)" }
67
- let(:output_without_tags) { "- #{host_without_tags}.delivery.puppetlabs.net (ubuntu-1604-x86_64, 9.66/12 hours)" }
157
+ it 'prints a vmpooler output with host fqdn, template, duration info, and tags when supplied' do
158
+ hostname = 'aiydvzpg23r415q'
159
+ response_body = { hostname => {
160
+ 'template' => 'redhat-7-x86_64',
161
+ 'lifetime' => 48,
162
+ 'running' => 7.67,
163
+ 'state' => 'running',
164
+ 'tags' => {
165
+ 'user' => 'bob',
166
+ 'role' => 'agent'
167
+ },
168
+ 'ip' => '127.0.0.1',
169
+ 'domain' => 'delivery.mycompany.net'
170
+ }}
171
+ output = "- aiydvzpg23r415q.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)"
68
172
 
69
- it 'prints an output with host fqdn, template and duration info' do
70
- allow(Utils).to receive(:get_vm_info).
71
- with(host_without_tags, false, url).
72
- and_return(host_info_without_tags)
173
+ expect(Utils).to receive(:puts).with(output)
73
174
 
74
- expect(Utils).to receive(:puts).with("Running VMs:")
75
- expect(Utils).to receive(:puts).with(output_without_tags)
175
+ service = Service.new(MockOptions.new, {'url' => url})
176
+ allow(service).to receive(:query)
177
+ .with(nil, hostname)
178
+ .and_return(response_body)
76
179
 
77
- Utils.prettyprint_hosts(host_without_tags, false, url)
180
+ Utils.pretty_print_hosts(nil, service, hostname)
78
181
  end
79
182
 
80
- it 'prints an output with host fqdn, template, duration info, and tags when supplied' do
81
- allow(Utils).to receive(:get_vm_info).
82
- with(host_with_tags, false, url).
83
- and_return(host_info_with_tags)
84
-
85
- expect(Utils).to receive(:puts).with("Running VMs:")
86
- expect(Utils).to receive(:puts).with(output_with_tags)
183
+ it 'prints a nonstandard pooler output with host, template, and time remaining' do
184
+ hostname = "sol11-9.delivery.mycompany.net"
185
+ response_body = { hostname => {
186
+ 'fqdn' => hostname,
187
+ 'os_triple' => 'solaris-11-sparc',
188
+ 'reserved_by_user' => 'first.last',
189
+ 'reserved_for_reason' => '',
190
+ 'hours_left_on_reservation' => 35.89
191
+ }}
192
+ output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining)"
193
+
194
+ expect(Utils).to receive(:puts).with(output)
195
+
196
+ service = Service.new(MockOptions.new, {'url' => url, 'type' => 'ns'})
197
+ allow(service).to receive(:query)
198
+ .with(nil, hostname)
199
+ .and_return(response_body)
200
+
201
+ Utils.pretty_print_hosts(nil, service, hostname)
202
+ end
87
203
 
88
- Utils.prettyprint_hosts(host_with_tags, false, url)
204
+ it 'prints a nonstandard pooler output with host, template, time remaining, and reason' do
205
+ hostname = 'sol11-9.delivery.mycompany.net'
206
+ response_body = { hostname => {
207
+ 'fqdn' => hostname,
208
+ 'os_triple' => 'solaris-11-sparc',
209
+ 'reserved_by_user' => 'first.last',
210
+ 'reserved_for_reason' => 'testing',
211
+ 'hours_left_on_reservation' => 35.89
212
+ }}
213
+ output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining, reason: testing)"
214
+
215
+ expect(Utils).to receive(:puts).with(output)
216
+
217
+ service = Service.new(MockOptions.new, {'url' => url, 'type' => 'ns'})
218
+ allow(service).to receive(:query)
219
+ .with(nil, hostname)
220
+ .and_return(response_body)
221
+
222
+ Utils.pretty_print_hosts(nil, service, hostname)
89
223
  end
90
224
  end
91
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vmfloaty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-31 00:00:00.000000000 Z
11
+ date: 2017-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 0.8.1
55
55
  description: A helper tool for vmpooler to help you stay afloat
56
56
  email:
57
- - brian.cain@puppetlabs.com
57
+ - brianccain@gmail.com
58
58
  executables:
59
59
  - floaty
60
60
  extensions: []
@@ -69,13 +69,17 @@ files:
69
69
  - lib/vmfloaty/conf.rb
70
70
  - lib/vmfloaty/errors.rb
71
71
  - lib/vmfloaty/http.rb
72
+ - lib/vmfloaty/nonstandard_pooler.rb
72
73
  - lib/vmfloaty/pooler.rb
74
+ - lib/vmfloaty/service.rb
73
75
  - lib/vmfloaty/ssh.rb
74
76
  - lib/vmfloaty/utils.rb
75
77
  - lib/vmfloaty/version.rb
76
78
  - spec/spec_helper.rb
77
79
  - spec/vmfloaty/auth_spec.rb
80
+ - spec/vmfloaty/nonstandard_pooler_spec.rb
78
81
  - spec/vmfloaty/pooler_spec.rb
82
+ - spec/vmfloaty/service_spec.rb
79
83
  - spec/vmfloaty/utils_spec.rb
80
84
  homepage: https://github.com/briancain/vmfloaty
81
85
  licenses:
@@ -97,12 +101,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
101
  version: '0'
98
102
  requirements: []
99
103
  rubyforge_project:
100
- rubygems_version: 2.6.12
104
+ rubygems_version: 2.2.2
101
105
  signing_key:
102
106
  specification_version: 4
103
107
  summary: CLI application to interface with vmpooler
104
108
  test_files:
105
109
  - spec/spec_helper.rb
106
110
  - spec/vmfloaty/auth_spec.rb
111
+ - spec/vmfloaty/nonstandard_pooler_spec.rb
107
112
  - spec/vmfloaty/pooler_spec.rb
113
+ - spec/vmfloaty/service_spec.rb
108
114
  - spec/vmfloaty/utils_spec.rb