generated from renoki-co/laravel-package-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathServiceTest.php
169 lines (134 loc) · 5.67 KB
/
ServiceTest.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace RenokiCo\PhpK8s\Test;
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
use RenokiCo\PhpK8s\Kinds\K8sService;
use RenokiCo\PhpK8s\ResourcesList;
class ServiceTest extends TestCase
{
public function test_service_build()
{
$svc = $this->cluster->service()
->setName('nginx')
->setLabels(['tier' => 'backend'])
->setAnnotations(['nginx/ann' => 'yes'])
->setSelectors(['app' => 'frontend'])
->addPorts([['protocol' => 'TCP', 'port' => 80, 'targetPort' => 80]])
->setPorts([['protocol' => 'TCP', 'port' => 80, 'targetPort' => 80]]);
$this->assertEquals('v1', $svc->getApiVersion());
$this->assertEquals('nginx', $svc->getName());
$this->assertEquals(['tier' => 'backend'], $svc->getLabels());
$this->assertEquals(['nginx/ann' => 'yes'], $svc->getAnnotations());
$this->assertEquals(['app' => 'frontend'], $svc->getSelectors());
$this->assertEquals([['protocol' => 'TCP', 'port' => 80, 'targetPort' => 80]], $svc->getPorts());
}
public function test_service_from_yaml()
{
$svc = $this->cluster->fromYamlFile(__DIR__.'/yaml/service.yaml');
$this->assertEquals('v1', $svc->getApiVersion());
$this->assertEquals('nginx', $svc->getName());
$this->assertEquals(['tier' => 'backend'], $svc->getLabels());
$this->assertEquals(['nginx/ann' => 'yes'], $svc->getAnnotations());
$this->assertEquals(['app' => 'frontend'], $svc->getSelectors());
$this->assertEquals([[
'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80,
]], $svc->getPorts());
}
public function test_service_api_interaction()
{
$this->runCreationTests();
$this->runGetAllTests();
$this->runGetTests();
$this->runUpdateTests();
$this->runWatchAllTests();
$this->runWatchTests();
$this->runDeletionTests();
}
public function runCreationTests()
{
$svc = $this->cluster->service()
->setName('nginx')
->setLabels(['tier' => 'backend'])
->setAnnotations(['nginx/ann' => 'yes'])
->setSelectors(['app' => 'frontend'])
->setPorts([
['protocol' => 'TCP', 'port' => 80, 'targetPort' => 80],
]);
$this->assertFalse($svc->isSynced());
$this->assertFalse($svc->exists());
$svc = $svc->createOrUpdate();
$this->assertTrue($svc->isSynced());
$this->assertTrue($svc->exists());
$this->assertInstanceOf(K8sService::class, $svc);
$this->assertEquals('v1', $svc->getApiVersion());
$this->assertEquals('nginx', $svc->getName());
$this->assertEquals(['tier' => 'backend'], $svc->getLabels());
$this->assertEquals(['nginx/ann' => 'yes'], $svc->getAnnotations());
$this->assertEquals(['app' => 'frontend'], $svc->getSelectors());
$this->assertEquals([[
'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80,
]], $svc->getPorts());
$this->assertEquals("{$svc->getName()}.{$svc->getNamespace()}.svc.cluster.local", $svc->getClusterDns());
}
public function runGetAllTests()
{
$services = $this->cluster->getAllServices();
$this->assertInstanceOf(ResourcesList::class, $services);
foreach ($services as $svc) {
$this->assertInstanceOf(K8sService::class, $svc);
$this->assertNotNull($svc->getName());
}
}
public function runGetTests()
{
$svc = $this->cluster->getServiceByName('nginx');
$this->assertInstanceOf(K8sService::class, $svc);
$this->assertTrue($svc->isSynced());
$this->assertEquals('v1', $svc->getApiVersion());
$this->assertEquals('nginx', $svc->getName());
$this->assertEquals(['tier' => 'backend'], $svc->getLabels());
$this->assertEquals(['nginx/ann' => 'yes'], $svc->getAnnotations());
$this->assertEquals(['app' => 'frontend'], $svc->getSelectors());
$this->assertEquals([[
'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80,
]], $svc->getPorts());
}
public function runUpdateTests()
{
$svc = $this->cluster->getServiceByName('nginx');
$this->assertTrue($svc->isSynced());
$svc->setAnnotations([]);
$svc->createOrUpdate();
$this->assertTrue($svc->isSynced());
$this->assertEquals('v1', $svc->getApiVersion());
$this->assertEquals('nginx', $svc->getName());
$this->assertEquals(['tier' => 'backend'], $svc->getLabels());
$this->assertEquals([], $svc->getAnnotations());
$this->assertEquals(['app' => 'frontend'], $svc->getSelectors());
$this->assertEquals([[
'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80,
]], $svc->getPorts());
}
public function runDeletionTests()
{
$service = $this->cluster->getServiceByName('nginx');
$this->assertTrue($service->delete());
$this->expectException(KubernetesAPIException::class);
$this->cluster->getSecretByName('nginx');
}
public function runWatchAllTests()
{
$watch = $this->cluster->service()->watchAll(function ($type, $service) {
if ($service->getName() === 'nginx') {
return true;
}
}, ['timeoutSeconds' => 10]);
$this->assertTrue($watch);
}
public function runWatchTests()
{
$watch = $this->cluster->service()->watchByName('nginx', function ($type, $service) {
return $service->getName() === 'nginx';
}, ['timeoutSeconds' => 10]);
$this->assertTrue($watch);
}
}