report_options: Syntax fix
[ninja.git] / test / unit_test / tests / orm_Test.php
blob9f67cb010fcaf6ed7cc2127b1033e8e13c41aa70
1 <?php
2 require_once ('op5/objstore.php');
3 require_once ('op5/livestatus.php');
4 class ORM_Test extends PHPUnit_Framework_TestCase {
6 /**
7 * The virtual environment this test is using
9 * @var array
11 protected $objects = array (
12 "commands" => array (
13 array ('id' => 1,'line' => "this is a command",'name' => 'cmda'),
14 array ('id' => 2,'line' => "this is a another command",
15 'name' => 'cmdb'),
16 array ('id' => 3,'line' => "this is a command again",
17 'name' => 'cmdc')),"services" => array (),"comments" => array (),
18 "hosts" => array (
19 array ('accept_passive_checks' => 1,'acknowledged' => 0,
20 'acknowledgement_type' => 0,
21 'action_url' => '/monitor/index.php/configuration/configure',
22 'action_url_expanded' => '/monitor/index.php/configuration/configure',
23 'active_checks_enabled' => 0,'address' => 'localhost',
24 'alias' => 'OP5 Monitor Server',
25 'check_command' => 'check-host-alive',
26 'check_flapping_recovery_notification' => 0,
27 'check_freshness' => 0,'check_interval' => 5,
28 'check_options' => 0,'check_period' => '24x7',
29 'check_source' => '','check_type' => 1,'checks_enabled' => 0,
30 'childs' => array (),'comments' => array (5),
31 'comments_with_info' => array (
32 array (5,'a_user','A little comment')),
33 'contact_groups' => array ('support-group'),
34 'contacts' => array ('monitor'),'current_attempt' => 3,
35 'current_notification_number' => 0,
36 'custom_variable_names' => array ('TYPE'),
37 'custom_variable_values' => array ('core'),
38 'custom_variables' => array ('TYPE' => 'core'),
39 'display_name' => 'monitor','downtimes' => array (),
40 'downtimes_with_info' => array (),'event_handler' => '',
41 'event_handler_enabled' => 1,'execution_time' => 0,
42 'filename' => '','first_notification_delay' => 0,
43 'flap_detection_enabled' => 1,
44 'groups' => array ('unix-servers','op5_monitor_servers',
45 'network'),'hard_state' => 2,'has_been_checked' => 1,
46 'high_flap_threshold' => 0,'hourly_value' => 36,
47 'icon_image' => 'op5eye.png',
48 'icon_image_alt' => 'OP5 Monitor Server',
49 'icon_image_expanded' => 'op5eye.png','id' => 3,
50 'in_check_period' => 1,'in_notification_period' => 1,
51 'initial_state' => 0,'is_executing' => 0,'is_flapping' => 0,
52 'last_check' => 1391082608,'last_hard_state' => 0,
53 'last_hard_state_change' => 1391082608,'last_notification' => 0,
54 'last_state' => 0,'last_state_change' => 1391082608,
55 'last_time_down' => 1381753813,'last_time_unreachable' => 0,
56 'last_time_up' => 1390393394,'latency' => 0.939,
57 'long_plugin_output' => 'Line one\\nLine two',
58 'low_flap_threshold' => 0,'max_check_attempts' => 3,
59 'modified_attributes' => 2,
60 'modified_attributes_list' => array ('active_checks_enabled'),
61 'name' => 'monitor','next_check' => 0,'next_notification' => 0,
62 'no_more_notifications' => 0,'notes' => '',
63 'notes_expanded' => '','notes_url' => '/',
64 'notes_url_expanded' => '/','notification_interval' => 0,
65 'notification_period' => '24x7','notifications_enabled' => 1,
66 'num_services' => 35,'num_services_crit' => 5,
67 'num_services_hard_crit' => 5,'num_services_hard_ok' => 26,
68 'num_services_hard_unknown' => 4,'num_services_hard_warn' => 0,
69 'num_services_ok' => 26,'num_services_pending' => 0,
70 'num_services_unknown' => 4,'num_services_warn' => 0,
71 'obsess' => 1,'obsess_over_host' => 1,'parents' => array (),
72 'pending_flex_downtime' => 0,'percent_state_change' => 0,
73 'perf_data' => '','plugin_output' => 'Never gonna make you cry,',
74 'pnpgraph_present' => 1,'process_performance_data' => 1,
75 'retry_interval' => 1,'scheduled_downtime_depth' => 0,
76 'services' => array ('op5backup state','Zombie processes',
77 'Users'),
78 'services_with_info' => array (
79 array ('op5backup state',2,1,'check_op5backup2 CRITICAL'),
80 array ('Zombie processes',0,1,'PROCS OK: 0 processes'),
81 array ('Users',0,1,'USERS OK - 1 users currently logged in')),
82 'services_with_state' => array (array ('op5backup state',2,1),
83 array ('Zombie processes',0,1),array ('Users',0,1)),
84 'should_be_scheduled' => 0,'state' => 2,'state_type' => 1,
85 'statusmap_image' => 'op5eye.png','total_services' => 35,
86 'worst_service_hard_state' => 2,'worst_service_state' => 2,
87 'x_3d' => 0,'y_3d' => 0,'z_3d' => 0)));
88 /**
89 * Contains a reference to the mocked livestatus, so we can see some debug
90 * variables
92 * @var MockLivestatus
94 protected $ls = false;
95 /**
96 * Make sure the enviornment is clean, and livestatus is mocked
98 public function setUp() {
99 op5objstore::instance()->mock_clear();
100 op5objstore::instance()->clear();
101 $this->ls = new MockLivestatus($this->objects);
102 op5objstore::instance()->mock_add('op5Livestatus', $this->ls);
105 * Remove mock environment
107 public function tearDown() {
108 op5objstore::instance()->mock_clear();
109 op5objstore::instance()->clear();
112 * Test that when you fetch all columns, you get all columns exported,
113 * including virtual columns
115 public function test_fetch_all() {
116 $orm_structure = Module_Manifest_Model::get('orm_structure');
118 $columns = array_merge(array_keys(Host_Model::$rewrite_columns),
119 array_keys($orm_structure['hosts']));
121 $this->fetch_and_test_single_host(false, $columns);
124 * Test that when requesting a couple of columns, that is the columns you
125 * will get
127 public function test_some_columns() {
128 $req_cols = array ('name','comments');
129 $exp_cols = array ('name','comments');
130 $this->fetch_and_test_single_host($req_cols, $exp_cols);
131 $this->assertContains('name', $this->ls->last_columns);
132 $this->assertContains('comments', $this->ls->last_columns);
135 * Test that nonexisting columns doesn't get fetched, and isn't exported
137 public function test_nonexisting_columns() {
138 $req_cols = array ('name','comments','boll');
139 $exp_cols = array ('name','comments');
140 $this->fetch_and_test_single_host($req_cols, $exp_cols);
141 $this->assertContains('name', $this->ls->last_columns);
142 $this->assertContains('comments', $this->ls->last_columns);
143 $this->assertNotContains('boll', $this->ls->last_columns);
147 * Test that when requesting a virtual column, that column is retreieved and
148 * exported, but no columns extra.
149 * Also verify that the dependent column actually is fetched
151 public function test_virtual_columns() {
152 $req_cols = array ('name','source_node');
153 $exp_cols = array ('name','source_node');
154 $this->fetch_and_test_single_host($req_cols, $exp_cols);
155 /* source_node is dependent on check_source, but isn't used in macros */
156 $this->assertContains('check_source', $this->ls->last_columns);
160 * Test whether sub object names is resolved, with correct virtual column rewriting
162 * We don't have any services in the test, but test that the correct columns is requested at least.
164 public function test_sub_object_columns() {
165 $req_cols = array ('description', 'host.name','state_text','host.state_text');
166 $exp_cols = array ('description', 'host_name','state','has_been_checked','host_state','host_has_been_checked');
167 ServicePool_Model::all()->it($req_cols);
168 /* source_node is dependent on check_source, but isn't used in macros */
169 sort($exp_cols);
170 $this->assertEquals($exp_cols, $this->ls->last_columns);
174 * Test column renaming, which is having different name in backend and frontend
176 public function test_renaming() {
177 $req_cols = array ('description', 'host.name','notes_url','host.action_url');
178 $exp_cols = array ('description', 'host_name','notes_url_expanded','host_action_url_expanded');
179 ServicePool_Model::all()->it($req_cols);
180 /* source_node is dependent on check_source, but isn't used in macros */
181 sort($exp_cols);
182 $this->assertEquals($exp_cols, $this->ls->last_columns);
186 * Test sub sub objects should only resolve one level of object abstraction
188 * For example comment -> service -> host should just resolve as comment -> host.
190 public function test_sub_sub_object_columns() {
191 $req_cols = array ('id','is_service','host.name','service.description','service.host.state');
192 $exp_cols = array ('id','is_service','host_name','service_description','host_state');
193 CommentPool_Model::all()->it($req_cols);
194 /* source_node is dependent on check_source, but isn't used in macros */
195 sort($exp_cols);
196 $this->assertEquals($exp_cols, $this->ls->last_columns);
200 * Test if key columns is fetched automatically for base object (not for related)
202 public function test_key_columns() {
203 $req_cols = array ('comment','host.state');
204 $exp_cols = array ('comment','host_state','id','is_service');
205 CommentPool_Model::all()->it($req_cols);
206 /* source_node is dependent on check_source, but isn't used in macros */
207 sort($exp_cols);
208 $this->assertEquals($exp_cols, $this->ls->last_columns);
212 * Test performance data processing.
214 * This doesn't actually test the ORM, but a helper entirely used by ORM
216 public function test_performance_data_conversion() {
217 $perf_data_str = "datasource=31 'Data Saucer'=32c;;;32;34 dattenSaucen=93%;~32:2;~3: invalid 'dd\'escaped'=13Gb;32";
218 $expect = array ('datasource' => array ('value' => 31.0),
219 'Data Saucer' => array ('value' => 32.0,'unit' => 'c','min' => 32.0,
220 'max' => 34.0),
221 'dattenSaucen' => array ('value' => 93.0,'unit' => '%',
222 'warn' => '~32:2','crit' => '~3:','min' => 0.0,'max' => 100.0),
223 'dd\'escaped' => array ('value' => 13.0,'unit' => 'Gb','warn' => '32'));
225 $perf_data = performance_data::process_performance_data(
226 $perf_data_str);
227 $this->assertSame($perf_data, $expect);
231 * @expectedException ORMException
232 * @expectedExceptionMessage Table 'hosts' has no column 'kaka'
234 public function test_sort_on_missing_column() {
235 HostPool_Model::all()->it(false, array('kaka'));
239 * Test that config url contains the host name
241 * This test depends on the configuration containing the macro $HOSTNAME$,
242 * and that config_url is the same with or without name field
244 public function test_host_config_url() {
245 $obj = HostPool_Model::all()->it(array ('config_url'))->current();
246 $config_url_single = $obj->get_config_url();
248 $obj = HostPool_Model::all()->it(array ('config_url','name'))->current();
249 $config_url_name = $obj->get_config_url();
251 $this->assertSame($config_url_single, $config_url_name);
253 $obj = HostPool_Model::all()->it(array ('name'))->current();
254 $host_name = $obj->get_name();
255 $this->assertTrue(
256 false !== strpos($config_url_single, urlencode($host_name)));
258 $this->assertNotEmpty($host_name);
262 * Fetch a host object, and test that when requesting a couple of columns,
263 * only the columns in an expect list is exported
265 * @param $req_cols array,
266 * or false for all
267 * @param $exp_cols array
269 private function fetch_and_test_single_host($req_cols, $exp_cols) {
270 $obj = HostPool_Model::all()->it($req_cols)->current()->export();
272 foreach ($exp_cols as $col) {
273 /* Also assert that rewrite columns exists */
274 $this->assertArrayHasKey($col, $obj);
275 unset($obj[$col]);
277 /* And when everything is removed, the array should be empty */
278 $this->assertEmpty($obj);