slap.py 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
##############################################################################
#
# 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 xml_marshaller
import unittest
import urlparse
import httplib
import slapos.slap
import os

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

class SlapMixin(unittest.TestCase):
  """
  Usefull 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._patchHttplib()
      self.server_url = 'http://localhost/'
    else:
      self.server_url = self._server_url
48
    print 'Testing against SLAP server %r' % self.server_url
49 50
    self.slap = slapos.slap.slap()
    self.partition_id = 'PARTITION_01'
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  def tearDown(self):
    if self._server_url is None:
      self._unpatchHttplib()

  def _patchHttplib(self):
    """Overrides httplib"""
    import mock.httplib

    self.saved_httplib = dict()

    for fake in vars(mock.httplib):
      self.saved_httplib[fake] = getattr(httplib, fake, None)
      setattr(httplib, fake, getattr(mock.httplib, fake))

  def _unpatchHttplib(self):
    """Restores httplib overriding"""
    import httplib
    for name, original_value in self.saved_httplib.items():
      setattr(httplib, name, original_value)
    del self.saved_httplib

  def _getTestComputerId(self):
    """
    Returns the computer id used by the test
    """
    return self.id()
    return os.environ.get('TEST_SLAP_COMPUTER_ID', self.id())

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

  def test_slap_initialisation(self):
    """
87
    Asserts that slap initialisation works properly in case of
88 89 90 91
    passing correct url
    """
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection(self.server_url)
92 93 94 95
    self.assertTrue(
        slap_instance._connection_helper.host in self.server_url)
    self.assertTrue(
        slap_instance._connection_helper.path in self.server_url)
96 97 98

  def test_slap_initialisation_wrong_url(self):
    """
99
    Asserts that slap initialisation raises exception when passed url
100 101 102 103
    is not correct
    """
    server_url = 'https://user:pass@server/path/path?parameter=notAcceptable'
    slap_instance = slapos.slap.slap()
104
    self.assertRaises(AttributeError,
105 106 107 108
                      slap_instance.initializeConnection, server_url)

  def test_registerComputer_with_new_guid(self):
    """
109
    Asserts that calling slap.registerComputer with new guid returns
110 111 112 113 114 115 116 117 118 119
    Computer object
    """
    computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer = self.slap.registerComputer(computer_guid)
    self.assertTrue(isinstance(computer, slapos.slap.Computer))

  def test_registerComputer_with_existing_guid(self):
    """
120
    Asserts that calling slap.registerComputer with already used guid
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    returns Computer object
    """
    computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer = self.slap.registerComputer(computer_guid)
    self.assertTrue(isinstance(computer, slapos.slap.Computer))

    computer2 = self.slap.registerComputer(computer_guid)
    self.assertTrue(isinstance(computer2, slapos.slap.Computer))

  # 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):
    """
136
    Asserts that calling slap.registerSoftwareRelease with new guid
137 138 139 140 141 142 143 144 145 146 147
    returns SoftwareRelease object
    """
    software_release_uri = 'http://server/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(software_release_uri)
    self.assertTrue(isinstance(software_release,
        slapos.slap.SoftwareRelease))

  def test_registerSoftwareRelease_with_existing_uri(self):
    """
148
    Asserts that calling slap.registerSoftwareRelease with already
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    used guid returns SoftwareRelease object
    """
    software_release_uri = 'http://server/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(software_release_uri)
    self.assertTrue(isinstance(software_release,
        slapos.slap.SoftwareRelease))

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

  def test_registerComputerPartition_new_partition_id_known_computer_guid(self):
    """
164
    Asserts that calling slap.registerComputerPartition on known computer
165 166
    returns ComputerPartition object
    """
167 168
    computer_guid = self._getTestComputerId()
    partition_id = self.partition_id
169
    self.slap.initializeConnection(self.server_url)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    self.slap.registerComputer(computer_guid)

    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         parsed_qs['computer_reference'][0] == computer_guid and \
         parsed_qs['computer_partition_reference'][0] == partition_id:
        partition = slapos.slap.ComputerPartition(
            computer_guid, partition_id)
        return (200, {}, xml_marshaller.xml_marshaller.dumps(partition))
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response

    partition = self.slap.registerComputerPartition(computer_guid, partition_id)
186 187 188 189
    self.assertTrue(isinstance(partition, slapos.slap.ComputerPartition))

  def test_registerComputerPartition_existing_partition_id_known_computer_guid(self):
    """
190
    Asserts that calling slap.registerComputerPartition on known computer
191 192 193
    returns ComputerPartition object
    """
    self.test_registerComputerPartition_new_partition_id_known_computer_guid()
194
    partition = self.slap.registerComputerPartition(self._getTestComputerId(),
195 196 197 198 199
        self.partition_id)
    self.assertTrue(isinstance(partition, slapos.slap.ComputerPartition))

  def test_registerComputerPartition_unknown_computer_guid(self):
    """
200
    Asserts that calling slap.registerComputerPartition on unknown
201 202
    computer raises (not defined yet) exception
    """
203
    raise NotImplementedError()
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)

    partition_id = 'PARTITION_01'

    self.assertRaises(UndefinedYetException,
        self.slap.registerComputerPartition, computer_guid, partition_id)

class TestComputer(SlapMixin):
  """
  Tests slapos.slap.slap.Computer class functionality
  """

  def test_computer_getComputerPartitionList_no_partition(self):
    """
    Asserts that calling Computer.getComputerPartitionList without Computer
    Partitions returns empty list
    """
223 224 225 226 227
    computer_guid = self._getTestComputerId()
    slap = self.slap
    slap.initializeConnection(self.server_url)
    computer = self.slap.registerComputer(computer_guid)
    self.assertEqual(computer.getComputerPartitionList(), [])
228 229 230

  def test_computer_getComputerPartitionList_only_partition(self):
    """
231
    Asserts that calling Computer.getComputerPartitionList with only
232 233 234 235 236 237 238 239 240 241 242 243 244
    Computer Partitions returns empty list
    """
    self.computer_guid = self._getTestComputerId()
    partition_id = 'PARTITION_01'
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    self.computer = self.slap.registerComputer(self.computer_guid)
    self.partition = self.slap.registerComputerPartition(self.computer_guid,
        partition_id)
    self.assertEqual(self.computer.getComputerPartitionList(), [])

  def test_computer_reportUsage_non_valid_xml_raises(self):
    """
245
    Asserts that calling Computer.reportUsage with non DTD
246 247 248 249 250 251 252 253 254 255 256 257 258 259
    (not defined yet) XML raises (not defined yet) exception
    """
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    self.computer = self.slap.registerComputer(self.computer_guid)
    non_dtd_xml = """<xml>
<non-dtd-parameter name="xerxes">value<non-dtd-parameter name="xerxes">
</xml>"""
    self.assertRaises(UndefinedYetException, self.computer.reportUsage,
        non_dtd_xml)

  def test_computer_reportUsage_valid_xml_invalid_partition_raises(self):
    """
260
    Asserts that calling Computer.reportUsage with DTD (not defined
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    yet) XML which refers to invalid partition raises (not defined yet)
    exception
    """
    self.computer_guid = self._getTestComputerId()
    partition_id = 'PARTITION_01'
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    self.computer = self.slap.registerComputer(self.computer_guid)
    self.partition = self.slap.registerComputerPartition(self.computer_guid,
        partition_id)
    # XXX: As DTD is not defined currently proper XML is not known
    bad_partition_dtd_xml = """<xml>
<computer-partition id='ANOTHER_PARTITION>96.5% CPU</computer-partition>
</xml>"""
    self.assertRaises(UndefinedYetException, self.computer.reportUsage,
        bad_partition_dtd_xml)

class RequestWasCalled(Exception):
  pass

class TestComputerPartition(SlapMixin):
  """
  Tests slapos.slap.slap.ComputerPartition class functionality
  """

  def test_request_sends_request(self):
    partition_id = 'PARTITION_01'
    request_called_amount = 0
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         'computer_reference' in parsed_qs and \
         'computer_partition_reference' in parsed_qs:
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_reference'][0],
          parsed_qs['computer_partition_reference'][0])
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
      elif parsed_url.path == 'getComputerInformation' and \
         'computer_id' in parsed_qs:
        slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0])
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_id'][0],
          partition_id)
        slap_computer._computer_partition_list = [slap_partition]
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer))
      elif parsed_url.path == 'requestComputerPartition':
        raise RequestWasCalled
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer_partition = self.slap.registerComputerPartition(
        self.computer_guid, partition_id)
    self.assertRaises(RequestWasCalled, computer_partition.request,
        'http://server/new/' + self._getTestComputerId(),
        'software_type',
        'myref')

  def test_request_not_raises(self):
    partition_id = 'PARTITION_01'
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         'computer_reference' in parsed_qs and \
         'computer_partition_reference' in parsed_qs:
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_reference'][0],
          parsed_qs['computer_partition_reference'][0])
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
      elif parsed_url.path == 'getComputerInformation' and \
         'computer_id' in parsed_qs:
        slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0])
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_id'][0],
          partition_id)
        slap_computer._computer_partition_list = [slap_partition]
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer))
      elif parsed_url.path == 'requestComputerPartition':
        return (408, {}, '')
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer_partition = self.slap.registerComputerPartition(
        self.computer_guid, partition_id)
    requested_partition = computer_partition.request(
        'http://server/new/' + self._getTestComputerId(),
        'software_type',
        'myref')
    self.assertTrue(isinstance(requested_partition,
        slapos.slap.ComputerPartition))

  def test_request_raises_later(self):
    partition_id = 'PARTITION_01'
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         'computer_reference' in parsed_qs and \
         'computer_partition_reference' in parsed_qs:
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_reference'][0],
          parsed_qs['computer_partition_reference'][0])
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
      elif parsed_url.path == 'getComputerInformation' and \
         'computer_id' in parsed_qs:
        slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0])
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_id'][0],
          partition_id)
        slap_computer._computer_partition_list = [slap_partition]
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer))
      elif parsed_url.path == 'requestComputerPartition':
        return (408, {}, '')
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer_partition = self.slap.registerComputerPartition(
        self.computer_guid, partition_id)
    requested_partition = computer_partition.request(
        'http://server/new/' + self._getTestComputerId(),
        'software_type',
        'myref')
    self.assertTrue(isinstance(requested_partition,
        slapos.slap.ComputerPartition))
    # as request method does not raise, accessing data raises
399 400
    self.assertRaises(slapos.slap.ResourceNotReady,
                      requested_partition.getId)
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

  def test_request_fullfilled_work(self):
    partition_id = 'PARTITION_01'
    requested_partition_id = 'PARTITION_02'
    computer_guid = self._getTestComputerId()
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         'computer_reference' in parsed_qs and \
         'computer_partition_reference' in parsed_qs:
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_reference'][0],
          parsed_qs['computer_partition_reference'][0])
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
      elif parsed_url.path == 'getComputerInformation' and \
         'computer_id' in parsed_qs:
        slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0])
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
          parsed_qs['computer_id'][0],
          partition_id)
        slap_computer._computer_partition_list = [slap_partition]
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer))
      elif parsed_url.path == 'requestComputerPartition':
        from slapos.slap.slap import SoftwareInstance
        slap_partition = SoftwareInstance(
          slap_computer_id=computer_guid,
          slap_computer_partition_id=requested_partition_id)
        return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer_partition = self.slap.registerComputerPartition(
        computer_guid, partition_id)
    requested_partition = computer_partition.request(
        'http://server/new/' + self._getTestComputerId(),
        'software_type',
        'myref')
    self.assertTrue(isinstance(requested_partition,
        slapos.slap.ComputerPartition))
    # 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_new_computer_partition_state(self, state):
    """
450
    Helper method to automate assertions of failing states on new Computer
451 452
    Partition
    """
453
    computer_guid = self._getTestComputerId()
454
    partition_id = 'PARTITION_01'
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         parsed_qs['computer_reference'][0] == computer_guid and \
         parsed_qs['computer_partition_reference'][0] == partition_id:
        partition = slapos.slap.ComputerPartition(
            computer_guid, partition_id)
        return (200, {}, xml_marshaller.xml_marshaller.dumps(partition))
      else:
        return (404, {}, '')
    httplib.HTTPConnection._callback = server_response

471
    computer_partition = self.slap.registerComputerPartition(
472
        computer_guid, partition_id)
473
    method = getattr(computer_partition, state)
474
    self.assertRaises(slapos.slap.NotFoundError, method)
475 476 477

  def test_available_new_ComputerPartition_raises(self):
    """
478
    Asserts that calling ComputerPartition.available on new partition
479 480 481 482 483 484
    raises (not defined yet) exception
    """
    self._test_new_computer_partition_state('available')

  def test_building_new_ComputerPartition_raises(self):
    """
485
    Asserts that calling ComputerPartition.building on new partition raises
486 487 488 489 490 491
    (not defined yet) exception
    """
    self._test_new_computer_partition_state('building')

  def test_started_new_ComputerPartition_raises(self):
    """
492
    Asserts that calling ComputerPartition.started on new partition raises
493 494 495 496 497 498
    (not defined yet) exception
    """
    self._test_new_computer_partition_state('started')

  def test_stopped_new_ComputerPartition_raises(self):
    """
499
    Asserts that calling ComputerPartition.stopped on new partition raises
500 501 502 503 504 505 506 507
    (not defined yet) exception
    """
    self._test_new_computer_partition_state('stopped')

  def test_error_new_ComputerPartition_works(self):
    """
    Asserts that calling ComputerPartition.error on new partition works
    """
508
    computer_guid = self._getTestComputerId()
509
    partition_id = 'PARTITION_01'
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    slap = self.slap
    slap.initializeConnection(self.server_url)

    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      if parsed_url.path == 'registerComputerPartition' and \
         parsed_qs['computer_reference'][0] == computer_guid and \
         parsed_qs['computer_partition_reference'][0] == partition_id:
        partition = slapos.slap.ComputerPartition(
            computer_guid, partition_id)
        return (200, {}, xml_marshaller.xml_marshaller.dumps(partition))
      elif parsed_url.path == 'softwareInstanceError':
        parsed_qs_body = urlparse.parse_qs(body)
        # XXX: why do we have computer_id and not computer_reference?
        # XXX: why do we have computer_partition_id and not
        # computer_partition_reference?
        if parsed_qs_body['computer_id'][0] == computer_guid and \
           parsed_qs_body['computer_partition_id'][0] == partition_id and \
           parsed_qs_body['error_log'][0] == 'some error':
          return (200, {}, '')

      return (404, {}, '')
    httplib.HTTPConnection._callback = server_response

    computer_partition = slap.registerComputerPartition(
        computer_guid, partition_id)
537 538 539 540 541 542 543 544 545 546
    # XXX: Interface does not define return value
    computer_partition.error('some error')

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

  def _test_new_software_release_state(self, state):
    """
547
    Helper method to automate assertions of failing states on new Software
548 549 550 551 552 553 554 555 556 557 558 559
    Release
    """
    self.software_release_uri = 'http://server/' + self._getTestComputerId()
    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(UndefinedYetException, method)

  def test_available_new_SoftwareRelease_raises(self):
    """
560
    Asserts that calling SoftwareRelease.available on new software release
561 562 563 564 565 566
    raises (not defined yet) exception
    """
    self._test_new_software_release_state('available')

  def test_building_new_SoftwareRelease_raises(self):
    """
567
    Asserts that calling SoftwareRelease.building on new software release
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    raises (not defined yet) exception
    """
    self._test_new_software_release_state('building')

  def test_error_new_SoftwareRelease_works(self):
    """
    Asserts that calling SoftwareRelease.error on new software release works
    """
    self.software_release_uri = 'http://server/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    software_release = self.slap.registerSoftwareRelease(
        self.software_release_uri)
    # XXX: Interface does not define return value
    software_release.error('some error')

class TestOpenOrder(SlapMixin):
  def test_request_sends_request(self):
    software_release_uri = 'http://server/new/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      if parsed_url.path == 'requestComputerPartition':
        raise RequestWasCalled
    httplib.HTTPConnection._callback = server_response
    self.assertRaises(RequestWasCalled, open_order.request, software_release_uri, 'myrefe')

  def test_request_not_raises(self):
    software_release_uri = 'http://server/new/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    computer_partition = open_order.request(software_release_uri, 'myrefe')
    self.assertTrue(isinstance(computer_partition,
        slapos.slap.ComputerPartition))

  def test_request_raises_later(self):
    software_release_uri = 'http://server/new/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    def server_response(self, path, method, body, header):
      return (408, {}, '')
    httplib.HTTPConnection._callback = server_response
    computer_partition = open_order.request(software_release_uri, 'myrefe')
    self.assertTrue(isinstance(computer_partition,
        slapos.slap.ComputerPartition))

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

  def test_request_fullfilled_work(self):
    software_release_uri = 'http://server/new/' + self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    # XXX: Interface lack registerOpenOrder method declaration
    open_order = self.slap.registerOpenOrder()
    computer_guid = self._getTestComputerId()
    requested_partition_id = 'PARTITION_01'
    def server_response(self, path, method, body, header):
      parsed_url = urlparse.urlparse(path.lstrip('/'))
      parsed_qs = urlparse.parse_qs(parsed_url.query)
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
        slap_computer_id=computer_guid,
        slap_computer_partition_id=requested_partition_id)
      return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition))
    httplib.HTTPConnection._callback = server_response

    computer_partition = open_order.request(software_release_uri, 'myrefe')
    self.assertTrue(isinstance(computer_partition,
        slapos.slap.ComputerPartition))
    self.assertTrue(requested_partition_id, computer_partition.getId())

if __name__ == '__main__':
  print 'You can point to any SLAP server by setting TEST_SLAP_SERVER_URL '\
      'environment variable'
  unittest.main()