summaryrefslogtreecommitdiff
path: root/lib/launch_control/processed_builds_unittest.py
blob: 91443b6427d554e7b2aa26f31668640c817b9150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Test processed_builds."""

from __future__ import print_function

import os

from chromite.lib.launch_control import processed_builds
from chromite.lib import cros_test_lib


# Unitests often need access to internals of the thing they test.
# pylint: disable=protected-access

class ProcessedBuildsStorageTest(cros_test_lib.TempDirTestCase):
  """Test our helper library for storing processed build ids."""

  def setUp(self):
    self.testfile = os.path.join(self.tempdir, 'testfile.json')

  def testStartStop(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile):
      pass

    self.assertFileContents(self.testfile, '{}')

  def testFetchEmpty(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      self.assertEqual(ps.GetProcessedBuilds('branch', 'target'), [])

    self.assertFileContents(self.testfile, '{"branch": {"target": []}}')

  def testPurgeEmpty(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      ps.PurgeOldBuilds('branch', 'target', [1, 2, 3])

    self.assertFileContents(self.testfile, '{"branch": {"target": []}}')

  def testAddEmpty(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      ps.AddProcessedBuild('branch', 'target', 1)

    self.assertFileContents(self.testfile, '{"branch": {"target": [1]}}')

  def testMultipleUses(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      ps.AddProcessedBuild('branch', 'target', 1)
      ps.AddProcessedBuild('branch', 'target', 2)

    self.assertFileContents(self.testfile, '{"branch": {"target": [1, 2]}}')

    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      # Try adding twice, should only happen once.
      ps.AddProcessedBuild('branch', 'target', 3)
      ps.AddProcessedBuild('branch', 'target', 3)

    self.assertFileContents(self.testfile, '{"branch": {"target": [1, 2, 3]}}')

    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      ps.PurgeOldBuilds('branch', 'target', [2, 3])
      ps.AddProcessedBuild('branch', 'target', 4)

    self.assertFileContents(self.testfile, '{"branch": {"target": [2, 3, 4]}}')

    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      self.assertEqual(ps.GetProcessedBuilds('branch', 'target'), [2, 3, 4])

  def testAddMultipleBranchTargets(self):
    with processed_builds.ProcessedBuildsStorage(self.testfile) as ps:
      ps.AddProcessedBuild('branch1', 'target', 1)
      ps.AddProcessedBuild('branch2', 'target', 1)
      ps.AddProcessedBuild('branch2', 'target', 2)
      ps.AddProcessedBuild('branch2', 'target2', 3)

      self.assertEqual(ps.GetProcessedBuilds('branch2', 'target'),
                       [1, 2])

    self.assertFileContents(
        self.testfile,
        '{"branch1": {"target": [1]},'
        ' "branch2": {"target": [1, 2], "target2": [3]}}')