Commit 4d17595c authored by Romain Courteaud's avatar Romain Courteaud

slapos_subscription_request: drop SubscriptionCondition_renderKVMClusterParameter

parent 2ab0f527
"""
Take the Text Content and adapt it as template for create
an variation for a specific software release.
This adapt the definition of KVM to instantiate multiple VMs instead a single
for example.
"""
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
import json
parameter_text = context.getTextContent()
if not amount or parameter_text is None:
return None
json_parameter = context.Base_instanceXmlToDict(parameter_text)
if "_" in json_parameter:
# This is json-in-xml serialisation
json_parameter = json.loads(json_parameter["_"])
else:
raise ValueError("KVM Cluster only supports serialised values!")
KVM0 = json_parameter["kvm-partition-dict"]["KVM0"]
for i in range(amount):
if i == 0:
k = KVM0.copy()
k["sticky-compute-node"] = True
json_parameter["kvm-partition-dict"]["KVM" + str(i)] = k
else:
json_parameter["kvm-partition-dict"]["KVM" + str(i)] = KVM0
xml_parameter = """<?xml version="1.0" encoding="utf-8"?>
<instance>
<parameter id="_">%s</parameter>
</instance>""" % json.dumps(json_parameter, indent=2)
return xml_parameter
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>amount=0, REQUEST=None, **kw</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>SubscriptionCondition_renderKVMClusterParameter</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -127,102 +127,6 @@ class TestBase_instanceXmlToDict(TestSubscriptionSkinsMixin):
self.assertEqual(self.portal.Base_instanceXmlToDict(xml), {'_': '{"aa": "bb"}'})
class TestSubscriptionCondition_renderKVMClusterParameter(TestSubscriptionSkinsMixin):
def test_simple_raise_if_request_is_present(self):
subscription_condition = self.newSubscriptionCondition()
self.assertRaises(Unauthorized, subscription_condition.SubscriptionCondition_renderKVMClusterParameter,
REQUEST=self.portal.REQUEST)
def test_simple_parameter_is_none(self):
subscription_condition = self.newSubscriptionCondition()
self.assertEqual(None, subscription_condition.SubscriptionCondition_renderKVMClusterParameter(amount=0))
self.assertEqual(None, subscription_condition.SubscriptionCondition_renderKVMClusterParameter(amount=1))
def test_simple_raise_if_parameter_isnt_serialised(self):
parameter_xml = """<?xml version="1.0" encoding="utf-8"?>
<instance>
<parameter id="oioi">notserialised</parameter>
</instance>"""
subscription_condition = self.newSubscriptionCondition(
text_content=parameter_xml)
self.assertRaises(ValueError, subscription_condition.SubscriptionCondition_renderKVMClusterParameter,
amount=5)
def test_simple_kvm_rendering(self):
parameter_xml = """<?xml version="1.0" encoding="utf-8"?>
<instance>
<parameter id="_">{
"kvm-partition-dict": {
"KVM0": {
"cpu-count": 40,
"cpu-max-count": 41,
"ram-size": 245760,
"ram-max-size": 245761,
"disk-device-path": "/dev/sdb",
"project-guid": "PROJ-XXXX",
"disable-ansible-promise": true
}
}
}</parameter>
</instance>"""
subscription_condition = self.newSubscriptionCondition(
text_content=parameter_xml)
# Amount is 0, so return None
self.assertEqual(None, subscription_condition.SubscriptionCondition_renderKVMClusterParameter())
self.assertEqual(None, subscription_condition.SubscriptionCondition_renderKVMClusterParameter(amount=0))
self.assertEqual(subscription_condition.SubscriptionCondition_renderKVMClusterParameter(amount=1).strip(),
"""<?xml version="1.0" encoding="utf-8"?>
<instance>
<parameter id="_">{
"kvm-partition-dict": {
"KVM0": {
"disable-ansible-promise": true,
"disk-device-path": "/dev/sdb",
"cpu-count": 40,
"ram-max-size": 245761,
"ram-size": 245760,
"project-guid": "PROJ-XXXX",
"cpu-max-count": 41,
"sticky-compute-node": true
}
}
}</parameter>
</instance>""")
self.assertEqual(subscription_condition.SubscriptionCondition_renderKVMClusterParameter(amount=2).strip(),
"""<?xml version="1.0" encoding="utf-8"?>
<instance>
<parameter id="_">{
"kvm-partition-dict": {
"KVM0": {
"disable-ansible-promise": true,
"disk-device-path": "/dev/sdb",
"cpu-count": 40,
"ram-max-size": 245761,
"ram-size": 245760,
"project-guid": "PROJ-XXXX",
"cpu-max-count": 41,
"sticky-compute-node": true
},
"KVM1": {
"disk-device-path": "/dev/sdb",
"cpu-count": 40,
"ram-max-size": 245761,
"cpu-max-count": 41,
"disable-ansible-promise": true,
"ram-size": 245760,
"project-guid": "PROJ-XXXX"
}
}
}</parameter>
</instance>""")
class TestSubscriptionCondition_renderParameter(TestSubscriptionSkinsMixin):
@simulate('SubscriptionCondition_renderParameterSampleScript', '*args, **kwargs','return args, kwargs')
......
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