1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/TestEnvironmentsLib |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace DataSift\Storyplayer\TestEnvironmentsLib; |
45
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\ConfigLib\WrappedConfig; |
47
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* the class for the config that represents a single test environment |
51
|
|
|
* |
52
|
|
|
* @category Libraries |
53
|
|
|
* @package Storyplayer/TestEnvironmentsLib |
54
|
|
|
* @author Stuart Herbert <[email protected]> |
55
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
56
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
57
|
|
|
* @link http://datasift.github.io/storyplayer |
58
|
|
|
*/ |
59
|
|
|
class TestEnvironmentConfig extends WrappedConfig |
60
|
|
|
{ |
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
parent::__construct(self::ROOT_IS_OBJECT); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function validateConfig() |
67
|
|
|
{ |
68
|
|
|
// do we have any config? |
69
|
|
|
$config = $this->getConfig(); |
70
|
|
|
|
71
|
|
|
// backwards-compatibility with 2.0.0-pre releases |
72
|
|
|
if (isset($config->{'0'})) { |
73
|
|
|
// convert to an object |
74
|
|
|
// |
75
|
|
|
// this is the documented format |
76
|
|
|
// |
77
|
|
|
// 2.0.0-pre had this file as an array of groups. we don't |
78
|
|
|
// want to break that for DataSift, who are actively using |
79
|
|
|
// it |
80
|
|
|
$tmp = new BaseObject; |
81
|
|
|
$tmp->groups = $config; |
82
|
|
|
$config = $tmp; |
83
|
|
|
$this->setData('', $config); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// do we have any defined groups? |
87
|
|
|
if (!isset($config->groups)) { |
88
|
|
|
throw new E4xx_TestEnvironmentNeedsGroups(); |
89
|
|
|
} |
90
|
|
|
foreach ($config->groups as $index => $group) { |
91
|
|
|
// what type of machines does this group define? |
92
|
|
|
if (!isset($group->type)) { |
93
|
|
|
throw new E4xx_TestEnvironmentGroupNeedsAType($index); |
94
|
|
|
} |
95
|
|
|
// is this a valid type? |
96
|
|
|
$this->validateGroupType($index, $group->type); |
97
|
|
|
|
98
|
|
|
// do we have any machines defined? |
99
|
|
|
if (!isset($group->details, $group->details->machines)) { |
100
|
|
|
throw new E4xx_TestEnvironmentGroupNeedsMachines($index); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// are the machines valid? |
104
|
|
|
foreach ($group->details->machines as $hostId => $machine) { |
105
|
|
|
// does each machine have a role? |
106
|
|
|
if (!isset($machine->roles)) { |
107
|
|
|
throw new E4xx_TestEnvironmentMachineNeedsARole($index, $hostId); |
108
|
|
|
} |
109
|
|
|
// is the roles an array? |
110
|
|
|
if (!is_array($machine->roles)) { |
111
|
|
|
throw new E4xx_TestEnvironmentMachineRolesMustBeArray($index, $hostId, gettype($machine->roles)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function validateGroupType($groupIndex, $typeName) |
118
|
|
|
{ |
119
|
|
|
$className = "DataSift\Storyplayer\HostLib\\" . $typeName; |
120
|
|
|
if (!class_exists($className)) { |
121
|
|
|
throw new E4xx_InvalidTestEnvironmentGroupType($groupIndex, $typeName, $className); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function mergeSystemUnderTestConfig($sutConfig) |
126
|
|
|
{ |
127
|
|
|
// do we have anything to merge? |
128
|
|
|
if (!$sutConfig->hasData('roles')) { |
129
|
|
|
// nothing to merge |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// get the list of roles |
134
|
|
|
$sutRoles = $sutConfig->getData('roles'); |
135
|
|
|
|
136
|
|
|
// get our config |
137
|
|
|
$envConfig = $this->getConfig(); |
138
|
|
|
|
139
|
|
|
foreach ($sutRoles as $sutRole) { |
140
|
|
|
foreach ($envConfig->groups as $envDetails) { |
141
|
|
|
foreach ($envDetails->details->machines as $machine) { |
142
|
|
|
if (in_array($sutRole->role, $machine->roles) || in_array('*', $machine->roles)) { |
143
|
|
|
if (!isset($machine->params)) { |
144
|
|
|
$machine->params = new BaseObject; |
145
|
|
|
} |
146
|
|
|
else if (! $machine->params instanceof BaseObject) { |
147
|
|
|
$tmp = new BaseObject; |
148
|
|
|
$tmp->mergeFrom($machine->params); |
149
|
|
|
$machine->params = $tmp; |
150
|
|
|
} |
151
|
|
|
$machine->params->mergeFrom($sutRole->params); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|