Skip to content

Commit

Permalink
Test libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
dann1 committed Oct 10, 2023
1 parent 661d425 commit 820c2f4
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 124 deletions.
52 changes: 52 additions & 0 deletions tests/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby

# How to use: See .github/workflows/rspec.yml

# Standard library
require 'json'
require 'yaml'

# Gems
require 'rspec'
require 'rack/test'

# Engine libraries
require 'opennebula'
require 'json-schema'
require_relative '../src/client/client'
require_relative '../src/server/runtime'

$LOAD_PATH << "#{__dir__}/lib"
require 'log'
require 'crud'

SR = 'Serverless Runtime'

# Initialize Provision Engine ruby client
conf_engine = YAML.load_file('/etc/provision-engine/engine.conf')
endpoint = "http://#{conf_engine[:host]}:#{conf_engine[:port]}"
auth = ENV['TESTS_AUTH'] || 'oneadmin:opennebula'
engine_client = ProvisionEngine::Client.new(endpoint, auth)

# Initialize test configuration
rspec = {
:conf => YAML.load_file('./conf.yaml'),
:engine_client => engine_client,
:sr_templates => []
}

describe 'Provision Engine API' do
include Rack::Test::Methods

let(:rspec) { rspec }

# test every serverless runtime template available under template directory
Dir.entries("#{__dir__}/templates").select do |sr_template|
# blacklist template from tests by changing preffix or suffix
next unless sr_template.start_with?('sr_') && sr_template.end_with?('.json')

include_context('crud', sr_template)
end

include_context('inspect logs')
end
72 changes: 72 additions & 0 deletions tests/lib/crud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
RSpec.shared_context 'crud' do |sr_template|
it "create a #{SR}" do
pp "Requesting #{SR} creation with template #{sr_template}"

specification = File.read("templates/#{sr_template}")
specification = JSON.parse(specification)

response = rspec[:engine_client].create(specification)

expect(response.code.to_i).to eq(201)

runtime = JSON.parse(response.body)
pp runtime

rspec[:id] = runtime['SERVERLESS_RUNTIME']['ID'].to_i

validation = ProvisionEngine::ServerlessRuntime.validate(runtime)
pp validation[1]

expect(validation[0]).to be(true)
end

it "read a #{SR}" do
attempts = rspec[:conf][:timeouts][:get]

1.upto(attempts) do |t|
sleep 1
expect(t == attempts).to be(false)

response = rspec[:engine_client].get(rspec[:id])

expect(response.code.to_i).to eq(200)

runtime = JSON.parse(response.body)
pp runtime

# TODO: Check flow service for failed states like 7 => FAILED_DEPLOYING
next unless runtime['SERVERLESS_RUNTIME']['FAAS']['STATE'] == 'ACTIVE'

break
end
end

it "not update #{SR}" do
response = rspec[:engine_client].update(rspec[:id], {})

expect(response.code.to_i).to eq(501)

body = JSON.parse(response.body)
pp body

expect(body).to eq('Serverless Runtime update not implemented')
end

it "delete a #{SR}" do
attempts = rspec[:conf][:timeouts][:get]

1.upto(attempts) do |t|
sleep 1
expect(t == attempts).to be(false)

response = rspec[:engine_client].delete(rspec[:id])
rc = response.code.to_i

next unless rc == 204

expect(rc).to eq(204)

break
end
end
end
16 changes: 16 additions & 0 deletions tests/lib/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.shared_context 'inspect logs' do
it 'print every log' do
logcation = '/var/log/provision-engine'
sep = '-'*32
pp sep

Dir.entries(logcation).each do |entry|
next if ['.', '..'].include?(entry)

pp entry
pp sep
pp File.read("#{logcation}/#{entry}")
pp sep
end
end
end
6 changes: 3 additions & 3 deletions tests/templates/sr_capacity.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"SERVERLESS_RUNTIME": {
"FAAS": {
"MEMORY": 133,
"CPU": 0.33,
"MEMORY": 111,
"CPU": 0.44,
"VCPU": 3,
"DISK_SIZE": 1337,
"DISK_SIZE": 420,
"FLAVOUR": "Function"
},
"SCHEDULING": {},
Expand Down
14 changes: 7 additions & 7 deletions tests/templates/sr_complex.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"SERVERLESS_RUNTIME": {
"NAME": "Example Serverless Runtime",
"FAAS": {
"CPU": 0.69,
"VCPU": 1,
"MEMORY": 128,
"DISK_SIZE": 1024,
"CPU": 0.11,
"VCPU": 2,
"MEMORY": 133,
"DISK_SIZE": 1025,
"FLAVOUR": "Function"
},
"DAAS": {
"CPU": 0.69,
"CPU": 0.33,
"VCPU": 1,
"MEMORY": 128,
"DISK_SIZE": 1024,
"MEMORY": 197,
"DISK_SIZE": 1337,
"FLAVOUR": "Data"
},
"SCHEDULING": {},
Expand Down
114 changes: 0 additions & 114 deletions tests/tests.rb

This file was deleted.

0 comments on commit 820c2f4

Please sign in to comment.