Completed
Pull Request — master (#36)
by Jindun
12:33
created

ServiceTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 140
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnknownEnvVariableTypePayload() 0 5 1
A testUnvalidRequestMemoryPattern() 0 7 1
A testUnknownVolumeTypePayload() 0 5 1
A testMissingServiceNamePayload() 0 5 1
A testValidPayload() 0 9 1
A testUnvalidRequestCpuPattern() 0 7 1
A testSettersAndAdders() 0 46 1
1
<?php
2
namespace TheAentMachine\Registry;
3
4
use PHPUnit\Framework\TestCase;
5
use TheAentMachine\Aenthill\CommonMetadata;
6
use TheAentMachine\Service\Exception\ServiceException;
7
use TheAentMachine\Service\Service;
8
9
class ServiceTest extends TestCase
10
{
11
    private const VALID_PAYLOAD = <<< 'JSON'
12
{
13
  "serviceName" : "foo",
14
  "service": {
15
    "image"         : "foo/bar:baz",
16
    "command"       : ["foo", "-bar", "-baz", "--qux"],
17
    "internalPorts" : [1, 2, 3],
18
    "dependsOn"     : ["foo", "bar"],
19
    "ports"         : [{"source": 80, "target": 8080}],
20
    "environment"   : {
21
                        "FOO": {"value": "foo", "type": "sharedEnvVariable"},
22
                        "BAR": {"value": "bar", "type": "sharedSecret"},
23
                        "BAZ": {"value": "baz", "type": "imageEnvVariable"},
24
                        "QUX": {"value": "qux", "type": "containerEnvVariable"}
25
                      },
26
    "labels"        : {
27
                        "foo": {"value": "fooo"},
28
                        "bar": {"value": "baar"}
29
                      },               
30
    "volumes"       : [
31
                        {"type": "volume", "source": "foo", "target": "/foo", "readOnly": true},
32
                        {"type": "bind", "source": "/bar", "target": "/bar", "readOnly": false},
33
                        {"type": "tmpfs", "source": "baz"}
34
                      ],
35
    "needVirtualHost": true,
36
    "needBuild": true
37
  },
38
  "dockerfileCommands": [
39
    "RUN composer install"
40
  ],
41
  "destEnvTypes": [
42
    "DEV"
43
  ],
44
  "requestMemory": "64Mi",
45
  "requestCpu": "250m",
46
  "limitMemory": "128Mi",
47
  "limitCpu": "500m"
48
}
49
JSON;
50
51
    private const MISSING_SERVICE_NAME_PAYLOAD = <<< 'JSON'
52
{
53
  "service": {
54
    "internalPorts": [80]
55
  }
56
}
57
JSON;
58
59
    private const UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD = <<< 'JSON'
60
{
61
  "serviceName": "foo",
62
  "service": {
63
    "environment": {
64
      "FOO": {
65
        "value": "fooo",
66
        "type": "YIKES_THATS_SOME_BAD_TYPE_HERE"
67
      }
68
    }
69
  }
70
}
71
JSON;
72
73
    private const UNKNOWN_VOLUME_TYPE_PAYLOAD = <<< 'JSON'
74
{
75
  "serviceName": "foo",
76
  "service": {
77
    "volumes": [
78
      {
79
        "type": "AGAIN?WTF",
80
        "source": "foo"
81
      }
82
    ]
83
  }
84
}
85
JSON;
86
87
    public function testValidPayload(): void
88
    {
89
        $array = \GuzzleHttp\json_decode(self::VALID_PAYLOAD, true);
90
        $service = Service::parsePayload($array);
91
        $out = $service->jsonSerialize();
92
        $this->assertEquals($array, $out);
93
        $this->assertTrue($service->isForDevEnvType());
94
        $this->assertFalse($service->isForTestEnvType());
95
        $this->assertFalse($service->isForProdEnvType());
96
    }
97
98
    public function testMissingServiceNamePayload(): void
99
    {
100
        $this->expectException(ServiceException::class);
101
        $array = \GuzzleHttp\json_decode(self::MISSING_SERVICE_NAME_PAYLOAD, true);
102
        Service::parsePayload($array)->jsonSerialize();
103
    }
104
105
    public function testUnknownEnvVariableTypePayload(): void
106
    {
107
        $this->expectException(ServiceException::class);
108
        $array = \GuzzleHttp\json_decode(self::UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD, true);
109
        Service::parsePayload($array)->jsonSerialize();
110
    }
111
112
    public function testUnknownVolumeTypePayload(): void
113
    {
114
        $this->expectException(ServiceException::class);
115
        $array = \GuzzleHttp\json_decode(self::UNKNOWN_VOLUME_TYPE_PAYLOAD, true);
116
        Service::parsePayload($array)->jsonSerialize();
117
    }
118
119
    public function testSettersAndAdders(): void
120
    {
121
        $s = new Service();
122
        $s->setServiceName('foo');
123
        $s->setImage('foo/bar:baz');
124
        $s->setCommand(['foo', '-bar', '-baz']);
125
        $s->addCommand('--qux');
126
        $s->setInternalPorts([1, 2]);
127
        $s->addInternalPort(3);
128
        $s->setDependsOn(['foo']);
129
        $s->addDependsOn('bar');
130
        $s->addPort(80, 8080);
131
        $s->addLabel('foo', 'fooo');
132
        $s->addLabel('bar', 'baar');
133
        $s->addSharedEnvVariable('FOO', 'foo');
134
        $s->addSharedSecret('BAR', 'bar');
135
        $s->addImageEnvVariable('BAZ', 'baz');
136
        $s->addContainerEnvVariable('QUX', 'qux');
137
        $s->addNamedVolume('foo', '/foo', true);
138
        $s->addBindVolume('/bar', '/bar', false);
139
        $s->addTmpfsVolume('baz');
140
        $s->addDockerfileCommand('RUN composer install');
141
        $s->setNeedVirtualHost(true);
142
        $s->setNeedBuild(true);
143
        $s->addDestEnvType(CommonMetadata::ENV_TYPE_DEV, true);
144
        $s->setRequestMemory('64Mi');
145
        $s->setRequestCpu('250m');
146
        $s->setLimitMemory('128Mi');
147
        $s->setLimitCpu('500m');
148
        $outArray = $s->jsonSerialize();
149
        $expectedArray = json_decode(self::VALID_PAYLOAD, true);
150
        $this->assertEquals($outArray, $expectedArray);
151
152
        $outArray = $s->imageJsonSerialize();
153
        $expectedArray = [
154
            'serviceName' => 'foo',
155
            'dockerfileCommands' => [
156
                'FROM foo/bar:baz',
157
                'ENV BAZ=baz',
158
                'COPY /bar /bar',
159
                'CMD foo -bar -baz --qux',
160
                'RUN composer install'
161
            ],
162
            'destEnvTypes' => ['DEV']
163
        ];
164
        $this->assertEquals($outArray, $expectedArray);
165
    }
166
167
    public function testUnvalidRequestMemoryPattern(): void
168
    {
169
        $s = new Service();
170
        $s->setServiceName('foo');
171
        $s->setRequestMemory('0.5Zi');
172
        $this->expectException(ServiceException::class);
173
        $s->jsonSerialize();
174
    }
175
176
    public function testUnvalidRequestCpuPattern(): void
177
    {
178
        $s = new Service();
179
        $s->setServiceName('foo');
180
        $s->setRequestCpu('0,1');
181
        $this->expectException(ServiceException::class);
182
        $s->jsonSerialize();
183
    }
184
}