1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Add more OCCI tests

This commit is contained in:
Daniel Molina 2012-02-22 17:16:39 +01:00
parent 66e314c021
commit e97040d3e5
17 changed files with 602 additions and 0 deletions

View File

@ -0,0 +1 @@
<COMPUTE_COLLECTION></COMPUTE_COLLECTION>

View File

@ -0,0 +1,20 @@
<COMPUTE href="http://localhost:4567/compute/0">
<ID>0</ID>
<USER href="http://localhost:4567/user/2" name="my_first_occi_user"/>
<GROUP>oneadmin</GROUP>
<CPU>1</CPU>
<MEMORY>1024</MEMORY>
<NAME>Compute</NAME>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small">small</INSTANCE_TYPE>
<STATE>PENDING</STATE>
<DISK id="0">
<STORAGE href="http://localhost:4567/storage/0" name="Storage"/>
<TYPE>DISK</TYPE>
<TARGET>hde</TARGET>
</DISK>
<NIC>
<NETWORK href="http://localhost:4567/network/0" name="Network"/>
<IP>192.168.1.1</IP>
<MAC>02:00:c0:a8:01:01</MAC>
</NIC>
</COMPUTE>

View File

@ -0,0 +1,20 @@
<COMPUTE href="http://localhost:4567/compute/0">
<ID>0</ID>
<USER href="http://localhost:4567/user/2" name="my_first_occi_user"/>
<GROUP>oneadmin</GROUP>
<CPU>1</CPU>
<MEMORY>1024</MEMORY>
<NAME>Compute</NAME>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small">small</INSTANCE_TYPE>
<STATE>DONE</STATE>
<DISK id="0">
<STORAGE href="http://localhost:4567/storage/0" name="Storage"/>
<TYPE>DISK</TYPE>
<TARGET>hde</TARGET>
</DISK>
<NIC>
<NETWORK href="http://localhost:4567/network/0" name="Network"/>
<IP>192.168.1.1</IP>
<MAC>02:00:c0:a8:01:01</MAC>
</NIC>
</COMPUTE>

View File

@ -0,0 +1,20 @@
<COMPUTE href="http://localhost:4567/compute/1">
<ID>1</ID>
<USER href="http://localhost:4567/user/3" name="my_second_occi_user"/>
<GROUP>users</GROUP>
<CPU>1</CPU>
<MEMORY>1024</MEMORY>
<NAME>Compute2</NAME>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small">small</INSTANCE_TYPE>
<STATE>PENDING</STATE>
<DISK id="0">
<STORAGE href="http://localhost:4567/storage/1" name="Storage2"/>
<TYPE>DISK</TYPE>
<TARGET>hde</TARGET>
</DISK>
<NIC>
<NETWORK href="http://localhost:4567/network/1" name="Network2"/>
<IP>192.168.2.1</IP>
<MAC>02:00:c0:a8:02:01</MAC>
</NIC>
</COMPUTE>

View File

@ -0,0 +1,20 @@
<COMPUTE href="http://localhost:4567/compute/1">
<ID>1</ID>
<USER href="http://localhost:4567/user/3" name="my_second_occi_user"/>
<GROUP>users</GROUP>
<CPU>1</CPU>
<MEMORY>1024</MEMORY>
<NAME>Compute2</NAME>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small">small</INSTANCE_TYPE>
<STATE>DONE</STATE>
<DISK id="0">
<STORAGE href="http://localhost:4567/storage/1" name="Storage2"/>
<TYPE>DISK</TYPE>
<TARGET>hde</TARGET>
</DISK>
<NIC>
<NETWORK href="http://localhost:4567/network/1" name="Network2"/>
<IP>192.168.2.1</IP>
<MAC>02:00:c0:a8:02:01</MAC>
</NIC>
</COMPUTE>

View File

@ -0,0 +1,20 @@
<INSTANCE_TYPE_COLLECTION>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/large" name="large">
<ID>large</ID>
<NAME>large</NAME>
<CPU>8</CPU>
<MEMORY>8192</MEMORY>
</INSTANCE_TYPE>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/medium" name="medium">
<ID>medium</ID>
<NAME>medium</NAME>
<CPU>4</CPU>
<MEMORY>4096</MEMORY>
</INSTANCE_TYPE>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small" name="small">
<ID>small</ID>
<NAME>small</NAME>
<CPU>1</CPU>
<MEMORY>1024</MEMORY>
</INSTANCE_TYPE>
</INSTANCE_TYPE_COLLECTION>

View File

@ -0,0 +1,5 @@
<INSTANCE_TYPE_COLLECTION>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/large" name="large"/>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/medium" name="medium"/>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/small" name="small"/>
</INSTANCE_TYPE_COLLECTION>

View File

@ -0,0 +1 @@
<NETWORK_COLLECTION></NETWORK_COLLECTION>

View File

@ -0,0 +1,11 @@
<NETWORK href="http://localhost:4567/network/0">
<ID>0</ID>
<NAME>Network</NAME>
<USER href="http://localhost:4567/user/2" name="my_first_occi_user"/>
<GROUP>oneadmin</GROUP>
<DESCRIPTION>Network of the user my_first_occi_user</DESCRIPTION>
<ADDRESS>192.168.1.1</ADDRESS>
<SIZE>125</SIZE>
<USED_LEASES>0</USED_LEASES>
<PUBLIC>NO</PUBLIC>
</NETWORK>

View File

@ -0,0 +1,11 @@
<NETWORK href="http://localhost:4567/network/1">
<ID>1</ID>
<NAME>Network2</NAME>
<USER href="http://localhost:4567/user/3" name="my_second_occi_user"/>
<GROUP>users</GROUP>
<DESCRIPTION>Network of the user my_second_occi_user</DESCRIPTION>
<ADDRESS>192.168.2.1</ADDRESS>
<SIZE>125</SIZE>
<USED_LEASES>0</USED_LEASES>
<PUBLIC>NO</PUBLIC>
</NETWORK>

7
src/cloud/occi/test/fixtures/root.xml vendored Normal file
View File

@ -0,0 +1,7 @@
<COLLECTIONS>
<COMPUTE_COLLECTION href="http://localhost:4567/compute"/>
<INSTANCE_TYPE_COLLECTION href="http://localhost:4567/instance_type"/>
<NETWORK_COLLECTION href="http://localhost:4567/network"/>
<STORAGE_COLLECTION href="http://localhost:4567/storage"/>
<USER_COLLECTION href="http://localhost:4567/user"/>
</COLLECTIONS>

View File

@ -0,0 +1 @@
<STORAGE_COLLECTION></STORAGE_COLLECTION>

View File

@ -0,0 +1,13 @@
<STORAGE href="http://localhost:4567/storage/0">
<ID>0</ID>
<NAME>Storage</NAME>
<USER href="http://localhost:4567/user/2" name="my_first_occi_user"/>
<GROUP>oneadmin</GROUP>
<STATE>READY</STATE>
<TYPE>DATABLOCK</TYPE>
<DESCRIPTION>Storage of the user my_first_occi_user</DESCRIPTION>
<SIZE>100</SIZE>
<FSTYPE>ext3</FSTYPE>
<PUBLIC>NO</PUBLIC>
<PERSISTENT>NO</PERSISTENT>
</STORAGE>

View File

@ -0,0 +1,13 @@
<STORAGE href="http://localhost:4567/storage/1">
<ID>1</ID>
<NAME>Storage2</NAME>
<USER href="http://localhost:4567/user/3" name="my_second_occi_user"/>
<GROUP>users</GROUP>
<STATE>READY</STATE>
<TYPE>DATABLOCK</TYPE>
<DESCRIPTION>Storage of the user my_second_occi_user</DESCRIPTION>
<SIZE>100</SIZE>
<FSTYPE>ext3</FSTYPE>
<PUBLIC>NO</PUBLIC>
<PERSISTENT>NO</PERSISTENT>
</STORAGE>

View File

@ -0,0 +1,283 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
# The following methods are helpers defined in spec_helper
# - compare_xml
# - get_fixture
# - network_template
# - storage_template
# - compute_template
describe 'OCCI tests' do
before(:all) do
@user_oneadmin = "my_first_occi_user"
`oneuser create #{@user_oneadmin} my_pass`.scan(/^ID: (\d+)/) { |uid|
`oneuser show #{uid.first}`.scan(/PASSWORD\s*:\s*(\w*)/) { |password|
@user_pass = password.first.strip
}
`oneuser chgrp #{uid.first} 0`
}
@user_users = "my_second_occi_user"
`oneuser create #{@user_users} my_pass2`.scan(/^ID: (\d+)/) { |uid|
`oneuser show #{uid.first}`.scan(/PASSWORD\s*:\s*(\w*)/) { |password|
@user_pass2 = password.first.strip
}
}
# Define BRIDGE attirbute in network.erb, otherwise the NETWORK creation will
`sed -i.bck "s%^#\\(BRIDGE = \\).*$%\\1 br0%" $ONE_LOCATION/etc/occi_templates/network.erb`
end
describe "with a user of the oneadmin group" do
before(:each) do
basic_authorize(@user_oneadmin, @user_pass)
end
it "should retrieve the list of collections" do
get '/'
compare_xml(last_response.body, get_fixture('/root.xml'))
last_response.status.should == 200
end
it "should retrieve the list of INSTANCE_TYPEs" do
get '/instance_type'
compare_xml(last_response.body, get_fixture('/instance_type/list.xml'))
last_response.status.should == 200
end
it "should retrieve the extended list of INSTANCE_TYPEs" do
get '/instance_type', {'verbose'=>true}
compare_xml(last_response.body, get_fixture('/instance_type/extended.xml'))
last_response.status.should == 200
end
context "for NETWORK" do
it "should retrieve the empty list" do
get '/network'
compare_xml(last_response.body, get_fixture('/network/empty.xml'))
last_response.status.should == 200
end
it "should create a new NETWORK" do
network = {
:name => "Network",
:description => "Network of the user #{@user_oneadmin}",
:address => "192.168.1.0",
:size => "100",
:pubic => "YES"
}
post '/network', network_template(network)
compare_xml(last_response.body, get_fixture('/network/first_net.xml'))
last_response.status.should == 201
end
it "should retrieve the NETWORK with ID 0" do
get '/network/0'
compare_xml(last_response.body, get_fixture('/network/first_net.xml'))
last_response.status.should == 200
end
end
context "for STORAGE" do
it "should retrieve the empty list" do
get '/storage'
compare_xml(last_response.body, get_fixture('/storage/empty.xml'))
last_response.status.should == 200
end
it "should create a new STORAGE, type DATABLOCK. This request waits until the IMAGE is ready in OpenNebula" do
storage = {
:name => "Storage",
:description => "Storage of the user #{@user_oneadmin}",
:type => "DATABLOCK",
:size => "100",
:fstype => "ext3"
}
post '/storage', {'occixml' => storage_template(storage)}
compare_xml(last_response.body, get_fixture('/storage/first_storage.xml'))
last_response.status.should == 201
end
it "should retrieve the STORAGE with ID 0" do
get '/storage/0'
compare_xml(last_response.body, get_fixture('/storage/first_storage.xml'))
last_response.status.should == 200
end
end
context "for COMPUTE" do
it "should retrieve the empty list" do
get '/compute'
compare_xml(last_response.body, get_fixture('/compute/empty.xml'))
last_response.status.should == 200
end
it "should create a new COMPUTE using the previous NETWORK (ID=0) and STORAGE(ID=0)" do
compute = {
:name => "Compute",
:instance_type => "small",
:disk => [ {:storage => '0'} ],
:nic => [ {:network => '0'} ]
}
post '/compute', compute_template(compute)
compare_xml(last_response.body, get_fixture('/compute/first_compute.xml'))
last_response.status.should == 201
end
it "should retrieve the COMPUTE with ID 0" do
get '/compute/0'
compare_xml(last_response.body, get_fixture('/compute/first_compute.xml'))
last_response.status.should == 200
end
it "should terminate (DONE) the COMPUTE with ID 0" do
compute = {
:id => "0",
:state => "DONE"
}
put '/compute/0', compute_action(compute)
compare_xml(last_response.body, get_fixture('/compute/first_compute_done.xml'))
last_response.status.should == 202
end
end
end
describe "with a user of the users group" do
before(:each) do
basic_authorize(@user_users, @user_pass2)
end
it "should retrieve the list of collections" do
get '/'
compare_xml(last_response.body, get_fixture('/root.xml'))
last_response.status.should == 200
end
it "should retrieve the list of INSTANCE_TYPEs" do
get '/instance_type'
compare_xml(last_response.body, get_fixture('/instance_type/list.xml'))
last_response.status.should == 200
end
it "should retrieve the extended list of INSTANCE_TYPEs" do
get '/instance_type', {'verbose'=>true}
compare_xml(last_response.body, get_fixture('/instance_type/extended.xml'))
last_response.status.should == 200
end
context "for NETWORK" do
it "should retrieve the empty list" do
get '/network'
compare_xml(last_response.body, get_fixture('/network/empty.xml'))
last_response.status.should == 200
end
it "should create a new NETWORK" do
network = {
:name => "Network2",
:description => "Network of the user #{@user_users}",
:address => "192.168.2.0",
:size => "100",
:pubic => "YES"
}
post '/network', network_template(network)
compare_xml(last_response.body, get_fixture('/network/second_net.xml'))
last_response.status.should == 201
end
it "should retrieve the NETWORK with ID 1" do
get '/network/1'
compare_xml(last_response.body, get_fixture('/network/second_net.xml'))
last_response.status.should == 200
end
end
context "for STORAGE" do
it "should retrieve the empty list" do
get '/storage'
compare_xml(last_response.body, get_fixture('/storage/empty.xml'))
last_response.status.should == 200
end
it "should create a new STORAGE, type DATABLOCK. This request waits until the IMAGE is ready in OpenNebula" do
storage = {
:name => "Storage2",
:description => "Storage of the user #{@user_users}",
:type => "DATABLOCK",
:size => "100",
:fstype => "ext3"
}
post '/storage', {'occixml' => storage_template(storage)}
compare_xml(last_response.body, get_fixture('/storage/second_storage.xml'))
last_response.status.should == 201
end
it "should retrieve the STORAGE with ID 1" do
get '/storage/1'
compare_xml(last_response.body, get_fixture('/storage/second_storage.xml'))
last_response.status.should == 200
end
end
context "for COMPUTE" do
it "should retrieve the empty list" do
get '/compute'
compare_xml(last_response.body, get_fixture('/compute/empty.xml'))
last_response.status.should == 200
end
it "should create a new COMPUTE using the previous NETWORK (ID=1) and STORAGE(ID=1)" do
compute = {
:name => "Compute2",
:instance_type => "small",
:disk => [ {:storage => '1'} ],
:nic => [ {:network => '1'} ]
}
post '/compute', compute_template(compute)
compare_xml(last_response.body, get_fixture('/compute/second_compute.xml'))
last_response.status.should == 201
end
it "should retrieve the COMPUTE with ID 1" do
get '/compute/1'
compare_xml(last_response.body, get_fixture('/compute/second_compute.xml'))
last_response.status.should == 200
end
it "should terminate (DONE) the COMPUTE with ID 1" do
compute = {
:id => "1",
:state => "DONE"
}
put '/compute/1', compute_action(compute)
compare_xml(last_response.body, get_fixture('/compute/second_compute_done.xml'))
last_response.status.should == 202
end
end
end
end

View File

@ -0,0 +1,4 @@
--colour
--format progress
--loadby mtime
--reverse

View File

@ -24,6 +24,8 @@ require 'rubygems'
require 'rspec'
require 'rack/test'
require 'rexml/document'
# Load the Sinatra app
require 'occi-server'
@ -39,3 +41,153 @@ set :environment, :test
def app
Sinatra::Application
end
def get_fixture(path)
File.read(FIXTURES_PATH + path).strip
end
def compare_xml(a, b)
a = REXML::Document.new(a.to_s)
b = REXML::Document.new(b.to_s)
normalized = Class.new(REXML::Formatters::Pretty) do
def write_text(node, output)
super(node.to_s.strip, output)
end
end
normalized.new(indentation=0,ie_hack=false).write(node=a, a_normalized='')
normalized.new(indentation=0,ie_hack=false).write(node=b, b_normalized='')
a_normalized.should == b_normalized
end
OCCI_NETWORK = %q{
<NETWORK>
<% if hash[:name] %>
<NAME><%= hash[:name] %></NAME>
<% end %>
<% if hash[:description] %>
<DESCRIPTION><%= hash[:description] %></DESCRIPTION>
<% end %>
<% if hash[:address] %>
<ADDRESS><%= hash[:address] %></ADDRESS>
<% end %>
<% if hash[:size] %>
<SIZE><%= hash[:size] %></SIZE>
<% end %>
<% if hash[:public] %>
<PUBLIC><%= hash[:public] %></PUBLIC>
<% end %>
</NETWORK>
}
def network_template(hash)
ERB.new(OCCI_NETWORK).result(binding)
end
OCCI_IMAGE = %q{
<STORAGE>
<% if hash[:name] %>
<NAME><%= hash[:name] %></NAME>
<% end %>
<% if hash[:type] %>
<TYPE><%= hash[:type] %></TYPE>
<% end %>
<% if hash[:description] %>
<DESCRIPTION><%= hash[:description] %></DESCRIPTION>
<% end %>
<% if hash[:size] %>
<SIZE><%= hash[:size] %></SIZE>
<% end %>
<% if hash[:fstype] %>
<FSTYPE><%= hash[:fstype] %></FSTYPE>
<% end %>
<% if hash[:public] %>
<PUBLIC><%= hash[:public] %></PUBLIC>
<% end %>
<% if hash[:persistent] %>
<PERSISTENT><%= hash[:persistent] %></PERSISTENT>
<% end %>
</STORAGE>
}
def storage_template(hash)
ERB.new(OCCI_IMAGE).result(binding)
end
OCCI_VM = %q{
<COMPUTE>
<% if hash[:name] %>
<NAME><%= hash[:name] %></NAME>
<% end %>
<% if hash[:instance_type] %>
<INSTANCE_TYPE href="http://localhost:4567/instance_type/<%= hash[:instance_type] %>"/>
<% end %>
<% if hash[:disk] %>
<% hash[:disk].each { |disk| %>
<DISK>
<% if disk[:storage] %>
<STORAGE href="http://localhost:4567/storage/<%= disk[:storage] %>"/>
<% end %>
</DISK>
<% } %>
<% end %>
<% if hash[:nic] %>
<% hash[:nic].each { |nic| %>
<NIC>
<% if nic[:network] %>
<NETWORK href="http://localhost:4567/network/<%= nic[:network] %>"/>
<% end %>
<% if nic[:ip] %>
<IP><%= nic[:ip] %></IP>
<% end %>
</NIC>
<% } %>
<% end %>
<% if hash[:context] %>
<CONTEXT>
<% hash[:context].each { |key, value| %>
<<%= key.to_s.upcase %>><%= value %></<%= key.to_s.upcase %>>
<% } %>
</CONTEXT>
<% end %>
</COMPUTE>
}
OCCI_VM_ACTION = %q{
<COMPUTE>
<% if hash[:id] %>
<ID><%= hash[:id] %></ID>
<% end %>
<% if hash[:state] %>
<STATE><%= hash[:state] %></STATE>
<% end %>
</COMPUTE>
}
def compute_template(hash)
ERB.new(OCCI_VM).result(binding)
end
def compute_action(hash)
ERB.new(OCCI_VM_ACTION).result(binding)
end