fog-libvirt 0.8.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -1
- data/fog-libvirt.gemspec +2 -2
- data/lib/fog/libvirt/compute.rb +7 -7
- data/lib/fog/libvirt/models/compute/server.rb +15 -10
- data/lib/fog/libvirt/models/compute/templates/server.xml.erb +10 -0
- data/lib/fog/libvirt/models/compute/templates/volume.xml.erb +8 -0
- data/lib/fog/libvirt/models/compute/volume.rb +2 -2
- data/lib/fog/libvirt/requests/compute/list_domains.rb +1 -1
- data/lib/fog/libvirt/requests/compute/list_networks.rb +13 -3
- data/lib/fog/libvirt/requests/compute/list_pools.rb +12 -9
- data/lib/fog/libvirt/requests/compute/vm_action.rb +3 -3
- data/lib/fog/libvirt/version.rb +1 -1
- data/minitests/server/user_data_iso_test.rb +0 -3
- data/minitests/test_helper.rb +1 -1
- data/tests/helper.rb +1 -1
- data/tests/helpers/formats_helper.rb +58 -56
- data/tests/helpers/formats_helper_tests.rb +22 -25
- data/tests/helpers/mock_helper.rb +1 -1
- data/tests/helpers/succeeds_helper.rb +2 -2
- data/tests/libvirt/requests/compute/list_pools_tests.rb +71 -0
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7115a1bb2c554de544d464049d48c6a15f2b39499a729180ab0e37bab55d3f3
|
4
|
+
data.tar.gz: c38e323849b149376f72edacd821788aa9df923d7bd4ca334dd0792a1ade5fab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc0dd759cec37c6e89557dec04bc05a679150183474cf589509cf0a36d51a129bcfd521b0ecbb2682b8eb76ef1d2bd25f81bff8e3117ba35961fd8d8f2d1a285
|
7
|
+
data.tar.gz: f7f1661e4e48a4fdaa68475ca34eb6f35bd346981891ea7cdb49a4b05d236b2886bf125ffb51ec2538277d3b7fe7584f3338eea30e0c4011a4712290d7370533
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
fog-libvirt is a libvirt provider for [fog](https://github.com/fog/fog).
|
4
4
|
|
5
|
-
[![Build Status](https://
|
5
|
+
[![Build Status](https://github.com/fog/fog-libvirt/actions/workflows/ruby.yml/badge.svg)](https://github.com/fog/fog-libvirt/actions/workflows/ruby.yml)
|
6
6
|
[![Dependency Status](https://gemnasium.com/fog/fog.png)](https://gemnasium.com/fog/fog-libvirt)
|
7
7
|
[![Code Climate](https://codeclimate.com/github/fog/fog.png)](https://codeclimate.com/github/fog/fog-libvirt)
|
8
8
|
[![Gem Version](https://fury-badge.herokuapp.com/rb/fog.png)](http://badge.fury.io/rb/fog-libvirt)
|
@@ -18,6 +18,60 @@ $ sudo gem install fog-libvirt
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
Example REPL session:
|
22
|
+
|
23
|
+
```
|
24
|
+
>> require "fog/libvirt"
|
25
|
+
=> true
|
26
|
+
>> compute = Fog::Compute.new(provider: :libvirt, libvirt_uri: "qemu:///session")
|
27
|
+
=> #<Fog::Libvirt::Compute::Real:46980 @uri=#<Fog::Libvirt::Util::URI:0x0000000002def920 @parsed_uri=#<URI::Generic qemu:/session>, @uri="qemu:///session"...
|
28
|
+
>> server = compute.servers.create(name: "test")
|
29
|
+
=>
|
30
|
+
<Fog::Libvirt::Compute::Server
|
31
|
+
id="bbb663e4-723b-4165-bc29-c77b54b12bca",
|
32
|
+
cpus=1,
|
33
|
+
cputime=0,
|
34
|
+
os_type="hvm",
|
35
|
+
memory_size=262144,
|
36
|
+
max_memory_size=262144,
|
37
|
+
name="test",
|
38
|
+
arch="x86_64",
|
39
|
+
persistent=true,
|
40
|
+
domain_type="kvm",
|
41
|
+
uuid="bbb663e4-723b-4165-bc29-c77b54b12bca",
|
42
|
+
autostart=false,
|
43
|
+
nics=[ <Fog::Libvirt::Compute::Nic
|
44
|
+
mac="52:54:00:d1:18:23",
|
45
|
+
id=nil,
|
46
|
+
type="network",
|
47
|
+
network="default",
|
48
|
+
bridge=nil,
|
49
|
+
model="virtio"
|
50
|
+
>],
|
51
|
+
volumes=[ <Fog::Libvirt::Compute::Volume
|
52
|
+
id=nil,
|
53
|
+
pool_name="1Download",
|
54
|
+
key=nil,
|
55
|
+
name="test.img",
|
56
|
+
path="/home/lzap/1Download/test.img",
|
57
|
+
capacity="10G",
|
58
|
+
allocation="1G",
|
59
|
+
owner=nil,
|
60
|
+
group=nil,
|
61
|
+
format_type="raw",
|
62
|
+
backing_volume=nil
|
63
|
+
>],
|
64
|
+
active=false,
|
65
|
+
boot_order=["hd", "cdrom", "network"],
|
66
|
+
display={:type=>"vnc", :port=>"-1", :listen=>"127.0.0.1"},
|
67
|
+
cpu={},
|
68
|
+
hugepages=false,
|
69
|
+
guest_agent=true,
|
70
|
+
virtio_rng={},
|
71
|
+
state="shutoff"
|
72
|
+
>
|
73
|
+
```
|
74
|
+
|
21
75
|
See [README.md](https://github.com/fog/fog-libvirt/blob/master/lib/fog/libvirt/models/compute/README.md).
|
22
76
|
|
23
77
|
## Contributing
|
data/fog-libvirt.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.rdoc_options = ["--charset=UTF-8"]
|
24
24
|
s.extra_rdoc_files = %w[README.md]
|
25
25
|
|
26
|
-
s.required_ruby_version = '>= 2.
|
26
|
+
s.required_ruby_version = '>= 2.5.0'
|
27
27
|
|
28
28
|
s.add_dependency("fog-core", ">= 1.27.4")
|
29
29
|
s.add_dependency("fog-json")
|
@@ -40,7 +40,7 @@ Gem::Specification.new do |s|
|
|
40
40
|
s.add_development_dependency("shindo", "~> 0.3.4")
|
41
41
|
s.add_development_dependency("simplecov")
|
42
42
|
s.add_development_dependency("yard")
|
43
|
-
s.add_development_dependency("mocha", "~> 1.
|
43
|
+
s.add_development_dependency("mocha", "~> 1.15.0")
|
44
44
|
|
45
45
|
# Let's not ship dot files and gemfiles
|
46
46
|
git_files = `git ls-files`.split("\n")
|
data/lib/fog/libvirt/compute.rb
CHANGED
@@ -120,16 +120,16 @@ module Fog
|
|
120
120
|
# on macosx, chances are we are using libvirt through homebrew
|
121
121
|
# the client will default to a socket location based on it's own location (/opt)
|
122
122
|
# we conveniently point it to /var/run/libvirt/libvirt-sock
|
123
|
-
# if no socket option has been specified explicitly
|
123
|
+
# if no socket option has been specified explicitly and
|
124
|
+
# if the socket exists
|
124
125
|
|
125
|
-
|
126
|
+
socketpath="/var/run/libvirt/libvirt-sock"
|
127
|
+
if RUBY_PLATFORM =~ /darwin/ && File.exist?(socketpath)
|
126
128
|
querystring=::URI.parse(uri).query
|
127
129
|
if querystring.nil?
|
128
|
-
append="?socket
|
129
|
-
|
130
|
-
|
131
|
-
append="&socket=/var/run/libvirt/libvirt-sock"
|
132
|
-
end
|
130
|
+
append="?socket=#{socketpath}"
|
131
|
+
elsif !::CGI.parse(querystring).key?("socket")
|
132
|
+
append="&socket=#{socketpath}"
|
133
133
|
end
|
134
134
|
end
|
135
135
|
uri+append
|
@@ -90,9 +90,13 @@ module Fog
|
|
90
90
|
volumes.first.path if volumes and volumes.first
|
91
91
|
end
|
92
92
|
|
93
|
-
def destroy(options={ :destroy_volumes => false})
|
93
|
+
def destroy(options={ :destroy_volumes => false, :flags => 0 })
|
94
94
|
poweroff unless stopped?
|
95
|
-
|
95
|
+
if options[:flags].zero?
|
96
|
+
service.vm_action(uuid, :undefine)
|
97
|
+
else
|
98
|
+
service.vm_action(uuid, :undefine, options[:flags])
|
99
|
+
end
|
96
100
|
volumes.each { |vol| vol.destroy } if options[:destroy_volumes]
|
97
101
|
true
|
98
102
|
end
|
@@ -347,6 +351,9 @@ module Fog
|
|
347
351
|
ip_address.chomp
|
348
352
|
end
|
349
353
|
|
354
|
+
# Locale-friendly removal of non-alpha nums
|
355
|
+
DOMAIN_CLEANUP_REGEXP = Regexp.compile('[\W_-]')
|
356
|
+
|
350
357
|
# This retrieves the ip address of the mac address using ip_command
|
351
358
|
# It returns an array of public and private ip addresses
|
352
359
|
# Currently only one ip address is returned, but in the future this could be multiple
|
@@ -360,7 +367,7 @@ module Fog
|
|
360
367
|
ip_command_global=service_arg.ip_command.nil? ? 'grep $mac /var/log/arpwatch.log|sed -e "s/new station//"|sed -e "s/changed ethernet address//g" |sed -e "s/reused old ethernet //" |tail -1 |cut -d ":" -f 4-| cut -d " " -f 3' : service_arg.ip_command
|
361
368
|
ip_command_local=options[:ip_command].nil? ? ip_command_global : options[:ip_command]
|
362
369
|
|
363
|
-
ip_command="mac=#{mac}; server_name=#{name}; "+ip_command_local
|
370
|
+
ip_command="mac=#{mac}; server_name=#{name.gsub(DOMAIN_CLEANUP_REGEXP, '_')}; "+ip_command_local
|
364
371
|
|
365
372
|
ip_address=nil
|
366
373
|
|
@@ -404,13 +411,11 @@ module Fog
|
|
404
411
|
ip_address = nil
|
405
412
|
nic = self.nics.find {|nic| nic.mac==mac}
|
406
413
|
if !nic.nil?
|
407
|
-
service.networks.all
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
break
|
413
|
-
end
|
414
|
+
net = service.networks.all(:name => nic.network).first
|
415
|
+
if !net.nil?
|
416
|
+
leases = net.dhcp_leases(mac, 0)
|
417
|
+
# Assume the lease expiring last is the current IP address
|
418
|
+
ip_address = leases.sort_by { |lse| lse["expirytime"] }.last["ipaddr"] if !leases.empty?
|
414
419
|
end
|
415
420
|
end
|
416
421
|
|
@@ -110,6 +110,13 @@
|
|
110
110
|
<backend model='<%= rng_backend_model %>'/>
|
111
111
|
<% end -%>
|
112
112
|
</rng>
|
113
|
+
<% if arch == "s390x" -%>
|
114
|
+
<controller type="scsi" index="0" model="virtio-scsi"/>
|
115
|
+
<console type="pty">
|
116
|
+
<target type="sclp"/>
|
117
|
+
</console>
|
118
|
+
<memballoon model="virtio"/>
|
119
|
+
<% else -%>
|
113
120
|
<serial type='pty'>
|
114
121
|
<target port='0'/>
|
115
122
|
</serial>
|
@@ -118,6 +125,7 @@
|
|
118
125
|
</console>
|
119
126
|
<input type='tablet' bus='usb'/>
|
120
127
|
<input type='mouse' bus='ps2'/>
|
128
|
+
<% end -%>
|
121
129
|
<%
|
122
130
|
display_type = display[:type]
|
123
131
|
|
@@ -137,9 +145,11 @@ unless display[:password].empty?
|
|
137
145
|
display_password = "passwd='#{display[:password]}'"
|
138
146
|
end
|
139
147
|
-%>
|
148
|
+
<% if arch != "s390x" -%>
|
140
149
|
<graphics type='<%= display_type %>' port='<%= display_port %>' autoport='<%= autoport %>' <%= display_listen %> <%= display_password %> />
|
141
150
|
<video>
|
142
151
|
<model type='cirrus' vram='9216' heads='1'/>
|
143
152
|
</video>
|
153
|
+
<% end -%>
|
144
154
|
</devices>
|
145
155
|
</domain>
|
@@ -5,8 +5,12 @@
|
|
5
5
|
<target>
|
6
6
|
<format type="<%= format_type %>"/>
|
7
7
|
<permissions>
|
8
|
+
<% if owner -%>
|
8
9
|
<owner><%= owner %></owner>
|
10
|
+
<% end -%>
|
11
|
+
<% if group -%>
|
9
12
|
<group><%= group %></group>
|
13
|
+
<% end -%>
|
10
14
|
<mode>0744</mode>
|
11
15
|
<label>virt_image_t</label>
|
12
16
|
</permissions>
|
@@ -16,8 +20,12 @@
|
|
16
20
|
<path><%= backing_volume.path %></path>
|
17
21
|
<format type="<%= backing_volume.format_type %>"/>
|
18
22
|
<permissions>
|
23
|
+
<% if owner -%>
|
19
24
|
<owner><%= owner %></owner>
|
25
|
+
<% end -%>
|
26
|
+
<% if group -%>
|
20
27
|
<group><%= group %></group>
|
28
|
+
<% end -%>
|
21
29
|
<mode>0744</mode>
|
22
30
|
<label>virt_image_t</label>
|
23
31
|
</permissions>
|
@@ -31,7 +31,7 @@ module Fog
|
|
31
31
|
|
32
32
|
begin
|
33
33
|
bridge_name = net.bridge_name
|
34
|
-
rescue Libvirt::Error
|
34
|
+
rescue ::Libvirt::Error
|
35
35
|
bridge_name = ''
|
36
36
|
end
|
37
37
|
|
@@ -44,8 +44,8 @@ module Fog
|
|
44
44
|
end
|
45
45
|
|
46
46
|
class Mock
|
47
|
-
def list_networks(
|
48
|
-
[ {
|
47
|
+
def list_networks(filter={ })
|
48
|
+
networks = [ {
|
49
49
|
:uuid => 'a29146ea-39b2-412d-8f53-239eef117a32',
|
50
50
|
:name => 'net1',
|
51
51
|
:bridge_name => 'virbr0'
|
@@ -56,6 +56,16 @@ module Fog
|
|
56
56
|
:bridge_name => 'virbr1'
|
57
57
|
}
|
58
58
|
]
|
59
|
+
return networks if filter.empty?
|
60
|
+
|
61
|
+
case filter.keys.first
|
62
|
+
when :uuid
|
63
|
+
[networks.find(:uuid => filter[:uuid]).first]
|
64
|
+
when :name
|
65
|
+
[networks.find(:name => filter[:name]).first]
|
66
|
+
else
|
67
|
+
networks
|
68
|
+
end
|
59
69
|
end
|
60
70
|
end
|
61
71
|
end
|
@@ -5,19 +5,22 @@ module Fog
|
|
5
5
|
def list_pools(filter = { })
|
6
6
|
data=[]
|
7
7
|
if filter.key?(:name)
|
8
|
-
data << find_pool_by_name(filter[:name])
|
8
|
+
data << find_pool_by_name(filter[:name], filter[:include_inactive])
|
9
9
|
elsif filter.key?(:uuid)
|
10
|
-
data << find_pool_by_uuid(filter[:uuid])
|
10
|
+
data << find_pool_by_uuid(filter[:uuid], filter[:include_inactive])
|
11
11
|
else
|
12
12
|
(client.list_storage_pools + client.list_defined_storage_pools).each do |name|
|
13
|
-
data << find_pool_by_name(name)
|
13
|
+
data << find_pool_by_name(name, filter[:include_inactive])
|
14
14
|
end
|
15
15
|
end
|
16
16
|
data.compact
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
-
|
20
|
+
|
21
|
+
private_class_method def self.pool_to_attributes(pool, include_inactive = nil)
|
22
|
+
return nil unless pool.active? || include_inactive
|
23
|
+
|
21
24
|
states=[:inactive, :building, :running, :degrated, :inaccessible]
|
22
25
|
{
|
23
26
|
:uuid => pool.uuid,
|
@@ -27,19 +30,19 @@ module Fog
|
|
27
30
|
:name => pool.name,
|
28
31
|
:allocation => pool.info.allocation,
|
29
32
|
:capacity => pool.info.capacity,
|
30
|
-
:num_of_volumes => pool.num_of_volumes,
|
33
|
+
:num_of_volumes => pool.active? ? pool.num_of_volumes : nil,
|
31
34
|
:state => states[pool.info.state]
|
32
35
|
}
|
33
36
|
end
|
34
37
|
|
35
|
-
def find_pool_by_name name
|
36
|
-
pool_to_attributes(client.lookup_storage_pool_by_name(name))
|
38
|
+
def find_pool_by_name name, include_inactive
|
39
|
+
pool_to_attributes(client.lookup_storage_pool_by_name(name), include_inactive)
|
37
40
|
rescue ::Libvirt::RetrieveError
|
38
41
|
nil
|
39
42
|
end
|
40
43
|
|
41
|
-
def find_pool_by_uuid uuid
|
42
|
-
pool_to_attributes(client.lookup_storage_pool_by_uuid(uuid))
|
44
|
+
def find_pool_by_uuid uuid, include_inactive
|
45
|
+
pool_to_attributes(client.lookup_storage_pool_by_uuid(uuid), include_inactive)
|
43
46
|
rescue ::Libvirt::RetrieveError
|
44
47
|
nil
|
45
48
|
end
|
@@ -2,15 +2,15 @@ module Fog
|
|
2
2
|
module Libvirt
|
3
3
|
class Compute
|
4
4
|
class Real
|
5
|
-
def vm_action(uuid, action)
|
5
|
+
def vm_action(uuid, action, *params)
|
6
6
|
domain = client.lookup_domain_by_uuid(uuid)
|
7
|
-
domain.send(action)
|
7
|
+
domain.send(action, *params)
|
8
8
|
true
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
class Mock
|
13
|
-
def vm_action(uuid, action)
|
13
|
+
def vm_action(uuid, action, *params)
|
14
14
|
true
|
15
15
|
end
|
16
16
|
end
|
data/lib/fog/libvirt/version.rb
CHANGED
@@ -32,7 +32,6 @@ class UserDataIsoTest < Minitest::Test
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_volume_is_created_during_user_data_iso_generation
|
35
|
-
iso_path = "iso_file_path"
|
36
35
|
@server.stubs(:system).returns(true)
|
37
36
|
Fog::Libvirt::Compute::Volumes.any_instance.expects(:create).
|
38
37
|
with(has_entries(:name => @server.cloud_init_volume_name)).
|
@@ -43,7 +42,6 @@ class UserDataIsoTest < Minitest::Test
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def test_volume_is_uploaded_during_user_data_iso_generation
|
46
|
-
iso_path = "iso_file_path"
|
47
45
|
@server.stubs(:system).returns(true)
|
48
46
|
Fog::Libvirt::Compute::Volumes.any_instance.stubs(:create).returns(@compute.volumes.new)
|
49
47
|
Fog::Libvirt::Compute::Volume.any_instance.expects(:upload_image).returns(true)
|
@@ -52,7 +50,6 @@ class UserDataIsoTest < Minitest::Test
|
|
52
50
|
end
|
53
51
|
|
54
52
|
def test_iso_file_is_set_during_user_data_iso_generation
|
55
|
-
iso_path = "iso_file_path"
|
56
53
|
@server.stubs(:system).returns(true)
|
57
54
|
Fog::Libvirt::Compute::Volumes.any_instance.stubs(:create).returns(@compute.volumes.new)
|
58
55
|
Fog::Libvirt::Compute::Volume.any_instance.stubs(:upload_image)
|
data/minitests/test_helper.rb
CHANGED
data/tests/helper.rb
CHANGED
@@ -3,7 +3,7 @@ ENV['FOG_CREDENTIAL'] = ENV['FOG_CREDENTIAL'] || 'default'
|
|
3
3
|
|
4
4
|
require 'fog/libvirt'
|
5
5
|
|
6
|
-
Excon.defaults.merge!(:
|
6
|
+
Excon.defaults.merge!(debug_request: true, debug_response: true)
|
7
7
|
|
8
8
|
require File.expand_path(File.join(File.dirname(__FILE__), 'helpers', 'mock_helper'))
|
9
9
|
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fog/schema/data_validator'
|
2
4
|
|
3
5
|
# format related hackery
|
4
6
|
# allows both true.is_a?(Fog::Boolean) and false.is_a?(Fog::Boolean)
|
@@ -15,62 +17,62 @@ module Fog
|
|
15
17
|
module Array; end
|
16
18
|
end
|
17
19
|
end
|
18
|
-
[FalseClass, TrueClass].each {|klass| klass.send(:include, Fog::Boolean)}
|
19
|
-
[FalseClass, TrueClass, NilClass, Fog::Boolean].each {|klass| klass.send(:include, Fog::Nullable::Boolean)}
|
20
|
-
[NilClass, String].each {|klass| klass.send(:include, Fog::Nullable::String)}
|
21
|
-
[NilClass, Time].each {|klass| klass.send(:include, Fog::Nullable::Time)}
|
22
|
-
[Integer, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Integer)}
|
23
|
-
[Float, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Float)}
|
24
|
-
[Hash, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Hash)}
|
25
|
-
[Array, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Array)}
|
20
|
+
[FalseClass, TrueClass].each { |klass| klass.send(:include, Fog::Boolean) }
|
21
|
+
[FalseClass, TrueClass, NilClass, Fog::Boolean].each { |klass| klass.send(:include, Fog::Nullable::Boolean) }
|
22
|
+
[NilClass, String].each { |klass| klass.send(:include, Fog::Nullable::String) }
|
23
|
+
[NilClass, Time].each { |klass| klass.send(:include, Fog::Nullable::Time) }
|
24
|
+
[Integer, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Integer) }
|
25
|
+
[Float, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Float) }
|
26
|
+
[Hash, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Hash) }
|
27
|
+
[Array, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Array) }
|
26
28
|
|
27
29
|
module Shindo
|
30
|
+
# Generates a Shindo test that compares a hash schema to the result
|
31
|
+
# of the passed in block returning true if they match.
|
32
|
+
#
|
33
|
+
# The schema that is passed in is a Hash or Array of hashes that
|
34
|
+
# have Classes in place of values. When checking the schema the
|
35
|
+
# value should match the Class.
|
36
|
+
#
|
37
|
+
# Strict mode will fail if the data has additional keys. Setting
|
38
|
+
# +strict+ to +false+ will allow additional keys to appear.
|
39
|
+
#
|
40
|
+
# @param [Hash] schema A Hash schema
|
41
|
+
# @param [Hash] options Options to change validation rules
|
42
|
+
# @option options [Boolean] :allow_extra_keys
|
43
|
+
# If +true+ does not fail when keys are in the data that are
|
44
|
+
# not specified in the schema. This allows new values to
|
45
|
+
# appear in API output without breaking the check.
|
46
|
+
# @option options [Boolean] :allow_optional_rules
|
47
|
+
# If +true+ does not fail if extra keys are in the schema
|
48
|
+
# that do not match the data. Not recommended!
|
49
|
+
# @yield Data to check with schema
|
50
|
+
#
|
51
|
+
# @example Using in a test
|
52
|
+
# Shindo.tests("comparing welcome data against schema") do
|
53
|
+
# data = {:welcome => "Hello" }
|
54
|
+
# data_matches_schema(:welcome => String) { data }
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# comparing welcome data against schema
|
58
|
+
# + data matches schema
|
59
|
+
#
|
60
|
+
# @example Example schema
|
61
|
+
# {
|
62
|
+
# "id" => String,
|
63
|
+
# "ram" => Integer,
|
64
|
+
# "disks" => [
|
65
|
+
# {
|
66
|
+
# "size" => Float
|
67
|
+
# }
|
68
|
+
# ],
|
69
|
+
# "dns_name" => Fog::Nullable::String,
|
70
|
+
# "active" => Fog::Boolean,
|
71
|
+
# "created" => DateTime
|
72
|
+
# }
|
73
|
+
#
|
74
|
+
# @return [Boolean]
|
28
75
|
class Tests
|
29
|
-
# Generates a Shindo test that compares a hash schema to the result
|
30
|
-
# of the passed in block returning true if they match.
|
31
|
-
#
|
32
|
-
# The schema that is passed in is a Hash or Array of hashes that
|
33
|
-
# have Classes in place of values. When checking the schema the
|
34
|
-
# value should match the Class.
|
35
|
-
#
|
36
|
-
# Strict mode will fail if the data has additional keys. Setting
|
37
|
-
# +strict+ to +false+ will allow additional keys to appear.
|
38
|
-
#
|
39
|
-
# @param [Hash] schema A Hash schema
|
40
|
-
# @param [Hash] options Options to change validation rules
|
41
|
-
# @option options [Boolean] :allow_extra_keys
|
42
|
-
# If +true+ does not fail when keys are in the data that are
|
43
|
-
# not specified in the schema. This allows new values to
|
44
|
-
# appear in API output without breaking the check.
|
45
|
-
# @option options [Boolean] :allow_optional_rules
|
46
|
-
# If +true+ does not fail if extra keys are in the schema
|
47
|
-
# that do not match the data. Not recommended!
|
48
|
-
# @yield Data to check with schema
|
49
|
-
#
|
50
|
-
# @example Using in a test
|
51
|
-
# Shindo.tests("comparing welcome data against schema") do
|
52
|
-
# data = {:welcome => "Hello" }
|
53
|
-
# data_matches_schema(:welcome => String) { data }
|
54
|
-
# end
|
55
|
-
#
|
56
|
-
# comparing welcome data against schema
|
57
|
-
# + data matches schema
|
58
|
-
#
|
59
|
-
# @example Example schema
|
60
|
-
# {
|
61
|
-
# "id" => String,
|
62
|
-
# "ram" => Integer,
|
63
|
-
# "disks" => [
|
64
|
-
# {
|
65
|
-
# "size" => Float
|
66
|
-
# }
|
67
|
-
# ],
|
68
|
-
# "dns_name" => Fog::Nullable::String,
|
69
|
-
# "active" => Fog::Boolean,
|
70
|
-
# "created" => DateTime
|
71
|
-
# }
|
72
|
-
#
|
73
|
-
# @return [Boolean]
|
74
76
|
def data_matches_schema(schema, options = {})
|
75
77
|
test('data matches schema') do
|
76
78
|
validator = Fog::Schema::DataValidator.new
|
@@ -84,9 +86,9 @@ module Shindo
|
|
84
86
|
def formats(format, strict = true)
|
85
87
|
test('has proper format') do
|
86
88
|
if strict
|
87
|
-
options = {:
|
89
|
+
options = { allow_extra_keys: false, allow_optional_rules: true }
|
88
90
|
else
|
89
|
-
options = {:
|
91
|
+
options = { allow_extra_keys: true, allow_optional_rules: true }
|
90
92
|
end
|
91
93
|
validator = Fog::Schema::DataValidator.new
|
92
94
|
valid = validator.validate(yield, format, options)
|
@@ -1,37 +1,37 @@
|
|
1
1
|
Shindo.tests('test_helper', 'meta') do
|
2
2
|
|
3
3
|
tests('comparing welcome data against schema') do
|
4
|
-
data = {:
|
5
|
-
data_matches_schema(:
|
4
|
+
data = { welcome: 'Hello' }
|
5
|
+
data_matches_schema(welcome: String) { data }
|
6
6
|
end
|
7
7
|
|
8
8
|
tests('#data_matches_schema') do
|
9
9
|
tests('when value matches schema expectation') do
|
10
|
-
data_matches_schema(
|
10
|
+
data_matches_schema('key' => String) { { 'key' => 'Value' } }
|
11
11
|
end
|
12
12
|
|
13
13
|
tests('when values within an array all match schema expectation') do
|
14
|
-
data_matches_schema(
|
14
|
+
data_matches_schema('key' => [Integer]) { { 'key' => [1, 2] } }
|
15
15
|
end
|
16
16
|
|
17
17
|
tests('when nested values match schema expectation') do
|
18
|
-
data_matches_schema(
|
18
|
+
data_matches_schema('key' => { nested_key: String }) { { 'key' => { nested_key: 'Value' } } }
|
19
19
|
end
|
20
20
|
|
21
21
|
tests('when collection of values all match schema expectation') do
|
22
|
-
data_matches_schema([{
|
22
|
+
data_matches_schema([{ 'key' => String }]) { [{ 'key' => 'Value' }, { 'key' => 'Value' }] }
|
23
23
|
end
|
24
24
|
|
25
25
|
tests('when collection is empty although schema covers optional members') do
|
26
|
-
data_matches_schema([{
|
26
|
+
data_matches_schema([{ 'key' => String }], allow_optional_rules: true) { [] }
|
27
27
|
end
|
28
28
|
|
29
29
|
tests('when additional keys are passed and not strict') do
|
30
|
-
data_matches_schema({
|
30
|
+
data_matches_schema({ 'key' => String }, allow_extra_keys: true) { { 'key' => 'Value', extra: 'Bonus' } }
|
31
31
|
end
|
32
32
|
|
33
33
|
tests('when value is nil and schema expects NilClass') do
|
34
|
-
data_matches_schema(
|
34
|
+
data_matches_schema('key' => NilClass) { { 'key' => nil } }
|
35
35
|
end
|
36
36
|
|
37
37
|
tests('when value and schema match as hashes') do
|
@@ -43,46 +43,45 @@ Shindo.tests('test_helper', 'meta') do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
tests('when value is a Time') do
|
46
|
-
data_matches_schema(
|
46
|
+
data_matches_schema('time' => Time) { { 'time' => Time.now } }
|
47
47
|
end
|
48
48
|
|
49
49
|
tests('when key is missing but value should be NilClass (#1477)') do
|
50
|
-
data_matches_schema({
|
50
|
+
data_matches_schema({ 'key' => NilClass }, allow_optional_rules: true) { {} }
|
51
51
|
end
|
52
52
|
|
53
53
|
tests('when key is missing but value is nullable (#1477)') do
|
54
|
-
data_matches_schema({
|
54
|
+
data_matches_schema({ 'key' => Fog::Nullable::String }, allow_optional_rules: true) { {} }
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
tests('#formats backwards compatible changes') do
|
59
|
-
|
60
59
|
tests('when value matches schema expectation') do
|
61
|
-
formats(
|
60
|
+
formats('key' => String) { { 'key' => 'Value' } }
|
62
61
|
end
|
63
62
|
|
64
63
|
tests('when values within an array all match schema expectation') do
|
65
|
-
formats(
|
64
|
+
formats('key' => [Integer]) { { 'key' => [1, 2] } }
|
66
65
|
end
|
67
66
|
|
68
67
|
tests('when nested values match schema expectation') do
|
69
|
-
formats(
|
68
|
+
formats('key' => { nested_key: String }) { { 'key' => { nested_key: 'Value' } } }
|
70
69
|
end
|
71
70
|
|
72
71
|
tests('when collection of values all match schema expectation') do
|
73
|
-
formats([{
|
72
|
+
formats([{ 'key' => String }]) { [{ 'key' => 'Value' }, { 'key' => 'Value' }] }
|
74
73
|
end
|
75
74
|
|
76
75
|
tests('when collection is empty although schema covers optional members') do
|
77
|
-
formats([{
|
76
|
+
formats([{ 'key' => String }]) { [] }
|
78
77
|
end
|
79
78
|
|
80
79
|
tests('when additional keys are passed and not strict') do
|
81
|
-
formats({
|
80
|
+
formats({ 'key' => String }, false) { { 'key' => 'Value', :extra => 'Bonus' } }
|
82
81
|
end
|
83
82
|
|
84
83
|
tests('when value is nil and schema expects NilClass') do
|
85
|
-
formats(
|
84
|
+
formats('key' => NilClass) { { 'key' => nil } }
|
86
85
|
end
|
87
86
|
|
88
87
|
tests('when value and schema match as hashes') do
|
@@ -94,17 +93,15 @@ Shindo.tests('test_helper', 'meta') do
|
|
94
93
|
end
|
95
94
|
|
96
95
|
tests('when value is a Time') do
|
97
|
-
formats(
|
96
|
+
formats('time' => Time) { { 'time' => Time.now } }
|
98
97
|
end
|
99
98
|
|
100
99
|
tests('when key is missing but value should be NilClass (#1477)') do
|
101
|
-
formats(
|
100
|
+
formats('key' => NilClass) { {} }
|
102
101
|
end
|
103
102
|
|
104
103
|
tests('when key is missing but value is nullable (#1477)') do
|
105
|
-
formats(
|
104
|
+
formats('key' => Fog::Nullable::String) { {} }
|
106
105
|
end
|
107
|
-
|
108
106
|
end
|
109
|
-
|
110
107
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class FakePool < Fog::Model
|
2
|
+
# Fake pool object to allow exercising the internal parsing of pools
|
3
|
+
# returned by the client queries
|
4
|
+
identity :uuid
|
5
|
+
|
6
|
+
attribute :persistent
|
7
|
+
attribute :autostart
|
8
|
+
attribute :active
|
9
|
+
attribute :name
|
10
|
+
attribute :num_of_volumes
|
11
|
+
attr_reader :info
|
12
|
+
|
13
|
+
class FakeInfo < Fog::Model
|
14
|
+
attribute :allocation
|
15
|
+
attribute :capacity
|
16
|
+
attribute :state
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(attributes = {})
|
20
|
+
@info = FakeInfo.new(attributes.dup.delete(:info))
|
21
|
+
super(attributes)
|
22
|
+
end
|
23
|
+
|
24
|
+
def active?
|
25
|
+
active
|
26
|
+
end
|
27
|
+
|
28
|
+
def autostart?
|
29
|
+
autostart
|
30
|
+
end
|
31
|
+
|
32
|
+
def persistent?
|
33
|
+
persistent
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Shindo.tests("Fog::Compute[:libvirt] | list_pools request", 'libvirt') do
|
38
|
+
|
39
|
+
compute = Fog::Compute[:libvirt]
|
40
|
+
|
41
|
+
tests("Lists Pools") do
|
42
|
+
response = compute.list_pools
|
43
|
+
test("should be an array") { response.kind_of? Array }
|
44
|
+
test("should have two pools") { response.length == 2 }
|
45
|
+
end
|
46
|
+
|
47
|
+
tests("Handle Inactive Pools") do
|
48
|
+
inactive_pool = {
|
49
|
+
:uuid => 'pool.uuid',
|
50
|
+
:persistent => true,
|
51
|
+
:autostart => true,
|
52
|
+
:active => false,
|
53
|
+
:name => 'inactive_pool1',
|
54
|
+
:info => {
|
55
|
+
:allocation => 123456789,
|
56
|
+
:capacity => 123456789,
|
57
|
+
:state => 2 # running
|
58
|
+
},
|
59
|
+
:num_of_volumes => 3
|
60
|
+
}
|
61
|
+
|
62
|
+
response = ::Fog::Libvirt::Compute::Real.send(:pool_to_attributes, FakePool.new(inactive_pool), true)
|
63
|
+
|
64
|
+
test("should be hash of attributes") { response.kind_of? Hash }
|
65
|
+
|
66
|
+
response = ::Fog::Libvirt::Compute::Real.send(:pool_to_attributes, FakePool.new(inactive_pool))
|
67
|
+
|
68
|
+
test("should be nil") { response.nil? }
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fog-libvirt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- geemus (Wesley Beary)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fog-core
|
@@ -212,14 +212,14 @@ dependencies:
|
|
212
212
|
requirements:
|
213
213
|
- - "~>"
|
214
214
|
- !ruby/object:Gem::Version
|
215
|
-
version: 1.
|
215
|
+
version: 1.15.0
|
216
216
|
type: :development
|
217
217
|
prerelease: false
|
218
218
|
version_requirements: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
220
|
- - "~>"
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version: 1.
|
222
|
+
version: 1.15.0
|
223
223
|
description: This library can be used as a module for 'fog' or as standalone libvirt
|
224
224
|
provider.
|
225
225
|
email: geemus@gmail.com
|
@@ -306,6 +306,7 @@ files:
|
|
306
306
|
- tests/libvirt/requests/compute/create_domain_tests.rb
|
307
307
|
- tests/libvirt/requests/compute/define_domain_tests.rb
|
308
308
|
- tests/libvirt/requests/compute/dhcp_leases_tests.rb
|
309
|
+
- tests/libvirt/requests/compute/list_pools_tests.rb
|
309
310
|
- tests/libvirt/requests/compute/update_autostart_tests.rb
|
310
311
|
- tests/libvirt/requests/compute/update_display.rb
|
311
312
|
homepage: http://github.com/fog/fog-libvirt
|
@@ -321,14 +322,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
321
322
|
requirements:
|
322
323
|
- - ">="
|
323
324
|
- !ruby/object:Gem::Version
|
324
|
-
version: 2.
|
325
|
+
version: 2.5.0
|
325
326
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
326
327
|
requirements:
|
327
328
|
- - ">="
|
328
329
|
- !ruby/object:Gem::Version
|
329
330
|
version: '0'
|
330
331
|
requirements: []
|
331
|
-
rubygems_version: 3.
|
332
|
+
rubygems_version: 3.3.7
|
332
333
|
signing_key:
|
333
334
|
specification_version: 2
|
334
335
|
summary: Module for the 'fog' gem to support libvirt
|
@@ -354,5 +355,6 @@ test_files:
|
|
354
355
|
- tests/libvirt/requests/compute/create_domain_tests.rb
|
355
356
|
- tests/libvirt/requests/compute/define_domain_tests.rb
|
356
357
|
- tests/libvirt/requests/compute/dhcp_leases_tests.rb
|
358
|
+
- tests/libvirt/requests/compute/list_pools_tests.rb
|
357
359
|
- tests/libvirt/requests/compute/update_autostart_tests.rb
|
358
360
|
- tests/libvirt/requests/compute/update_display.rb
|