Skip to content

Commit 9e46c2d

Browse files
committed
Testing system improvements
- Consul agent start / stop no longer managed by travis - Added simple single-agent Consul manager
1 parent 0c39cad commit 9e46c2d

File tree

8 files changed

+146
-39
lines changed

8 files changed

+146
-39
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
/.idea
44
/*.iml
55
/composer.lock
6-
/tmp
6+
/tmp
7+
/consul.log
8+
/consul.pid

.travis.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,5 @@ install:
2121
- chmod +x $HOME/bin/consul
2222
- composer install --no-interaction --no-progress --no-suggest --optimize-autoloader
2323

24-
before_script:
25-
- ./tests/run_consul.sh
26-
2724
script:
2825
- ./vendor/bin/phpunit
29-
30-
after_script:
31-
- ./tests/stop_consul.sh

phpunit.local.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
stopOnFailure="true"
1212
processIsolation="false"
1313
backupGlobals="false"
14-
>
14+
>
15+
16+
<php>
17+
<env name="CONSUL_HTTP_ADDR" value="127.0.0.1:8500"/>
18+
<env name="CONSUL_HTTP_SSL" value="0"/>
19+
</php>
20+
1521
<testsuite name="definition">
1622
<directory>./tests/Definition</directory>
1723
<directory>./tests/Usage</directory>
@@ -24,8 +30,8 @@
2430
</filter>
2531

2632
<logging>
27-
<log type="coverage-html" target="./tmp/tests/html" />
28-
<log type="plain" target="./tmp/tests/php-consul-api-tests.log" />
33+
<log type="coverage-html" target="./tmp/tests/html"/>
34+
<log type="plain" target="./tmp/tests/php-consul-api-tests.log"/>
2935
</logging>
3036

3137
</phpunit>

phpunit.xml.dist

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
processIsolation="false"
1313
backupGlobals="false"
1414
>
15+
16+
<php>
17+
<env name="CONSUL_HTTP_ADDR" value="127.0.0.1:8500"/>
18+
<env name="CONSUL_HTTP_SSL" value="0"/>
19+
</php>
20+
1521
<testsuite name="definition">
1622
<directory>./tests/Definition</directory>
1723
<directory>./tests/Usage</directory>
@@ -24,7 +30,7 @@
2430
</filter>
2531

2632
<logging>
27-
<log type="plain" target="./tmp/tests/php-consul-api-tests.log" />
33+
<log type="plain" target="./tmp/tests/php-consul-api-tests.log"/>
2834
</logging>
2935

3036
</phpunit>

tests/ConsulManager.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace DCarbone\PHPConsulAPITests;
2+
3+
/*
4+
Copyright 2016-2017 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/**
20+
* Class ConsulManager
21+
* @package DCarbone\PHPConsulAPITests
22+
*/
23+
abstract class ConsulManager {
24+
25+
const PID_FILE = __DIR__ . '/../consul.pid';
26+
27+
const START_SINGLE_CMD = __DIR__ . '/run_consul.sh';
28+
const STOP_SINGLE_CMD = __DIR__ . '/stop_consul.sh';
29+
30+
/**
31+
* Start a single instance of a consul agent in -dev mode
32+
*/
33+
public static function startSingle() {
34+
if (file_exists(self::PID_FILE)) {
35+
self::stopSingle();
36+
}
37+
38+
shell_exec(self::START_SINGLE_CMD);
39+
40+
if (file_exists(self::PID_FILE)) {
41+
echo "\nconsul pid: " . file_get_contents(self::PID_FILE) . "\n";
42+
} else {
43+
echo "\nunable to locate consul.pid file!\n";
44+
}
45+
46+
// sleep to allow consul to setup
47+
sleep(5);
48+
}
49+
50+
/**
51+
* Stop running instance
52+
*/
53+
public static function stopSingle() {
54+
if (file_exists(self::PID_FILE)) {
55+
shell_exec(self::STOP_SINGLE_CMD);
56+
if (file_exists(self::PID_FILE)) {
57+
unlink(self::PID_FILE);
58+
}
59+
sleep(2);
60+
}
61+
}
62+
}

tests/Usage/ConfigUsageTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use DCarbone\PHPConsulAPI\Config;
44
use DCarbone\PHPConsulAPI\Consul;
5+
use DCarbone\PHPConsulAPITests\ConsulManager;
56

67
/*
78
Copyright 2016-2017 Daniel Carbone ([email protected])
@@ -27,6 +28,14 @@ class ConfigUsageTest extends \PHPUnit_Framework_TestCase {
2728
const DEFAULT_ADDRESS = '127.0.0.1:8500';
2829
const DEFAULT_SCHEME = 'http';
2930

31+
protected function setUp() {
32+
ConsulManager::startSingle();
33+
}
34+
35+
protected function tearDown() {
36+
ConsulManager::stopSingle();
37+
}
38+
3039
/**
3140
* @return Config
3241
*/
@@ -42,8 +51,8 @@ public function testCanConstructConfig() {
4251
public function testConfigDefaults() {
4352
$config = new Config();
4453

45-
$expectedAddress = getenv(Consul::HTTPAddrEnvName) ?: self::DEFAULT_ADDRESS;
46-
$expectedScheme = (bool)getenv(Consul::HTTPSSLEnvName) ? 'https' : self::DEFAULT_SCHEME;
54+
$expectedAddress = $_ENV[Consul::HTTPAddrEnvName] ?: self::DEFAULT_ADDRESS;
55+
$expectedScheme = $_ENV[Consul::HTTPSSLEnvName] ? 'https' : self::DEFAULT_SCHEME;
4756

4857
$this->assertEquals($expectedAddress,
4958
$config->getAddress(),

tests/Usage/KV/KVClientUsageTest.php

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,82 @@
2121
use DCarbone\PHPConsulAPI\KV\KVPair;
2222
use DCarbone\PHPConsulAPI\QueryMeta;
2323
use DCarbone\PHPConsulAPI\WriteMeta;
24+
use DCarbone\PHPConsulAPITests\ConsulManager;
2425

2526
/**
2627
* Class KVClientUsageTest
2728
* @package DCarbone\PHPConsulAPITests\Usage\KV
2829
*/
2930
class KVClientUsageTest extends \PHPUnit_Framework_TestCase {
31+
32+
const KVKey = 'testkey';
33+
const KVValue = 'testvalue';
34+
35+
/**
36+
* Sets up the fixture, for example, open a network connection.
37+
* This method is called before a test is executed.
38+
*/
39+
protected function setUp() {
40+
ConsulManager::startSingle();
41+
}
42+
43+
/**
44+
* Tears down the fixture, for example, close a network connection.
45+
* This method is called after a test is executed.
46+
*/
47+
protected function tearDown() {
48+
ConsulManager::stopSingle();
49+
}
50+
3051
/**
3152
* @return KVClient
3253
*/
3354
public function testCanConstructClient() {
34-
$kv = new KVClient(Config::newDefaultConfig());
55+
$kv = new KVClient(new Config());
3556
$this->assertInstanceOf(KVClient::class, $kv);
3657
return $kv;
3758
}
3859

3960
/**
4061
* @depends testCanConstructClient
41-
* @param KVClient $client
4262
*/
43-
public function testKVLifecycle(KVClient $client) {
44-
$kvp = new KVPair(['Key' => 'testkey', 'Value' => 'testvalue']);
63+
public function testCanPutKey() {
64+
$client = new KVClient(new Config());
4565

46-
list($wm, $err) = $client->put($kvp);;
66+
list($wm, $err) = $client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue]));
4767
$this->assertNull($err, sprintf('Unable to set kvp: %s', (string)$err));
4868
$this->assertInstanceOf(WriteMeta::class, $wm);
69+
}
4970

50-
list($kvp, $qm, $err) = $client->get('testkey');
51-
$this->assertNull($err, sprintf('Unable to get kvp: %s', (string)$err));
71+
/**
72+
* @depends testCanPutKey
73+
*/
74+
public function testCanGetKey() {
75+
$client = new KVClient(new Config());
76+
$client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue]));
77+
78+
list($kv, $qm, $err) = $client->get(self::KVKey);
79+
$this->assertNull($err, sprintf('KV::get returned error: %s', (string)$err));
5280
$this->assertInstanceOf(QueryMeta::class, $qm);
53-
$this->assertInstanceOf(KVPair::class, $kvp);
54-
$this->assertEquals('testvalue', $kvp->Value);
81+
$this->assertInstanceOf(KVPair::class, $kv);
82+
}
5583

56-
list($wm, $err) = $client->delete('testkey');
57-
$this->assertNull($err, sprintf('Unable to delete kvp: %s', (string)$err));
58-
$this->assertInstanceOf(WriteMeta::class, $wm);
84+
/**
85+
* @depends testCanPutKey
86+
*/
87+
public function testCanDeleteKey() {
88+
$client = new KVClient(new Config());
89+
$client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue]));
5990

60-
list($kvp, $qm, $err) = $client->get('testkey');
61-
$this->assertNull($err);
62-
$this->assertInstanceOf(QueryMeta::class, $qm);
63-
$this->assertNull($kvp);
91+
list($wm, $err) = $client->delete(self::KVKey);
92+
$this->assertNull($err, sprintf('KV::delete returned error: %s', $err));
93+
$this->assertInstanceOf(
94+
WriteMeta::class,
95+
$wm,
96+
sprintf(
97+
'expected "%s", saw "%s"',
98+
WriteMeta::class,
99+
is_object($wm) ? get_class($wm) : gettype($wm)
100+
));
64101
}
65102
}

tests/run_consul.sh

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
#!/usr/bin/env bash
22

3-
${HOME}/bin/consul agent -dev &
4-
5-
PID=$!
6-
7-
echo "consul pid: ${PID}"
8-
9-
echo ${PID} > consul.pid
10-
echo "sleeping for 10 seconds to give consul time to set up..."
11-
sleep 10
12-
3+
/usr/bin/env consul agent -dev >> consul.log 2>&1 & echo $! > consul.pid

0 commit comments

Comments
 (0)