Passed
Pull Request — master (#36)
by Jindun
02:06
created

ServiceTest::testUnvalidRequestMemoryPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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