slap.py 45.5 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
##############################################################################
#
# 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.
#
##############################################################################
27

28
import logging
29
import os
30 31
import unittest
import urlparse
32
import tempfile
33

34 35
import httmock

36
import slapos.slap
37 38
import xml_marshaller

39 40 41 42

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

43

44 45
class SlapMixin(unittest.TestCase):
  """
46
  Useful methods for slap tests
47 48 49 50 51 52 53
  """
  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
54
    print 'Testing against SLAP server %r' % self.server_url
55 56
    self.slap = slapos.slap.slap()
    self.partition_id = 'PARTITION_01'
57 58
    if os.environ.has_key('SLAPGRID_INSTANCE_ROOT'):
      del os.environ['SLAPGRID_INSTANCE_ROOT']
59 60

  def tearDown(self):
61
    pass
62

63 64 65 66 67 68
  def _getTestComputerId(self):
    """
    Returns the computer id used by the test
    """
    return self.id()

69

70 71 72 73 74 75 76
class TestSlap(SlapMixin):
  """
  Test slap against slap server
  """

  def test_slap_initialisation(self):
    """
77
    Asserts that slap initialisation works properly in case of
78 79 80 81
    passing correct url
    """
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection(self.server_url)
82
    self.assertEquals(slap_instance._connection_helper.slapgrid_uri, self.server_url)
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  def test_slap_initialisation_ipv6_and_port(self):
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://1234:1234:1234:1234:1:1:1:1:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[1234: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://1234:1234:1234:1234:1:1:1:1/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[1234: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://[1234:1234:1234:1234:1:1:1:1]:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://[1234: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):
117
    # XXX this really opens a connection !
118 119 120 121 122 123 124
    slap_instance = slapos.slap.slap()
    slap_instance.initializeConnection("http://foo.com:5000/foo/")
    self.assertEqual(
        slap_instance._connection_helper.slapgrid_uri,
        "http://foo.com:5000/foo/"
    )

125 126
  def test_registerComputer_with_new_guid(self):
    """
127
    Asserts that calling slap.registerComputer with new guid returns
128 129 130 131 132 133
    Computer object
    """
    computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer = self.slap.registerComputer(computer_guid)
Marco Mariani's avatar
Marco Mariani committed
134
    self.assertIsInstance(computer, slapos.slap.Computer)
135 136 137

  def test_registerComputer_with_existing_guid(self):
    """
138
    Asserts that calling slap.registerComputer with already used guid
139 140 141 142 143 144
    returns Computer object
    """
    computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
    computer = self.slap.registerComputer(computer_guid)
Marco Mariani's avatar
Marco Mariani committed
145
    self.assertIsInstance(computer, slapos.slap.Computer)
146 147

    computer2 = self.slap.registerComputer(computer_guid)
Marco Mariani's avatar
Marco Mariani committed
148
    self.assertIsInstance(computer2, slapos.slap.Computer)
149 150 151 152 153

  # 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):
    """
154
    Asserts that calling slap.registerSoftwareRelease with new guid
155 156 157 158 159 160
    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)
Marco Mariani's avatar
Marco Mariani committed
161
    self.assertIsInstance(software_release, slapos.slap.SoftwareRelease)
162 163 164

  def test_registerSoftwareRelease_with_existing_uri(self):
    """
165
    Asserts that calling slap.registerSoftwareRelease with already
166 167 168 169 170 171
    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)
Marco Mariani's avatar
Marco Mariani committed
172
    self.assertIsInstance(software_release, slapos.slap.SoftwareRelease)
173 174

    software_release2 = self.slap.registerSoftwareRelease(software_release_uri)
Marco Mariani's avatar
Marco Mariani committed
175
    self.assertIsInstance(software_release2, slapos.slap.SoftwareRelease)
176 177 178

  def test_registerComputerPartition_new_partition_id_known_computer_guid(self):
    """
179
    Asserts that calling slap.registerComputerPartition on known computer
180 181
    returns ComputerPartition object
    """
182 183
    computer_guid = self._getTestComputerId()
    partition_id = self.partition_id
184
    self.slap.initializeConnection(self.server_url)
185 186
    self.slap.registerComputer(computer_guid)

187 188 189 190 191 192 193 194 195 196 197 198
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
            and qs == {
                'computer_reference': [computer_guid],
                'computer_partition_reference': [partition_id]
                }):
        partition = slapos.slap.ComputerPartition(computer_guid, partition_id)
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(partition)
                }
199
      else:
200 201 202
        return {'status_code': 400}

    self._handler = handler
203

204 205 206
    with httmock.HTTMock(handler):
      partition = self.slap.registerComputerPartition(computer_guid, partition_id)
      self.assertIsInstance(partition, slapos.slap.ComputerPartition)
207 208 209

  def test_registerComputerPartition_existing_partition_id_known_computer_guid(self):
    """
210
    Asserts that calling slap.registerComputerPartition on known computer
211 212 213
    returns ComputerPartition object
    """
    self.test_registerComputerPartition_new_partition_id_known_computer_guid()
214 215 216 217
    with httmock.HTTMock(self._handler):
      partition = self.slap.registerComputerPartition(self._getTestComputerId(),
                                                      self.partition_id)
      self.assertIsInstance(partition, slapos.slap.ComputerPartition)
218 219 220

  def test_registerComputerPartition_unknown_computer_guid(self):
    """
221
    Asserts that calling slap.registerComputerPartition on unknown
222
    computer raises NotFoundError exception
223 224 225 226 227
    """
    computer_guid = self._getTestComputerId()
    self.slap.initializeConnection(self.server_url)
    partition_id = 'PARTITION_01'

228 229 230 231 232 233 234 235
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
            and qs == {
                'computer_reference': [computer_guid],
                'computer_partition_reference': [partition_id]
                }):
        return {'status_code': 404}
236
      else:
237 238 239 240 241 242
        return {'status_code': 0}

    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputerPartition,
                        computer_guid, partition_id)
243

244

245 246 247 248 249 250 251
  def test_getFullComputerInformation_empty_computer_guid(self):
    """
    Asserts that calling getFullComputerInformation with empty computer_id
    raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

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

256 257 258 259
    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap._connection_helper.getFullComputerInformation,
                        None)
260 261 262 263 264 265 266 267

  def test_registerComputerPartition_empty_computer_guid(self):
    """
    Asserts that calling registerComputerPartition with empty computer_id
    raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

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

272 273 274 275
    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputerPartition,
                        None, 'PARTITION_01')
276 277 278 279 280 281 282 283

  def test_registerComputerPartition_empty_computer_partition_id(self):
    """
    Asserts that calling registerComputerPartition with empty
    computer_partition_id raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

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

288 289 290 291
    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputerPartition,
                        self._getTestComputerId(), None)
292 293 294 295 296 297 298 299

  def test_registerComputerPartition_empty_computer_guid_empty_computer_partition_id(self):
    """
    Asserts that calling registerComputerPartition with empty
    computer_partition_id raises early, before calling master.
    """
    self.slap.initializeConnection(self.server_url)

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

304 305 306 307
    with httmock.HTTMock(handler):
      self.assertRaises(slapos.slap.NotFoundError,
                        self.slap.registerComputerPartition,
                        None, None)
308

309

310 311 312 313 314 315 316 317 318 319
  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']

320 321 322 323 324 325 326 327 328 329 330 331
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/getSoftwareReleaseListFromSoftwareProduct'
            and qs == {'software_product_reference': [software_product_reference]}):
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(software_release_url_list)
                }

    with httmock.HTTMock(handler):
      self.assertEqual(
        self.slap.getSoftwareReleaseListFromSoftwareProduct(
332
          software_product_reference=software_product_reference),
333 334
        software_release_url_list
      )
335 336 337 338 339 340 341 342 343 344 345

  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']

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/getSoftwareReleaseListFromSoftwareProduct'
         and qs == {'software_release_url': [software_release_url]}):
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(software_release_url_list)
                }

    with httmock.HTTMock(handler):
      self.assertEqual(
        self.slap.getSoftwareReleaseListFromSoftwareProduct(
            software_release_url=software_release_url),
        software_release_url_list
      )
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

  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
    )

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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
  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 = urlparse.parse_qs(url.query)
      if (url.path == '/getHateoasUrl'):
        return {
                'status_code': 200,
                'content': hateoas_url
                }

    with httmock.HTTMock(handler):
      self.slap.initializeConnection('http://bar')
    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 = urlparse.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://bar', 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 = urlparse.parse_qs(url.query)
      if (url.path == '/getHateoasUrl'):
        return {
                'status_code': 404,
                }

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

437 438 439 440 441 442 443 444 445 446
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
    """
447 448 449
    computer_guid = self._getTestComputerId()
    slap = self.slap
    slap.initializeConnection(self.server_url)
450

451 452 453 454 455
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
              and 'computer_reference' in qs
              and 'computer_partition_reference' in qs):
456
        slap_partition = slapos.slap.ComputerPartition(
457 458 459 460 461 462 463 464 465
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getFullComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
466 467
        slap_computer._software_release_list = []
        slap_computer._computer_partition_list = []
468 469 470 471 472 473
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
        return {'status_code': 408}
474
      else:
475
        return {'status_code': 404}
476

477 478 479
    with httmock.HTTMock(handler):
      computer = self.slap.registerComputer(computer_guid)
      self.assertEqual(computer.getComputerPartitionList(), [])
480

481 482 483 484 485 486 487
  def _test_computer_empty_computer_guid(self, computer_method):
    """
    Helper method checking if calling Computer method with empty id raises
    early.
    """
    self.slap.initializeConnection(self.server_url)

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

492 493 494 495
    with httmock.HTTMock(handler):
      computer = self.slap.registerComputer(None)
      self.assertRaises(slapos.slap.NotFoundError,
                        getattr(computer, computer_method))
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

  def test_computer_getComputerPartitionList_empty_computer_guid(self):
    """
    Asserts that calling getComputerPartitionList with empty
    computer_guid raises early, before calling master.
    """
    self._test_computer_empty_computer_guid('getComputerPartitionList')

  def test_computer_getSoftwareReleaseList_empty_computer_guid(self):
    """
    Asserts that calling getSoftwareReleaseList with empty
    computer_guid raises early, before calling master.
    """
    self._test_computer_empty_computer_guid('getSoftwareReleaseList')

511 512
  def test_computer_getComputerPartitionList_only_partition(self):
    """
513
    Asserts that calling Computer.getComputerPartitionList with only
514 515 516 517 518 519
    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)
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
            and qs == {
                'computer_reference': [self.computer_guid],
                'computer_partition_reference': [partition_id]
                }):
        partition = slapos.slap.ComputerPartition(self.computer_guid, partition_id)
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(partition)
                }
      elif (url.path == '/getFullComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
        slap_computer._computer_partition_list = []
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      else:
        return {'status_code': 400}

    with httmock.HTTMock(handler):
      self.computer = self.slap.registerComputer(self.computer_guid)
      self.partition = self.slap.registerComputerPartition(self.computer_guid,
                                                           partition_id)
      self.assertEqual(self.computer.getComputerPartitionList(), [])
549

550
  @unittest.skip("Not implemented")
551 552
  def test_computer_reportUsage_non_valid_xml_raises(self):
    """
553
    Asserts that calling Computer.reportUsage with non DTD
554 555
    (not defined yet) XML raises (not defined yet) exception
    """
556

557 558 559 560 561 562 563
    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>"""
564 565 566
    self.assertRaises(UndefinedYetException,
                      self.computer.reportUsage,
                      non_dtd_xml)
567

568
  @unittest.skip("Not implemented")
569 570
  def test_computer_reportUsage_valid_xml_invalid_partition_raises(self):
    """
571
    Asserts that calling Computer.reportUsage with DTD (not defined
572 573 574 575 576 577 578 579 580
    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,
581
                                                         partition_id)
582 583 584 585
    # 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>"""
586 587 588 589
    self.assertRaises(UndefinedYetException,
                      self.computer.reportUsage,
                      bad_partition_dtd_xml)

590 591 592 593

class RequestWasCalled(Exception):
  pass

594

595 596 597 598 599 600 601
class TestComputerPartition(SlapMixin):
  """
  Tests slapos.slap.slap.ComputerPartition class functionality
  """

  def test_request_sends_request(self):
    partition_id = 'PARTITION_01'
602

603 604 605 606 607
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
              and 'computer_reference' in qs
              and 'computer_partition_reference' in qs):
608
        slap_partition = slapos.slap.ComputerPartition(
609 610 611 612 613 614 615 616 617
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
618 619
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
620
            qs['computer_id'][0],
621
            partition_id)
622
        slap_computer._computer_partition_list = [slap_partition]
623 624 625 626 627
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
628 629
        raise RequestWasCalled
      else:
630 631 632 633 634 635 636 637 638 639 640 641 642 643
        return {
                'status_code': 404
                }

    with httmock.HTTMock(handler):
      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')
644 645 646

  def test_request_not_raises(self):
    partition_id = 'PARTITION_01'
647

648 649 650 651 652
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
              and 'computer_reference' in qs
              and 'computer_partition_reference' in qs):
653
        slap_partition = slapos.slap.ComputerPartition(
654 655 656 657 658 659 660 661 662
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
663 664
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
665
            qs['computer_id'][0],
666
            partition_id)
667
        slap_computer._computer_partition_list = [slap_partition]
668 669 670 671 672 673
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
        return {'status_code': 408}
674
      else:
675
        return {'status_code': 404}
676

677 678 679
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
680 681 682 683 684 685 686 687
    with httmock.HTTMock(handler):
      computer_partition = self.slap.registerComputerPartition(
          self.computer_guid, partition_id)
      requested_partition = computer_partition.request(
          'http://server/new/' + self._getTestComputerId(),
          'software_type',
          'myref')
      self.assertIsInstance(requested_partition, slapos.slap.ComputerPartition)
688 689 690

  def test_request_raises_later(self):
    partition_id = 'PARTITION_01'
691

692 693 694 695 696
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition' and
              'computer_reference' in qs and
              'computer_partition_reference' in qs):
697
        slap_partition = slapos.slap.ComputerPartition(
698 699 700 701 702 703 704 705 706
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
707 708
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
709
            qs['computer_id'][0],
710
            partition_id)
711
        slap_computer._computer_partition_list = [slap_partition]
712 713 714 715 716 717
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
        return {'status_code': 408}
718
      else:
719
        return {'status_code': 404}
720

721 722 723
    self.computer_guid = self._getTestComputerId()
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
724 725 726 727 728 729 730 731 732 733 734
    with httmock.HTTMock(handler):
      computer_partition = self.slap.registerComputerPartition(
          self.computer_guid, partition_id)
      requested_partition = computer_partition.request(
          'http://server/new/' + self._getTestComputerId(),
          'software_type',
          'myref')
      self.assertIsInstance(requested_partition, slapos.slap.ComputerPartition)
      # as request method does not raise, accessing data raises
      self.assertRaises(slapos.slap.ResourceNotReady,
                        requested_partition.getId)
735 736 737 738 739

  def test_request_fullfilled_work(self):
    partition_id = 'PARTITION_01'
    requested_partition_id = 'PARTITION_02'
    computer_guid = self._getTestComputerId()
740

741 742 743 744 745
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition' and
              'computer_reference' in qs and
              'computer_partition_reference' in qs):
746
        slap_partition = slapos.slap.ComputerPartition(
747 748 749 750 751 752 753 754
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getComputerInformation' and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
755 756
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
757
            qs['computer_id'][0],
758
            partition_id)
759
        slap_computer._computer_partition_list = [slap_partition]
760 761 762 763 764
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
765 766
        from slapos.slap.slap import SoftwareInstance
        slap_partition = SoftwareInstance(
767 768
            slap_computer_id=computer_guid,
            slap_computer_partition_id=requested_partition_id)
769 770 771 772
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
773
      else:
774 775
        return {'status_code': 404}

776

777 778
    self.slap = slapos.slap.slap()
    self.slap.initializeConnection(self.server_url)
779 780 781 782 783 784 785 786 787 788 789 790

    with httmock.HTTMock(handler):
      computer_partition = self.slap.registerComputerPartition(
          computer_guid, partition_id)
      requested_partition = computer_partition.request(
          'http://server/new/' + self._getTestComputerId(),
          'software_type',
          'myref')
      self.assertIsInstance(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())
791

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  def test_request_with_slapgrid_request_transaction(self):
    from slapos.slap.slap import COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
    partition_id = 'PARTITION_01'
    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 = COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME % partition_id
    transaction_file_path = os.path.join(partition_root, transaction_file_name)

    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition'
              and 'computer_reference' in qs
              and 'computer_partition_reference' in qs):
        slap_partition = slapos.slap.ComputerPartition(
            qs['computer_reference'][0],
            qs['computer_partition_reference'][0])
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
                }
      elif (url.path == '/getComputerInformation'
              and 'computer_id' in qs):
        slap_computer = slapos.slap.Computer(qs['computer_id'][0])
        slap_computer._software_release_list = []
        slap_partition = slapos.slap.ComputerPartition(
            qs['computer_id'][0],
            partition_id)
        slap_computer._computer_partition_list = [slap_partition]
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
                }
      elif url.path == '/requestComputerPartition':
        raise RequestWasCalled
      else:
        return {
                'status_code': 404
                }

    with httmock.HTTMock(handler):
      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.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content = f.read()
        self.assertEqual(content, '')
      self.assertRaises(RequestWasCalled,
                        computer_partition.request,
                        'http://server/new/' + self._getTestComputerId(),
                        'software_type', 'myref')
      self.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().strip().split('\n')
        self.assertEqual(content_list, ['myref'])

      # Not override
      computer_partition = self.slap.registerComputerPartition(
          self.computer_guid, partition_id)
      self.assertTrue(os.path.exists(transaction_file_path))
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().strip().split('\n')
        self.assertEqual(content_list, ['myref'])

      # Request a second instance
      self.assertRaises(RequestWasCalled,
                        computer_partition.request,
                        'http://server/new/' + self._getTestComputerId(),
                        'software_type', 'mysecondref')
      with open(transaction_file_path, 'r') as f:
        content_list = f.read().strip().split('\n')
        self.assertEquals(list(set(content_list)), ['myref', 'mysecondref'])

870 871
  def _test_new_computer_partition_state(self, state):
    """
872
    Helper method to automate assertions of failing states on new Computer
873 874
    Partition
    """
875
    computer_guid = self._getTestComputerId()
876
    partition_id = 'PARTITION_01'
877 878 879
    slap = self.slap
    slap.initializeConnection(self.server_url)

880 881 882 883 884
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition' and
              qs['computer_reference'][0] == computer_guid and
              qs['computer_partition_reference'][0] == partition_id):
885 886
        partition = slapos.slap.ComputerPartition(
            computer_guid, partition_id)
887 888 889 890
        return {
                'status_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(partition)
                }
891
      else:
892 893
        return {'status_code': 404}

894

895 896 897 898 899
    with httmock.HTTMock(handler):
      computer_partition = self.slap.registerComputerPartition(
          computer_guid, partition_id)
      self.assertRaises(slapos.slap.NotFoundError,
                        getattr(computer_partition, state))
900 901 902

  def test_available_new_ComputerPartition_raises(self):
    """
903
    Asserts that calling ComputerPartition.available on new partition
904 905 906 907 908 909
    raises (not defined yet) exception
    """
    self._test_new_computer_partition_state('available')

  def test_building_new_ComputerPartition_raises(self):
    """
910
    Asserts that calling ComputerPartition.building on new partition raises
911 912 913 914 915 916
    (not defined yet) exception
    """
    self._test_new_computer_partition_state('building')

  def test_started_new_ComputerPartition_raises(self):
    """
917
    Asserts that calling ComputerPartition.started on new partition raises
918 919 920 921 922 923
    (not defined yet) exception
    """
    self._test_new_computer_partition_state('started')

  def test_stopped_new_ComputerPartition_raises(self):
    """
924
    Asserts that calling ComputerPartition.stopped on new partition raises
925 926 927 928 929 930 931 932
    (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
    """
933
    computer_guid = self._getTestComputerId()
934
    partition_id = 'PARTITION_01'
935 936 937
    slap = self.slap
    slap.initializeConnection(self.server_url)

938 939 940 941 942
    def handler(url, req):
      qs = urlparse.parse_qs(url.query)
      if (url.path == '/registerComputerPartition' and
              qs['computer_reference'][0] == computer_guid and
              qs['computer_partition_reference'][0] == partition_id):
943 944
        partition = slapos.slap.ComputerPartition(
            computer_guid, partition_id)
945 946 947 948 949 950
        return {
                'statu_code': 200,
                'content': xml_marshaller.xml_marshaller.dumps(partition)
                }
      elif url.path == '/softwareInstanceError':
        parsed_qs_body = urlparse.parse_qs(req.body)
951 952 953
        # XXX: why do we have computer_id and not computer_reference?
        # XXX: why do we have computer_partition_id and not
        # computer_partition_reference?
954 955 956
        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'):
957
          return {'status_code': 200}
958

959
      return {'status_code': 404}
960

961 962 963 964 965 966

    with httmock.HTTMock(handler):
      computer_partition = slap.registerComputerPartition(
          computer_guid, partition_id)
      # XXX: Interface does not define return value
      computer_partition.error('some error')
967

968

969 970 971 972 973 974 975
class TestSoftwareRelease(SlapMixin):
  """
  Tests slap.SoftwareRelease class functionality
  """

  def _test_new_software_release_state(self, state):
    """
976
    Helper method to automate assertions of failing states on new Software
977 978 979 980 981 982 983 984
    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)
985
    self.assertRaises(NameError, method)
986 987 988

  def test_available_new_SoftwareRelease_raises(self):
    """
989
    Asserts that calling SoftwareRelease.available on new software release
990
    raises NameError exception
991 992 993 994 995
    """
    self._test_new_software_release_state('available')

  def test_building_new_SoftwareRelease_raises(self):
    """
996
    Asserts that calling SoftwareRelease.building on new software release
997
    raises NameError exception
998 999 1000 1001 1002
    """
    self._test_new_software_release_state('building')

  def test_error_new_SoftwareRelease_works(self):
    """
1003
    Asserts that calling SoftwareRelease.error on software release works
1004
    """
1005 1006 1007 1008 1009
    computer_guid = self._getTestComputerId()
    software_release_uri = 'http://server/' + self._getTestComputerId()
    slap = self.slap
    slap.initializeConnection(self.server_url)

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    def handler(url, req):
      qs = urlparse.parse_qs(req.body)
      if (url.path == '/softwareReleaseError' and
              qs['computer_id'][0] == computer_guid and
              qs['url'][0] == software_release_uri and
              qs['error_log'][0] == 'some error'):
        return {
                'status_code': 200
                }
      return {'status_code': 404}
1020

1021

1022 1023 1024 1025
    with httmock.HTTMock(handler):
      software_release = self.slap.registerSoftwareRelease(software_release_uri)
      software_release._computer_guid = computer_guid
      software_release.error('some error')
1026

1027

1028 1029 1030 1031 1032 1033 1034
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()
1035

1036 1037
    def handler(url, req):
      if url.path == '/requestComputerPartition':
1038
        raise RequestWasCalled
1039

1040 1041 1042 1043
    with httmock.HTTMock(handler):
      self.assertRaises(RequestWasCalled,
                        open_order.request,
                        software_release_uri, 'myrefe')
1044

1045
  @unittest.skip('unclear what should be returned')
1046 1047 1048 1049 1050
  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
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

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

    with httmock.HTTMock(handler):
      open_order = self.slap.registerOpenOrder()
      computer_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
1061 1062 1063 1064 1065 1066 1067

  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()
1068

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

1072 1073 1074
    with httmock.HTTMock(handler):
      computer_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
1075

1076 1077
      self.assertRaises(slapos.slap.ResourceNotReady,
                        computer_partition.getId)
1078 1079 1080 1081 1082 1083 1084 1085 1086

  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'
1087

1088
    def handler(url, req):
1089 1090
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
1091 1092
          slap_computer_id=computer_guid,
          slap_computer_partition_id=requested_partition_id)
1093 1094 1095 1096
      return {
              'status_code': 200,
              'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
              }
1097

1098 1099 1100 1101
    with httmock.HTTMock(handler):
      computer_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
      self.assertEqual(requested_partition_id, computer_partition.getId())
1102

1103

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
  def test_request_getConnectionParameter(self):
    """ Backward compatibility API for slapproxy older them 1.0.1 """
    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'

1114
    def handler(url, req):
1115 1116 1117 1118 1119
      from slapos.slap.slap import SoftwareInstance
      slap_partition = SoftwareInstance(
          _connection_dict = {"url": 'URL_CONNECTION_PARAMETER'},
          slap_computer_id=computer_guid,
          slap_computer_partition_id=requested_partition_id)
1120 1121 1122 1123
      return {
              'status_code': 200,
              'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
              }
1124 1125


1126 1127 1128 1129
    with httmock.HTTMock(handler):
      computer_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
      self.assertEqual(requested_partition_id, computer_partition.getId())
1130
      self.assertEqual("URL_CONNECTION_PARAMETER",
1131
                       computer_partition.getConnectionParameter('url'))
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143


  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._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'

1144
    def handler(url, req):
1145 1146 1147 1148 1149 1150 1151 1152
      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_computer_id=computer_guid,
          slap_computer_partition_id=requested_partition_id)
1153 1154 1155 1156 1157 1158 1159 1160 1161
      return {
              'status_code': 200,
              'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
              }

    with httmock.HTTMock(handler):
      computer_partition = open_order.request(software_release_uri, 'myrefe')
      self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
      self.assertEqual(requested_partition_id, computer_partition.getId())
1162
      self.assertEqual("URL_CONNECTION_PARAMETER",
1163
                       computer_partition.getConnectionParameter('url'))
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207


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',
    )

1208
  def test_get_product_getattr(self):
1209 1210 1211
    """
    Test that __getattr__ method is bound to get() method.
    """
1212 1213
    self.getSoftwareReleaseListFromSoftwareProduct_response = ['0']
    self.product_collection.foo
1214 1215 1216 1217
    self.assertEqual(
      self.product_collection.__getattr__,
      self.product_collection.get
    )
1218
    self.assertEqual(self.product_collection.foo, '0')
1219

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