|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Component test for SizeRestrictedPackageCache |
|
5
|
|
|
* Writes to temporary locations in the filesystem |
|
6
|
|
|
*/ |
|
7
|
|
|
class SizeRestrictedPackageCacheTest extends SapphireTest { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
protected $tempPath = null; |
|
10
|
|
|
|
|
11
|
|
|
protected $cache, $gen, $log; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
public function setUp() { |
|
14
|
|
|
// We muck with the filesystem, create one folder that contains all the stuff we create, |
|
15
|
|
|
// and delete it entirely on tearDown() |
|
16
|
|
|
$this->tempPath = TEMP_FOLDER . '/PackageCacheTest-' . rand(1000000,9999999); |
|
17
|
|
|
mkdir($this->tempPath); |
|
18
|
|
|
|
|
19
|
|
|
$this->cache = new SizeRestrictedPackageCache; |
|
20
|
|
|
$this->cache->setCacheSize(3); |
|
21
|
|
|
mkdir($this->tempPath .'/cache'); |
|
22
|
|
|
$this->cache->setBaseDir($this->tempPath .'/cache'); |
|
23
|
|
|
|
|
24
|
|
|
$this->gen = new PackageCacheTest_MockGenerator; |
|
25
|
|
|
// To do: refactor so as not to be pipelinetest-specific (see also SimplePackageGeneratorTest) |
|
26
|
|
|
$this->log = new PipelineTest_MockLog(null); |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
public function tearDown() { |
|
30
|
|
|
if($this->tempPath) Filesystem::removeFolder($this->tempPath); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function testSizeRestriction() { |
|
35
|
|
|
// This testing relies on the fact that MockGenerator won't use the gitDir |
|
36
|
|
|
|
|
37
|
|
|
// 2 in each project doesn't rupture the cache, all files will exist |
|
38
|
|
|
$files = array(); |
|
39
|
|
|
$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log); |
|
40
|
|
|
// This is needed so that a.tar.gz is the oldest |
|
41
|
|
|
sleep(1); |
|
42
|
|
|
$files['project1.b'] = $this->cache->getPackageFilename($this->gen, 'project1', 'b', null, $this->log); |
|
43
|
|
|
$files['project2.a'] = $this->cache->getPackageFilename($this->gen, 'project2', 'a', null, $this->log); |
|
44
|
|
|
$files['project2.b'] = $this->cache->getPackageFilename($this->gen, 'project2', 'b', null, $this->log); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertFileExists($files['project1.a']); |
|
|
|
|
|
|
47
|
|
|
$this->assertFileExists($files['project1.b']); |
|
|
|
|
|
|
48
|
|
|
$this->assertFileExists($files['project2.a']); |
|
|
|
|
|
|
49
|
|
|
$this->assertFileExists($files['project2.b']); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
// 4 in the same project will mean the oldest is killed |
|
52
|
|
|
$files['project1.c'] = $this->cache->getPackageFilename($this->gen, 'project1', 'c', null, $this->log); |
|
53
|
|
|
$files['project1.d'] = $this->cache->getPackageFilename($this->gen, 'project1', 'd', null, $this->log); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertFileNotExists($files['project1.a']); |
|
|
|
|
|
|
56
|
|
|
$this->assertFileExists($files['project1.b']); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check that a subsequent re-fetch will mark the file as newer and so not garbage collect it |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testSizeRestrictionIsLastAccessed() { |
|
63
|
|
|
// 2 in each project doesn't rupture the cache, all files will exist |
|
64
|
|
|
$files = array(); |
|
65
|
|
|
$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log); |
|
66
|
|
|
sleep(1); |
|
67
|
|
|
$files['project1.b'] = $this->cache->getPackageFilename($this->gen, 'project1', 'b', null, $this->log); |
|
68
|
|
|
sleep(1); |
|
69
|
|
|
$files['project1.c'] = $this->cache->getPackageFilename($this->gen, 'project1', 'c', null, $this->log); |
|
70
|
|
|
// Re-fetch a, now B will be deleted before it |
|
71
|
|
|
$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log); |
|
72
|
|
|
$files['project1.d'] = $this->cache->getPackageFilename($this->gen, 'project1', 'd', null, $this->log); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertFileExists($files['project1.a']); |
|
|
|
|
|
|
75
|
|
|
$this->assertFileNotExists($files['project1.b']); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check that caching is actually happening |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testCacheDoesntRegenerateUnnecessarily() { |
|
82
|
|
|
$this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log); |
|
83
|
|
|
$this->assertTrue($this->gen->popWasCalled()); |
|
|
|
|
|
|
84
|
|
|
$this->cache->getPackageFilename($this->gen, 'project2', 'a', null, $this->log); |
|
85
|
|
|
$this->assertTrue($this->gen->popWasCalled()); |
|
|
|
|
|
|
86
|
|
|
$this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log); |
|
87
|
|
|
$this->assertFalse($this->gen->popWasCalled()); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Stub PackageGenerator that creates an empty file |
|
95
|
|
|
*/ |
|
96
|
|
|
class PackageCacheTest_MockGenerator extends PackageGenerator { |
|
|
|
|
|
|
97
|
|
|
protected $wasCalled = false; |
|
98
|
|
|
|
|
99
|
|
|
public function getIdentifier() { |
|
100
|
|
|
return ""; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function generatePackage($sha, $baseDir, $outputFilename, DeploynautLogFile $log) { |
|
104
|
|
|
touch($outputFilename); |
|
105
|
|
|
$this->wasCalled = true; |
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function popWasCalled() { |
|
110
|
|
|
if($this->wasCalled) { |
|
111
|
|
|
$this->wasCalled = false; |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.