class Object

Constants

BASE_STORAGE_DIR

By default the location is /var/tmp/deltacloud-{USER}/

BASE_URL
CMWG_NAMESPACE
COLL_URL
CURL_LOADED
DATABASE_MIGRATIONS_DIR

Detect if there are some pending migrations to run. We don't actually run migrations during server startup, just print a warning to console

DATA_DIR
DEFAULT_CONFIG
INSTANCE_ID
MACHINE_CONFIG_ID
MOCK_STORAGE_DIR

The mock driver YAML files are stored in BASE_STORAGE_DIR/mock You can overide this by setting 'DELTACLOUD_MOCK_STORAGE' environment variable

MULTIPART_LOADED
NS
REXML_FORMATTERS

Public Class Methods

__name__() click to toggle source

Destroy test instance when all test are done

# File tests/drivers/gogrid/instances_test.rb, line 29
def self.__name__
  # record_retries below calls this
  "instances_test_class"
end
create_test_instance() click to toggle source
# File tests/drivers/gogrid/instances_test.rb, line 19
def self.create_test_instance
  driver = Deltacloud::new(:gogrid, credentials)
  VCR.use_cassette "instances_create_test_instance" do
    @@instance = driver.instance(:id => INSTANCE_ID)
    @@instance ||= driver.create_instance(fixed_image_id,
                                          :name=> INSTANCE_ID)
  end
end
destroy_test_instance() click to toggle source
# File tests/drivers/gogrid/instances_test.rb, line 34
def self.destroy_test_instance
  driver = Deltacloud::new(:gogrid, credentials)

  # Go fast when running off a recording
  opts = record_retries('', :time_between_retry => 60)
  @@instance.wait_for!(driver, opts) do |i|
    i.actions.include?(:destroy)
  end

  VCR.use_cassette "instances_destroy_test_instance" do
    driver.destroy_instance(@@instance.id)
    @@instance = nil
  end
end
standard_index_operation(opts={}) click to toggle source
# File lib/deltacloud/helpers/rabbit_helper.rb, line 19
def self.standard_index_operation(opts={})
  collection_name = @collection_name
  operation :index, :with_capability => opts[:capability] || collection_name do
    control { filter_all collection_name }
  end
end
standard_show_operation(opts={}) click to toggle source
# File lib/deltacloud/helpers/rabbit_helper.rb, line 26
def self.standard_show_operation(opts={})
  collection_name = @collection_name
  operation :show, :with_capability => opts[:capability] || collection_name do
    control { show collection_name.to_s.singularize.intern }
  end
end

Public Instance Methods

app() click to toggle source
# File tests/cimi/collections/cloud_entry_point_test.rb, line 10
def app; run_frontend(:cimi) end
blob_name_google() click to toggle source
# File tests/drivers/google/buckets_test.rb, line 26
def blob_name_google
  "testblobk1ds91kVdelmegoogel"
end
bucket_name_google() click to toggle source
# File tests/drivers/google/buckets_test.rb, line 22
def bucket_name_google
  "testbucki2rpux3wdelmegoogel"
end
check(obj) click to toggle source
# File tests/cimi/model/schema_spec.rb, line 95
def check(obj)
  obj.wont_be_nil
  obj[:meter].href.must_equal 'http://example.org/'
end
check_empty_struct(obj) click to toggle source
# File tests/cimi/model/schema_spec.rb, line 178
def check_empty_struct(obj)
  obj.wont_be_nil
  obj[:struct].wont_be_nil
  obj[:struct].scalar.must_be_nil
  obj[:struct].href.must_be_nil
end
check_json_serialization_for(model, sample_id, optional_attrs=[]) click to toggle source
# File tests/deltacloud/common.rb, line 5
def check_json_serialization_for(model, sample_id, optional_attrs=[])
  header 'Accept', 'application/json'
  get root_url + "/#{model.to_s.pluralize}"
  status.must_equal 200
  json[model.to_s.pluralize].wont_be_empty
  get root_url + "/#{model.to_s.pluralize}/#{sample_id}"
  status.must_equal 200
  json[model.to_s].wont_be_empty
  klass = Deltacloud.const_get(model.to_s.camelize)
  klass.attributes.each do |attr|
    attr = attr.to_s.gsub(/_id$/,'') if attr.to_s =~ /_id$/
    json[model.to_s].keys.must_include attr.to_s unless optional_attrs.include? attr
  end
end
check_model(model) click to toggle source
# File tests/cimi/model/schema_spec.rb, line 262
def check_model(model)
  model.things.size.must_equal 3
  model.things.first.must_respond_to :name
  model.things.first.href.must_equal "/things/1"
  model.things.first.name.must_equal "first"
  model.things.last.name.must_be_nil
  model.things.last.href.must_equal "/things/3"
end
check_struct(obj, opts = {}) click to toggle source
# File tests/cimi/model/schema_spec.rb, line 167
def check_struct(obj, opts = {})
  obj.wont_be_nil
  obj[:struct].wont_be_nil
  obj[:struct].scalar.must_equal "v1"
  if opts[:nil_href]
    obj[:struct].href.must_be_nil
  else
    obj[:struct].href.must_equal "http://example.org/"
  end
end
check_structs(obj) click to toggle source
# File tests/cimi/model/schema_spec.rb, line 272
def check_structs(obj)
  obj.wont_be_nil
  obj[:structs].size.must_equal 2
  obj[:structs][0].scalar.must_equal "v1"
  obj[:structs][0].href.must_equal "http://example.org/1"
  obj[:structs][1].scalar.must_equal "v2"
  obj[:structs][1].href.must_equal "http://example.org/2"
end
create_model(model_class, attr) click to toggle source
# File tests/cimi/collections/volumes_test.rb, line 8
def create_model(model_class, attr)
  model = model_class.new(attr)
  svc_class = CIMI::Service::const_get(model_class.name.split('::').last)
  svc_class.new(nil, :model => model).save
  model
end
create_resources() click to toggle source

Setup resources we need for the tests

# File tests/drivers/ec2/common.rb, line 35
def create_resources
  VCR.use_cassette "create_resources" do
    driver = Deltacloud::new(:ec2, credentials)
    @@ec2 = driver.client
    @@vpc = @@ec2.create_vpc("172.16.0.0/16").first
    @@subnet = @@ec2.create_subnet(@@vpc[:vpc_id], "172.16.3.0/24", "us-east-1b").first
  end
end
created_blob_local_file() click to toggle source
# File tests/drivers/google/common.rb, line 12
def created_blob_local_file
  File.join(File.dirname(__FILE__),"data","deltacloud_blob_test.png")
end
credentials() click to toggle source
# File tests/drivers/ec2/buckets_test.rb, line 9
def credentials
{
  :user => "AKIAJATNOR5HKG3FK27Q",
  :password => "dPe47rAlKhlBdTYNbL4ZsMthDga08vEL9d3MS5UO"
}
end
destroy_resources() click to toggle source
# File tests/drivers/ec2/common.rb, line 44
def destroy_resources
  VCR.use_cassette "destroy_resources" do
    @@ec2.delete_subnet(@@subnet[:subnet_id]) if @@subnet
    @@ec2.delete_vpc(@@vpc[:vpc_id]) if @@vpc
  end
end
disks() click to toggle source
# File tests/cimi/collections/machines_test.rb, line 147
def disks
  (xml/'Collection/Machine/disks').wont_be_empty
  (xml/'Collection/Machine/disks').each do |d|
    d[:href].wont_be_empty
    d[:href].must_match(/^http/)
    d.at('id').wont_be_nil
    d.at('count').wont_be_nil
    d.at('Disk/id').wont_be_nil
    d.at('Disk/description').wont_be_nil
    d.at('Disk/capacity').wont_be_nil
    d.at('Disk/created').wont_be_nil
  end
end
fixed_image_id() click to toggle source
# File tests/drivers/gogrid/common.rb, line 9
def fixed_image_id
  # A fixed image we use throughout the tests; if GoGrid ever removes it
  # we need to change it here
  "9928"
end
formats() click to toggle source
# File tests/cimi/collections/common.rb, line 17
def formats; [ 'application/xml', 'application/json' ]; end
get_current_memory_usage() click to toggle source

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

# File lib/deltacloud/core_ext/base.rb, line 16
def get_current_memory_usage
  %xps -o rss= -p #{Process.pid}`.to_i
end
headers() click to toggle source
# File tests/test_helper.rb, line 70
def headers; last_response.headers; end
ids(coll) click to toggle source
# File tests/cimi/collections/machines_test.rb, line 65
def ids(coll)
  xml.xpath("/c:Machine/c:#{coll}/c:id", NS)
end
is_port_open?(ip, port) click to toggle source
# File tests/deltacloud/launcher_test.rb, line 12
def is_port_open?(ip, port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end
  return false
end
json() click to toggle source
# File tests/test_helper.rb, line 73
def json; JSON::parse(response_body); end
kill_process(pid) click to toggle source
# File tests/deltacloud/launcher_test.rb, line 39
def kill_process(pid)
  # Die!
  puts "Sending KILL to #{pid}"
  Process.kill('KILL', pid) rescue ''
  sleep(1)
end
library_present?(name) click to toggle source

See if we can require name and return true if the library is there, false otherwise. Note that, as a side effect, the library will be loaded

# File bin/deltacloudd, line 28
def library_present?(name)
  begin
    require name
    true
  rescue LoadError
    false
  end
end
machine(*expand) click to toggle source
# File tests/cimi/collections/machines_test.rb, line 58
def machine(*expand)
  url = '/machines/inst1'
  url += "?$expand=#{expand.join(",")}" unless expand.empty?
  get root_url url
  status.must_equal 200
end
machines(*select) click to toggle source
# File tests/cimi/collections/machine_images_test.rb, line 37
def machines(*select)
  url = "/machine_images"
  url += "?$select=#{select.join(",")}" unless select.empty?
  get root_url url
  status.must_equal 200
end
make_volume_create() click to toggle source
# File tests/cimi/collections/volumes_test.rb, line 28
def make_volume_create
  vt = CIMI::Model::VolumeTemplate.new
  vt.volume_config.href = @config.id
  CIMI::Model::VolumeCreate.new(:name => "new_cimi_volume_#{Time.now.to_i}",
       :volume_template => vt)
end
model() click to toggle source
# File tests/cimi/collections/common.rb, line 48
def model
  model_class.parse(last_response.body, last_response.content_type)
end
model_class() click to toggle source
# File tests/cimi/collections/common.rb, line 19
def model_class
  resource = nil
  resp = last_response
  ct = resp.content_type
  if ct == "application/json"
    json = JSON::parse(last_response.body)
    json["resourceURI"].wont_be_nil
    resource = json["resourceURI"].split("/").last
  elsif ct == "application/xml"
    xml = Nokogiri::XML(last_response.body)
    if xml.root.name == "Collection"
      resource = xml.root["resourceURI"].split("/").last
    else
      resource = xml.root.name
    end
  elsif resp.body.nil? || resp.body.size == 0
    raise "Can not construct model from empty body"
  else
    raise "Unexpected content type #{resp.content_type}"
  end
  if resource == "DiskCollection"
    # We scope subcollections within the containing class
    # CIMI resource_uri's don't
    CIMI::Model::Machine::DiskCollection
  else
    CIMI::Model::const_get(resource)
  end
end
openstack_image_id() click to toggle source
# File tests/drivers/openstack/common.rb, line 12
def openstack_image_id
  'bf7ce59a-d9f9-45d4-9313-f45b16436602'
end
parse_xml(xml, opts = {}) click to toggle source
# File tests/cimi/spec_helper.rb, line 32
def parse_xml(xml, opts = {})
  opts[:force_content] = true
  opts[:keep_root] = true unless opts.has_key?(:keep_root)
  XmlSimple.xml_in(xml, opts)
end
profile_memory(&block) click to toggle source
# File lib/deltacloud/core_ext/base.rb, line 20
def profile_memory(&block)
  before = get_current_memory_usage
  file, line, _ = caller[0].split(':')
  if block_given?
    instance_eval(&block)
    puts "[#{file}:#{line}: #{(get_current_memory_usage - before) / 1024} MB (consumed)]"
  else
    before = 0
    puts "[#{file}:#{line}: #{(get_current_memory_usage - before) / 1024} MB (all)]"
  end
end
raise_error(id) click to toggle source
# File tests/drivers/base/exceptions_test.rb, line 31
def raise_error(id); ExceptionTestClass.new.raise_exception(id); end
read_data_file(name) click to toggle source
# File tests/cimi/spec_helper.rb, line 38
def read_data_file(name)
  IO::read(File::join(DATA_DIR, name))
end
record_retries(name='', opts = {}) click to toggle source
# File tests/test_helper.rb, line 39
def record_retries(name='', opts = {})
  opts[:before] = Proc.new { |r, &block|
    VCR.use_cassette("#{__name__}-#{name.empty? ? '' : "#{name}-"}#{r}", &block)
  }
  if VCR.configuration.default_cassette_options[:record] == :none
    opts[:time_between_retry] = 0
  end
  opts
end
response_body() click to toggle source
# File tests/test_helper.rb, line 71
def response_body; last_response.body; end
root_url() click to toggle source

The #root_url here is '/' because the application is not mounted and so by default Sinatra mounts it on '/'

# File tests/ec2/common.rb, line 25
def root_url; '/'; end
run_frontend(frontend=:deltacloud) click to toggle source
# File tests/test_helper.rb, line 59
def run_frontend(frontend=:deltacloud)
  Rack::Builder.new {
    use Rack::MatrixParams
    map Deltacloud[frontend].root_url do
      use Rack::MatrixParams
      run Deltacloud[frontend].klass
    end
  }.to_app
end
should_properly_serialize_model(model_class, xml, json) click to toggle source
# File tests/cimi/spec_helper.rb, line 114
def should_properly_serialize_model(model_class, xml, json)
  # Roundtrip in same format
  model_class.from_xml(xml).must_serialize_to xml, :fmt => :xml
  model_class.from_json(json).must_serialize_to json, :fmt => :json
  # Roundtrip crossing format
  model_class.from_xml(xml).must_serialize_to json, :fmt => :json
  model_class.from_json(json).must_serialize_to xml, :fmt => :xml
end
status() click to toggle source
# File tests/test_helper.rb, line 69
def status; last_response.status; end
to_dom(model) click to toggle source
# File tests/cimi/model/collection_spec.rb, line 99
def to_dom(model)
  doc = Nokogiri::XML(model.to_xml)
  doc.root.add_namespace("c", doc.namespaces["xmlns"])
  doc
end
to_json(model) click to toggle source
# File tests/cimi/model/collection_spec.rb, line 105
def to_json(model)
  JSON.parse(model.to_json)
end
vcr_record_mode() click to toggle source
# File tests/test_helper.rb, line 49
def vcr_record_mode
  (ENV['VCR_RECORD'] || :none).to_sym
end
vcr_recording?() click to toggle source
# File tests/test_helper.rb, line 53
def vcr_recording?
  vcr_record_mode != :none
end
wait_for_port_open(port) click to toggle source
# File tests/deltacloud/launcher_test.rb, line 28
def wait_for_port_open(port)
  retries = 5
  begin
    raise unless is_port_open?('127.0.0.1', port)
    true
  rescue
    sleep(1) && retry if (retries-=1) != 0
    false
  end
end
xml() click to toggle source
# File tests/test_helper.rb, line 72
def xml; Nokogiri::XML(response_body); end