test_slapproxy.py 74.4 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
# vim: set et sts=2:
3 4
##############################################################################
#
5
# Copyright (c) 2012, 2013, 2014 Vifib SARL and Contributors.
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
# 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 advised 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.
#
##############################################################################

Bryton Lacquement's avatar
Bryton Lacquement committed
31 32
import six
from six.moves import configparser
33 34 35
import os
import logging
import shutil
36
import socket
37 38 39 40
try:
  import subprocess32 as subprocess
except ImportError:
  import subprocess
41
import sys
42
import tempfile
43
import time
44
import unittest
45
import mock
46

47 48
import slapos.proxy
import slapos.proxy.views as views
49
import slapos.slap
50
import slapos.slap.slap
Bryton Lacquement's avatar
Bryton Lacquement committed
51
from slapos.util import loads, dumps, sqlite_connect, bytes2str
52

53 54 55
import sqlite3
import pkg_resources

56

57 58 59
class WrongFormat(Exception):
  pass

60 61

class ProxyOption(object):
62 63 64 65
  """
  Will simulate options given to slapproxy
  """
  def __init__(self, proxy_db):
66 67
    self.verbose = True
    self.database_uri = proxy_db
68 69 70
    self.console = False
    self.log_file = None

71 72

class BasicMixin(object):
73
  def setUp(self):
74 75 76
    """
    Will set files and start slapproxy
    """
77 78 79 80 81
    self._tempdir = tempfile.mkdtemp()
    logging.basicConfig(level=logging.DEBUG)
    self.setFiles()
    self.startProxy()

82
  def createSlapOSConfigurationFile(self):
Bryton Lacquement's avatar
Bryton Lacquement committed
83 84
    with open(self.slapos_cfg, 'w') as f:
      f.write("""[slapos]
85 86 87 88 89 90 91 92
software_root = %(tempdir)s/opt/slapgrid
instance_root = %(tempdir)s/srv/slapgrid
master_url = %(proxyaddr)s
computer_id = computer
[slapproxy]
host = 127.0.0.1
port = 8080
database_uri = %(tempdir)s/lib/proxy.db
93
""" % {'tempdir': self._tempdir, 'proxyaddr': self.proxyaddr})
94 95 96 97 98 99 100 101 102 103

  def setFiles(self):
    """
    Set environment to run slapproxy
    """
    self.slapos_cfg = os.path.join(self._tempdir, 'slapos.cfg')
    self.proxy_db = os.path.join(self._tempdir, 'lib', 'proxy.db')
    self.proxyaddr = 'http://localhost:80/'
    self.computer_id = 'computer'
    self.createSlapOSConfigurationFile()
104
    for directory in ['opt', 'srv', 'lib']:
105
      path = os.path.join(self._tempdir, directory)
106 107 108
      os.mkdir(path)

  def startProxy(self):
109 110 111
    """
    Set config for slapproxy and start it
    """
112
    conf = slapos.proxy.ProxyConfig(logger=logging.getLogger())
Bryton Lacquement's avatar
Bryton Lacquement committed
113
    configp = configparser.SafeConfigParser()
114 115 116
    configp.read(self.slapos_cfg)
    conf.mergeConfig(ProxyOption(self.proxy_db), configp)
    conf.setConfig()
117
    views.app.config['TESTING'] = True
118
    slapos.proxy.setupFlaskConfiguration(conf)
119

120
    self.app_config = views.app.config
121 122
    self.app = views.app.test_client()

123
  def format_for_number_of_partitions(self, partition_amount, computer_id=None):
124 125 126 127
    """
    Will simulate a slapformat first run
    and create "partition_amount" partitions
    """
128 129
    if not computer_id:
      computer_id = self.computer_id
130
    computer_dict = {
131
        'reference': computer_id,
132 133
        'address': '12.34.56.78',
        'netmask': '255.255.255.255',
134 135
        'partition_list': [],
    }
136 137 138 139 140 141 142 143 144 145

    # Remove the existing partitions from the db
    request_dict = {
        'computer_id': self.computer_id,
        'xml': dumps(computer_dict),
    }
    rv = self.app.post('/loadComputerConfigurationFromXML',
                  data=request_dict)

    # Register the partitions
146 147 148
    for i in range(partition_amount):
      partition_example = {
          'reference': 'slappart%s' % i,
149 150 151 152 153
          'address_list': [
              {'addr': '1.2.3.4', 'netmask': '255.255.255.255'},
              {'addr': '4.3.2.1', 'netmask': '255.255.255.255'}
           ],
           'tap': {'name': 'tap0'},
154
      }
155 156
      computer_dict['partition_list'].append(partition_example)

157 158
    request_dict = {
        'computer_id': self.computer_id,
Bryton Lacquement's avatar
Bryton Lacquement committed
159
        'xml': dumps(computer_dict),
160
    }
161
    rv = self.app.post('/loadComputerConfigurationFromXML',
162
                  data=request_dict)
163
    self.assertEqual(rv._status_code, 200)
164

165
  def tearDown(self):
166 167 168
    """
    Remove files generated for test
    """
169
    shutil.rmtree(self._tempdir, True)
170
    views.is_schema_already_executed = False
171 172


173 174 175
class TestLoadComputerConfiguration(BasicMixin, unittest.TestCase):
  """tests /loadComputerConfigurationFromXML the endpoint for format
  """
176 177 178
  def setUp(self):
    super(TestLoadComputerConfiguration, self).setUp()
    self.computer_dict = {
179 180 181 182 183 184 185 186 187 188 189 190 191
        'reference': self.computer_id,
        'address': '12.34.56.78',
        'netmask': '255.255.255.255',
        'partition_list': [
            {
                'reference': 'slappart1',
                'address_list': [
                    {
                        'addr': '1.2.3.4',
                        'netmask': '255.255.255.255'
                    },
                ],
                'tap': {'name': 'tap0'},
192 193 194 195 196 197 198 199 200 201
            },
            {
                'reference': 'slappart2',
                'address_list': [
                    {
                        'addr': '5.6.7.8',
                        'netmask': '255.255.255.255'
                    },
                ],
                'tap': {'name': 'tap1'},
202 203 204
            }
        ],
    }
205 206

  def test_loadComputerConfigurationFromXML_keep_partitions(self):
207 208
    rv = self.app.post('/loadComputerConfigurationFromXML', data={
        'computer_id': self.computer_id,
209
        'xml': dumps(self.computer_dict),
210 211
    })
    self.assertEqual(rv._status_code, 200)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    self.app.post('/requestComputerPartition', data={
      'computer_id': self.computer_id,
      'software_release': 'https://example.com/software.cfg',
      'software_type': 'default',
      'computer_partition_id': 'slappart1',
      'state': dumps('started'),
      'partition_parameter_xml': dumps({'foo': 'bar'}),
      'filter_xml': dumps({}),
      'shared_xml': dumps({})
    })

    computer = loads(
        self.app.get('/getFullComputerInformation', query_string={'computer_id': self.computer_id}).data)
    self.assertEqual(
        ['https://example.com/software.cfg'],
        [p.getSoftwareRelease().getURI() for p in computer._computer_partition_list if p.getId() == 'slappart1'])

    # load configuration from XML again
    rv = self.app.post('/loadComputerConfigurationFromXML', data={
        'computer_id': self.computer_id,
        'xml': dumps(self.computer_dict),
    })
    self.assertEqual(rv._status_code, 200)

    # partition is kept
    computer = loads(
        self.app.get('/getFullComputerInformation', query_string={'computer_id': self.computer_id}).data)
    self.assertEqual(
        ['https://example.com/software.cfg'],
        [p.getSoftwareRelease().getURI() for p in computer._computer_partition_list if p.getId() == 'slappart1'])

  def test_loadComputerConfigurationFromXML_remove_partitions(self):
    rv = self.app.post('/loadComputerConfigurationFromXML', data={
        'computer_id': self.computer_id,
        'xml': dumps(self.computer_dict),
    })
    self.assertEqual(rv._status_code, 200)
    # call again with one less partition reference, old partition will be removed
251
    # and a new partition will be used.
252 253
    self.computer_dict['partition_list'] = [self.computer_dict['partition_list'][0]]
    self.computer_dict['partition_list'][0]['reference'] = 'something else'
254 255
    rv = self.app.post('/loadComputerConfigurationFromXML', data={
        'computer_id': self.computer_id,
256
        'xml': dumps(self.computer_dict),
257 258 259 260 261 262 263 264 265
    })
    self.assertEqual(rv._status_code, 200)
    computer = loads(
        self.app.get('/getFullComputerInformation', query_string={'computer_id': self.computer_id}).data)
    self.assertEqual(
        ['something else'],
        [p.getId() for p in computer._computer_partition_list])


266
class TestInformation(BasicMixin, unittest.TestCase):
267 268 269
  """
  Test Basic response of slapproxy
  """
270

271
  def test_getComputerInformation(self):
272 273 274 275
    """
    Check that getComputerInformation return a Computer
    and database is generated
    """
276
    rv = self.app.get('/getComputerInformation?computer_id=%s' % self.computer_id)
277
    self.assertIsInstance(
Bryton Lacquement's avatar
Bryton Lacquement committed
278
        loads(rv.data),
279
        slapos.slap.Computer)
280 281
    self.assertTrue(os.path.exists(self.proxy_db))

282
  def test_getFullComputerInformation(self):
283 284 285 286
    """
    Check that getFullComputerInformation return a Computer
    and database is generated
    """
287
    rv = self.app.get('/getFullComputerInformation?computer_id=%s' % self.computer_id)
288
    self.assertIsInstance(
Bryton Lacquement's avatar
Bryton Lacquement committed
289
        loads(rv.data),
290
        slapos.slap.Computer)
291 292
    self.assertTrue(os.path.exists(self.proxy_db))

293
  def test_getComputerInformation_wrong_computer(self):
294 295 296 297
    """
    Test that computer information won't be given to a requester different
    from the one specified
    """
298
    with self.assertRaises(slapos.slap.NotFoundError):
299
      self.app.get('/getComputerInformation?computer_id=%s42' % self.computer_id)
300

301
  def test_partition_are_empty(self):
302 303 304
    """
    Test that empty partition are empty :)
    """
305
    self.format_for_number_of_partitions(10)
306
    rv = self.app.get('/getFullComputerInformation?computer_id=%s' % self.computer_id)
Bryton Lacquement's avatar
Bryton Lacquement committed
307
    computer = loads(rv.data)
308 309 310 311 312
    for slap_partition in computer._computer_partition_list:
        self.assertIsNone(slap_partition._software_release_document)
        self.assertEqual(slap_partition._requested_state, 'destroyed')
        self.assertEqual(slap_partition._need_modification, 0)

313 314 315 316 317 318 319 320 321 322 323 324 325
  def test_getSoftwareReleaseListFromSoftwareProduct_software_product_reference(self):
    """
    Check that calling getSoftwareReleaseListFromSoftwareProduct() in slapproxy
    using a software_product_reference as parameter behaves correctly.
    """
    software_product_reference = 'my_product'
    software_release_url = 'my_url'
    self.app_config['software_product_list'] = {
        software_product_reference: software_release_url
    }
    response = self.app.get('/getSoftwareReleaseListFromSoftwareProduct'
                            '?software_product_reference=%s' %\
                            software_product_reference)
Bryton Lacquement's avatar
Bryton Lacquement committed
326
    software_release_url_list = loads(response.data)
327 328 329 330 331 332 333 334 335 336 337 338 339 340
    self.assertEqual(
        software_release_url_list,
        [software_release_url]
    )

  def test_getSoftwareReleaseListFromSoftwareProduct_noSoftwareProduct(self):
    """
    Check that calling getSoftwareReleaseListFromSoftwareProduct() in slapproxy
    using a software_product_reference that doesn't exist as parameter
    returns empty list.
    """
    self.app_config['software_product_list'] = {'random': 'random'}
    response = self.app.get('/getSoftwareReleaseListFromSoftwareProduct'
                            '?software_product_reference=idonotexist')
Bryton Lacquement's avatar
Bryton Lacquement committed
341
    software_release_url_list = loads(response.data)
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
    self.assertEqual(
        software_release_url_list,
        []
    )

  def test_getSoftwareReleaseListFromSoftwareProduct_bothParameter(self):
    """
    Test that a call to getSoftwareReleaseListFromSoftwareProduct with no
    parameter raises
    """
    self.assertRaises(
        AssertionError,
       self.app.get,
       '/getSoftwareReleaseListFromSoftwareProduct'
       '?software_product_reference=foo'
       '&software_release_url=bar'
    )

  def test_getSoftwareReleaseListFromSoftwareProduct_noParameter(self):
    """
    Test that a call to getSoftwareReleaseListFromSoftwareProduct with both
    software_product_reference and software_release_url parameters raises
    """
    self.assertRaises(
        AssertionError,
        self.app.get, '/getSoftwareReleaseListFromSoftwareProduct'
    )

370 371 372 373 374 375 376
  def test_getComputerPartitionCertificate(self):
    """
    Tests that getComputerPartitionCertificate method is implemented in slapproxy.
    """
    rv = self.app.get(
      '/getComputerPartitionCertificate?computer_id=%s&computer_partition_id=%s' % (
      self.computer_id, 'slappart0'))
Bryton Lacquement's avatar
Bryton Lacquement committed
377
    response = loads(rv.data)
378
    self.assertEqual({'certificate': '', 'key': ''}, response)
379

380 381 382 383 384
  def test_computerBang(self):
    """
    Tests that computerBang method is implemented in slapproxy.
    """
    rv = self.app.post( '/computerBang?computer_id=%s' % ( self.computer_id))
Bryton Lacquement's avatar
Bryton Lacquement committed
385
    response = loads(rv.data)
386
    self.assertEqual('', response)
387

388
class MasterMixin(BasicMixin, unittest.TestCase):
389 390 391
  """
  Define advanced tool for test proxy simulating behavior slap library tools
  """
392
  def _requestComputerPartition(self, software_release, software_type, partition_reference,
393
              partition_id=None,
394 395
              shared=False, partition_parameter_kw=None, filter_kw=None,
              state=None):
396
    """
397
    Check parameters, call requestComputerPartition server method and return result
398
    """
399 400 401 402
    if partition_parameter_kw is None:
      partition_parameter_kw = {}
    if filter_kw is None:
      filter_kw = {}
403
   # Let's enforce a default software type
404 405 406
    if software_type is None:
      software_type = 'default'

407 408
    request_dict = {
        'computer_id': self.computer_id,
409 410 411 412
        'computer_partition_id': partition_id,
        'software_release': software_release,
        'software_type': software_type,
        'partition_reference': partition_reference,
Bryton Lacquement's avatar
Bryton Lacquement committed
413 414 415 416
        'shared_xml': dumps(shared),
        'partition_parameter_xml': dumps(partition_parameter_kw),
        'filter_xml': dumps(filter_kw),
        'state': dumps(state),
417
    }
418
    return self.app.post('/requestComputerPartition', data=request_dict)
419

420
  def request(self, *args, **kw):
421 422 423 424
    """
    Simulate a request with above parameters
    Return response by server (a computer partition or an error)
    """
425
    rv = self._requestComputerPartition(*args, **kw)
426
    time.sleep(0.01)
427
    requested_at = time.time()
428
    self.assertEqual(rv._status_code, 200)
429
    xml = rv.data
Bryton Lacquement's avatar
Bryton Lacquement committed
430
    software_instance = loads(xml)
431 432

    computer_partition = slapos.slap.ComputerPartition(
433 434
        software_instance.slap_computer_id,
        software_instance.slap_computer_partition_id)
435

436
    computer_partition.__dict__.update(software_instance.__dict__)
437 438 439 440 441

    if not kw.get('shared'):
      self.assertLessEqual(
        float(computer_partition._parameter_dict['timestamp']), requested_at)

442 443
    return computer_partition

444
  def supply(self, url, computer_id=None, state='available'):
445 446 447 448 449 450 451
    if not computer_id:
      computer_id = self.computer_id
    request_dict = {'url':url, 'computer_id': computer_id, 'state':state}
    rv = self.app.post('/supplySupply',
                       data=request_dict)
    # XXX return a Software Release

452 453
  def setConnectionDict(self, partition_id,
                        connection_dict, slave_reference=None):
454
    self.app.post('/setComputerPartitionConnectionXml', data={
455 456
        'computer_id': self.computer_id,
        'computer_partition_id': partition_id,
Bryton Lacquement's avatar
Bryton Lacquement committed
457
        'connection_xml': dumps(connection_dict),
458
        'slave_reference': slave_reference})
459

460
  def getPartitionInformation(self, computer_partition_id):
461 462 463
    """
    Return computer information as stored in proxy for corresponding id
    """
464
    computer = self.getFullComputerInformation()
465 466 467 468
    for instance in computer._computer_partition_list:
      if instance._partition_id == computer_partition_id:
        return instance

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  def getFullComputerInformation(self):
    return loads(self.app.get('/getFullComputerInformation?computer_id=%s' % self.computer_id).data)


class TestSoftwareInstallation(MasterMixin, unittest.TestCase):
  def setUp(self):
    super(TestSoftwareInstallation, self).setUp()
    self.software_release_url = self.id()

  def assertSoftwareState(self, state):
    # Check that the software was requested in `state`
    sr, = self.getFullComputerInformation()._software_release_list
    self.assertEqual(sr.getState(), state)

  def test_installation_success(self):
    self.supply(self.software_release_url)
    self.assertSoftwareState('available')

    self.app.post('/buildingSoftwareRelease', data={
      'url': self.software_release_url,
      'computer_id': self.computer_id
    })
    # there's no intermediate "building" state, because state is
    # the "requested state".
    self.assertSoftwareState('available')

    self.app.post('/availableSoftwareRelease', data={
      'url': self.software_release_url,
      'computer_id': self.computer_id
    })
    self.assertSoftwareState('available')

  def test_installation_failed(self):
    self.supply(self.software_release_url)
    self.assertSoftwareState('available')

    self.app.post('/buildingSoftwareRelease', data={
      'url': self.software_release_url,
      'computer_id': self.computer_id
    })
    self.assertSoftwareState('available')

    self.app.post('/softwareReleaseError', data={
      'url': self.software_release_url,
      'computer_id': self.computer_id
    })
    self.assertSoftwareState('available')

  def test_destroyed(self):
    self.supply(self.software_release_url)
    self.assertSoftwareState('available')

    self.supply(self.software_release_url, state="destroyed")
    self.assertSoftwareState('destroyed')

    self.app.post('/destroyedSoftwareRelease', data={
      'url': self.software_release_url,
      'computer_id': self.computer_id
    })
    # this really remove the software from DB
    self.assertEqual([], self.getFullComputerInformation()._software_release_list)

531

532
class TestRequest(MasterMixin):
533 534 535
  """
  Set of tests for requests
  """
536 537 538 539 540

  def test_request_consistent_parameters(self):
    """
    Check that all different parameters related to requests (like instance_guid, state) are set and consistent
    """
541
    self.format_for_number_of_partitions(1)
542
    partition = self.request('http://sr//', None, 'MyFirstInstance', 'slappart0')
543
    self.assertEqual(partition.getState(), 'started')
544
    self.assertEqual(partition.getInstanceGuid(), 'computer-slappart0')
545

546
  def test_two_request_one_partition_free(self):
547
    """
Marco Mariani's avatar
Marco Mariani committed
548 549
    Since slapproxy does not implement scope, providing two partition_id
    values will still succeed, even if only one partition is available.
550
    """
551
    self.format_for_number_of_partitions(1)
552
    self.assertIsInstance(self.request('http://sr//', None,
553
                                       'MyFirstInstance', 'slappart2'),
554
                          slapos.slap.ComputerPartition)
Marco Mariani's avatar
Marco Mariani committed
555
    self.assertIsInstance(self.request('http://sr//', None,
556
                                       'MyFirstInstance', 'slappart3'),
Marco Mariani's avatar
Marco Mariani committed
557
                          slapos.slap.ComputerPartition)
558

559
  def test_two_request_two_partition_free(self):
560 561 562 563
    """
    If two requests are made with two available partition
    both will succeed
    """
564
    self.format_for_number_of_partitions(2)
565
    self.assertIsInstance(self.request('http://sr//', None,
566
                                       'MyFirstInstance', 'slappart2'),
567
                          slapos.slap.ComputerPartition)
568
    self.assertIsInstance(self.request('http://sr//', None,
569
                                       'MyFirstInstance', 'slappart3'),
570 571
                          slapos.slap.ComputerPartition)

572
  def test_two_same_request_from_one_partition(self):
573 574 575
    """
    Request will return same partition for two equal requests
    """
576
    self.format_for_number_of_partitions(2)
577 578 579 580 581 582 583 584
    def do_request():
      return self.request('http://sr//', None, 'MyFirstInstance', 'slappart2')
    requested_at = time.time()
    partition = do_request()
    self.assertLessEqual(float(str(requested_at)),
      float(partition._parameter_dict['timestamp']))
    time.sleep(.1) # check timestamp does not change for an identical request
    self.assertEqual(partition.__dict__, do_request().__dict__)
585

586 587 588 589 590
  def test_instance_bang(self):
    """
    Check that instance bang update the timestamps of all partitions in the
    instance.
    """
591
    self.format_for_number_of_partitions(3)
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    requested_at = time.time()
    parent = self.request('http://sr//', None, 'MyFirstInstance')
    child = self.request('http://sr//', None, 'MySubInstance',
                         parent._partition_id)
    other = self.request('http://sr//', None, 'AnotherInstance')
    for partition in parent, child, other:
      self.assertLessEqual(float(str(requested_at)),
        float(partition._parameter_dict['timestamp']))
    other_timestamp = other._parameter_dict['timestamp']
    def getTimestamp(partition):
      return self.getPartitionInformation(
        partition._partition_id)._parameter_dict['timestamp']
    for partition in parent, child:
      time.sleep(.1)
      bang_at = time.time()
      self.app.post('/softwareInstanceBang', data={
        'computer_id': self.computer_id,
        'computer_partition_id': partition._partition_id,
        'message': self.id()})
      timestamp = getTimestamp(parent)
      self.assertLessEqual(float(str(requested_at)), float(timestamp))
      self.assertEqual(getTimestamp(child), timestamp)
      self.assertEqual(getTimestamp(other), other_timestamp)

616 617 618 619
  def test_request_propagate_partition_state(self):
    """
    Request will return same partition for two equal requests
    """
620
    self.format_for_number_of_partitions(2)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    def request_parent(**kw):
      return self.request('http://sr//', None, 'MyFirstInstance', **kw)
    def request_child():
      return self.request('http://sr//', None, 'MySubInstance', parent_id)

    parent = request_parent()
    parent_id = parent._partition_id
    child = request_child()
    self.assertEqual('started', parent.getState())
    self.assertEqual('started', child.getState())

    for state in 'stopped', 'started':
      parent = request_parent(state=state)
      child = request_child()
      self.assertEqual(state, parent.getState())
      self.assertEqual(state, child.getState())
637 638 639 640 641

  def test_request_parent_started_children_stopped(self):
    """
    Request will return same partition for two equal requests
    """
642
    self.format_for_number_of_partitions(2)
643 644 645
    partition_parent = self.request('http://sr//', None, 'MyFirstInstance')
    parent_dict = partition_parent.__dict__
    partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'])
646

647 648 649 650 651
    self.assertEqual(partition_parent.getState(), 'started')
    self.assertEqual(partition_child.getState(), 'started')

    partition_parent = self.request('http://sr//', None, 'MyFirstInstance')
    partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'], state='stopped')
652

653 654 655
    self.assertEqual(partition_parent.getState(), 'started')
    self.assertEqual(partition_child.getState(), 'stopped')

656
  def test_two_requests_with_different_parameters_but_same_reference(self):
657
    """
658 659
    Request will return same partition for two different requests but will
    only update parameters
660
    """
661
    self.format_for_number_of_partitions(2)
662 663
    wanted_domain1 = 'fou.org'
    wanted_domain2 = 'carzy.org'
664

665
    request1 = self.request('http://sr//', None, 'MyFirstInstance', 'slappart2',
666
                            partition_parameter_kw={'domain': wanted_domain1})
667 668
    requested_result1 = self.getPartitionInformation(request1._partition_id)
    time.sleep(.1) # so that timestamp changes
669
    request2 = self.request('http://sr//', 'Papa', 'MyFirstInstance', 'slappart2',
670
                            partition_parameter_kw={'domain': wanted_domain2})
671
    requested_result2 = self.getPartitionInformation(request2._partition_id)
672
    # Test we received same partition
673 674
    for key in '_partition_id', '_computer_id':
      self.assertEqual(getattr(request1, key), getattr(request2, key))
675
    # Test that only parameters changed
676
    for key in requested_result2.__dict__:
677 678 679 680 681 682
      if key == '_software_release_document':
        self.assertEqual(getattr(requested_result2, key).__dict__,
                         getattr(requested_result1, key).__dict__)
      elif key != '_parameter_dict':
        self.assertEqual(getattr(requested_result2, key),
                         getattr(requested_result1, key))
683 684 685 686 687
    #Test parameters where set correctly
    self.assertEqual(wanted_domain1,
                     requested_result1._parameter_dict['domain'])
    self.assertEqual(wanted_domain2,
                     requested_result2._parameter_dict['domain'])
688

689 690 691 692 693 694
    t1 = request1._parameter_dict['timestamp']
    t2 = request2._parameter_dict['timestamp']
    self.assertLess(float(t1), float(t2))
    self.assertEqual(t1, requested_result1._parameter_dict['timestamp'])
    self.assertEqual(t2, requested_result2._parameter_dict['timestamp'])

695 696 697 698 699
  def test_two_requests_with_different_parameters_and_sr_url_but_same_reference(self):
    """
    Request will return same partition for two different requests but will
    only update parameters
    """
700
    self.format_for_number_of_partitions(2)
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    wanted_domain1 = 'fou.org'
    wanted_domain2 = 'carzy.org'

    request1 = self.request('http://sr//', None, 'MyFirstInstance', 'slappart2',
                            partition_parameter_kw={'domain': wanted_domain1})
    request1_dict = request1.__dict__
    requested_result1 = self.getPartitionInformation(
        request1_dict['_partition_id'])
    request2 = self.request('http://sr1//', 'Papa', 'MyFirstInstance', 'slappart2',
                            partition_parameter_kw={'domain': wanted_domain2})
    request2_dict = request2.__dict__
    requested_result2 = self.getPartitionInformation(
        request2_dict['_partition_id'])
    # Test we received same partition
    for key in ['_partition_id', '_computer_id']:
      self.assertEqual(request1_dict[key], request2_dict[key])
    # Test that parameters and software_release url changed
    for key in requested_result2.__dict__:
      if key not in ['_parameter_dict',
                     '_software_release_document']:
        self.assertEqual(requested_result2.__dict__[key],
                         requested_result1.__dict__[key])
      elif key in ['_software_release_document']:
        # software_release will be updated
        self.assertEqual(requested_result2.__dict__[key].__dict__['_software_release'],
                         'http://sr1//')
        self.assertEqual(requested_result1.__dict__[key].__dict__['_software_release'],
                         'http://sr//')
    #Test parameters where set correctly
    self.assertEqual(wanted_domain1,
                     requested_result1._parameter_dict['domain'])
    self.assertEqual(wanted_domain2,
                     requested_result2._parameter_dict['domain'])
734

735
  def test_two_different_request_from_two_partition(self):
736
    """
Marco Mariani's avatar
Marco Mariani committed
737 738
    Since slapproxy does not implement scope, two request with
    different partition_id will still return the same partition.
739
    """
740
    self.format_for_number_of_partitions(2)
Marco Mariani's avatar
Marco Mariani committed
741
    self.assertEqual(
742 743
        self.request('http://sr//', None, 'MyFirstInstance', 'slappart2').__dict__,
        self.request('http://sr//', None, 'MyFirstInstance', 'slappart3').__dict__)
744

745
  def test_two_different_request_from_one_partition(self):
746 747 748 749
    """
    Two different request from same partition
    will return two different partitions
    """
750
    self.format_for_number_of_partitions(2)
751
    self.assertNotEqual(
752
        self.request('http://sr//', None, 'MyFirstInstance', 'slappart2').__dict__,
753
        self.request('http://sr//', None, 'frontend', 'slappart2').__dict__)
754

755 756 757 758
  def test_request_with_nonascii_parameters(self):
    """
    Verify that request with non-ascii parameters is correctly accepted
    """
759
    self.format_for_number_of_partitions(1)
760 761 762 763
    request = self.request('http://sr//', None, 'myinstance', 'slappart0',
                           partition_parameter_kw={'text': u'Привет Мир!'})
    self.assertIsInstance(request, slapos.slap.ComputerPartition)

764 765 766 767
  def test_request_with_int(self):
    """
    Verify that request with int parameters is correctly accepted
    """
768
    self.format_for_number_of_partitions(1)
769 770 771 772 773 774 775 776
    request = self.request('http://sr//', None, 'myinstance', 'slappart0',
                           partition_parameter_kw={'int': 1})
    self.assertIsInstance(request, slapos.slap.ComputerPartition)

  def test_request_set_connection_parameters_with_int(self):
    """
    Verify that request int works in connection parameters
    """
777
    self.format_for_number_of_partitions(1)
778 779 780 781 782 783 784 785 786 787 788
    partition_id = self.request('http://sr//', None, 'myinstance', 'slappart0')._partition_id
    # Set connection parameter
    partition = self.getPartitionInformation(partition_id)

    self.setConnectionDict(partition_id=partition_id,
                           connection_dict={'foo': 1})

    # Get updated information for the partition
    partition_new = self.request('http://sr//', None, 'myinstance', 'slappart0')
    self.assertEqual(partition_new.getConnectionParameter('foo'), '1')

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  def test_request_frontend(self):
    # slapproxy tells client to bypass "simple" frontends by just using the URL.
    request = self.request(
        'http://git.erp5.org/gitweb/slapos.git/blob_plain/HEAD:/software/apache-frontend/software.cfg',
        None,
        self.id(),
        'slappart0',
        shared=True,
        partition_parameter_kw={'url': 'https://[::1]:123/', })
    self.assertEqual(
        'https://[::1]:123/',
        request.getConnectionParameterDict()['secure_access'])
    self.assertEqual(
        '[::1]:123',
        request.getConnectionParameterDict()['domain'])

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
  def test_request_kvm_frontend(self):
    # slapproxy tells client to bypass kvm vnc frontends by building an URL using the backend.
    request = self.request(
        'http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.92:/software/kvm/software.cfg',
        'frontend',
        self.id(),
        'slappart0',
        shared=True,
        partition_parameter_kw={'host': '::1', 'port': '123'})
    self.assertEqual(
        'https://[::1]:123/',
        request.getConnectionParameterDict()['url'])
    self.assertEqual(
        '[::1]',
        request.getConnectionParameterDict()['domainname'])
    self.assertEqual(
        '123',
        request.getConnectionParameterDict()['port'])
    self.assertEqual(
        '/',
        request.getConnectionParameterDict()['path'])

827

828
class TestSlaveRequest(MasterMixin):
829 830 831
  """
  Test requests related to slave instances.
  """
832
  def test_slave_request_no_corresponding_partition(self):
833 834 835
    """
    Slave instance request will fail if no corresponding are found
    """
836
    self.format_for_number_of_partitions(2)
837 838
    rv = self._requestComputerPartition('http://sr//', None, 'MyFirstInstance', 'slappart2', shared=True)
    self.assertEqual(rv._status_code, 404)
839

840
  def test_slave_request_set_parameters(self):
841 842 843 844 845 846 847
    """
    Parameters sent in slave request must be put in slave master
    slave instance list.
    1. We request a slave instance we defined parameters
    2. We check parameters are in the dictionnary defining slave in
        slave master slave_instance_list
    """
848
    self.format_for_number_of_partitions(6)
849 850
    # Provide partition
    master_partition_id = self.request('http://sr//', None,
851
                                       'MyFirstInstance', 'slappart4')._partition_id
852 853
    # First request of slave instance
    wanted_domain = 'fou.org'
854
    self.request('http://sr//', None, 'MyFirstInstance', 'slappart2', shared=True,
855
                 partition_parameter_kw={'domain': wanted_domain})
856 857 858 859
    # Get updated information for master partition
    master_partition = self.getPartitionInformation(master_partition_id)

    our_slave = master_partition._parameter_dict['slave_instance_list'][0]
860
    self.assertEqual(our_slave.get('domain'), wanted_domain)
861

862 863 864 865 866
  def test_master_instance_with_no_slave(self):
    """
    Test that a master instance with no requested slave
    has an empty slave_instance_list parameter.
    """
867
    self.format_for_number_of_partitions(6)
868 869 870 871 872
    # Provide partition
    master_partition_id = self.request('http://sr//', None, 'MyMasterInstance', 'slappart4')._partition_id
    master_partition = self.getPartitionInformation(master_partition_id)
    self.assertEqual(len(master_partition._parameter_dict['slave_instance_list']), 0)

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
  def test_slave_request_updates_timestamp(self):
    self.format_for_number_of_partitions(1)
    # Provide partition
    master_partition = self.request('http://sr//', None, 'MyMasterInstance', 'slappart4')

    def getPartition():
      return self.getFullComputerInformation()._computer_partition_list[0]

    def getTimestamp(partition):
      return float(partition._parameter_dict['timestamp'])

    before = getPartition()
    before_timestamp = getTimestamp(before)
    self.assertEqual(len(before._parameter_dict['slave_instance_list']), 0)
    time.sleep(0.1)

    self.request('http://sr//', None, 'MyFirstSlave', shared=True)
    after = getPartition()
    after_timestamp = getTimestamp(after)
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 1)
    self.assertLess(before_timestamp, after_timestamp, 'Adding first slave shall change the timestamp')

    before_timestamp = after_timestamp
    time.sleep(0.1)
    self.request('http://sr//', None, 'MySecondSlave', shared=True)
    after = getPartition()
    after_timestamp = getTimestamp(after)
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
    self.assertLess(before_timestamp, after_timestamp, 'Adding second slave shall change the timestamp')

    before_timestamp = after_timestamp
    time.sleep(0.1)
    self.request('http://sr//', None, 'MySecondSlave', shared=True, partition_parameter_kw={'a':'b'})
    after = getPartition()
    after_timestamp = getTimestamp(after)
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
    self.assertLess(before_timestamp, after_timestamp, 'Updating any slave shall change the timestamp')

    before_timestamp = after_timestamp
    time.sleep(0.1)
    self.request('http://sr//', None, 'MySecondSlave', shared=True, partition_parameter_kw={'a':'b'})
    after = getPartition()
    after_timestamp = getTimestamp(after)
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
    self.assertEqual(before_timestamp, after_timestamp, 'No-op change of the slave shall not change the timestamp')

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
  def test_slave_request_destroyed(self):
    self.format_for_number_of_partitions(1)
    # Provide partition
    master_partition = self.request('http://sr//', None, 'MyMasterInstance', 'slappart4')
    def getPartition():
      return self.getFullComputerInformation()._computer_partition_list[0]
    def getTimestamp(partition):
      return float(partition._parameter_dict['timestamp'])

    self.request('http://sr//', None, 'MyFirstSlave', shared=True)
    self.request('http://sr//', None, 'MySecondSlave', shared=True)
    after = getPartition()
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 2)
    before_timestamp = getTimestamp(after)

    time.sleep(.1)
    self.request('http://sr//', None, 'MyFirstSlave', shared=True, state='destroyed')
    after = getPartition()
    after_timestamp = getTimestamp(after)
    self.assertEqual(len(after._parameter_dict['slave_instance_list']), 1)
    self.assertLess(before_timestamp, after_timestamp, 'Slave destroy shall result with timestamp update')
    self.assertEqual(after._parameter_dict['slave_instance_list'][0]['slave_reference'], '_MySecondSlave')

942

943
  def test_slave_request_set_parameters_are_updated(self):
944 945 946 947 948 949 950 951 952 953
    """
    Parameters sent in slave request must be put in slave master
    slave instance list and updated when they change.
    1. We request a slave instance we defined parameters
    2. We check parameters are in the dictionnary defining slave in
        slave master slave_instance_list
    3. We request same slave instance with changed parameters
    4. We check parameters are in the dictionnary defining slave in
        slave master slave_instance_list have changed
    """
954
    self.format_for_number_of_partitions(6)
955 956
    # Provide partition
    master_partition_id = self.request('http://sr//', None,
957
                                       'MyFirstInstance', 'slappart4')._partition_id
958 959
    # First request of slave instance
    wanted_domain_1 = 'crazy.org'
960
    self.request('http://sr//', None, 'MyFirstInstance', 'slappart2', shared=True,
961
                 partition_parameter_kw={'domain': wanted_domain_1})
962 963 964
    # Get updated information for master partition
    master_partition = self.getPartitionInformation(master_partition_id)
    our_slave = master_partition._parameter_dict['slave_instance_list'][0]
965
    self.assertEqual(our_slave.get('domain'), wanted_domain_1)
966 967 968

    # Second request of slave instance
    wanted_domain_2 = 'maluco.org'
969
    self.request('http://sr//', None, 'MyFirstInstance', 'slappart2', shared=True,
970
                 partition_parameter_kw={'domain': wanted_domain_2})
971 972 973 974
    # Get updated information for master partition
    master_partition = self.getPartitionInformation(master_partition_id)

    our_slave = master_partition._parameter_dict['slave_instance_list'][0]
975 976
    self.assertNotEqual(our_slave.get('domain'), wanted_domain_1)
    self.assertEqual(our_slave.get('domain'), wanted_domain_2)
977

978 979 980 981 982 983 984
  def test_slave_request_set_connection_parameters(self):
    """
    Parameters set in slave instance by master instance must be put in slave instance connection parameters.
    1. We request a slave instance
    2. We set connection parameters for this slave instance
    2. We check parameter is present when we do request() for the slave.
    """
985
    self.format_for_number_of_partitions(6)
986 987 988 989 990 991
    # Provide partition
    master_partition_id = self.request('http://sr//', None, 'MyMasterInstance', 'slappart4')._partition_id
    # First request of slave instance
    self.request('http://sr//', None, 'MySlaveInstance', 'slappart2', shared=True)
    # Set connection parameter
    master_partition = self.getPartitionInformation(master_partition_id)
992
    # XXX change slave reference to be compatible with multiple nodes
993 994 995
    self.setConnectionDict(partition_id=master_partition._partition_id,
                           connection_dict={'foo': 'bar'},
                           slave_reference=master_partition._parameter_dict['slave_instance_list'][0]['slave_reference'])
996

997 998 999 1000
    # Get updated information for slave partition
    slave_partition = self.request('http://sr//', None, 'MySlaveInstance', 'slappart2', shared=True)
    self.assertEqual(slave_partition.getConnectionParameter('foo'), 'bar')

1001
  def test_slave_request_one_corresponding_partition(self):
1002 1003 1004
    """
    Successfull request slave instance follow these steps:
    1. Provide one corresponding partition
1005
    2. Ask for Slave instance. But no connection parameters
1006 1007
       But slave is added to Master Instance slave list
    3. Master Instance get updated information (including slave list)
1008
    4. Master instance post information about slave connection parameters
1009
    5. Ask for slave instance is successfull and return a computer instance
1010
        with connection information
1011
    """
1012
    self.format_for_number_of_partitions(6)
1013 1014
    # Provide partition
    master_partition_id = self.request('http://sr//', None,
1015
                                       'MyFirstInstance', 'slappart4')._partition_id
1016
    # First request of slave instance
1017
    name = 'MyFirstInstance'
1018
    requester = 'slappart2'
1019
    our_slave = self.request('http://sr//', None, name, requester, shared=True)
1020
    self.assertIsInstance(our_slave, slapos.slap.ComputerPartition)
1021
    self.assertEqual(our_slave._connection_dict, {})
1022
    # Get updated information for master partition
1023 1024
    master_partition = self.getPartitionInformation(master_partition_id)
    slave_for_master = master_partition._parameter_dict['slave_instance_list'][0]
1025
    # Send information about slave
1026
    slave_address = {'url': '%s.master.com'}
1027 1028 1029
    self.setConnectionDict(partition_id=master_partition._partition_id,
                           connection_dict=slave_address,
                           slave_reference=slave_for_master['slave_reference'])
1030
    # Successfull slave request with connection parameters
1031 1032
    our_slave = self.request('http://sr//', None,
                             name, requester, shared=True)
1033 1034
    self.assertIsInstance(our_slave, slapos.slap.ComputerPartition)
    self.assertEqual(slave_address, our_slave._connection_dict)
1035

1036 1037 1038 1039 1040 1041
  def test_slave_request_instance_guid(self):
    """
    Test that instance_guid support behaves correctly.
    Warning: proxy doesn't gives unique id of instance, but gives instead unique id
    of partition.
    """
1042
    self.format_for_number_of_partitions(1)
1043 1044 1045 1046 1047
    partition = self.request('http://sr//', None, 'MyInstance', 'slappart1')
    slave = self.request('http://sr//', None, 'MySlaveInstance', 'slappart1',
         shared=True, filter_kw=dict(instance_guid=partition._instance_guid))
    self.assertEqual(slave._partition_id, partition._partition_id)

1048

1049 1050 1051 1052 1053 1054
class TestMultiNodeSupport(MasterMixin):
  def test_multi_node_support_different_software_release_list(self):
    """
    Test that two different registered computers have their own
    Software Release list.
    """
1055 1056
    self.format_for_number_of_partitions(6, computer_id='COMP-0')
    self.format_for_number_of_partitions(6, computer_id='COMP-1')
1057 1058 1059 1060 1061 1062 1063
    software_release_1_url = 'http://sr1'
    software_release_2_url = 'http://sr2'
    software_release_3_url = 'http://sr3'
    self.supply(software_release_1_url, 'COMP-0')
    self.supply(software_release_2_url, 'COMP-1')
    self.supply(software_release_3_url, 'COMP-0')
    self.supply(software_release_3_url, 'COMP-1')
1064

1065 1066
    computer_default = loads(self.app.get('/getFullComputerInformation?computer_id=%s' % self.computer_id).data)
    computer_0 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-0').data)
1067
    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    self.assertEqual(len(computer_default._software_release_list), 0)
    self.assertEqual(len(computer_0._software_release_list), 2)
    self.assertEqual(len(computer_1._software_release_list), 2)

    self.assertEqual(
        computer_0._software_release_list[0]._software_release,
        software_release_1_url
    )
    self.assertEqual(
        computer_0._software_release_list[0]._computer_guid,
        'COMP-0'
    )

    self.assertEqual(
        computer_0._software_release_list[1]._software_release,
        software_release_3_url
    )
    self.assertEqual(
        computer_0._software_release_list[1]._computer_guid,
        'COMP-0'
    )

    self.assertEqual(
        computer_1._software_release_list[0]._software_release,
        software_release_2_url
    )
    self.assertEqual(
        computer_1._software_release_list[0]._computer_guid,
        'COMP-1'
    )

    self.assertEqual(
        computer_1._software_release_list[1]._software_release,
        software_release_3_url
    )
    self.assertEqual(
        computer_1._software_release_list[1]._computer_guid,
        'COMP-1'
    )

  def test_multi_node_support_remove_software_release(self):
    """
    Test that removing a software from a Computer doesn't
    affect other computer
    """
    software_release_url = 'http://sr'
1114 1115
    self.format_for_number_of_partitions(6, computer_id='COMP-0')
    self.format_for_number_of_partitions(6, computer_id='COMP-1')
1116 1117 1118 1119 1120 1121
    self.supply(software_release_url, 'COMP-0')
    self.supply(software_release_url, 'COMP-1')
    self.supply(software_release_url, 'COMP-0', state='destroyed')
    computer_0 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-0').data)
    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    # at this point, software is requested to be destroyed on COMP-0
    self.assertEqual(
        computer_0._software_release_list[0].getURI(),
        software_release_url
    )
    self.assertEqual(
        computer_0._software_release_list[0].getComputerId(),
        'COMP-0'
    )
    self.assertEqual(
        computer_0._software_release_list[0].getState(),
        'destroyed'
    )
1135

1136
    # but is still requested for installation on COMP-1
1137
    self.assertEqual(
1138
        computer_1._software_release_list[0].getURI(),
1139 1140 1141
        software_release_url
    )
    self.assertEqual(
1142
        computer_1._software_release_list[0].getComputerId(),
1143 1144
        'COMP-1'
    )
1145 1146 1147 1148
    self.assertEqual(
        computer_1._software_release_list[0].getState(),
        'available'
    )
1149 1150 1151 1152 1153 1154 1155 1156

  def test_multi_node_support_instance_default_computer(self):
    """
    Test that instance request behaves correctly with default computer
    """
    software_release_url = 'http://sr'
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
1157 1158
    self.format_for_number_of_partitions(6, computer_id=computer_0_id)
    self.format_for_number_of_partitions(6, computer_id=computer_1_id)
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

    # Request without SLA -> goes to default computer only.
    # It should fail if we didn't registered partitions for default computer
    # (default computer is always registered)
    rv = self._requestComputerPartition('http://sr//', None, 'MyFirstInstance', 'slappart2')
    self.assertEqual(rv._status_code, 404)

    rv = self._requestComputerPartition('http://sr//', None, 'MyFirstInstance', 'slappart2',
                                        filter_kw={'computer_guid':self.computer_id})
    self.assertEqual(rv._status_code, 404)

    # Register default computer: deployment works
1171
    self.format_for_number_of_partitions(1)
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    self.request('http://sr//', None, 'MyFirstInstance', 'slappart0')
    computer_default = loads(self.app.get(
        '/getFullComputerInformation?computer_id=%s' % self.computer_id).data)
    self.assertEqual(len(computer_default._software_release_list), 0)

    # No free space on default computer: request without SLA fails
    rv = self._requestComputerPartition('http://sr//', None, 'CanIHasPartition', 'slappart2',
                                        filter_kw={'computer_guid':self.computer_id})
    self.assertEqual(rv._status_code, 404)

  def test_multi_node_support_instance(self):
    """
    Test that instance request behaves correctly with several
    registered computers
    """
    software_release_url = 'http://sr'
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
    software_release_1 = 'http://sr//'
    software_release_2 = 'http://othersr//'

1193
    self.format_for_number_of_partitions(2, computer_id=computer_1_id)
1194 1195 1196 1197 1198 1199

    # Deploy to first non-default computer using SLA
    # It should fail since computer is not registered
    rv = self._requestComputerPartition(software_release_1, None, 'MyFirstInstance', 'slappart2', filter_kw={'computer_guid':computer_0_id})
    self.assertEqual(rv._status_code, 404)

1200
    self.format_for_number_of_partitions(2, computer_id=computer_0_id)
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

    # Deploy to first non-default computer using SLA
    partition = self.request(software_release_1, None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id})
    self.assertEqual(partition.getState(), 'started')
    self.assertEqual(partition._partition_id, 'slappart0')
    self.assertEqual(partition._computer_id, computer_0_id)
    # All other instances should be empty
    computer_0 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-0').data)
    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)
    self.assertEqual(computer_0._computer_partition_list[0]._software_release_document._software_release, software_release_1)
    self.assertTrue(computer_0._computer_partition_list[1]._software_release_document == None)
    self.assertTrue(computer_1._computer_partition_list[0]._software_release_document == None)
    self.assertTrue(computer_1._computer_partition_list[1]._software_release_document == None)

    # Deploy to second non-default computer using SLA
    partition = self.request(software_release_2, None, 'MySecondInstance', 'slappart0', filter_kw={'computer_guid':computer_1_id})
    self.assertEqual(partition.getState(), 'started')
    self.assertEqual(partition._partition_id, 'slappart0')
    self.assertEqual(partition._computer_id, computer_1_id)
    # The two remaining instances should be free, and MyfirstInstance should still be there
    computer_0 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-0').data)
    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)
    self.assertEqual(computer_0._computer_partition_list[0]._software_release_document._software_release, software_release_1)
    self.assertTrue(computer_0._computer_partition_list[1]._software_release_document == None)
    self.assertEqual(computer_1._computer_partition_list[0]._software_release_document._software_release, software_release_2)
    self.assertTrue(computer_1._computer_partition_list[1]._software_release_document == None)

  def test_multi_node_support_change_instance_state(self):
    """
    Test that destroying an instance (i.e change state) from a Computer doesn't
    affect other computer
    """
    software_release_url = 'http://sr'
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
1236 1237
    self.format_for_number_of_partitions(6, computer_id=computer_0_id)
    self.format_for_number_of_partitions(6, computer_id=computer_1_id)
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    partition_first = self.request('http://sr//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id})
    partition_second = self.request('http://sr//', None, 'MySecondInstance', 'slappart0', filter_kw={'computer_guid':computer_1_id})

    partition_first = self.request('http://sr//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id}, state='stopped')

    computer_0 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-0').data)
    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)
    self.assertEqual(computer_0._computer_partition_list[0].getState(), 'stopped')
    self.assertEqual(computer_0._computer_partition_list[1].getState(), 'destroyed')
    self.assertEqual(computer_1._computer_partition_list[0].getState(), 'started')
    self.assertEqual(computer_1._computer_partition_list[1].getState(), 'destroyed')

  def test_multi_node_support_same_reference(self):
    """
    Test that requesting an instance with same reference to two
    different nodes behaves like master: once an instance is assigned to a node,
    changing SLA will not change node.
    """
    software_release_url = 'http://sr'
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
1259 1260
    self.format_for_number_of_partitions(2, computer_id=computer_0_id)
    self.format_for_number_of_partitions(2, computer_id=computer_1_id)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    partition = self.request('http://sr//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id})
    partition = self.request('http://sr//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_1_id})

    self.assertEqual(partition._computer_id, computer_0_id)

    computer_1 = loads(self.app.get('/getFullComputerInformation?computer_id=COMP-1').data)
    self.assertTrue(computer_1._computer_partition_list[0]._software_release_document == None)
    self.assertTrue(computer_1._computer_partition_list[1]._software_release_document == None)

  def test_multi_node_support_slave_instance(self):
    """
    Test that slave instances are correctly deployed if SLA is specified
    but deployed only on default computer if not specified (i.e not deployed
    if default computer doesn't have corresponding master instance).
    """
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
1278 1279 1280
    self.format_for_number_of_partitions(2, computer_id=computer_0_id)
    self.format_for_number_of_partitions(2, computer_id=computer_1_id)
    self.format_for_number_of_partitions(2)
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    self.request('http://sr2//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id})
    self.request('http://sr//', None, 'MyOtherInstance', 'slappart0', filter_kw={'computer_guid':computer_1_id})

    # Request slave without SLA: will fail
    rv = self._requestComputerPartition('http://sr//', None, 'MySlaveInstance', 'slappart2', shared=True)
    self.assertEqual(rv._status_code, 404)

    # Request slave with SLA on incorrect computer: will fail
    rv = self._requestComputerPartition('http://sr//', None, 'MySlaveInstance', 'slappart2', shared=True, filter_kw={'computer_guid':computer_0_id})
    self.assertEqual(rv._status_code, 404)

    # Request computer on correct computer: will succeed
    partition = self.request('http://sr//', None, 'MySlaveInstance', 'slappart2', shared=True, filter_kw={'computer_guid':computer_1_id})
    self.assertEqual(partition._computer_id, computer_1_id)

  def test_multi_node_support_instance_guid(self):
    """
    Test that instance_guid support behaves correctly with multiple nodes.
    Warning: proxy doesn't gives unique id of instance, but gives instead unique id
    of partition.
    """
    computer_0_id = 'COMP-0'
    computer_1_id = 'COMP-1'
1304 1305 1306
    self.format_for_number_of_partitions(2, computer_id=computer_0_id)
    self.format_for_number_of_partitions(2, computer_id=computer_1_id)
    self.format_for_number_of_partitions(2)
1307 1308
    partition_computer_0 = self.request('http://sr2//', None, 'MyFirstInstance', 'slappart0', filter_kw={'computer_guid':computer_0_id})
    partition_computer_1 = self.request('http://sr//', None, 'MyOtherInstance', 'slappart0', filter_kw={'computer_guid':computer_1_id})
1309
    partition_computer_default = self.request('http://sr//', None, 'MyThirdInstance', 'slappart0')
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

    self.assertEqual(partition_computer_0.getInstanceGuid(), 'COMP-0-slappart0')
    self.assertEqual(partition_computer_1.getInstanceGuid(), 'COMP-1-slappart0')
    self.assertEqual(partition_computer_default.getInstanceGuid(), 'computer-slappart0')

  def test_multi_node_support_getComputerInformation(self):
    """
    Test that computer information will not be given if computer is not registered.
    Test that it still should work for the 'default' computer specified in slapos config
    even if not yet registered.
1320
    Test that computer information is given if computer is registered.
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    """
    new_computer_id = '%s42' % self.computer_id
    with self.assertRaises(slapos.slap.NotFoundError):
      self.app.get('/getComputerInformation?computer_id=%s42' % new_computer_id)

    try:
      self.app.get('/getComputerInformation?computer_id=%s' % self.computer_id)
    except slapos.slap.NotFoundError:
      self.fail('Could not fetch informations for default computer.')

1331
    self.format_for_number_of_partitions(1, computer_id=new_computer_id)
1332 1333 1334 1335 1336
    try:
      self.app.get('/getComputerInformation?computer_id=%s' % new_computer_id)
    except slapos.slap.NotFoundError:
      self.fail('Could not fetch informations for registered computer.')

1337

1338 1339 1340 1341 1342 1343 1344 1345
class TestMultiMasterSupport(MasterMixin):
  """
  Test multimaster support in slapproxy.
  """
  external_software_release = 'http://mywebsite.me/exteral_software_release.cfg'
  software_release_not_in_list = 'http://mywebsite.me/exteral_software_release_not_listed.cfg'

  def setUp(self):
1346
    self.addCleanup(self.stopExternalProxy)
1347
    self.external_proxy_host = os.environ.get('LOCAL_IPV4', '127.0.0.1')
1348 1349 1350
    self.external_proxy_port = 8281
    self.external_master_url = 'http://%s:%s' % (self.external_proxy_host, self.external_proxy_port)
    self.external_computer_id = 'external_computer'
1351 1352
    self.external_proxy_slap = slapos.slap.slap()
    self.external_proxy_slap.initializeConnection(self.external_master_url)
1353 1354 1355

    super(TestMultiMasterSupport, self).setUp()

1356
    self.db = sqlite_connect(self.proxy_db)
1357 1358 1359 1360 1361 1362 1363 1364 1365
    self.external_slapproxy_configuration_file_location = os.path.join(
        self._tempdir, 'external_slapos.cfg')
    self.createExternalProxyConfigurationFile()
    self.startExternalProxy()

  def tearDown(self):
    super(TestMultiMasterSupport, self).tearDown()

  def createExternalProxyConfigurationFile(self):
Bryton Lacquement's avatar
Bryton Lacquement committed
1366 1367
    with open(self.external_slapproxy_configuration_file_location, 'w') as f:
      f.write("""[slapos]
1368 1369
computer_id = %(external_computer_id)s
[slapproxy]
1370
host = %(host)s
1371 1372 1373 1374
port = %(port)s
database_uri = %(tempdir)s/lib/external_proxy.db
""" % {
    'tempdir': self._tempdir,
1375
    'host': self.external_proxy_host,
1376 1377 1378 1379 1380 1381 1382 1383
    'port': self.external_proxy_port,
    'external_computer_id': self.external_computer_id
    })

  def startExternalProxy(self):
    """
    Start external slapproxy
    """
1384
    logging.getLogger().info('Starting external proxy, listening to %s:%s' % (self.external_proxy_host, self.external_proxy_port))
1385 1386
    # XXX This uses a hack to run current code of slapos.core
    import slapos
1387 1388
    self.external_proxy_process = subprocess.Popen(
        [
1389
            sys.executable, '%s/../cli/entry.py' % os.path.dirname(slapos.tests.__file__),
1390 1391
            'proxy', 'start', '--cfg', self.external_slapproxy_configuration_file_location
        ],
1392 1393
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
1394 1395
        env={"PYTHONPATH": ':'.join(sys.path)}
    )
1396
    # Wait a bit for proxy to be started
1397 1398 1399 1400 1401 1402
    attempts = 0
    while (attempts < 20):
      try:
        self.external_proxy_slap._connection_helper.GET('/')
      except slapos.slap.NotFoundError:
        break
Bryton Lacquement's avatar
Bryton Lacquement committed
1403
      except (slapos.slap.ConnectionError, socket.error):
1404
        attempts = attempts + 1
1405
        time.sleep(0.1 * attempts)
1406 1407
    else:
      self.fail('Could not start external proxy.')
1408

1409 1410
  def stopExternalProxy(self):
    self.external_proxy_process.kill()
1411 1412 1413 1414 1415 1416

  def createSlapOSConfigurationFile(self):
    """
    Overwrite default slapos configuration file to enable specific multimaster
    behaviours.
    """
Bryton Lacquement's avatar
Bryton Lacquement committed
1417
    configuration = bytes2str(pkg_resources.resource_string(
1418
        'slapos.tests', os.path.join('test_slapproxy', 'slapos_multimaster.cfg.in')
Bryton Lacquement's avatar
Bryton Lacquement committed
1419
    )) % {
1420 1421 1422 1423
        'tempdir': self._tempdir, 'proxyaddr': self.proxyaddr,
        'external_proxy_host': self.external_proxy_host,
        'external_proxy_port': self.external_proxy_port
    }
1424 1425
    with open(self.slapos_cfg, 'w') as f:
      f.write(configuration)
1426

1427
  def external_proxy_format_for_number_of_partitions(self, partition_amount, computer_id=None):
1428 1429 1430 1431 1432 1433 1434 1435
    """
    Will simulate a slapformat first run
    and create "partition_amount" partitions
    """
    if not computer_id:
      computer_id = self.external_computer_id
    computer_dict = {
        'reference': computer_id,
1436 1437
        'address': '12.34.56.78',
        'netmask': '255.255.255.255',
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
        'partition_list': [],
    }
    for i in range(partition_amount):
      partition_example = {
          'reference': 'slappart%s' % i,
          'address_list': [
              {'addr': '1.2.3.4', 'netmask': '255.255.255.255'},
              {'addr': '4.3.2.1', 'netmask': '255.255.255.255'}
           ],
           'tap': {'name': 'tap0'},
      }
      computer_dict['partition_list'].append(partition_example)

    request_dict = {
        'computer_id':