Commit 6c4e5c2a authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Titouan Soulard

slapconfiguration: Update, fix and add tests

parent c9a4591c
......@@ -30,11 +30,13 @@ import logging
import os
import slapos.slap
from slapos.slap.slap import json_loads_byteified
from slapos.recipe.librecipe import unwrap
import six
from six.moves.configparser import RawConfigParser
from netaddr import valid_ipv4, valid_ipv6
from slapos.util import mkdir_p, dumps
from slapos.util import mkdir_p, dumps, calculate_dict_hash
from slapos import format as slapformat
......@@ -151,17 +153,17 @@ class Recipe(object):
"compute_partition_id": options["partition"],
})
options["instance-state"] = software_instance.get("state")
options["slap_software_type"] = software_instance.get("software_type")
options["slap_computer_partition_id"] = software_instance.get("compute_partition_id")
options["slap_computer_id"] = software_instance.get("compute_node_id")
options["slap_software_release_url"] = software_instance.get("software_release_uri")
options["slap-software-type"] = software_instance.get("software_type")
options["slap-computer-partition-id"] = software_instance.get("compute_partition_id")
options["slap-computer-id"] = software_instance.get("compute_node_id")
options["slap-software-release-url"] = software_instance.get("software_release_uri")
options["timestamp"] = software_instance.get("processing_timestamp")
options["instance-title"] = software_instance.get("title")
options["root-instance-title"] = software_instance.get("root_instance_title")
options["instance-guid"] = software_instance.get("reference")
ip_list = software_instance.get("ip_list", [])
full_ip_list = software_instance.get("full_ip_list", [])
parameter_dict = json.loads(software_instance.get("parameters"))
parameter_dict = json_loads_byteified(software_instance.get("parameters"))
# Get Share instance list
result_shared_instance_list = slap.jio_api_connector.allDocs({
......@@ -175,19 +177,22 @@ class Recipe(object):
"portal_type": "Software Instance",
"reference": shared_instance_brain.get("reference"),
})
shared_instance_parameter = json.loads(shared_instance.get("parameters"))
shared_instance_connection = json.loads(shared_instance.get("connection_parameters"))
shared_instance_parameter = json_loads_byteified(shared_instance.get("parameters"))
shared_instance_connection = shared_instance.get("connection_parameters")
shared_instance_list.append({
'slave_title': shared_instance.get("title"),
'slap_software_type': \
shared_instance.get("type"),
shared_instance.get("software_type"),
'slave_reference': shared_instance.get("reference"),
'timestamp': shared_instance.get("timestamp"),
'timestamp': shared_instance.get("processing_timestamp"),
'xml': dumps(shared_instance_parameter),
'parameters': shared_instance.get("parameters"),
'parameters': shared_instance_parameter,
'connection_xml': dumps(shared_instance_connection),
'connection_parameters': shared_instance.get("connection_parameters"),
'connection_parameters': shared_instance_connection,
'connection-parameter-hash': calculate_dict_hash(shared_instance_connection),
})
options["slave-instance-list"] = shared_instance_list
options["shared-instance-list"] = shared_instance_list
else:
computer_partition = slap.registerComputerPartition(
options['computer'],
......
......@@ -8,6 +8,7 @@ import tempfile
from collections import defaultdict
from slapos.recipe import slapconfiguration
from slapos import format as slapformat
from slapos.util import dumps, calculate_dict_hash
class APIRequestHandler(object):
......@@ -55,14 +56,57 @@ class SlapConfigurationTest(unittest.TestCase):
os.unlink(self.resource_file)
os.rmdir(self.instance_root)
@mock.patch("slapos.slap.slap")
def test_correct_naming(self, MockClient):
"""Test correct naming of variables from resource file."""
MockClient.initializeConnection.return_value = None
MockClient.getInstanceParameterDict.return_value = dict()
options = defaultdict(str)
recipe = slapconfiguration.Recipe(self.buildout, "slapconfiguration", options)
def test_new_api(self):
"""Test proper call with new api"""
options = {
"url": "http://127.0.0.1:80",
"software-instance-reference": "SOFTINST-12",
}
parameter_dict = {"foo": "bar", "hello": "bye"}
instance_data = {
"reference": options["software-instance-reference"],
"state": "started",
"software_type": "Couscous",
"compute_partition_id": "slappartx12",
"compute_node_id": "COMP-321",
"software_release_uri": "foo.cfg",
"processing_timestamp": 1223231231,
"title": "MyInstance",
"root_instance_title": "MyInstanceRoot",
"ip_list": [
[
"slaptap9",
"fe80::1ff:fe23:4567:890a"
],
[
"slaptap9",
"10.0.246.114"
]
],
"parameters": json.dumps(parameter_dict),
"connection_parameters": {"1": 2, "3": "YourURL"},
}
api_handler = APIRequestHandler([
("/api/get", json.dumps(instance_data)),
("/api/allDocs", json.dumps({"result_list": []}))
])
with httmock.HTTMock(api_handler.request_handler):
slapconfiguration.Recipe(self.buildout, "slapconfiguration", options)
self.assertEqual(options["instance-state"], instance_data.get("state"))
self.assertEqual(options["slap-software-type"], instance_data.get("software_type"))
self.assertEqual(options["slap-computer-partition-id"], instance_data.get("compute_partition_id"))
self.assertEqual(options["slap-computer-id"], instance_data.get("compute_node_id"))
self.assertEqual(options["slap-software-release-url"], instance_data.get("software_release_uri"))
self.assertEqual(options["timestamp"], instance_data.get("processing_timestamp"))
self.assertEqual(options["instance-title"], instance_data.get("title"))
self.assertEqual(options["root-instance-title"], instance_data.get("root_instance_title"))
self.assertEqual(options["instance-guid"], instance_data.get("reference"))
self.assertEqual(options["ipv4"], set([instance_data.get("ip_list")[1][1]]))
self.assertEqual(options["ipv6"], set([instance_data.get("ip_list")[0][1]]))
for key, value in parameter_dict.items():
options['configuration.' + key] = value
self.assertEqual(options['tun-ipv4'], "192.168.0.1",
"Folded attrs should be separated by -")
......@@ -71,7 +115,7 @@ class SlapConfigurationTest(unittest.TestCase):
self.assertEqual(options['address-list'], [10, 20],
"All underscores should be replaced with -")
def test_new_api(self):
def test_new_api_with_shared_instance(self):
"""Test proper call with new api"""
options = {
......@@ -102,18 +146,46 @@ class SlapConfigurationTest(unittest.TestCase):
"parameters": json.dumps(parameter_dict),
"connection_parameters": {"1": 2, "3": "YourURL"},
}
shared_instance_parameter_dict = {"zooo": "heee", "bye": "hello"}
shared_instance_data = {
"reference": "SHARED-124",
"state": "started",
"software_type": "Couscous",
"compute_partition_id": "slappartx12",
"compute_node_id": "COMP-321",
"software_release_uri": "foo.cfg",
"processing_timestamp": 14444332,
"title": "MySharedInstance",
"root_instance_title": "MySharedInstance",
"ip_list": [
[
"slaptap9",
"fe80::1ff:fe23:4567:890a"
],
[
"slaptap9",
"10.0.246.114"
]
],
"parameters": json.dumps(shared_instance_parameter_dict),
"connection_parameters": {"4": 6, "8": "YourURL2"},
}
api_handler = APIRequestHandler([
("/api/get", json.dumps(instance_data)),
("/api/allDocs", json.dumps({"result_list": []}))
("/api/allDocs", json.dumps({"result_list": [{
"portal_type": "Shared Instance",
"reference": shared_instance_data["reference"]
}]})),
("/api/get", json.dumps(shared_instance_data)),
])
with httmock.HTTMock(api_handler.request_handler):
slapconfiguration.Recipe(self.buildout, "slapconfiguration", options)
self.assertEqual(options["instance-state"], instance_data.get("state"))
self.assertEqual(options["slap_software_type"], instance_data.get("software_type"))
self.assertEqual(options["slap_computer_partition_id"], instance_data.get("compute_partition_id"))
self.assertEqual(options["slap_computer_id"], instance_data.get("compute_node_id"))
self.assertEqual(options["slap_software_release_url"], instance_data.get("software_release_uri"))
self.assertEqual(options["slap-software-type"], instance_data.get("software_type"))
self.assertEqual(options["slap-computer-partition-id"], instance_data.get("compute_partition_id"))
self.assertEqual(options["slap-computer-id"], instance_data.get("compute_node_id"))
self.assertEqual(options["slap-software-release-url"], instance_data.get("software_release_uri"))
self.assertEqual(options["timestamp"], instance_data.get("processing_timestamp"))
self.assertEqual(options["instance-title"], instance_data.get("title"))
self.assertEqual(options["root-instance-title"], instance_data.get("root_instance_title"))
......@@ -122,3 +194,19 @@ class SlapConfigurationTest(unittest.TestCase):
self.assertEqual(options["ipv6"], set([instance_data.get("ip_list")[0][1]]))
for key, value in parameter_dict.items():
options['configuration.' + key] = value
self.assertEqual(options["slave-instance-list"], options["shared-instance-list"])
self.assertEqual(options["shared-instance-list"],[
{
'slave_title': shared_instance_data.get("title"),
'slap_software_type': \
shared_instance_data.get("software_type"),
'slave_reference': shared_instance_data.get("reference"),
'timestamp': shared_instance_data.get("processing_timestamp"),
'xml': dumps(shared_instance_parameter_dict),
'parameters': shared_instance_parameter_dict,
'connection_xml': dumps(shared_instance_data.get("connection_parameters")),
'connection_parameters': shared_instance_data.get("connection_parameters"),
'connection-parameter-hash': calculate_dict_hash(shared_instance_data.get("connection_parameters")),
}
])
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment