Skip to content

Commit

Permalink
Improve test speed
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Sep 18, 2023
1 parent 526477b commit dc4b4ef
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 18 deletions.
5 changes: 3 additions & 2 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Tosca:

ARTIFACTS_PATH = os.path.dirname(os.path.realpath(__file__)) + "/tosca-types/artifacts"
ARTIFACTS_REMOTE_REPO = "https://raw.githubusercontent.com/indigo-dc/tosca-types/master/artifacts/"
GET_TIMEOUT = 20

logger = logging.getLogger('InfrastructureManager')

Expand Down Expand Up @@ -796,7 +797,7 @@ def _gen_configure_from_interfaces(self, node, compute, interfaces):
if implementation_url[0] in ['http', 'https', 'ftp']:
script_path = implementation_url[2]
try:
resp = requests.get(implementation)
resp = requests.get(implementation, timeout=self.GET_TIMEOUT)
script_content = resp.text
if resp.status_code != 200:
raise Exception(resp.reason + "\n" + resp.text)
Expand All @@ -814,7 +815,7 @@ def _gen_configure_from_interfaces(self, node, compute, interfaces):
f.close()
else:
try:
resp = requests.get(Tosca.ARTIFACTS_REMOTE_REPO + implementation)
resp = requests.get(Tosca.ARTIFACTS_REMOTE_REPO + implementation, timeout=self.GET_TIMEOUT)
script_content = resp.text
if resp.status_code != 200:
raise Exception(resp.reason + "\n" + resp.text)
Expand Down
57 changes: 49 additions & 8 deletions scripts/db_1_14_X_to_1_15_X.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,53 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import json

sys.path.append("..")
sys.path.append(".")

from IM.InfrastructureList import InfrastructureList
from IM.InfrastructureInfo import InfrastructureInfo
from IM.db import DataBase
from IM.auth import Authentication


def get_data_from_db(db, inf_id):
"""
Get data from DB.
"""

if db.db_type == DataBase.MONGO:
res = db.find("inf_list", {"id": inf_id}, {"data": True})
else:
res = db.select("select data from inf_list where id = %s", (inf_id,))

if len(res) > 0:
elem = res[0]
if db.db_type == DataBase.MONGO:
data = elem[data]
else:
data = elem[0]
try:
newinf = InfrastructureInfo()
dic = json.loads(data)
newinf.deleted = dic['deleted']
newinf.id = dic['id']
if dic['auth']:
newinf.auth = Authentication.deserialize(dic['auth'])
return newinf
except Exception:
print("ERROR reading infrastructure from database, ignoring it!.")
else:
return None


if __name__ == "__main__":
DATA_DB= "sqlite:///etc/im/inf.dat"
if len(sys.argv) != 2:
sys.stderr.write("Usage: %s <db_file>\n" % sys.argv[0])
sys.exit(-1)

DATA_DB = sys.argv[1]

db = DataBase(DATA_DB)
if db.connect():
Expand All @@ -34,15 +71,19 @@
db.execute("ALTER TABLE `inf_list` ADD COLUMN `auth` BLOB;")

infs = []
for inf_id in InfrastructureList._get_inf_ids_from_db():
res = InfrastructureList._get_data_from_db(DATA_DB, inf_id, True)
if res:
auth = res[inf_id].auth.serialize()
res = db.execute("UPDATE `inf_list` SET `auth` = %s WHERE id = %s", (auth, inf_id))

db.close()
for inf_id in InfrastructureList._get_inf_ids_from_db(get_all=True):
try:
inf = get_data_from_db(db, inf_id)
print(inf_id)
if inf:
auth = inf.auth.serialize()
res = db.execute("UPDATE `inf_list` SET `auth` = %s WHERE id = %s", (auth, inf_id))
except Exception as e:
sys.stderr.write("Error updating auth field in Inf ID: %s. Ignoring.\n" % inf_id)
else:
sys.stdout.write("There are no inf_list table. Do not need to update.")

db.close()
else:
sys.stderr.write("Error connecting with DB: %s\n" % DATA_DB)
sys.exit(-1)
Expand Down
25 changes: 25 additions & 0 deletions test/files/tosca_simple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tosca_definitions_version: tosca_simple_yaml_1_0

description: TOSCA test for the IM

topology_template:

node_templates:

server:
type: tosca.nodes.Compute
capabilities:
# Host container properties
host:
properties:
num_cpus: 1
mem_size: 4 GB
os:
properties:
type: linux
distribution: ubuntu
version: 18.04

outputs:
server:
value: { get_attribute: [ server, public_address ] }
19 changes: 11 additions & 8 deletions test/unit/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
sys.path.append("..")
sys.path.append(".")

from IM.tosca.Tosca import Tosca
from IM.config import Config
from IM import __version__ as version
from IM.InfrastructureManager import (DeletedInfrastructureException,
Expand Down Expand Up @@ -304,19 +305,20 @@ def test_CreateInfrastructure(self, bottle_request, get_infrastructure, CreateIn
"id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
"username = user; password = pass"),
"Content-Type": "text/yaml"}
bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml")

CreateInfrastructure.return_value = "1"

res = RESTCreateInfrastructure()
self.assertEqual(res, "http://imserver.com/infrastructures/1")

bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.headers["Content-Type"] = "application/json"
bottle_request.body = read_file_as_bytes("../files/test_simple.json")
CreateInfrastructure.side_effect = InvaliddUserException()
res = RESTCreateInfrastructure()
self.assertEqual(res, "Error Getting Inf. info: Invalid InfrastructureManager credentials")

bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/test_simple.json")
CreateInfrastructure.side_effect = UnauthorizedUserException()
res = RESTCreateInfrastructure()
self.assertEqual(res, "Error Creating Inf.: Access to this infrastructure not granted.")
Expand Down Expand Up @@ -448,22 +450,23 @@ def test_AddResource(self, bottle_request, get_infrastructure, AddResource):
"id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
"username = user; password = pass"),
"Content-Type": "text/yaml"}
bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml")

res = RESTAddResource("1")
self.assertEqual(res, "http://imserver.com/infrastructures/1/vms/1")

bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.headers["Content-Type"] = "application/json"
bottle_request.body = read_file_as_bytes("../files/test_simple.json")
AddResource.side_effect = DeletedInfrastructureException()
res = RESTAddResource("1")
self.assertEqual(res, "Error Adding resources: Deleted infrastructure.")

bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/test_simple.json")
AddResource.side_effect = IncorrectInfrastructureException()
res = RESTAddResource("1")
self.assertEqual(res, "Error Adding resources: Invalid infrastructure ID or access not granted.")

bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/test_simple.json")
AddResource.side_effect = UnauthorizedUserException()
res = RESTAddResource("1")
self.assertEqual(res, "Error Adding resources: Access to this infrastructure not granted.")
Expand Down Expand Up @@ -523,7 +526,7 @@ def test_AlterVM(self, bottle_request, AlterVM):
"id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
"username = user; password = pass"),
"Content-Type": "text/yaml"}
bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml")

res = RESTAlterVM("1", "1")
self.assertEqual(res, "vm_info")
Expand Down

0 comments on commit dc4b4ef

Please sign in to comment.