##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import logging
import os
import unittest
from six.moves.urllib import parse
from six import PY3
import tempfile
import logging

from collections import OrderedDict
import httmock

import json
import mock

import slapos.slap
from slapos.util import dumps, calculate_dict_hash, dict2xml


class UndefinedYetException(Exception):
  """To catch exceptions which are not yet defined"""


logger = logging.getLogger('slapos.tests.slap')


class SlapMixin(unittest.TestCase):
  """
  Useful methods for slap tests
  """
  def setUp(self):
    self._server_url = os.environ.get('TEST_SLAP_SERVER_URL', None)
    if self._server_url is None:
      self.server_url = 'http://localhost/'
    else:
      self.server_url = self._server_url
    logger.debug('Testing against SLAP server %r', self.server_url)
    self.slap = slapos.slap.slap()
    self.partition_id = self.id()
    os.environ.pop('SLAPGRID_INSTANCE_ROOT', None)

  def tearDown(self):
    pass

  def _getTestComputeNodeId(self):
    """
    Returns the compute_node id used by the test
    """
    return self.id()


class TestSlap(SlapMixin):
  """
  Test slap against slap server

  This test can be used to test a running SLAP server by setting
  TEST_SLAP_SERVER_URL environment variable to the URL of this server.
  """

  def test_slap_initialisation(self):
    """
    Asserts that slap initialisation works properly in case of
    passing correct url
    """
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection(self.server_url)
    self.assertEqual(slap_instance._connection_helper.slapgrid_uri, self.server_url)

  def test_slap_initialisation_ipv6_and_port(self):
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://fe80:1234:1234:1234:1:1:1:1:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[fe80:1234:1234:1234:1:1:1:1]:5000/foo/"
    )

  def test_slap_initialisation_ipv6_without_port(self):
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://fe80:1234:1234:1234:1:1:1:1/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[fe80:1234:1234:1234:1:1:1:1]/foo/"
    )

  def test_slap_initialisation_ipv6_with_bracket(self):
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://[fe80:1234:1234:1234:1:1:1:1]:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[fe80:1234:1234:1234:1:1:1:1]:5000/foo/"
    )

  def test_slap_initialisation_ipv4(self):
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://127.0.0.1:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://127.0.0.1:5000/foo/"
    )

  def test_slap_initialisation_hostname(self):
    # XXX this really opens a connection !
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://example.com:80/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://example.com:80/foo/"
    )

  def test_registerComputeNode_with_new_guid(self):
    """
    Asserts that calling slap.registerComputeNode with new guid returns
    Compute Node object
    """
    compute_node_guid = self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    compute_node = self.slap.registerComputeNode(compute_node_guid)
    self.assertIsInstance(compute_node, slapos.slap.ComputeNode)

  def test_registerComputeNode_with_existing_guid(self):
    """
    Asserts that calling slap.registerComputeNode with already used guid
    returns Compute Node object
    """
    compute_node_guid = self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    compute_node = self.slap.registerComputeNode(compute_node_guid)
    self.assertIsInstance(compute_node, slapos.slap.ComputeNode)

    compute_node2 = self.slap.registerComputeNode(compute_node_guid)
    self.assertIsInstance(compute_node2, slapos.slap.ComputeNode)

  # XXX: There is naming conflict in slap library.
  # SoftwareRelease is currently used as suboject of Slap transmission object
  def test_registerSoftwareRelease_with_new_uri(self):
    """
    Asserts that calling slap.registerSoftwareRelease with new guid
    returns SoftwareRelease object
    """
    software_release_uri = 'http://server/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(software_release_uri)
    self.assertIsInstance(software_release, slapos.slap.SoftwareRelease)

  def test_registerSoftwareRelease_with_existing_uri(self):
    """
    Asserts that calling slap.registerSoftwareRelease with already
    used guid returns SoftwareRelease object
    """
    software_release_uri = 'http://server/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(software_release_uri)
    self.assertIsInstance(software_release, slapos.slap.SoftwareRelease)

    software_release2 = self.slap.registerSoftwareRelease(software_release_uri)
    self.assertIsInstance(software_release2, slapos.slap.SoftwareRelease)

  def test_registerComputePartition_new_partition_id_known_compute_node_guid(self):
    """
    Asserts that calling slap.registerComputePartition on known compute_node
    returns ComputePartition object
    """
    compute_node_guid = self._getTestComputeNodeId()
    partition_id = self.partition_id
    self.slap.initializeConnection(self.server_url)
    self.slap.registerComputeNode(compute_node_guid)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and qs == {
                'compute_node_reference': [compute_node_guid],
                'compute_partition_reference': [partition_id]
                }:
        partition = slapos.slap.ComputePartition(compute_node_guid, partition_id)
        return {
                'status_code': 200,
                'content': dumps(partition)
                }
      else:
        return {'status_code': 400}

    self._handler = handler

    with httmock.HTTMock(handler):
      partition = self.slap.registerComputePartition(compute_node_guid, partition_id)
      self.assertIsInstance(partition, slapos.slap.ComputePartition)

  def test_registerComputePartition_existing_partition_id_known_compute_node_guid(self):
    """
    Asserts that calling slap.registerComputePartition on known compute_node
    returns ComputePartition object
    """
    self.test_registerComputePartition_new_partition_id_known_compute_node_guid()
    with httmock.HTTMock(self._handler):
      partition = self.slap.registerComputePartition(self._getTestComputeNodeId(),
                                                      self.partition_id)
      self.assertIsInstance(partition, slapos.slap.ComputePartition)

  def test_registerComputePartition_unknown_compute_node_guid(self):
    """
    Asserts that calling slap.registerComputePartition on unknown
    compute_node raises NotFoundError exception
    """
    compute_node_guid = self._getTestComputeNodeId()
    self.slap.initializeConnection(self.server_url)
    partition_id = self.id()

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and qs == {
              'compute_node_reference': [compute_node_guid],
              'compute_partition_reference': [partition_id]
              }:
        return {'status_code': 404}
      else:
        return {'status_code': 0}

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputePartition,
                        compute_node_guid, partition_id)


  def test_getFullComputeNodeInformation_empty_compute_node_guid(self):
    """
    Asserts that calling getFullComputeNodeInformation with empty compute_node_id
    raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      # Shouldn't even be called
      self.assertFalse(True)

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap._connection_helper.getFullComputeNodeInformation,
                        None)

  def test_registerComputePartition_empty_compute_node_guid(self):
    """
    Asserts that calling registerComputePartition with empty compute_node_id
    raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      # Shouldn't even be called
      self.assertFalse(True)

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputePartition,
                        None, 'PARTITION_01')

  def test_registerComputePartition_empty_compute_partition_id(self):
    """
    Asserts that calling registerComputePartition with empty
    compute_partition_id raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      # Shouldn't even be called
      self.assertFalse(True)

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputePartition,
                        self._getTestComputeNodeId(), None)

  def test_registerComputePartition_empty_compute_node_guid_empty_compute_partition_id(self):
    """
    Asserts that calling registerComputePartition with empty
    compute_partition_id raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      # Shouldn't even be called
      self.assertFalse(True)

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputePartition,
                        None, None)


  def test_getSoftwareReleaseListFromSoftwareProduct_software_product_reference(self):
    """
    Check that slap.getSoftwareReleaseListFromSoftwareProduct calls
    "/getSoftwareReleaseListFromSoftwareProduct" URL with correct parameters,
    with software_product_reference parameter being specified.
    """
    self.slap.initializeConnection(self.server_url)
    software_product_reference = 'random_reference'
    software_release_url_list = ['1', '2']

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getSoftwareReleaseListFromSoftwareProduct' and qs == {
              'software_product_reference': [software_product_reference]
              }:
        return {
                'status_code': 200,
                'content': dumps(software_release_url_list)
                }

    with httmock.HTTMock(handler):
      self.assertEqual(
        self.slap.getSoftwareReleaseListFromSoftwareProduct(
          software_product_reference=software_product_reference),
        software_release_url_list
      )

  def test_getSoftwareReleaseListFromSoftwareProduct_software_release_url(self):
    """
    Check that slap.getSoftwareReleaseListFromSoftwareProduct calls
    "/getSoftwareReleaseListFromSoftwareProduct" URL with correct parameters,
    with software_release_url parameter being specified.
    """
    self.slap.initializeConnection(self.server_url)
    software_release_url = 'random_url'
    software_release_url_list = ['1', '2']

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getSoftwareReleaseListFromSoftwareProduct' and qs == {
              'software_release_url': [software_release_url]
              }:
        return {
                'status_code': 200,
                'content': dumps(software_release_url_list)
                }

    with httmock.HTTMock(handler):
      self.assertEqual(
        self.slap.getSoftwareReleaseListFromSoftwareProduct(
            software_release_url=software_release_url),
        software_release_url_list
      )

  def test_getSoftwareReleaseListFromSoftwareProduct_too_many_parameters(self):
    """
    Check that slap.getSoftwareReleaseListFromSoftwareProduct raises if
    both parameters are set.
    """
    self.assertRaises(
      AttributeError,
      self.slap.getSoftwareReleaseListFromSoftwareProduct, 'foo', 'bar'
    )

  def test_getSoftwareReleaseListFromSoftwareProduct_no_parameter(self):
    """
    Check that slap.getSoftwareReleaseListFromSoftwareProduct raises if
    both parameters are either not set or None.
    """
    self.assertRaises(
      AttributeError,
      self.slap.getSoftwareReleaseListFromSoftwareProduct
    )

  def test_initializeConnection_getHateoasUrl(self):
    """
    Test that by default, slap will try to fetch Hateoas URL from XML/RPC URL.
    """
    hateoas_url = 'foo'
    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getHateoasUrl':
        return {
                'status_code': 200,
                'content': hateoas_url
                }

    with httmock.HTTMock(handler):
      self.slap.initializeConnection('http://%s' % self.id())
    self.assertEqual(
        self.slap._hateoas_navigator.slapos_master_hateoas_uri,
        hateoas_url
    )

  def test_initializeConnection_specifiedHateoasUrl(self):
    """
    Test that if rest URL is specified, slap will NOT try to fetch
    Hateoas URL from XML/RPC URL.
    """
    hateoas_url = 'foo'
    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getHateoasUrl':
        self.fail('slap should not have contacted master to get Hateoas URL.')

    with httmock.HTTMock(handler):
      self.slap.initializeConnection('http://%s' % self.id(), slapgrid_rest_uri=hateoas_url)
    self.assertEqual(
        self.slap._hateoas_navigator.slapos_master_hateoas_uri,
        hateoas_url
    )

  def test_initializeConnection_noHateoasUrl(self):
    """
    Test that if no rest URL is specified and master does not know about rest,
    it still work.
    """
    hateoas_url = 'foo'
    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getHateoasUrl':
        return {
                'status_code': 404,
                }

    with httmock.HTTMock(handler):
      self.slap.initializeConnection('http://%s' % self.id())
    self.assertEqual(None, getattr(self.slap, '_hateoas_navigator', None))


class TestComputeNode(SlapMixin):
  """
  Tests slapos.slap.slap.ComputeNode class functionality
  """

  def test_compute_node_getComputePartitionList_no_partition(self):
    """
    Asserts that calling ComputeNode.getComputePartitionList without ComputeNode
    Partitions returns empty list
    """
    compute_node_guid = self._getTestComputeNodeId()
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
          'compute_node_reference' in qs and \
          'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getFullComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_compute_node._compute_partition_list = []
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        return {'status_code': 408}
      else:
        return {'status_code': 404}

    with httmock.HTTMock(handler):
      compute_node = self.slap.registerComputeNode(compute_node_guid)
      self.assertEqual(compute_node.getComputePartitionList(), [])

  def _test_compute_node_empty_compute_node_guid(self, compute_node_method):
    """
    Helper method checking if calling Compute Node method with empty id raises
    early.
    """
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      # Shouldn't even be called
      self.assertFalse(True)

    with httmock.HTTMock(handler):
      compute_node = self.slap.registerComputeNode(None)
      self.assertRaises(slapos.slap.NotFoundError,
                        getattr(compute_node, compute_node_method))

  def test_compute_node_getComputePartitionList_empty_compute_node_guid(self):
    """
    Asserts that calling getComputePartitionList with empty
    compute_node_guid raises early, before calling master.
    """
    self._test_compute_node_empty_compute_node_guid('getComputePartitionList')

  def test_compute_node_getSoftwareReleaseList_empty_compute_node_guid(self):
    """
    Asserts that calling getSoftwareReleaseList with empty
    compute_node_guid raises early, before calling master.
    """
    self._test_compute_node_empty_compute_node_guid('getSoftwareReleaseList')

  def test_compute_node_getComputePartitionList_only_partition(self):
    """
    Asserts that calling ComputeNode.getComputePartitionList with only
    Compute Partitions returns empty list
    """
    self.compute_node_guid = self._getTestComputeNodeId()
    partition_id = 'PARTITION_01'
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and qs == {
                'compute_node_reference': [self.compute_node_guid],
                'compute_partition_reference': [partition_id]
                }:
        partition = slapos.slap.ComputePartition(self.compute_node_guid, partition_id)
        return {
                'status_code': 200,
                'content': dumps(partition)
                }
      elif url.path == '/getFullComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._compute_partition_list = []
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      else:
        return {'status_code': 400}

    with httmock.HTTMock(handler):
      self.compute_node = self.slap.registerComputeNode(self.compute_node_guid)
      self.partition = self.slap.registerComputePartition(self.compute_node_guid,
                                                           partition_id)
      self.assertEqual(self.compute_node.getComputePartitionList(), [])

  @unittest.skip("Not implemented")
  def test_compute_node_reportUsage_non_valid_xml_raises(self):
    """
    Asserts that calling ComputeNode.reportUsage with non DTD
    (not defined yet) XML raises (not defined yet) exception
    """

    self.compute_node_guid = self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    self.compute_node = self.slap.registerComputeNode(self.compute_node_guid)
    non_dtd_xml = """<xml>
<non-dtd-parameter name="xerxes">value<non-dtd-parameter name="xerxes">
</xml>"""
    self.assertRaises(UndefinedYetException,
                      self.compute_node.reportUsage,
                      non_dtd_xml)

  @unittest.skip("Not implemented")
  def test_compute_node_reportUsage_valid_xml_invalid_partition_raises(self):
    """
    Asserts that calling ComputeNode.reportUsage with DTD (not defined
    yet) XML which refers to invalid partition raises (not defined yet)
    exception
    """
    self.compute_node_guid = self._getTestComputeNodeId()
    partition_id = 'PARTITION_01'
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    self.compute_node = self.slap.registerComputeNode(self.compute_node_guid)
    self.partition = self.slap.registerComputePartition(self.compute_node_guid,
                                                         partition_id)
    # XXX: As DTD is not defined currently proper XML is not known
    bad_partition_dtd_xml = """<xml>
<compute-partition id='ANOTHER_PARTITION>96.5% CPU</compute-partition>
</xml>"""
    self.assertRaises(UndefinedYetException,
                      self.compute_node.reportUsage,
                      bad_partition_dtd_xml)


class RequestWasCalled(Exception):
  pass


class TestComputePartition(SlapMixin):
  """
  Tests slapos.slap.slap.ComputePartition class functionality
  """

  def test_request_sends_request(self):
    partition_id = 'PARTITION_01'

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
          'compute_node_reference' in qs and \
          'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_id'][0],
            partition_id)
        slap_compute_node._compute_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        raise RequestWasCalled
      else:
        return {
                'status_code': 404
                }

    with httmock.HTTMock(handler):
      self.compute_node_guid = self._getTestComputeNodeId()
      self.slap = slapos.slap.slap()
      self.slap.initializeConnection(self.server_url)
      compute_partition = self.slap.registerComputePartition(
          self.compute_node_guid, partition_id)
      self.assertRaises(RequestWasCalled,
                        compute_partition.request,
                        'http://server/new/' + self._getTestComputeNodeId(),
                        'software_type', 'myref')

  def test_request_not_raises(self):
    partition_id = self.id()

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
          'compute_node_reference' in qs and \
          'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_id'][0],
            partition_id)
        slap_compute_node._compute_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        return {'status_code': 408}
      else:
        return {'status_code': 404}

    self.compute_node_guid = self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    with httmock.HTTMock(handler):
      compute_partition = self.slap.registerComputePartition(
          self.compute_node_guid, partition_id)
      requested_partition = compute_partition.request(
          'http://server/new/' + self._getTestComputeNodeId(),
          'software_type',
          'myref')
      self.assertIsInstance(requested_partition, slapos.slap.ComputePartition)

  def test_request_raises_later(self):
    partition_id = self.id()

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
              'compute_node_reference' in qs and \
              'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_id'][0],
            partition_id)
        slap_compute_node._compute_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        return {'status_code': 408}
      else:
        return {'status_code': 404}

    self.compute_node_guid = self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    with httmock.HTTMock(handler):
      compute_partition = self.slap.registerComputePartition(
          self.compute_node_guid, partition_id)
      requested_partition = compute_partition.request(
          'http://server/new/' + self._getTestComputeNodeId(),
          'software_type',
          'myref')
      self.assertIsInstance(requested_partition, slapos.slap.ComputePartition)
      # as request method does not raise, accessing data raises
      self.assertRaises(slapos.slap.ResourceNotReady,
                        requested_partition.getId)

  def test_request_fullfilled_work(self):
    partition_id = 'PARTITION_01-%s' % self.id()
    requested_partition_id = 'PARTITION_02-%s' % self.id()
    compute_node_guid = self._getTestComputeNodeId()

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
              'compute_node_reference' in qs and \
              'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_id'][0],
            partition_id)
        slap_compute_node._compute_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        from slapos.slap.slap import SoftwareInstance
        slap_partition = SoftwareInstance(
            slap_compute_node_id=compute_node_guid,
            slap_compute_partition_id=requested_partition_id)
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      else:
        return {'status_code': 404}


    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)

    with httmock.HTTMock(handler):
      compute_partition = self.slap.registerComputePartition(
          compute_node_guid, partition_id)
      requested_partition = compute_partition.request(
          'http://server/new/' + self._getTestComputeNodeId(),
          'software_type',
          'myref')
      self.assertIsInstance(requested_partition, slapos.slap.ComputePartition)
      # as request method does not raise, accessing data in case when
      # request was done works correctly
      self.assertEqual(requested_partition_id, requested_partition.getId())

  def test_request_with_slapgrid_request_transaction(self):
    from slapos.slap.slap import COMPUTE_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
    partition_id = self.id()
    instance_root = tempfile.mkdtemp()
    partition_root = os.path.join(instance_root, partition_id)
    os.mkdir(partition_root)
    os.environ['SLAPGRID_INSTANCE_ROOT'] = instance_root
    transaction_file_name = COMPUTE_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME % partition_id
    transaction_file_path = os.path.join(partition_root, transaction_file_name)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
           'compute_node_reference' in qs and \
           'compute_partition_reference' in qs:
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_reference'][0],
            qs['compute_partition_reference'][0])
        return {
                'status_code': 200,
                'content': dumps(slap_partition)
                }
      elif url.path == '/getComputeNodeInformation' and 'compute_node_id' in qs:
        slap_compute_node = slapos.slap.ComputeNode(qs['compute_node_id'][0])
        slap_compute_node._software_release_list = []
        slap_partition = slapos.slap.ComputePartition(
            qs['compute_node_id'][0],
            partition_id)
        slap_compute_node._compute_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': dumps(slap_compute_node)
                }
      elif url.path == '/requestComputePartition':
        raise RequestWasCalled
      else:
        return {
                'status_code': 404
                }

    with httmock.HTTMock(handler):
      self.compute_node_guid = self._getTestComputeNodeId()
      self.slap = slapos.slap.slap()
      self.slap.initializeConnection(self.server_url)
      compute_partition = self.slap.registerComputePartition(
          self.compute_node_guid, partition_id)

      self.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content = f.read()
        self.assertEqual(content, '')
      self.assertRaises(RequestWasCalled,
                        compute_partition.request,
                        'http://server/new/' + self._getTestComputeNodeId(),
                        'software_type', 'myref')
      self.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().splitlines()
        self.assertEqual(content_list, ['myref'])

      # Not override
      compute_partition = self.slap.registerComputePartition(
          self.compute_node_guid, partition_id)
      self.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().splitlines()
        self.assertEqual(content_list, ['myref'])

      # Request a second instance
      self.assertRaises(RequestWasCalled,
                        compute_partition.request,
                        'http://server/new/' + self._getTestComputeNodeId(),
                        'software_type', 'mysecondref')
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().splitlines()
        self.assertEqual(sorted(content_list), ['myref', 'mysecondref'])

  def _test_new_compute_partition_state(self, state):
    """
    Helper method to automate assertions of failing states on new ComputeNode
    Partition
    """
    compute_node_guid = self._getTestComputeNodeId()
    partition_id = self.id()
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
              qs['compute_node_reference'][0] == compute_node_guid and \
              qs['compute_partition_reference'][0] == partition_id:
        partition = slapos.slap.ComputePartition(
            compute_node_guid, partition_id)
        return {
                'status_code': 200,
                'content': dumps(partition)
                }
      else:
        return {'status_code': 404}


    with httmock.HTTMock(handler):
      compute_partition = self.slap.registerComputePartition(
          compute_node_guid, partition_id)
      self.assertRaises(slapos.slap.NotFoundError,
                        getattr(compute_partition, state))

  def test_started_new_ComputePartition_raises(self):
    """
    Asserts that calling ComputePartition.started on new partition raises
    (not defined yet) exception
    """
    self._test_new_compute_partition_state('started')

  def test_stopped_new_ComputePartition_raises(self):
    """
    Asserts that calling ComputePartition.stopped on new partition raises
    (not defined yet) exception
    """
    self._test_new_compute_partition_state('stopped')

  def test_error_new_ComputePartition_works(self):
    """
    Asserts that calling ComputePartition.error on new partition works
    """
    compute_node_guid = self._getTestComputeNodeId()
    partition_id = self.id()
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/registerComputePartition' and \
              qs['compute_node_reference'][0] == compute_node_guid and \
              qs['compute_partition_reference'][0] == partition_id:
        partition = slapos.slap.ComputePartition(
            compute_node_guid, partition_id)
        return {
                'statu_code': 200,
                'content': dumps(partition)
                }
      elif url.path == '/softwareInstanceError':
        parsed_qs_body = parse.parse_qs(req.body)
        # XXX: why do we have compute_node_id and not compute_node_reference?
        # XXX: why do we have compute_partition_id and not
        # compute_partition_reference?
        if (parsed_qs_body['compute_node_id'][0] == compute_node_guid and \
                parsed_qs_body['compute_partition_id'][0] == partition_id and \
                parsed_qs_body['error_log'][0] == 'some error'):
          return {'status_code': 200}

      return {'status_code': 404}


    with httmock.HTTMock(handler):
      compute_partition = slap.registerComputePartition(
          compute_node_guid, partition_id)
      # XXX: Interface does not define return value
      compute_partition.error('some error')

  def _test_setConnectionDict(
    self, connection_dict, slave_reference=None, connection_xml=None,
    getConnectionParameterDict=None, connection_parameter_hash=None):
    getInstanceParameter = []
    if connection_parameter_hash is not None:
      getInstanceParameter = [
        {
          'slave_reference': slave_reference,
          'connection-parameter-hash': connection_parameter_hash
        }
      ]
    with \
        mock.patch.object(
          slapos.slap.ComputePartition, '__init__', return_value=None), \
        mock.patch.object(
          slapos.slap.ComputePartition, 'getConnectionParameterDict',
          return_value=getConnectionParameterDict or {}), \
        mock.patch.object(
          slapos.slap.ComputePartition, 'getInstanceParameter',
          return_value=getInstanceParameter):
      partition = slapos.slap.ComputePartition()
      partition._connection_helper = mock.Mock()
      partition._compute_node_id = 'COMP-0'
      partition._partition_id = 'PART-0'
      partition._connection_helper.POST = mock.Mock()
      partition.setConnectionDict(
        connection_dict, slave_reference=slave_reference)
      if connection_xml:
        connection_xml = connection_xml.encode() if PY3 else connection_xml
        partition._connection_helper.POST.assert_called_with(
          'setComputePartitionConnectionXml',
          data={
            'slave_reference': slave_reference,
            'connection_xml': connection_xml,
            'compute_partition_id': 'PART-0',
            'compute_node_id': 'COMP-0'})
      else:
        partition._connection_helper.POST.assert_not_called()

  def test_setConnectionDict(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      connection_xml='<marshal><dictionary id="i2"><string>a</string>'
                     '<string>b</string></dictionary></marshal>')

  def test_setConnectionDict_optimised(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      getConnectionParameterDict={'a': 'b'},
      connection_xml=False)

  def test_setConnectionDict_optimised_tricky(self):
    self._test_setConnectionDict(
      {u'a': u'b', 'b': '', 'c': None},
      getConnectionParameterDict={'a': 'b', 'b': None, 'c': 'None'},
      connection_xml=False)

  def test_setConnectionDict_update(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      getConnectionParameterDict={'b': 'b'},
      connection_xml='<marshal><dictionary id="i2"><string>a</string>'
                     '<string>b</string></dictionary></marshal>')

  def test_setConnectionDict_slave(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      slave_reference='SLAVE-0',
      connection_xml='<marshal><dictionary id="i2"><string>a</string>'
                     '<string>b</string></dictionary></marshal>')

  def test_setConnectionDict_slave_expired_hash(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      slave_reference='SLAVE-0',
      connection_parameter_hash='mess',
      connection_xml='<marshal><dictionary id="i2"><string>a</string>'
                     '<string>b</string></dictionary></marshal>')

  def test_setConnectionDict_slave_hash(self):
    self._test_setConnectionDict(
      {'a': 'b'},
      slave_reference='SLAVE-0',
      connection_parameter_hash=calculate_dict_hash({'a': 'b'}),
      connection_xml=False)

  def test_setConnectionDict_slave_hash_tricky(self):
    self._test_setConnectionDict(
      {u'a': u'b', 'b': '', 'c': None},
      slave_reference='SLAVE-0',
      connection_parameter_hash=calculate_dict_hash({
        'a': 'b', 'b': None, 'c': 'None'}),
      connection_xml=False)


class TestSoftwareRelease(SlapMixin):
  """
  Tests slap.SoftwareRelease class functionality
  """

  def _test_new_software_release_state(self, state):
    """
    Helper method to automate assertions of failing states on new Software
    Release
    """
    self.software_release_uri = 'http://server/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(
        self.software_release_uri)
    method = getattr(software_release, state)
    self.assertRaises(NameError, method)

  def test_available_new_SoftwareRelease_raises(self):
    """
    Asserts that calling SoftwareRelease.available on new software release
    raises NameError exception
    """
    self._test_new_software_release_state('available')

  def test_building_new_SoftwareRelease_raises(self):
    """
    Asserts that calling SoftwareRelease.building on new software release
    raises NameError exception
    """
    self._test_new_software_release_state('building')

  def test_error_new_SoftwareRelease_works(self):
    """
    Asserts that calling SoftwareRelease.error on software release works
    """
    compute_node_guid = self._getTestComputeNodeId()
    software_release_uri = 'http://server/' + self._getTestComputeNodeId()
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def handler(url, req):
      qs = parse.parse_qs(req.body)
      if url.path == '/softwareReleaseError' and \
              qs['compute_node_id'][0] == compute_node_guid and \
              qs['url'][0] == software_release_uri and \
              qs['error_log'][0] == 'some error':
        return {
                'status_code': 200
                }
      return {'status_code': 404}


    with httmock.HTTMock(handler):
      software_release = self.slap.registerSoftwareRelease(software_release_uri)
      software_release._compute_node_guid = compute_node_guid
      software_release.error('some error')


class TestOpenOrder(SlapMixin):
  def test_request_sends_request(self):
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()

    def handler(url, req):
      if url.path == '/requestComputePartition':
        raise RequestWasCalled

    with httmock.HTTMock(handler):
      self.assertRaises(RequestWasCalled,
                        open_order.request,
                        software_release_uri, 'myrefe')

  @unittest.skip('unclear what should be returned')
  def test_request_not_raises(self):
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration

    def handler(url, req):
      if url.path == '/requestComputePartition':
        pass
        # XXX what to do here?

    with httmock.HTTMock(handler):
      open_order = self.slap.registerOpenOrder()
      compute_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(compute_partition, slapos.slap.ComputePartition)

  def test_request_raises_later(self):
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()

    def handler(url, req):
      return {'status_code': 408}

    with httmock.HTTMock(handler):
      compute_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(compute_partition, slapos.slap.ComputePartition)

      self.assertRaises(slapos.slap.ResourceNotReady,
                        compute_partition.getId)

  def test_request_fullfilled_work(self):
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    compute_node_guid = self._getTestComputeNodeId()
    requested_partition_id = self.id()

    def handler(url, req):
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
          slap_compute_node_id=compute_node_guid,
          slap_compute_partition_id=requested_partition_id)
      return {
              'status_code': 200,
              'content': dumps(slap_partition)
              }

    with httmock.HTTMock(handler):
      compute_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(compute_partition, slapos.slap.ComputePartition)
      self.assertEqual(requested_partition_id, compute_partition.getId())


  def test_request_getConnectionParameter(self):
    """ Backward compatibility API for slapproxy older them 1.0.1 """
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    compute_node_guid = self._getTestComputeNodeId()
    requested_partition_id = self.id()

    def handler(url, req):
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
          _connection_dict = {"url": 'URL_CONNECTION_PARAMETER'},
          slap_compute_node_id=compute_node_guid,
          slap_compute_partition_id=requested_partition_id)
      return {
              'status_code': 200,
              'content': dumps(slap_partition)
              }


    with httmock.HTTMock(handler):
      compute_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(compute_partition, slapos.slap.ComputePartition)
      self.assertEqual(requested_partition_id, compute_partition.getId())
      self.assertEqual("URL_CONNECTION_PARAMETER",
                       compute_partition.getConnectionParameter('url'))


  def test_request_connection_dict_backward_compatibility(self):
    """ Backward compatibility API for slapproxy older them 1.0.1 """
    software_release_uri = 'http://server/new/' + self._getTestComputeNodeId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    compute_node_guid = self._getTestComputeNodeId()
    requested_partition_id = self.id()

    def handler(url, req):
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
          connection_xml="""<?xml version='1.0' encoding='utf-8'?>
<instance>
  <parameter id="url">URL_CONNECTION_PARAMETER</parameter>
</instance>""",
          slap_compute_node_id=compute_node_guid,
          slap_compute_partition_id=requested_partition_id)
      return {
              'status_code': 200,
              'content': dumps(slap_partition)
              }

    with httmock.HTTMock(handler):
      compute_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(compute_partition, slapos.slap.ComputePartition)
      self.assertEqual(requested_partition_id, compute_partition.getId())
      self.assertEqual("URL_CONNECTION_PARAMETER",
                       compute_partition.getConnectionParameter('url'))

  def test_getInformation(self):
    self.slap = slapos.slap.slap()
    parameter_dict = {
      "param1": "value1",
      "param2_dict": {
        "param2_param1": "",
        "param2_param2_dict": {},
        "param2_param3_dict": {"param": "value"}
        }
      }

    link_keys = {
        "type": {
          "href": "urn:jio:get:portal_types/Instance Tree",
          "name": "Instance Tree"
          },
        }
    instance_tree_info_dict = {
        "connection_parameter_list": {
          "title": "my_connection_parameter_dict",
          "default": [
            {
              "connection_key": "key1",
              "connection_value": "value1"
              },
            {
              "connection_key": "key2",
              "connection_value": "value2"
              },
            ],
          "key": "field_my_connection_parameter_list",
          "type": "StringField"
          },
        "title": {
            "title": "Title",
            "default": "myrefe",
            "key": "field_my_title",
            "type": "StringField"
            },
        "text_content": {
            "title": "Parameter XML",
            "default": dict2xml({'_':json.dumps(parameter_dict)}),
            "key": "field_my_text_content",
            "type": "TextAreaField"
            },
        "slap_state": {
            "title": "Slap State",
            "default": "stop_requested",
            "key": "field_my_slap_state",
            "type": "StringField"
            },
        "source_title": {
            "title": "Current Organisation",
            "default": "",
            "key": "field_my_source_title",
            "type": "StringField"
            },
        "url_string": {
            "title": "Url String",
            "default": "https://lab.nexedi.com/nexedi/slapos/raw/1.0.115/software/kvm/software.cfg",
            "key": "field_my_url_string",
            "type": "StringField"
            }
        }
    view_dict = {}
    for k in instance_tree_info_dict:
      view_dict["my_%s" % k] = instance_tree_info_dict[k]
    view_dict["_embedded"] = {
        "form_definition": {
          "update_action_title": "",
          "_debug": "traverse",
          "pt": "form_view_editable",
          "title": "InstanceTree_viewAsHateoas",
          "_links": {
            "portal": {
                "href": "urn:jio:get:erp5",
                "name": "ERP5"
                },
            },
          "action": "InstanceTree_editWebMode",
          "update_action": ""
        }
      }
    view_dict["form_id"] = {
        "title": "form_id",
        "default": "InstanceTree_viewAsHateoas",
        "key": "form_id",
        "type": "StringField"
        }
    view_dict["_links"] = {
        "traversed_document": {
          "href": "urn:jio:get:instance_tree_module/my_refe_id",
          "name": "instance_tree_module/my_refe_id",
          "title": "myrefe"
          },
        "self": {
          "href": "https://localhost/instance_tree_module/my_refe_id/InstanceTree_viewAsHateoas"
          },
        "form_definition": {
          "href": "urn:jio:get:portal_skins/slapos_hal_json_style/InstanceTree_viewAsHateoas",
          "name": "InstanceTree_viewAsHateoas"
          }
        }
    hateoas_url = "/custom_hateoas_url"
    def handler(url, req):
      qs = parse.parse_qs(url.query)
      if url.path == '/getHateoasUrl':
        return {
            'status_code': 200,
            'content': "http://localhost" + hateoas_url
            }
      elif url.path == hateoas_url:
        return {
            'status_code': 200,
            'content': {
              "default_view": "view",
              "_links": {
                "traverse": {
                  "href": "https://localhost/SLAPTEST_getHateoas?mode=traverse{&relative_url,view}",
                  "name": "Traverse",
                  "templated": True
                  },
                "raw_search": {
                  "href": "https://localhost/SLAPTEST_getHateoas?mode=search{&query,select_list*,limit*,group_by*,sort_on*,local_roles*,selection_domain*}",
                  "name": "Raw Search",
                  "templated": True
                  },
                },
              "_debug": "root",
              "title": "Hateoas"
              }
            }
      elif url.path == '/SLAPTEST_getHateoas':
        if ("mode=search" in url.query):
          return {
              'status_code': 200,
              'content': {
                "_sort_on": "",
                "_embedded": {
                  "contents": [
                    {
                      "_links": {
                        "self": {
                          "href": "urn:jio:get:instance_tree_module/my_refe_id"
                          }
                        },
                      "relative_url": "instance_tree_module/my_refe_id",
                      "title": "myrefe"
                      }
                    ]
                  },
                "_debug": "search",
                "_limit": "200",
                "_local_roles": "",
                "_query": "portal_type:\"Instance Tree\" AND validation_state:validated AND title:=\"myrefe\"",
                "_select_list": [
                  "title",
                  "relative_url"
                  ],
                "_links": {
                  "self": {
                    "href": "https://localhost/SLAPTEST_getHateoas"
                    },
                  "portal": {
                    "href": "urn:jio:get:erp5",
                    "name": "ERP5"
                    },
                  "site_root": {
                    "href": "urn:jio:get:web_site_module/hateoas",
                    "name": "Hateoas"
                    }
                  },
                "_group_by": "",
                "_selection_domain": ""
                }
              }

        elif ("mode=traverse" in url.query):
          return {
              'status_code': 200,
              'content': {
                "_embedded": {
                  "_view": view_dict
                  },
                "_links": link_keys,
                "_debug": "traverse",
                "title": "myrefe"
                }
              }

    with httmock.HTTMock(handler):
      self.slap.initializeConnection('http://%s' % self.id())
      open_order = self.slap.registerOpenOrder()
      compute_node_guid = self._getTestComputeNodeId()
      requested_partition_id = self.id()
      software_instance = open_order.getInformation('myrefe')
      self.assertIsInstance(software_instance, slapos.slap.SoftwareInstance)
      for key in instance_tree_info_dict:
        if key not in link_keys:
          self.assertEqual(getattr(software_instance, '_' + key), instance_tree_info_dict[key]["default"])
      self.assertEqual(software_instance._parameter_dict, {'_': parameter_dict})
      self.assertEqual(software_instance._requested_state, instance_tree_info_dict['slap_state']["default"])
      self.assertEqual(software_instance._connection_dict, instance_tree_info_dict['connection_parameter_list']["default"])
      self.assertEqual(software_instance._software_release_url, instance_tree_info_dict['url_string']["default"])


class TestSoftwareProductCollection(SlapMixin):
  def setUp(self):
    SlapMixin.setUp(self)
    self.real_getSoftwareReleaseListFromSoftwareProduct =\
        slapos.slap.slap.getSoftwareReleaseListFromSoftwareProduct

    def fake_getSoftwareReleaseListFromSoftwareProduct(inside_self, software_product_reference):
      return self.getSoftwareReleaseListFromSoftwareProduct_response
    slapos.slap.slap.getSoftwareReleaseListFromSoftwareProduct =\
        fake_getSoftwareReleaseListFromSoftwareProduct

    self.product_collection = slapos.slap.SoftwareProductCollection(
        logging.getLogger(), slapos.slap.slap())

  def tearDown(self):
    slapos.slap.slap.getSoftwareReleaseListFromSoftwareProduct =\
        self.real_getSoftwareReleaseListFromSoftwareProduct

  def test_get_product(self):
    """
    Test that the get method (aliased to __getattr__) returns the first element
    of the list given by getSoftwareReleaseListFromSoftwareProduct (i.e the
    best one).
    """
    self.getSoftwareReleaseListFromSoftwareProduct_response = ['0', '1', '2']
    self.assertEqual(
      self.product_collection.get('random_reference'),
      self.getSoftwareReleaseListFromSoftwareProduct_response[0]
    )

  def test_get_product_empty_product(self):
    """
    Test that the get method (aliased to __getattr__) raises if no
    Software Release is related to the Software Product, or if the
    Software Product does not exist.
    """
    self.getSoftwareReleaseListFromSoftwareProduct_response = []
    self.assertRaises(
      AttributeError,
      self.product_collection.get, 'random_reference',
    )

  def test_get_product_getattr(self):
    """
    Test that __getattr__ method is bound to get() method.
    """
    self.getSoftwareReleaseListFromSoftwareProduct_response = ['0']
    self.product_collection.foo
    self.assertEqual(
      self.product_collection.__getattr__,
      self.product_collection.get
    )
    self.assertEqual(self.product_collection.foo, '0')

