test.erp5.testTaskDistribution.py 65.1 KB
Newer Older
1
from Products.DCWorkflow.DCWorkflow import ValidationFailed
2 3
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
import json 
4
from time import sleep
5
from DateTime import DateTime
6 7 8 9 10 11 12 13 14 15 16 17

class TestTaskDistribution(ERP5TypeTestCase):
  def afterSetUp(self):
    self.portal = portal = self.getPortalObject()
    self.test_node_module = self.portal.getDefaultModule(portal_type = 'Test Node Module')
    self.test_suite_module = self.portal.getDefaultModule(portal_type = 'Test Suite Module')
    self.test_result_module = self.portal.getDefaultModule(portal_type = 'Test Result Module')
    self.test_suite = self.portal.getDefaultModule(portal_type = 'Test Suite')
    self.tool = tool = self.portal.portal_task_distribution
    if getattr(tool, "TestTaskDistribution", None) is None:
      tool.newContent(id="TestTaskDistribution",
           portal_type="ERP5 Project Unit Test Distributor")
18
    tool.TestTaskDistribution.setMaxTestSuite(None)
19 20 21
    if getattr(tool, "TestPerformanceTaskDistribution", None) is None:
      tool.newContent(id="TestPerformanceTaskDistribution",
           portal_type="Cloud Performance Unit Test Distributor")
22 23 24
    if getattr(tool, "TestScalabilityTaskDistribution", None) is None:
      tool.newContent(id="TestScalabilityTaskDistribution",
           portal_type="ERP5 Scalability Distributor")
25 26
    self.distributor = tool.TestTaskDistribution
    self.performance_distributor = tool.TestPerformanceTaskDistribution
27
    self.scalability_distributor = tool.TestScalabilityTaskDistribution
28 29 30 31 32 33 34 35 36 37 38 39
    if getattr(portal, "test_test_node_module", None) is None:
      portal.newContent(portal_type="Test Node Module",
                        id="test_test_node_module")
    if getattr(portal, "test_test_suite_module", None) is None:
      portal.newContent(portal_type="Test Suite Module",
                        id="test_test_suite_module")
    self.test_suite_module = portal.test_test_suite_module
    self.test_node_module = portal.test_test_node_module
    self.test_suite_module.manage_delObjects(ids=[
      x for x in self.test_suite_module.objectIds()])
    self.test_node_module.manage_delObjects(ids=[
      x for x in self.test_node_module.objectIds()])
40 41 42 43 44

    original_class = self.distributor.__class__
    original_scalability_class = self.scalability_distributor.__class__
    original_performance_class = self.performance_distributor.__class__

45 46 47 48
    self._original_getTestNodeModule = original_class._getTestNodeModule
    def _getTestNodeModule(self):
      return self.getPortalObject().test_test_node_module
    original_class._getTestNodeModule = _getTestNodeModule
49 50 51
    original_scalability_class._getTestNodeModule = _getTestNodeModule
    original_performance_class._getTestNodeModule = _getTestNodeModule

52 53 54 55
    self._original_getTestSuiteModule = original_class._getTestSuiteModule
    def _getTestSuiteModule(self):
      return self.getPortalObject().test_test_suite_module
    original_class._getTestSuiteModule = _getTestSuiteModule
56 57 58
    original_scalability_class._getTestSuiteModule = _getTestSuiteModule
    original_performance_class._getTestSuiteModule = _getTestSuiteModule

59 60
    self._cleanupTestResult()

61 62 63 64 65 66 67 68 69 70
    self.test_node_module.newContent(
      portal_type='Test Node', title='Node0',
      test_suite_max=4).getTitle()
    default_test_suite = self.test_suite_module.newContent(
        portal_type='Test Suite', title='Default Test Suite',
        test_suite_title='Default Test Suite', int_index=1)
    default_test_suite.validate()
    self.default_test_title = default_test_suite.getTitle()
    self.tic()

71
  def beforeTearDown(self):
72 73 74 75
    original_class = self.distributor.__class__
    original_scalability_class = self.scalability_distributor.__class__
    original_performance_class = self.performance_distributor.__class__
    
76 77
    original_class._getTestNodeModule = self._original_getTestNodeModule
    original_class._getTestSuiteModule = self._original_getTestSuiteModule
78 79 80 81 82 83
    original_scalability_class._getTestNodeModule = self._original_getTestNodeModule
    original_scalability_class._getTestSuiteModule = self._original_getTestSuiteModule
    original_performance_class._getTestNodeModule = self._original_getTestNodeModule
    original_performance_class._getTestSuiteModule = self._original_getTestSuiteModule


84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  def _createTestNode(self, quantity=1, reference_correction=0,
                      specialise_value=None):
    if specialise_value is None:
      specialise_value = self.distributor
    test_node_list = []
    for i in range(quantity):
      test_node = self.test_node_module.newContent(
        title = 'UnitTestNode %i' % (i + 1 + reference_correction),
        test_suite_max = 4,
        specialise_value = specialise_value,
        )
      test_node_list.append(test_node)
    return test_node_list

  def _createTestSuite(self,quantity=1,priority=1, reference_correction=0,
100 101
                       specialise_value=None, title=None, portal_type="Test Suite",
                       graph_coordinate="1", cluster_configuration='{ "test": {{ count }} }'):
102 103
    if title is None:
      title = ""
104 105 106 107
    if specialise_value is None:
      specialise_value = self.distributor
    test_suite_list = []
    for i in range(quantity):
108 109 110
      test_suite_title = "test suite %i" % (i + 1 + reference_correction)
      if title:
        test_suite_title += " %s" % title
111

112
      test_suite =  self.test_suite_module.newContent(
113
                    portal_type = portal_type,
114 115 116 117 118
                    title = test_suite_title,
                    test_suite = 'B%i' % i,
                    int_index = priority,
                    specialise_value = specialise_value,
                   )
119 120

      test_suite.setClusterConfiguration(cluster_configuration)
121 122 123 124
      if portal_type == "Scalability Test Suite":
        test_suite.setGraphCoordinate(graph_coordinate)


125 126
      test_suite.newContent( portal_type= 'Test Suite Repository',
                        branch = 'master',
127
                        git_url = 'https://lab.nexedi.com/nexedi/erp5.git',
128 129 130 131 132 133 134 135 136
                        buildout_section_id  = 'erp5',
                        profile_path = 'software-release/software.cfg'
                        )
      test_suite.validate()
      test_suite_list.append(test_suite)
    return test_suite_list 

  def test_01_createTestNode(self):
    test_node = self._createTestNode()[0]
137
    self.assertEqual(test_node.getPortalType(), "Test Node")
138 139

  def test_02_createTestSuite(self):
140
    # Test Test Suite
141
    test_suite = self._createTestSuite()[0]
142 143 144
    self.assertEquals(test_suite.getPortalType(), "Test Suite")
    self.assertEquals(test_suite.getSpecialise(), self.distributor.getRelativeUrl())
    # Test Scalability Test Suite
145
    scalability_test_suite  = self._createTestSuite(
146 147
                                 portal_type="Scalability Test Suite",
                                 specialise_value = self.scalability_distributor
148
                               )[0]
149 150 151 152
    self.assertEquals(scalability_test_suite.getPortalType(),
                       "Scalability Test Suite")
    self.assertEquals(scalability_test_suite.getSpecialise(),
                       self.scalability_distributor.getRelativeUrl())
153

154 155 156 157 158 159 160 161 162 163 164 165 166
  def test_02b_checkConsistencyOnTestSuite(self):
    test_suite, = self._createTestSuite()
    self.tic()
    test_suite_repository, = test_suite.objectValues(portal_type="Test Suite Repository")
    self.checkPropertyConstraint(test_suite, 'title', 'ERP5-MASTER')
    self.checkPropertyConstraint(test_suite, 'test_suite', 'ERP5')
    self.checkPropertyConstraint(test_suite_repository, 'git_url', 'https://lab.nexedi.com/nexedi/erp5.git')
    self.checkPropertyConstraint(test_suite_repository, 'buildout_section_id', 'erp5')
    self.checkPropertyConstraint(test_suite_repository, 'branch', 'master')
    test_suite_repository.setGitUrl(None)
    test_suite.invalidate()
    self.assertRaises(ValidationFailed, self.portal.portal_workflow.doActionFor, test_suite, 'validate_action')

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  def test_02c_checkTestSuiteReference(self):
    """
    Make sure validation of test suite generate a reference, and revalidating
    a test suite should not change reference
    """
    test_suite, = self._createTestSuite()
    self.assertTrue(test_suite.getReference() != None)
    self.tic()
    test_suite.invalidate()
    self.tic()
    test_suite.setReference(None)
    test_suite.validate()
    reference = test_suite.getReference()
    self.assertTrue(reference != None)
    test_suite.invalidate()
    self.tic()
    test_suite.validate()
    self.assertEqual(reference, test_suite.getReference())

186 187 188 189
  def _callOptimizeAlarm(self):
    self.portal.portal_alarms.task_distributor_alarm_optimize.activeSense()
    self.tic()

190 191 192 193
  def _callRestartStuckTestResultAlarm(self):
    self.portal.portal_alarms.test_result_alarm_restarted_stuck_test_result.activeSense()
    self.tic()

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  def test_subscribeNode_ReturnValue(self):
    config = self.distributor.subscribeNode('COMP-X', 'QPK')
    config = json.loads(config)
    self.assertEqual(
      config,
      {'process_timeout': None}
    )

    self.distributor.edit(
      process_timeout=50
    )
    config = self.distributor.subscribeNode('COMP-X', 'QPK')
    config = json.loads(config)
    self.assertEqual(
      config,
      {'process_timeout': 50}
    )

212 213 214
  def test_03_startTestSuiteWithOneTestNode(self):
    config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP32-Node1"))
215
    self.assertEqual([], config_list)
216 217 218 219 220
    self._createTestSuite(quantity=3)
    self.tic()
    self._callOptimizeAlarm()
    config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP32-Node1"))
221 222
    self.assertEqual(3, len(config_list))
    self.assertEqual(set(['B0','B1','B2']),
223 224 225 226 227 228 229 230 231 232 233
                      set([x['test_suite'] for x in config_list]))

  def test_04_startTestSuiteWithTwoTestNode(self):
    """
    When we have two test suites and we have two test nodes, we should have
    one test suite distributed per test node
    """
    config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP32-Node1"))
    config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP32-Node2"))
234
    self.assertEqual([], config_list)
235 236 237 238 239 240
    self._createTestSuite(quantity=2)
    self.tic()
    self._callOptimizeAlarm()
    def checkConfigListForTestNode(test_node_title):
      config_list = json.loads(self.distributor.startTestSuite(
                             title=test_node_title))
241
      self.assertEqual(1, len(config_list))
242 243 244 245 246 247 248 249
      return (test_node_title, set([x['test_suite'] for x in config_list]))
    config1 = checkConfigListForTestNode("COMP32-Node1")
    config2 = checkConfigListForTestNode("COMP32-Node2")
    self.assertTrue([config1, config2] in  [
                    [('COMP32-Node1',set([u'B0'])), ('COMP32-Node2',set([u'B1']))],
                    [('COMP32-Node1',set([u'B1'])), ('COMP32-Node2',set([u'B0']))]],
                    "%r" % ([config1, config2],))

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  def processTest(self, test_title, revision, start_count=2, stop_count=2,
                  node_title='Node0'):
    """start_count: number of test line to start
       stop_count: number of test line to stop
    """
    status_dict = {}
    test_result_path, revision = self._createTestResult(revision=revision,
      test_list=['testFoo', 'testBar'], test_title=test_title, node_title=node_title)
    if start_count:
      line_url, test = self.distributor.startUnitTest(test_result_path,
                                                      node_title=node_title)
    if start_count == 2:
      next_line_url, next_test = self.distributor.startUnitTest(test_result_path,
                                                                node_title=node_title)
      self.assertEqual(set(['testFoo', 'testBar']), set([test, next_test]))
    if stop_count:
      self.distributor.stopUnitTest(line_url, status_dict, node_title=node_title)
    if stop_count == 2:
      self.tool.stopUnitTest(next_line_url, status_dict, node_title=node_title)
    test_result = self.portal.restrictedTraverse(test_result_path)
    self.assertEqual(test_result.getSimulationState(), "started")
    self.tic()
    if stop_count == 2:
      self.assertEquals(test_result.getSimulationState(), "stopped")
    else:
      self.assertEquals(test_result.getSimulationState(), "started")

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  def test_04b_startTestSuiteOrder(self):
    """
    When we have many test suites associated to one test nodes, the method
    startTestSuite should give first test suites with oldest test results. Like
    this we stop using the random order that was unfair for unlucky peoples
    """
    config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP42-Node1"))
    self.assertEqual([], config_list)
    self._createTestSuite(quantity=3)
    self.tic()
    self._callOptimizeAlarm()
    def getTestSuiteList():
      config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP42-Node1"))
      return ["%s" % x["test_suite_title"] for x in config_list]
    # By default we have random order between test suites
    self.assertEquals(set(["test suite 1", "test suite 2", "test suite 3"]),
                      set(getTestSuiteList()))
296

297 298
    # Check that if test suite 1 and test suite 2 are recently processed,
    # then next work must be test suite 3
299
    self.processTest("test suite 1", "r0=a")
300 301
    self.tic()
    sleep(1) # needed because creation date sql value does not record millesecond
302
    self.processTest("test suite 2", "r0=b")
303 304 305
    self.tic()
    sleep(1)
    self.assertEquals(getTestSuiteList()[0], "test suite 3")
306
    self.processTest("test suite 3", "r0=b")
307 308 309 310 311
    # after test suite 3, we now have to process test suite 1
    # since it is the oldest one
    self.tic()
    sleep(1)
    self.assertEquals(getTestSuiteList()[0], "test suite 1")
312
    self.processTest("test suite 1", "r0=c")
313
    # after test suite 1, we now have to process test suite 2
314 315 316 317
    # since it is the oldest one
    self.tic()
    sleep(1)
    self.assertEquals(getTestSuiteList()[0], "test suite 2")
318
    self.processTest("test suite 2", "r0=d")
319 320
    self.tic()
    sleep(1)
321
    # now let's say for any reason test suite 1 has been done
322
    self.processTest("test suite 1", "r0=e")
323 324 325 326 327
    self.tic()
    sleep(1)
    # we should then have by order 3, 2, 1
    self.assertEquals(["test suite 3", "test suite 2", "test suite 1"],
                      getTestSuiteList())
328
    # now launch all test of test 3, even if they are not finished yet
329
    self.processTest("test suite 3", "r0=f", stop_count=1)
330 331 332 333 334 335
    self.tic()
    sleep(1)
    self.assertEquals(["test suite 2", "test suite 1", "test suite 3"],
                      getTestSuiteList())
    # now launch partially tests of suite 2, it must have priority over
    # test 3, even if test 3 is older because all tests of test 3 are ongoing
336
    self.processTest("test suite 2", "r0=g", start_count=1, stop_count=0)
337 338 339 340
    self.tic()
    sleep(1)
    self.assertEquals(["test suite 1", "test suite 2", "test suite 3"],
                      getTestSuiteList())
341

342 343 344 345 346 347 348 349
  def test_04c_startTestSuiteOrderWithManyTestNodes(self):
    """
    When we have many test suites associated to many test nodes, the method
    startTestSuite should not only take into account the age of latest test result,
    but it should also give more test nodes to test suites having higher priority.
    This avoid affecting all test nodes to a test suite having medium priority
    while a test suite with high priority is waiting for more nodes to speed up.
    """
350
    for x in range(10):
351 352 353
      config_list = json.loads(self.distributor.startTestSuite(
                             title="COMP%s-Node1" % x))
    test_suite_list = self._createTestSuite(quantity=3)
354 355 356 357
    test_suite_1, test_suite_2, test_suite_3 = test_suite_list
    test_suite_list[0].setIntIndex(1) # test suite 1, up to 1 testnode
    test_suite_list[1].setIntIndex(5) # test suite 2, up to 5 testnodes
    test_suite_list[2].setIntIndex(8) # test suite 3, up to 10 testnodes
358 359 360 361 362 363 364 365
    self.tic()

    self._callOptimizeAlarm()
    def getTestSuiteList(title):
      config_list = json.loads(self.distributor.startTestSuite(
                             title=title))
      return ["%s" % x["test_suite_title"] for x in config_list]
    now = DateTime()
366 367 368 369
    def affectTestSuite(node_title, test_suite_list):
      test_node, = self.test_node_module.searchFolder(title=node_title)
      test_node = test_node.getObject()
      test_node.setAggregateValueList(test_suite_list)
370
    try:
371 372 373 374 375 376 377 378 379 380 381 382 383
      # Take a distribution that was calculated by alarm at some point. Though
      # hardcode the distribution, because this is not the purpose of this test
      # to check purpose algorithm
      affectTestSuite("COMP0-Node1", [test_suite_1, test_suite_2, test_suite_3])
      affectTestSuite("COMP1-Node1", [test_suite_2, test_suite_3])
      affectTestSuite("COMP2-Node1", [test_suite_2, test_suite_3])
      affectTestSuite("COMP3-Node1", [test_suite_2, test_suite_3])
      affectTestSuite("COMP4-Node1", [test_suite_3])
      affectTestSuite("COMP5-Node1", [test_suite_2, test_suite_3])
      affectTestSuite("COMP6-Node1", [test_suite_3])
      affectTestSuite("COMP7-Node1", [test_suite_3])
      affectTestSuite("COMP8-Node1", [test_suite_3])
      affectTestSuite("COMP9-Node1", [test_suite_3])
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      # process some test to have old test result in database
      self.processTest("test suite 1", "r0=a", node_title="COMP0-Node1")
      self.pinDateTime(now + 1.0/86400)
      self.processTest("test suite 2", "r0=a", node_title="COMP1-Node1")
      self.pinDateTime(now + 2.0/86400)
      self.processTest("test suite 3", "r0=a", node_title="COMP1-Node1")
      # so by default, when no test is running, older test suite is given first
      self.assertEquals(["test suite 1", "test suite 2", "test suite 3"],
            getTestSuiteList("COMP0-Node1"))
      self.assertEquals(["test suite 2", "test suite 3"],
            getTestSuiteList("COMP5-Node1"))
      # Start some work
      self.pinDateTime(now + 3.0/86400)
      self.processTest("test suite 2", "r0=b", node_title="COMP1-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # suite 1 is now the newest, it is checked last, test suite 3 has priority on test suite 2
      # since no test is running for it
      self.assertEquals(["test suite 1", "test suite 3", "test suite 2"],
            getTestSuiteList("COMP0-Node1"))
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      # Now let's say test suite 3 is starting too
      self.pinDateTime(now + 4.0/86400)
      self.processTest("test suite 3", "r0=b", node_title="COMP6-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # test suite 3 still should have the priority other test suite 2, because it
      # has higher priority
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      # another test node working on test suite 3
      self.processTest("test suite 3", "r0=b", node_title="COMP7-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # Now we have 2 testnodes for test suite 3, and only 1 for test suite 2, time to give
      # more priority to test suite 2
      self.assertEquals(["test suite 2", "test suite 3"],
            getTestSuiteList("COMP5-Node1"))
      self.assertEquals(["test suite 1", "test suite 2", "test suite 3"],
            getTestSuiteList("COMP0-Node1"))
      # so now a testnode working on test suite 2
      self.processTest("test suite 2", "r0=b", node_title="COMP2-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP2-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # Now we have 2 testnodes for test suite 3, and 2 for test suite 2, time to give
      # more priority to test suite 3
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      self.processTest("test suite 3", "r0=b", node_title="COMP8-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP2-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # COMP8-Node1 : test suite 3
      # Now we have 3 testnodes for test suite 3, and 2 for test suite 2, test suite 3 still
      # need priority due to higher priority
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      self.processTest("test suite 3", "r0=b", node_title="COMP9-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP2-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # COMP8-Node1 : test suite 3
      # COMP9-Node1 : test suite 3
      # Now we have 4 testnodes for test suite 3, and 2 for test suite 2, test suite 2 will
      # have again priority
      self.assertEquals(["test suite 2", "test suite 3"],
            getTestSuiteList("COMP5-Node1"))
      self.assertEquals(["test suite 1", "test suite 2", "test suite 3"],
            getTestSuiteList("COMP0-Node1"))
      self.processTest("test suite 2", "r0=b", node_title="COMP3-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP2-Node1 : test suite 2
      # COMP3-Node1 : test suite 2
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # COMP8-Node1 : test suite 3
      # COMP9-Node1 : test suite 3
      # Now we have 4 testnodes for test suite 3, and 3 for test suite 2, test suite 3 will
      # have again priority
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      self.processTest("test suite 3", "r0=b", node_title="COMP4-Node1", start_count=0, stop_count=0)
      # COMP1-Node1 : test suite 2
      # COMP2-Node1 : test suite 2
      # COMP3-Node1 : test suite 2
      # COMP4-Node1 : test suite 3
      # COMP6-Node1 : test suite 3
      # COMP7-Node1 : test suite 3
      # COMP8-Node1 : test suite 3
      # COMP9-Node1 : test suite 3
      self.assertEquals(["test suite 3", "test suite 2"],
            getTestSuiteList("COMP5-Node1"))
      self.assertEquals(["test suite 1", "test suite 3", "test suite 2"],
            getTestSuiteList("COMP0-Node1"))
    finally:
      self.unpinDateTime()

486 487 488
  def _cleanupTestResult(self):
    self.tic()
    cleanup_state_list = ['started', 'stopped']
489
    test_list =  self.test_result_module.searchFolder(title='"TEST FOO" OR "test suite %" OR "Default Test Suite"',
490 491 492 493 494 495
               simulation_state=cleanup_state_list)
    for test_result in test_list:
      if test_result.getSimulationState() in cleanup_state_list:
        test_result.cancel()
    self.tic()

496
  def _createTestResult(self, revision="r0=a,r1=a", node_title='Node0',
497
                              test_list=None, tic=1, allow_restart=False,
498
                              test_title=None, distributor=None):
499 500
    if test_title is None:
      test_title = self.default_test_title
501 502 503
    if distributor is None:
      distributor = self.distributor
    result =  distributor.createTestResult(
504
                               "", revision, test_list or [], allow_restart,
505
                               test_title=test_title, node_title=node_title)
506 507 508
    # we commit, since usually we have a remote call only doing this
    (self.tic if tic else self.commit)()
    return result
509

510 511
  def test_05_createTestResult(self):
    """
512
    We will check the method createTestResult of distributor
513
    """
514 515
    self._createTestNode()
    self.tic()
516
    test_result_path, revision = self._createTestResult()
517
    self.assertEqual("r0=a,r1=a", revision)
518 519 520 521
    self.assertTrue(test_result_path.startswith("test_result_module/"))
    # If we ask again with another revision, we should get with previous
    # revision
    next_test_result_path, next_revision = self._createTestResult(
522
      revision="r0=a,r1=b", node_title="UnitTestNode 1")
523 524
    self.assertEqual(revision, next_revision)
    self.assertEqual(next_test_result_path, test_result_path)
525 526
    # Check if test result object is well created
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
527 528
    self.assertEqual("Test Result", test_result.getPortalType())
    self.assertEqual(0, len(test_result.objectValues(
529 530 531 532
                             portal_type="Test Result Line")))
    # now check that if we pass list of test, new lines will be created
    next_test_result_path, next_revision = self._createTestResult(
      test_list=['testFoo', 'testBar'])
533
    self.assertEqual(next_test_result_path, test_result_path)
534
    line_list = test_result.objectValues(portal_type="Test Result Line")
535 536
    self.assertEqual(2, len(line_list))
    self.assertEqual(set(['testFoo', 'testBar']), set([x.getTitle() for x
537
                      in line_list]))
538
    line_url, _ = self.tool.startUnitTest(test_result_path)
539 540
    result = self._createTestResult(test_list=['testFoo', 'testBar'])
    self.assertEqual((test_result_path, revision), result)
541
    self.tool.startUnitTest(test_result_path)
542 543 544 545 546 547 548 549
    # all tests of this test suite are now started, stop affecting test node to it
    result = self._createTestResult(test_list=['testFoo', 'testBar'])
    self.assertEqual(None, result)
    # though, is we restart one line, we will have affectation again
    self.portal.restrictedTraverse(line_url).redraft()
    self.commit()
    result = self._createTestResult(test_list=['testFoo', 'testBar'])
    self.assertEqual((test_result_path, revision), result)
550
    self.tool.startUnitTest(test_result_path)
551

552 553 554 555 556 557 558 559 560 561
  def test_05b_createTestResultDoesNotReexecuteRevision(self):
    """
    Make sure to no retest former revision. This scenario must work
    - testnode call createTestResult with revision r0=b. Test is executed
    - By hand is created test with revision r0=a (to make testnode checking old
      revision). Test is executed
    - if testnode ask again for r0=b, no test must be created
    - if testnode ask for r0=c, then usual test is created/executed
    """
    # launch test r0=b
562 563
    test_result_path, _ = self._createTestResult(revision="r0=b", test_list=["testFoo"])
    line_url, _ = self.tool.startUnitTest(test_result_path)
564 565 566
    status_dict = {}
    self.tool.stopUnitTest(line_url, status_dict)
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
567
    self.tic()
568 569
    self.assertEqual("stopped", test_result.getSimulationState())
    # launch test r0=a
570 571
    test_result_path, _ = self._createTestResult(revision="r0=a", test_list=["testFoo"])
    line_url, _ = self.tool.startUnitTest(test_result_path)
572 573
    self.tool.stopUnitTest(line_url, status_dict)
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
574
    self.tic()
575 576 577 578 579
    self.assertEqual("stopped", test_result.getSimulationState())
    # Make sure we do not relaunch test with revision r0=b
    result = self._createTestResult(revision="r0=b", test_list=["testFoo"])
    self.assertEqual(None, result)
    # launch test r0=c
580 581
    test_result_path, _ = self._createTestResult(revision="r0=c", test_list=["testFoo"])
    line_url, _ = self.tool.startUnitTest(test_result_path)
582 583
    self.tool.stopUnitTest(line_url, status_dict)
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
584
    self.tic()
585 586
    self.assertEqual("stopped", test_result.getSimulationState())

587
  def test_05c_createTestResult_with_registered_test_node(self):
588
    test_result_path, _ = self._createTestResult()
589 590 591 592
    # check that Test Node Result used in Test Result is specialised
    # into registered Test Node
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
    test_result_node = test_result.contentValues(portal_type='Test Result Node')[0]
593
    self.assertEqual(test_result_node.getSpecialiseTitle(), 'Node0')
594

595 596 597 598
  def test_06_startStopUnitTest(self):
    """
    We will check methods startUnitTest/stopUnitTest of task distribution tool
    """
599
    test_result_path, _ = self._createTestResult(
600 601 602 603
      test_list=['testFoo', 'testBar'])
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
    line_url, test = self.tool.startUnitTest(test_result_path)
    next_line_url, next_test = self.tool.startUnitTest(test_result_path)
604 605 606
    # once all tests are affected, stop affecting resources on this test result
    next_result = self.tool.startUnitTest(test_result_path)
    self.assertEqual(None, next_result)
607
    # first launch, we have no time optimisations, so tests are
608 609
    # launched in alphabetical order
    self.assertEqual(['testBar', 'testFoo'], [test, next_test])
610 611 612 613 614 615 616 617 618 619 620 621 622 623
    status_dict = {}
    self.tool.stopUnitTest(line_url, status_dict)
    self.tool.stopUnitTest(next_line_url, status_dict)
    line = self.portal.unrestrictedTraverse(line_url)
    def checkDuration(line):
      duration = getattr(line, "duration", None)
      self.assertTrue(isinstance(duration, float))
      self.assertTrue(duration>0)
    checkDuration(line)
    next_line = self.portal.unrestrictedTraverse(next_line_url)
    checkDuration(next_line)
    # Make sure second test takes more time
    next_line.duration = line.duration + 1
    # So if we launch another unit test, it will process first the
624
    # one which is the slowest
625
    self.tic()
626
    self.assertEqual("stopped", test_result.getSimulationState())
627
    self.tic()
628
    next_test_result_path, _ = self._createTestResult(
629 630 631 632
      test_list=['testFoo', 'testBar'], revision="r0=a,r1=b")
    self.assertNotEquals(next_test_result_path, test_result_path)
    line_url, test = self.tool.startUnitTest(next_test_result_path)
    next_line_url, next_test = self.tool.startUnitTest(next_test_result_path)
633
    self.assertEqual(['testFoo', 'testBar'], [test, next_test])
634

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  def test_startUnitTestRunsFailedTestFirst(self):
    # simulate previous run
    test_result = self.portal.test_result_module.newContent(
            portal_type='Test Result',
            title=self.default_test_title,
            start_date=DateTime())
    test_result.newContent(
            portal_type='Test Result Line',
            title='testFailing',
            ).stop(test_count=1, duration=100, failure_count=1)
    test_result.newContent(
            portal_type='Test Result Line',
            title='testFast',
            ).stop(test_count=1, duration=50)
    test_result.newContent(
            portal_type='Test Result Line',
            title='testSlow',
            ).stop(test_count=1, duration=1000)
    test_result.stop()
    self.tic()
    test_result_path, _ = self._createTestResult(
      test_list=['testSlow', 'testFast', 'testFailing'])
    # we run first the tests failing in previous run
    self.assertEqual(
        ['testFailing', 'testSlow', 'testFast'],
        [self.tool.startUnitTest(test_result_path)[1] for _ in range(3)])

662 663 664 665 666 667
  def checkTestResultLine(self, test_result, expected):
    line_list = test_result.objectValues(portal_type="Test Result Line")
    found_list = [(x.getTitle(), x.getSimulationState()) for x in line_list]
    found_list.sort(key=lambda x: x[0])
    self.assertEqual(expected, found_list)

668 669 670 671 672
  def test_06b_restartStuckTest(self):
    """
    Check if a test result line is not stuck in 'started', if so, redraft
    if with alarm to let opportunity of another test node to work on it
    """
673
    test_result_path, _ = self._createTestResult(
674 675
      test_list=['testFoo', 'testBar'])
    test_result = self.portal.unrestrictedTraverse(test_result_path)
676
    line_url, _ = self.tool.startUnitTest(test_result_path)
677
    now = DateTime()
678
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'draft')])
679
    self._callRestartStuckTestResultAlarm()
680
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'draft')])
681
    line_url, _ = self.tool.startUnitTest(test_result_path)
682
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'started')])
683
    self._callRestartStuckTestResultAlarm()
684
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'started')])
685 686 687 688 689 690 691
    # now let change history to do like if a test result line was started
    # a long time ago
    line = self.portal.restrictedTraverse(line_url)
    for history_line in line.workflow_history["test_result_workflow"]:
      if history_line['action'] == 'start':
        history_line['time'] = now - 1
    self._callRestartStuckTestResultAlarm()
692
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'draft')])
693
    self.tool.stopUnitTest(line_url, {})
694
    self.checkTestResultLine(test_result, [('testBar', 'started'), ('testFoo', 'stopped')])
695

696
  def test_07_reportTaskFailure(self):
697 698 699 700 701 702
    """
    When all test nodes report failures, we should mark the test result as
    failed. If we do not do so, test node would always pickup same repository
    revision and might fail with same failure forever (for example, a slapos
    build issue).
    """
703 704 705
    self._createTestNode()
    test_result_path, _ = self._createTestResult()
    next_test_result_path, _ = self._createTestResult(node_title="UnitTestNode 1")
706 707
    self.assertEqual(test_result_path, next_test_result_path)
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
708
    self.assertEqual("started", test_result.getSimulationState())
709 710 711
    node_list = test_result.objectValues(portal_type="Test Result Node",
                                         sort_on=[("title", "ascending")])
    def checkNodeState(first_state, second_state):
712
      self.assertEqual([("Node0", first_state), ("UnitTestNode 1", second_state)],
713 714 715
              [(x.getTitle(), x.getSimulationState()) for x in node_list])
    checkNodeState("started", "started")
    self.tool.reportTaskFailure(test_result_path, {}, "Node0")
716
    self.assertEqual("started", test_result.getSimulationState())
717
    checkNodeState("failed", "started")
718
    self.tool.reportTaskFailure(test_result_path, {}, "UnitTestNode 1")
719
    self.assertEqual("failed", test_result.getSimulationState())
720 721
    checkNodeState("failed", "failed")

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
  def test_07b_reportTaskFailureWithRunningTest(self):
    """
    Similar to above test. Though, sometimes there is failure reported only because
    runTestSuite reached timeout. This happens when not enough testnode are working
    on a very long test suite. So code investigate if tests looked working fine, and
    it might try to not cancel test result if there is chance that tests could be
    continued.

    For example :
    - testnode0 start test suite Foo with revision r0 which would take 6 hours (other
      testnodes are busy)
    - after 4 hours, runTestSuite reach timeout of 4 hours (value set in test nodes).
      thus it report a failure. We do not cancel the test result since everything went
      fine up to know
    - after some time testnode0 come back to run test suite Foo, revision r0, and
      just do the 2 remaining hours. Test Suite can go up to the end even if we have
      timeout smaller than total time for test suite.
    """
    now = DateTime()
    try:
      self.pinDateTime(now - 1.0/24*2)
743
      test_result_path, _ = self._createTestResult(
744 745 746 747 748 749
                                               test_list=['testFoo', 'testBar'])
      test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
      self.assertEqual("started", test_result.getSimulationState())
      node, = test_result.objectValues(portal_type="Test Result Node",
                                           sort_on=[("title", "ascending")])
      self.assertEqual("started", node.getSimulationState())
750
      self.tool.startUnitTest(test_result_path)
751 752
      # We have a failure but with recent activities on tests
      self.pinDateTime(now - 1.0/24*1.5)
753
      self.distributor.reportTaskFailure(test_result_path, {}, "Node0")
754 755 756 757
      self.assertEqual("failed", node.getSimulationState())
      self.assertEqual("started", test_result.getSimulationState())
      # We have a failure but with no recent activities on tests
      self.pinDateTime(now)
758
      self.distributor.reportTaskFailure(test_result_path, {}, "Node0")
759 760 761 762 763
      self.assertEqual("failed", node.getSimulationState())
      self.assertEqual("failed", test_result.getSimulationState())
    finally:
      self.unpinDateTime()

764 765 766 767 768 769 770
  def test_07c_reportTaskFailureWithRedraftedTestResultLine(self):
    """
    Similar to above test.

    But on the other hand, if a test result line is started many times (due to
    automatic redraft), then this might just means we have issue of runTestSuite unable
    to finish tests, or we might have just tests that can never be executed within timeout time.
771 772
    In such case, it's better to mark test result as failed to give a chance to switch
    to new revision
773 774 775 776 777 778 779 780 781 782 783
    """
    now = DateTime()
    try:
      self.pinDateTime(now - 1.0/24*8)
      test_result_path, _ = self._createTestResult(test_list=['testFoo'])
      test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
      self.checkTestResultLine(test_result, [('testFoo', 'draft')])
      self.assertEqual("started", test_result.getSimulationState())
      node, = test_result.objectValues(portal_type="Test Result Node",
                                           sort_on=[("title", "ascending")])
      self.assertEqual("started", node.getSimulationState())
784
      self.distributor.startUnitTest(test_result_path, node_title="Node0")
785 786
      self.checkTestResultLine(test_result, [('testFoo', 'started')])
      # We have a failure but with recent activities on tests
787
      # so do not mark the test result as failed
788
      self.pinDateTime(now - 1.0/24*7.5)
789
      self.distributor.reportTaskFailure(test_result_path, {}, "Node0")
790 791
      self.assertEqual("failed", node.getSimulationState())
      self.assertEqual("started", test_result.getSimulationState())
792
      # test result line redrafted due to reportTaskFailure
793 794
      self.checkTestResultLine(test_result, [('testFoo', 'draft')])
      # Test is then relaunched
795 796
      self.pinDateTime(now - 1.0/24*7)
      self.tool.startUnitTest(test_result_path, node_title="Node0")
797 798 799
      self.checkTestResultLine(test_result, [('testFoo', 'started')])
      # We have another failure but remains only test result line that was already
      # redrafted, so we have to mark the test result as failed
800 801
      self.pinDateTime(now - 1.0/24*4)
      self.distributor.reportTaskFailure(test_result_path, {}, "Node0")
802 803 804 805 806
      self.assertEqual("failed", node.getSimulationState())
      self.assertEqual("failed", test_result.getSimulationState())
    finally:
      self.unpinDateTime()

807 808 809 810 811 812
  def test_08_checkWeCanNotCreateTwoTestResultInParallel(self):
    """
    To avoid duplicates of test result when several testnodes works on the
    same suite, we create test and we immediately reindex it. So we must
    be able to find new test immediately after.
    """
813 814 815 816 817
    self._createTestNode()
    self.tic()
    test_result_path, _ = self._createTestResult(tic=0)
    next_test_result_path, _ = self._createTestResult(
                                      node_title="UnitTestNode 1", tic=0)
818
    self.assertEqual(test_result_path, next_test_result_path)
819 820

  def _checkCreateTestResultAndAllowRestart(self, tic=False):
821 822
    test_result_path, _ = self._createTestResult(test_list=["testFoo"])
    line_url, _ = self.tool.startUnitTest(test_result_path)
823 824 825 826 827
    status_dict = {}
    self.tool.stopUnitTest(line_url, status_dict)
    if tic:
      self.tic()
    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
828
    self.assertEqual(None, self._createTestResult(test_list=["testFoo"]))
829
    next_test_result_path, _ = self._createTestResult(
830 831
      test_list=["testFoo"], allow_restart=True)
    self.assertTrue(next_test_result_path != test_result_path)
832 833
    self.tic()
    self.assertEqual("stopped", test_result.getSimulationState())
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

  def test_09_checkCreateTestResultAndAllowRestartWithoutTic(self):
    """
    The option allow restart of createTestResult enable to possibility to
    always launch tests even if the given revision is already tested.

    Is this really useful and used ?
    """
    self._checkCreateTestResultAndAllowRestart()    

  def test_09b_checkCreateTestResultAndAllowRestartWithTic(self):
    """
    The option allow restart of createTestResult enable to possibility to
    always launch tests even if the given revision is already tested. We
    try here with reindex after stopUnitTest
    """
    self._checkCreateTestResultAndAllowRestart(tic=True)

  def test_10_cancelTestResult(self):
    pass

855 856 857 858 859 860 861 862 863 864 865 866
  def test_10b_generateConfiguration(self):
    """
    It shall be possible on a test suite to define configuration we would like
    to use to create slapos instance.
    """
    test_suite, = self._createTestSuite(cluster_configuration=None)
    self.tic()
    self.assertEquals('{"configuration_list": [{}]}', self.distributor.generateConfiguration(test_suite.getTitle()))
    test_suite.setClusterConfiguration("{'foo': 3}")
    self.assertEquals('{"configuration_list": [{}]}', self.distributor.generateConfiguration(test_suite.getTitle()))
    test_suite.setClusterConfiguration('{"foo": 3}')
    self.assertEquals('{"configuration_list": [{"foo": 3}]}', self.distributor.generateConfiguration(test_suite.getTitle()))
867 868 869 870
    # make sure generateConfiguration does not fail if test suite is invalidated
    test_suite.invalidate()
    self.tic()
    self.assertEquals('{"configuration_list": [{}]}', self.distributor.generateConfiguration(test_suite.getTitle()))
871

872 873 874 875
  def _checkTestSuiteAggregateList(self, *args):
    self.tic()
    self._callOptimizeAlarm()
    for test_node, aggregate_list in args:
876 877
      test_note_aggregate_title_list = [x.split(" ")[-1] for x in test_node.getAggregateTitleList()]
      self.assertEqual(set(test_note_aggregate_title_list),
878 879
        set(aggregate_list),
        "incorrect aggregate for %r, got %r instead of %r" % \
880
        (test_node.getTitle(), test_note_aggregate_title_list, aggregate_list))
881 882 883 884 885 886 887

  def test_11_checkERP5ProjectOptimizationIsStable(self):
    """
    When we have two test suites and we have two test nodes, we should have
    one test suite distributed per test node
    """
    test_node_one, test_node_two = self._createTestNode(quantity=2)
888 889
    test_suite_one = self._createTestSuite(reference_correction=+0,
                              title='one')[0]
890
    self._createTestSuite(reference_correction=+1,
891
                              title='two')[0].getRelativeUrl()
892 893 894
    self.tic()
    self._callOptimizeAlarm()
    check = self._checkTestSuiteAggregateList
895 896
    check([test_node_one, ["one"]],
          [test_node_two, ["two"]])
897 898 899 900
    # first test suite is invalidated, so it should be removed from nodes, 
    # but this should not change assignment of second test suite
    test_suite_one.invalidate()
    check([test_node_one, []],
901
          [test_node_two, ["two"]])
902 903 904 905 906
    # an additional test node is added, with lower title, this should
    # still not change anyting
    test_node_zero = self._createTestNode(quantity=1, reference_correction=-1)[0]
    check([test_node_zero, []],
          [test_node_one, []],
907
          [test_node_two, ["two"]])
908
    # test suite one is validated again, it is installed on first
909 910
    # available test node
    test_suite_one.validate()
911
    check([test_node_zero, ["one"]],
912
          [test_node_one, []],
913
          [test_node_two, ["two"]])
914 915 916
    # for some reasons, test_node two is dead, so the work is distributed
    # to remaining test nodes
    test_node_two.invalidate()
917 918
    check([test_node_zero, ["one"]],
          [test_node_one, ["two"]],
919 920 921
          [test_node_two, []])
    # we add another test suite, since all test node already have one
    # test suite, the new test suite is given to first available one
922
    self._createTestSuite(reference_correction=+2,
923
                                title='three')[0].getRelativeUrl()
924 925
    check([test_node_zero, ["one", "three"]],
          [test_node_one, ["two"]],
926
          [test_node_two, []])
927 928 929
    # test node two is coming back. To have better repartition of work,
    # move some work from overloaded test node to less busy test node, while
    # still trying to move as less test suite as possible (here only one)
930
    test_node_two.validate()
931 932 933
    check([test_node_zero, ["three"]],
          [test_node_one, ["two"]],
          [test_node_two, ["one"]])
934
    # Now let's create a test suite needing between 1 to 2 test nodes
935 936
    # Make sure additional work is added without moving other test suites
    self._createTestSuite(reference_correction=+3,
937
                             priority=2, title='four')[0].getRelativeUrl()
938 939 940 941 942 943 944 945 946 947 948 949
    check([test_node_zero, ["three", "four"]],
          [test_node_one, ["two", "four"]],
          [test_node_two, ["one"]])
    # Now let's create a a test suite needing 1 nodes
    # to make sure test nodes with less work get the work first
    test_suite_five = self._createTestSuite(reference_correction=+4,
                             title='five')[0]
    check([test_node_zero, ["three", "four"]],
          [test_node_one, ["two", "four"]],
          [test_node_two, ["one", "five"]])
    # Now let's create another test suite needing between 2 to 3 test nodes
    # and increase priority of one suite to make all test nodes almost satured
950
    test_suite_five.setIntIndex(3)
951
    self._createTestSuite(reference_correction=+5,
952
                             priority=3, title='six')
953 954 955
    check([test_node_zero, ["three", "four","five", "six"]],
          [test_node_one, ["two", "four", "five", "six"]],
          [test_node_two, ["one", "five", "six"]])
956 957
    # Then, check what happens if all nodes are more than saturated
    # with a test suite needing between 3 to 5 test nodes
958
    self._createTestSuite(reference_correction=+6,
959
                             priority=5, title='seven')
960 961 962
    check([test_node_zero, ["three", "four", "five", "six"]],
          [test_node_one, ["two", "four", "five", "six"]],
          [test_node_two, ["one", "seven", "five", "six"]])
963
    # No place any more, adding more test suite has no consequence
964 965
    # we need 5*2 + 3*2  + 2*1 + 1*3 => 21 slots
    self._createTestSuite(reference_correction=+7,
966
                             priority=5, title='height')
967 968 969
    check([test_node_zero, ["three", "four", "five", "six"]],
          [test_node_one, ["two", "four", "five", "six"]],
          [test_node_two, ["one", "seven", "five", "six"]])
970
    # free some place by removing a test suite
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    # make sure free slots are fairly distributed to test suite having
    # less test nodes
    # We remove 3 slots, so we would need 18 slots
    test_suite_five.invalidate()
    check([test_node_zero, ["three", "four", "height", "six"]],
          [test_node_one, ["two", "four", "seven" , "six"]],
          [test_node_two, ["one", "seven", "height" , "six"]])
    # Check that additional test node would get work for missing assignments
    # No move a test suite is done since in average we miss slots
    test_node_three, = self._createTestNode(reference_correction=2)
    check([test_node_zero, ["three", "four", "height", "six"]],
          [test_node_one, ["two", "four", "seven" , "six"]],
          [test_node_two, ["one", "seven", "height" , "six"]],
          [test_node_three, ["seven", "height"]])
    # With even more test node, check that we move some work to less
    # busy test nodes
    test_node_four, = self._createTestNode(reference_correction=3)
    test_node_five, = self._createTestNode(reference_correction=4)
    check([test_node_zero, ["three", "six", "height"]],
          [test_node_one, ["two", "six", "seven"]],
          [test_node_two, ["one", "seven", "height"]],
          [test_node_three, ["four", "seven", "height"]],
          [test_node_four, ["four", "seven", "height"]],
          [test_node_five, ["six", "seven", "height"]])
    test_node_six, = self._createTestNode(reference_correction=5)
    test_node_seven, = self._createTestNode(reference_correction=6)
    check([test_node_zero, ["three", "height"]],
          [test_node_one, ["two", "seven"]],
          [test_node_two, ["one", "height"]],
          [test_node_three, ["seven", "height"]],
          [test_node_four, ["four", "seven", "height"]],
          [test_node_five, ["six", "seven", "height"]],
          [test_node_six, ["six", "seven"]],
          [test_node_seven, ["four", "six"]])
1005

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
  def test_11b_checkERP5ProjectDistributionWithCustomMaxQuantity(self):
    """
    Check that the property max_test_suite on the distributor could
    be used to customize the quantity of test suite affected per test node
    """
    test_node, = self._createTestNode(quantity=1)
    test_suite_list = self._createTestSuite(quantity=5)
    self.tool.TestTaskDistribution.setMaxTestSuite(None)
    self.tic()
    self._callOptimizeAlarm()
    self.assertEqual(4, len(set(test_node.getAggregateList())))
    self.tool.TestTaskDistribution.setMaxTestSuite(1)
    self._callOptimizeAlarm()
    self.assertEqual(1, len(set(test_node.getAggregateList())))
    self.tool.TestTaskDistribution.setMaxTestSuite(10)
    self._callOptimizeAlarm()
    self.assertEqual(5, len(set(test_node.getAggregateList())))
    self.assertEqual(set(test_node.getAggregateList()), 
                     set([x.getRelativeUrl() for x in test_suite_list]))

1026 1027 1028 1029 1030 1031 1032
  def test_12_checkCloudPerformanceOptimizationIsStable(self):
    """
    When we have two test suites and we have two test nodes, we should have
    one test suite distributed per test node
    """
    test_node_one, test_node_two = self._createTestNode(quantity=2,
                               specialise_value=self.performance_distributor)
1033 1034
    test_suite_one = self._createTestSuite(
                          title='one', specialise_value=self.performance_distributor)[0]
1035 1036
    self._createTestSuite(title='two', reference_correction=+1,
                          specialise_value=self.performance_distributor)
1037 1038 1039
    self.tic()
    self._callOptimizeAlarm()
    check = self._checkTestSuiteAggregateList
1040 1041
    check([test_node_one, ["one", "two"]],
          [test_node_two, ["one", "two"]])
1042 1043 1044
    # first test suite is invalidated, so it should be removed from nodes, 
    # but this should not change assignment of second test suite
    test_suite_one.invalidate()
1045 1046
    check([test_node_one, ["two"]],
          [test_node_two, ["two"]])
1047 1048 1049 1050
    # an additional test node is added, with lower title, it should
    # get in any case all test suite
    test_node_zero = self._createTestNode(quantity=1, reference_correction=-1,
                            specialise_value=self.performance_distributor)[0]
1051 1052 1053
    check([test_node_zero, ["two"]],
          [test_node_one, ["two"]],
          [test_node_two, ["two"]])
1054 1055 1056
    # test suite one is validating again, it is installed on first
    # available test node
    test_suite_one.validate()
1057 1058 1059
    check([test_node_zero, ["one", "two"]],
          [test_node_one, ["one", "two"]],
          [test_node_two, ["one", "two"]])
1060 1061 1062
    # for some reasons, test_node two is dead, this has no consequence
    # for others
    test_node_two.invalidate()
1063 1064 1065
    check([test_node_zero, ["one", "two"]],
          [test_node_one, ["one", "two"]],
          [test_node_two, ["one", "two"]])
1066 1067
    # we add another test suite, all test nodes should run it, except
    # test_node_two which is dead
1068 1069 1070 1071 1072
    self._createTestSuite(title="three", reference_correction=+2,
                             specialise_value=self.performance_distributor)
    check([test_node_zero, ["one", "two", "three"]],
          [test_node_one, ["one", "two", "three"]],
          [test_node_two, ["one", "two"]])
1073 1074
    # test node two is coming back. It should run all test suites
    test_node_two.validate()
1075 1076 1077
    check([test_node_zero, ["one", "two", "three"]],
          [test_node_one, ["one", "two", "three"]],
          [test_node_two, ["one", "two", "three"]])
1078
    # now we are going to
1079

1080
  def test_13_startTestSuiteAndCreateTestResultWithOneTestNodeAndPerformanceDistributor(self):
1081 1082
    config_list = json.loads(self.performance_distributor.startTestSuite(
                             title="COMP32-Node1"))
1083
    self.assertEqual([], config_list)
1084
    self._createTestSuite(quantity=2,
1085 1086 1087 1088 1089
                          specialise_value=self.performance_distributor)
    self.tic()
    self._callOptimizeAlarm()
    config_list = json.loads(self.performance_distributor.startTestSuite(
                             title="COMP32-Node1"))
1090
    self.assertEqual(2, len(config_list))
1091 1092
    self.assertEqual(set(['test suite 1|COMP32-Node1',
                           'test suite 2|COMP32-Node1']),
1093
                      set([x['test_suite_title'] for x in config_list]))
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    revision = 'a=a,b=b,c=c'
    result = self._createTestResult(test_title='test suite 1|COMP32-Node1',
                                    distributor=self.performance_distributor)
    self.tic()
    self.assertNotEqual(None, result)
    first_path = result[0]
    next_result = self._createTestResult(test_title='test suite 1|COMP42-Node1',
                                    distributor=self.performance_distributor)
    self.assertNotEqual(None, result)
    next_path = next_result[0]
    self.assertTrue(first_path != next_path)
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 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

  def test_14_subscribeNodeCheckERP5ScalabilityDistributor(self):
    """
    Check test node subscription.
    """
    test_node_module = self.test_node_module
    
    # Generate informations for nodes to subscribe
    nodes = dict([("COMP%d-Scalability-Node_test14" %i, "COMP-%d" %i) for i in range(0,5)])
    # Subscribe nodes
    for node_title in nodes.keys():
      self.scalability_distributor.subscribeNode(node_title, computer_guid=nodes[node_title])
    self.tic()
    # Get validated test nodes
    test_nodes = test_node_module.searchFolder(validation_state = 'validated')
    # Get test node title list
    test_node_titles = [x.getTitle() for x in test_nodes]
    # Check subscription
    for node_title in nodes.keys():
      self.assertTrue(node_title in test_node_titles)
    # Check ping date
    # TODO..

  def test_15_optimizeConfigurationCheckElectionERP5ScalabilityDistributor(self):
    """
    Check optimizeConfiguration method of scalability distributor.
     - Check the master election
    """
    test_node_module = self.test_node_module
    
    ## 1 (check election, classic)
    # Subscribe nodes
    self.scalability_distributor.subscribeNode("COMP1-Scalability-Node1", computer_guid="COMP-1")
    self.scalability_distributor.subscribeNode("COMP2-Scalability-Node2", computer_guid="COMP-2")
    self.scalability_distributor.subscribeNode("COMP3-Scalability-Node3", computer_guid="COMP-3")
    self.scalability_distributor.subscribeNode("COMP4-Scalability-Node4", computer_guid="COMP-4")
    self.tic()
    # Check test node election
    def getMasterAndSlaveNodeList():
      """
      Optimize the configuration and return which nodes are master/slave
      """
      self._callOptimizeAlarm()
      master_test_node_list = [x for x in test_node_module.searchFolder()\
                               if (x.getMaster() == True  and x.getValidationState() == 'validated')]
      slave_test_node_list =  [x for x in test_node_module.searchFolder()\
                               if (x.getMaster() == False and x.getValidationState() == 'validated')]
      return master_test_node_list, slave_test_node_list
    master_test_node_list, slave_test_node_list = getMasterAndSlaveNodeList()

    # -Only one master must be elected
    self.assertEquals(1, len(master_test_node_list))
    # -Others test node must not be the matser
    self.assertEquals(3, len(slave_test_node_list))
    
    # Get the current master test node 
    current_master_test_node_1 = master_test_node_list[0]
    
    ## 2 (check election, with adding new nodes)
    # Add new nodes
    self.scalability_distributor.subscribeNode("COMP5-Scalability-Node5", computer_guid="COMP-5")
    self.scalability_distributor.subscribeNode("COMP6-Scalability-Node6", computer_guid="COMP-6")
    self.tic()
    # Check test node election
    master_test_node_list, slave_test_node_list = getMasterAndSlaveNodeList()
    # -Only one master must be elected
    self.assertEquals(1, len(master_test_node_list))
    # -Others test node must not be the matser
    self.assertEquals(5, len(slave_test_node_list))

    # Get the current master test node
    current_master_test_node_2 =  master_test_node_list[0]
    # Master test node while he is alive
    self.assertEquals(current_master_test_node_1.getTitle(),
                      current_master_test_node_2.getTitle())

    ## 3 (check election, with master deletion)
    # Invalidate master
    current_master_test_node_2.invalidate()
    # Check test node election
    master_test_node_list, slave_test_node_list = getMasterAndSlaveNodeList()
    # -Only one master must be elected
    self.assertEquals(1, len(master_test_node_list))
    # -Others test node must not be the matser
    self.assertEquals(4, len(slave_test_node_list))

    # Get the current master test node 
    current_master_test_node_3 = master_test_node_list[0]
    # Master test node must be an other test node than previously
    self.assertNotEquals(current_master_test_node_2.getTitle(), 
                         current_master_test_node_3.getTitle())
    

  def test_16_startTestSuiteERP5ScalabilityDistributor(self):
    """
    Check test suite getting, for the scalability case only the master
    test node receive test suite.
    """
    test_node_module = self.test_node_module

    # Subscribe nodes
1206 1207 1208 1209 1210 1211
    self.scalability_distributor.subscribeNode("COMP1-Scalability-Node1", computer_guid="COMP-1")
    self.scalability_distributor.subscribeNode("COMP2-Scalability-Node2", computer_guid="COMP-2")
    self.scalability_distributor.subscribeNode("COMP3-Scalability-Node3", computer_guid="COMP-3")
    self.scalability_distributor.subscribeNode("COMP4-Scalability-Node4", computer_guid="COMP-4")
    # Create test suite
    self._createTestSuite(quantity=1,priority=1, reference_correction=0,
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
                       specialise_value=self.scalability_distributor, portal_type="Scalability Test Suite")  
    self.tic()
    self._callOptimizeAlarm()
    # Get current master test node
    master_test_nodes = [x for x in test_node_module.searchFolder()\
                         if (x.getMaster() == True and x.getValidationState() == "validated")]     
    current_master_test_node = master_test_nodes[0]
    self.tic()
    # Each node run startTestSuite
    config_nodes = {
                     'COMP1-Scalability-Node1' :
                        json.loads(self.scalability_distributor.startTestSuite(
                                   title="COMP1-Scalability-Node1")),
                     'COMP2-Scalability-Node2' :
                        json.loads(self.scalability_distributor.startTestSuite(
                                   title="COMP2-Scalability-Node2")),
                     'COMP3-Scalability-Node3' :
                        json.loads(self.scalability_distributor.startTestSuite(
                                   title="COMP3-Scalability-Node3")),
                     'COMP4-Scalability-Node4' :
                        json.loads(self.scalability_distributor.startTestSuite(
                                   title="COMP4-Scalability-Node4"))
                   }
    # Check if master has got a non empty configuration
    self.assertNotEquals(config_nodes[current_master_test_node.getTitle()], [])
    # -Delete master test node suite from dict
    del config_nodes[current_master_test_node.getTitle()]
    # Check if slave test node have got empty list
    for suite in config_nodes.values():
      self.assertEquals(suite, [])

  def test_17_isMasterTestnodeERP5ScalabilityDistributor(self):
    """
    Check the method isMasterTestnode()
    """
    test_node_module = self.test_node_module

    # Subscribe nodes
    self.scalability_distributor.subscribeNode("COMP1-Scalability-Node1", computer_guid="COMP-1")
    self.scalability_distributor.subscribeNode("COMP2-Scalability-Node2", computer_guid="COMP-2")
    self.scalability_distributor.subscribeNode("COMP3-Scalability-Node3", computer_guid="COMP-3")
    self.scalability_distributor.subscribeNode("COMP4-Scalability-Node4", computer_guid="COMP-4")
    self.tic()
    self._callOptimizeAlarm()
    # Optimize configuration
    self.scalability_distributor.optimizeConfiguration()
    self.tic()
    # Get test nodes 
    master_test_nodes = [x for x in test_node_module.searchFolder()
                         if (x.getMaster() == True and x.getValidationState() == 'validated')]
    slave_test_nodes = [x for x in test_node_module.searchFolder()
                         if (x.getMaster() == False and x.getValidationState() == 'validated')]
    # Check isMasterTestnode method
    for master in master_test_nodes:
      self.assertTrue(self.scalability_distributor.isMasterTestnode(master.getTitle()))
    for slave in slave_test_nodes:
      self.assertTrue(not self.scalability_distributor.isMasterTestnode(slave.getTitle()))

  def test_18_checkConfigurationGenerationERP5ScalabilityDistributor(self):
    """
    Check configuration generation
    """
    # Subscribe nodes
Łukasz Nowak's avatar
Łukasz Nowak committed
1275 1276 1277
    node_list = [
      ('NODE-%s' % (i,), self.scalability_distributor.subscribeNode("COMP%s-Scalability-Node%s" % (i, i), computer_guid="COMP-%s" % (i,))) for i in range(1,5)
    ]
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
    self.tic()
    self._callOptimizeAlarm()

    #
    def generateZopePartitionDict(i):
      """
      Generate a configuration wich uses jinja2
      """
      partition_dict = ""
      for j in range(0,i):
        family_name = ['user', 'activity'][j%2]
Łukasz Nowak's avatar
Łukasz Nowak committed
1289
        partition_dict += '"%s-%s":{\n' %(family_name, node_list[j][0])
1290 1291
        partition_dict += ' "instance-count": {{ count }},\n'
        partition_dict += ' "family": "%s",\n' %family_name
Łukasz Nowak's avatar
Łukasz Nowak committed
1292
        partition_dict += ' "computer_guid": "%s"\n' %node_list[j][0]
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
        partition_dict += '}' 
        if j != i-1:
          partition_dict += ',\n'
        else:
          partition_dict += '\n'
      return partition_dict


    # Generate a test suite
    # -Generate a configuration adapted to the test node list length
    cluster_configuration = '{"zope-partition-dict":{\n'
    zope_partition_dict = ""
    for i in range(1, len(node_list)+1):
      zope_partition_dict += "{%% if count == %d %%}\n" %i
      zope_partition_dict += generateZopePartitionDict(i)
      zope_partition_dict += "{% endif %}\n"
    cluster_configuration += zope_partition_dict + '\n}}'
    # -Generate graph coordinate
    graph_coordinate = range(1, len(node_list)+1)
    # -Create the test suite
1313
    self._createTestSuite(quantity=1,priority=1, reference_correction=0,
1314 1315 1316 1317
                       specialise_value=self.scalability_distributor, portal_type="Scalability Test Suite",
                       graph_coordinate=graph_coordinate, cluster_configuration=cluster_configuration)
    self.tic()

Łukasz Nowak's avatar
Łukasz Nowak committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
#    # Master test node launch startTestSuite
#    for node in node_list:
#      if node.getMaster():
#        test_suite_title = self.scalability_distributor.startTestSuite(title=node.getTitle())
##        log("test_suite_title: %s" %test_suite_title)
#        break
#    # Get configuration list generated from test suite
##    configuration_list = self.scalability_distributor.generateConfiguration(test_suite_title)
#   
#    # logs
##    log(configuration_list)    
1329

1330 1331
  def test_19_testMultiDistributor(self):
    pass
1332

1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
  def test_20_TestSuite_Periodicity(self):
    revision = 'a=a,b=b,c=c'
    self.addCleanup(self.unpinDateTime)
    test_suite = self.test_suite_module.newContent(
      portal_type='Test Suite',
      title='Periodicity Enabled Test Suite',
      int_index=1,
      # periodicity enabled
      enabled=True,
      # periodicity configuration
      periodicity_day_frequency=1,
      periodicity_hour=(13,),
      periodicity_minute=(0,),
    )
    test_suite.validate()
    self.tic()

    self.pinDateTime(DateTime('2017/05/01 00:00:00 UTC'))
    self.assertEqual(None, test_suite.getAlarmDate())
    first_test_result, got_revision = self._createTestResult(revision=revision, test_title='Periodicity Enabled Test Suite')
    self.assertTrue(first_test_result.startswith('test_result_module/'))
    self.assertEqual(revision, got_revision)
    self.assertEqual(DateTime('2017/05/01 13:00:00 UTC'), test_suite.getAlarmDate())
    # Finish the current test run
    self.portal.restrictedTraverse(first_test_result).stop()
    self.tic()

    self.pinDateTime(DateTime('2017/05/01 14:00:00 UTC'))
    second_test_result, got_revision = self._createTestResult(revision=revision, test_title='Periodicity Enabled Test Suite')
    self.assertTrue(second_test_result.startswith('test_result_module/'))
    self.assertNotEqual(first_test_result, second_test_result)
    self.assertEqual(revision, got_revision)
    self.assertEqual(DateTime('2017/05/02 13:00:00 UTC'), test_suite.getAlarmDate())
    # Finish the current test run
    self.portal.restrictedTraverse(second_test_result).stop()
    self.tic()

    self.pinDateTime(DateTime('2017/05/02 14:00:00 UTC'))
    third_test_result, got_revision = self._createTestResult(revision=revision, test_title='Periodicity Enabled Test Suite')
    self.assertTrue(third_test_result.startswith('test_result_module/'))
    self.assertNotEqual(third_test_result, second_test_result)
    self.assertEqual(DateTime('2017/05/03 13:00:00 UTC'), test_suite.getAlarmDate())
    self.tic()

  def test_21_TestSuite_Periodicity_disabled(self):
    self.addCleanup(self.unpinDateTime)
    test_suite = self.test_suite_module.newContent(
      portal_type='Test Suite',
      title='Periodicity Disabled Test Suite',
      int_index=1,
      # periodicity disabled
      enabled=False,
      # periodicity configuration
      periodicity_day_frequency=1,
      periodicity_hour=(13,),
      periodicity_minute=(0,),
    )
    test_suite.validate()
    today = DateTime('2017/05/01')
    self.tic()

    self.pinDateTime(today)
    self.assertEqual(None, test_suite.getAlarmDate())
    self._createTestResult(test_title='Periodicity Disabled Test Suite')
    self.assertEqual(None, test_suite.getAlarmDate())
    self.tic()

    self._createTestResult(test_title='Periodicity Disabled Test Suite')
    self.assertEqual(None, test_suite.getAlarmDate())