|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Petrica |
|
5
|
|
|
* Date: 3/30/2016 |
|
6
|
|
|
* Time: 23:17 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Petrica\StatsdSystem\Tests\Command; |
|
9
|
|
|
|
|
10
|
|
|
use Domnikl\Statsd\Client; |
|
11
|
|
|
use Domnikl\Statsd\Connection\UdpSocket; |
|
12
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
|
13
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
|
14
|
|
|
use Petrica\StatsdSystem\Command\NotifyCommand; |
|
15
|
|
|
use Petrica\StatsdSystem\Gauge\CpuAverageGauge; |
|
16
|
|
|
use Petrica\StatsdSystem\Gauge\MemoryGauge; |
|
17
|
|
|
|
|
18
|
|
|
class NotifyCommandTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setUp(); |
|
23
|
|
|
|
|
24
|
|
|
vfsStreamWrapper::register(); |
|
25
|
|
|
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); |
|
26
|
|
|
|
|
27
|
|
|
// Create configuration yml |
|
28
|
|
|
$yml = <<<'DOC' |
|
29
|
|
|
gauges: |
|
30
|
|
|
cpu: |
|
31
|
|
|
class: Petrica\StatsdSystem\Gauge\CpuAverageGauge |
|
32
|
|
|
arguments: ~ |
|
33
|
|
|
|
|
34
|
|
|
memory: |
|
35
|
|
|
class: Petrica\StatsdSystem\Gauge\MemoryGauge |
|
36
|
|
|
DOC; |
|
37
|
|
|
file_put_contents('gauges.yml', $yml); |
|
38
|
|
|
|
|
39
|
|
|
$yml = <<<'DOC' |
|
40
|
|
|
gauges: |
|
41
|
|
|
cpu: |
|
42
|
|
|
class: Petrica\StatsdSystem\Gauge\CpuAverageGauge |
|
43
|
|
|
arguments: ~ |
|
44
|
|
|
|
|
45
|
|
|
memory: |
|
46
|
|
|
DOC; |
|
47
|
|
|
file_put_contents('wrong_gauges.yml', $yml); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetConfig() |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var NotifyCommand $command */ |
|
53
|
|
|
$command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand') |
|
54
|
|
|
->disableOriginalConstructor() |
|
55
|
|
|
->getMock(); |
|
56
|
|
|
|
|
57
|
|
|
$class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand'); |
|
58
|
|
|
$getConfig = $class->getMethod('getConfiguration'); |
|
59
|
|
|
$getConfig->setAccessible(true); |
|
60
|
|
|
|
|
61
|
|
|
$input = $this->getInputInterface(); |
|
62
|
|
|
|
|
63
|
|
|
$input->method('getArgument') |
|
64
|
|
|
->willReturn('gauges.yml'); |
|
65
|
|
|
|
|
66
|
|
|
$loader = $getConfig->invokeArgs($command, array($input)); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals(array( |
|
69
|
|
|
'cpu' => array( |
|
70
|
|
|
'class' => 'Petrica\StatsdSystem\Gauge\CpuAverageGauge', |
|
71
|
|
|
'arguments' => null |
|
72
|
|
|
), |
|
73
|
|
|
'memory' => array( |
|
74
|
|
|
'class' => 'Petrica\StatsdSystem\Gauge\MemoryGauge' |
|
75
|
|
|
) |
|
76
|
|
|
), $loader); |
|
77
|
|
|
|
|
78
|
|
|
$input = $this->getInputInterface(); |
|
79
|
|
|
|
|
80
|
|
|
$input->method('getArgument') |
|
81
|
|
|
->willReturn('wrong_gauges.yml'); |
|
82
|
|
|
|
|
83
|
|
|
// Test wrong configuration exception |
|
84
|
|
|
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException'); |
|
85
|
|
|
$getConfig->invokeArgs($command, array($input)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testGetGauges() |
|
89
|
|
|
{ |
|
90
|
|
|
$command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand') |
|
91
|
|
|
->disableOriginalConstructor() |
|
92
|
|
|
->getMock(); |
|
93
|
|
|
|
|
94
|
|
|
$config = array( |
|
95
|
|
|
'cpu' => array( |
|
96
|
|
|
'class' => 'Petrica\StatsdSystem\Gauge\CpuAverageGauge', |
|
97
|
|
|
'arguments' => array('test' => 1) |
|
98
|
|
|
), |
|
99
|
|
|
'memory' => array( |
|
100
|
|
|
'class' => 'Petrica\StatsdSystem\Gauge\MemoryGauge' |
|
101
|
|
|
) |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand'); |
|
105
|
|
|
$getGauges = $class->getMethod('getGauges'); |
|
106
|
|
|
$getGauges->setAccessible(true); |
|
107
|
|
|
|
|
108
|
|
|
$gauges = $getGauges->invokeArgs($command, array($config)); |
|
109
|
|
|
|
|
110
|
|
|
$expects = array( |
|
111
|
|
|
'cpu' => new CpuAverageGauge(), |
|
112
|
|
|
'memory' => new MemoryGauge() |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals($expects, $gauges); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testGetStatsd() |
|
119
|
|
|
{ |
|
120
|
|
|
$command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand') |
|
121
|
|
|
->disableOriginalConstructor() |
|
122
|
|
|
->getMock(); |
|
123
|
|
|
|
|
124
|
|
|
$input = $this->getInputInterface(); |
|
125
|
|
|
|
|
126
|
|
|
$map = array( |
|
127
|
|
|
[ 'statsd-host', 'localhost' ], |
|
128
|
|
|
[ 'statsd-port', '8125' ], |
|
129
|
|
|
[ 'statsd-namespace', 'test' ] |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
$input->expects($this->atLeastOnce()) |
|
133
|
|
|
->method('getOption') |
|
134
|
|
|
->will($this->returnValueMap($map)); |
|
135
|
|
|
|
|
136
|
|
|
$class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand'); |
|
137
|
|
|
$getStatsd = $class->getMethod('getStatsd'); |
|
138
|
|
|
$getStatsd->setAccessible(true); |
|
139
|
|
|
|
|
140
|
|
|
$statsd = $getStatsd->invokeArgs($command, array($input)); |
|
141
|
|
|
|
|
142
|
|
|
$connection = new UdpSocket('localhost', 8125); |
|
143
|
|
|
$expects = new Client($connection, 'test'); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals($expects, $statsd); |
|
146
|
|
|
|
|
147
|
|
|
// Not equal host |
|
148
|
|
|
$connection = new UdpSocket('localhost1', 8125); |
|
149
|
|
|
$expects = new Client($connection, 'test'); |
|
150
|
|
|
|
|
151
|
|
|
$statsd = $getStatsd->invokeArgs($command, array($input)); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertNotEquals($expects, $statsd); |
|
154
|
|
|
|
|
155
|
|
|
// Not equal port |
|
156
|
|
|
$connection = new UdpSocket('localhost', 8126); |
|
157
|
|
|
$expects = new Client($connection, 'test'); |
|
158
|
|
|
|
|
159
|
|
|
$statsd = $getStatsd->invokeArgs($command, array($input)); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertNotEquals($expects, $statsd); |
|
162
|
|
|
|
|
163
|
|
|
// Not equal namespace |
|
164
|
|
|
$connection = new UdpSocket('localhost', 8125); |
|
165
|
|
|
$expects = new Client($connection, 'test1'); |
|
166
|
|
|
|
|
167
|
|
|
$statsd = $getStatsd->invokeArgs($command, array($input)); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertNotEquals($expects, $statsd); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Return a mock object of the inputinterface |
|
174
|
|
|
* |
|
175
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getInputInterface() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->getMockBuilder('Symfony\Component\Console\Input\ArrayInput') |
|
180
|
|
|
->disableOriginalConstructor() |
|
181
|
|
|
->setMethods(array( |
|
182
|
|
|
'getArgument', |
|
183
|
|
|
'getOption' |
|
184
|
|
|
)) |
|
185
|
|
|
->getMock(); |
|
186
|
|
|
} |
|
187
|
|
|
} |