|
| 1 | +from __future__ import absolute_import, division, unicode_literals |
| 2 | + |
| 3 | +import os |
| 4 | +import datetime |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +class TagExpiryTestCase(unittest.TestCase): |
| 11 | + repodir = None |
| 12 | + t3 = datetime.datetime.now()-datetime.timedelta(days=3) |
| 13 | + t7 = datetime.datetime.now()-datetime.timedelta(days=7) |
| 14 | + t10 = datetime.datetime.now()-datetime.timedelta(days=10) |
| 15 | + |
| 16 | + def _cmd(self, *args, **kwds): |
| 17 | + kwds.setdefault('cwd', self.repodir) |
| 18 | + return subprocess.check_output(args, **kwds) |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + self.repodir = tempfile.mkdtemp() |
| 22 | + self._cmd('git', 'init') |
| 23 | + self._cmd('git', 'config', 'user.name', 'Test') |
| 24 | + self._cmd('git', 'config', 'user.email', 'test@localhost') |
| 25 | + self._cmd('git', 'commit', '--allow-empty', '-m', 'Initial commit') |
| 26 | + |
| 27 | + # Create annotated tag seven days ago with defaults from git-gau-at. |
| 28 | + branch = self._cmd('git', 'rev-parse', '--abbrev-ref', '--symbolic', 'HEAD') |
| 29 | + datestr = self.t7.astimezone(datetime.timezone.utc).strftime("%Y%m%dT%H%M%SZ") |
| 30 | + tagname = "/".join(['build', branch.rstrip().decode(), datestr]) |
| 31 | + |
| 32 | + env = os.environ.copy() |
| 33 | + env.update({ |
| 34 | + 'GIT_COMMITTER_DATE': self.t7.isoformat() |
| 35 | + }) |
| 36 | + self._cmd('git', 'tag', '--annotate', '--message', 'Most recent build', tagname, env=env) |
| 37 | + |
| 38 | + # Create another tag three days ago with different pattern. |
| 39 | + env = os.environ.copy() |
| 40 | + env.update({ |
| 41 | + 'GIT_COMMITTER_DATE': self.t3.isoformat() |
| 42 | + }) |
| 43 | + self._cmd('git', 'tag', '--annotate', '--message', 'Most recent release', 'release/v2.0.3', env=env) |
| 44 | + |
| 45 | + # Create another tag ten days ago with different pattern. |
| 46 | + env = os.environ.copy() |
| 47 | + env.update({ |
| 48 | + 'GIT_COMMITTER_DATE': self.t10.isoformat() |
| 49 | + }) |
| 50 | + self._cmd('git', 'tag', '--annotate', '--message', 'Most recent release', 'release/v2.0.2') |
| 51 | + |
| 52 | + def tearDown(self): |
| 53 | + if self.repodir is not None: |
| 54 | + shutil.rmtree(self.repodir) |
| 55 | + |
| 56 | + def testExpirySimple(self): |
| 57 | + """ |
| 58 | + Static tag with simple script resulting in a message. |
| 59 | + """ |
| 60 | + |
| 61 | + script = 'echo Build expired!' |
| 62 | + |
| 63 | + branch = self._cmd('git', 'rev-parse', '--abbrev-ref', '--symbolic', 'HEAD') |
| 64 | + pattern = '/'.join(['build', branch.rstrip().decode(), '*']) |
| 65 | + |
| 66 | + # with ttl=two days, no expiry expected |
| 67 | + d2 = int(datetime.timedelta(days=2).total_seconds()) |
| 68 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d2), '/bin/sh', '-c', script) |
| 69 | + self.assertEqual(output, b'') |
| 70 | + |
| 71 | + # with ttl=six days, no expiry expected |
| 72 | + d6 = int(datetime.timedelta(days=6).total_seconds()) |
| 73 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d6), '/bin/sh', '-c', script) |
| 74 | + self.assertEqual(output, b'') |
| 75 | + |
| 76 | + # with ttl=eight days, expiry expected |
| 77 | + d8 = int(datetime.timedelta(days=8).total_seconds()) |
| 78 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d8), '/bin/sh', '-c', script) |
| 79 | + self.assertEqual(output, b'Build expired!\n') |
| 80 | + |
| 81 | + def testExpiryBranchPlaceholder(self): |
| 82 | + """ |
| 83 | + Tag pattern with branch placeholder with simple script resulting in a message. |
| 84 | + """ |
| 85 | + |
| 86 | + script = 'echo Build expired!' |
| 87 | + |
| 88 | + pattern = 'build/%b/*' |
| 89 | + |
| 90 | + # with ttl=two days, no expiry expected |
| 91 | + d2 = int(datetime.timedelta(days=2).total_seconds()) |
| 92 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d2), '/bin/sh', '-c', script) |
| 93 | + self.assertEqual(output, b'') |
| 94 | + |
| 95 | + # with ttl=six days, no expiry expected |
| 96 | + d6 = int(datetime.timedelta(days=6).total_seconds()) |
| 97 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d6), '/bin/sh', '-c', script) |
| 98 | + self.assertEqual(output, b'') |
| 99 | + |
| 100 | + # with ttl=eight days, expiry expected |
| 101 | + d8 = int(datetime.timedelta(days=8).total_seconds()) |
| 102 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d8), '/bin/sh', '-c', script) |
| 103 | + self.assertEqual(output, b'Build expired!\n') |
| 104 | + |
| 105 | + def testExpiryNoMatchingTag(self): |
| 106 | + """ |
| 107 | + Static tag with simple script resulting in a message. |
| 108 | + """ |
| 109 | + |
| 110 | + script = 'echo Build expired!' |
| 111 | + |
| 112 | + pattern = 'no-matching-category/*' |
| 113 | + |
| 114 | + # with ttl=two days, expiry expected |
| 115 | + d2 = int(datetime.timedelta(days=2).total_seconds()) |
| 116 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d2), '/bin/sh', '-c', script) |
| 117 | + self.assertEqual(output, b'Build expired!\n') |
| 118 | + |
| 119 | + # with ttl=six days, expiry expected |
| 120 | + d6 = int(datetime.timedelta(days=6).total_seconds()) |
| 121 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d6), '/bin/sh', '-c', script) |
| 122 | + self.assertEqual(output, b'Build expired!\n') |
| 123 | + |
| 124 | + # with ttl=eight days, expiry expected |
| 125 | + d8 = int(datetime.timedelta(days=8).total_seconds()) |
| 126 | + output = self._cmd('git', 'gau-tag-expiry', pattern, str(d8), '/bin/sh', '-c', script) |
| 127 | + self.assertEqual(output, b'Build expired!\n') |
| 128 | + |
| 129 | + def testFailOnExitStatusNonZero(self): |
| 130 | + """ |
| 131 | + Command with non-zero status will propagate. |
| 132 | + """ |
| 133 | + |
| 134 | + script = 'echo Expiry check failed; /bin/false' |
| 135 | + self.assertRaises(subprocess.CalledProcessError, self._cmd, |
| 136 | + 'git', 'gau-tag-expiry', '*', '-c', script) |
0 commit comments