Skip to content

Commit

Permalink
Tests refactor (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
dann1 committed Oct 11, 2023
1 parent 2964865 commit ee0df98
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 139 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/rspec.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
name: Test
name: Tests
on:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
tests:
runs-on: ubuntu-latest
test:
name: Test
runs-on: ubuntu-22.04
env:
oned: https://cognit-lab.sovereignedge.eu/RPC2
oneflow: https://cognit-lab-oneflow.sovereignedge.eu/
TESTS_AUTH: ${{ secrets.TESTS_AUTH }}
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install
- name: Install engine
run: ./install.sh

- name: Configure
run: cd ./tests && ./prepare.rb "${{ env.oned }}" "${{ env.oneflow }}"

# Maybe should be included in rspec
- name: Start engine
run: provision-engine-server start && sleep 2

- name: Test
run: cd ./tests && rspec tests.rb
- name: Run tests
run: cd ./tests && rspec init.rb

# Maybe should be included in rspec
- name: Stop engine engine
- name: Stop engine
run: provision-engine-server stop

- name: Uninstall
run: ./install.sh purge

2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ modules="client.rb configuration.rb log.rb server.rb runtime.rb"
gems=("opennebula" "sinatra" "logger" "json-schema") # check requires on server.rb

action="${1:-"install"}"
setup_mode="${2:-"symlink"}" # TODO: change to copy once is production ready
setup_mode="${2:-"symlink"}"

if [ "$action" = "clean" ]; then
clean
Expand Down
8 changes: 2 additions & 6 deletions src/server/server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env ruby

$LOAD_PATH << '/opt/provision-engine/' # install dir defined on install.sh

# Standard library
require 'json'
require 'yaml'
Expand All @@ -17,6 +15,7 @@
require 'opennebula'
require 'opennebula/oneflow_client'

$LOAD_PATH << '/opt/provision-engine/' # install dir defined on install.sh
# Engine libraries
require 'log'
require 'configuration'
Expand Down Expand Up @@ -211,11 +210,8 @@ def log_response(level, code, data, message)

auth = auth?

id = params[:id].to_i

client = ProvisionEngine::CloudClient.new(conf, auth)

# Obtain serverless runtime
id = params[:id].to_i

response = ProvisionEngine::ServerlessRuntime.get(client, id)
rc = response[0]
Expand Down
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
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
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
File renamed without changes.
112 changes: 0 additions & 112 deletions tests/tests.rb

This file was deleted.

0 comments on commit ee0df98

Please sign in to comment.