call('template.create', [ 'host' => self::TEMPLATE_NAME_PRE . "_" . $i, 'groups' => [ 'groupid' => 1 ]]); $ep = json_encode($response, JSON_PRETTY_PRINT); $this->assertArrayHasKey('templateids', $response['result'], $ep); $this->assertArrayHasKey(0, $response['result']['templateids'], $ep); array_push(self::$templateids, $response['result']['templateids'][0]); } } public function setupActions() { $response = $this->call('action.create', [ 'name' => 'create_host', 'eventsource' => EVENT_SOURCE_AUTOREGISTRATION, 'status' => 0, 'operations' => [ [ 'operationtype' => 2 ] ] ] ); $ep = json_encode($response, JSON_PRETTY_PRINT); $this->assertArrayHasKey('actionids', $response['result'], $ep); $this->assertEquals(1, count($response['result']['actionids']), $ep); $templateids_for_api_call = []; foreach (self::$templateids as $entry) { $t = ['templateid' => $entry]; array_push($templateids_for_api_call, $t); } $response = $this->call('action.create', [ 'name' => 'link_templates', 'eventsource' => 2, 'status' => 0, 'operations' => [ [ 'operationtype' => 6, 'optemplate' => $templateids_for_api_call ] ] ] ); $this->assertArrayHasKey('actionids', $response['result']); $this->assertEquals(1, count($response['result']['actionids'])); } /** * @inheritdoc */ public function prepareData() { $z = 'a'; for ($i = 0; $i < self::NUMBER_OF_TEMPLATES * self::NUMBER_OF_TRIGGERS_PER_TEMPLATE; $i++) { array_push(self::$stringids, $z); $z++; } sort(self::$stringids); $this->createTemplates(); for ($i = 0; $i < self::NUMBER_OF_TEMPLATES * self::NUMBER_OF_TRIGGERS_PER_TEMPLATE; $i++) { $templ_counter = floor($i / self::NUMBER_OF_TEMPLATES); $templateid_loc = self::$templateids[$templ_counter]; $response = $this->call('item.create', [ 'hostid' => $templateid_loc, 'name' => self::ITEM_NAME_PRE . "_" . self::$stringids[$i], 'key_' => self::ITEM_KEY_PRE . "_" . self::$stringids[$i], 'type' => ITEM_TYPE_TRAPPER, 'value_type' => ITEM_VALUE_TYPE_UINT64 ]); $this->assertArrayHasKey('itemids', $response['result']); $this->assertEquals(1, count($response['result']['itemids'])); $response = $this->call('trigger.create', [ 'description' => self::TRIGGER_DESCRIPTION_PRE . "_" . self::$stringids[$i], 'priority' => self::TRIGGER_PRIORITY, 'status' => self::TRIGGER_STATUS, 'comments' => self::TRIGGER_COMMENTS_PRE . "_" . self::$stringids[$i], 'url' => self::TRIGGER_URL_PRE . "_" . self::$stringids[$i], 'url_name' => self::TRIGGER_URL_NAME_PRE . "_" . self::$stringids[$i], 'type' => self::TRIGGER_TYPE, 'recovery_mode' => self::TRIGGER_RECOVERY_MODE, 'correlation_mode' => self::TRIGGER_CORRELATION_MODE, 'correlation_tag' => self::TRIGGER_CORRELATION_TAG_PRE . "_" . self::$stringids[$i], 'manual_close' => self::TRIGGER_MANUAL_CLOSE, 'opdata' => self::TRIGGER_OPDATA_PRE . "_" . self::$stringids[$i], 'event_name' => self::TRIGGER_EVENT_NAME_PRE . "_" . self::$stringids[$i], 'expression' => 'last(/' . self::TEMPLATE_NAME_PRE . "_" . $templ_counter . '/' . self::ITEM_KEY_PRE . "_" . self::$stringids[$i] . ')=2', 'recovery_expression' => 'last(/' . self::TEMPLATE_NAME_PRE . "_" . $templ_counter . '/' . self::ITEM_KEY_PRE . "_" . self::$stringids[$i] . ')=3', 'tags' => [ [ 'tag' => self::TAG_NAME_PRE . "_" . self::$stringids[$i], 'value' => self::TAG_VALUE_PRE . "_" . self::$stringids[$i] ] ] ]); $this->assertArrayHasKey('triggerids', $response['result']); $this->assertArrayHasKey(0, $response['result']['triggerids']); } $this->setupActions(); return true; } /** * Component configuration provider for server related tests. * * @return array */ public function serverConfigurationProvider() { return [ self::COMPONENT_SERVER => [ 'DebugLevel' => 4, 'LogFileSize' => 0, 'LogFile' => self::getLogPath(self::COMPONENT_SERVER), 'PidFile' => PHPUNIT_COMPONENT_DIR.'zabbix_server.pid', 'SocketDir' => PHPUNIT_COMPONENT_DIR, 'ListenPort' => self::getConfigurationValue(self::COMPONENT_SERVER, 'ListenPort', 10051) ] ]; } /** * Component configuration provider for agent related tests. * * @return array */ public function agentConfigurationProvider() { return [ self::COMPONENT_AGENT => [ 'Hostname' => self::HOST_NAME, 'ServerActive' => '127.0.0.1:'.self::getConfigurationValue(self::COMPONENT_SERVER, 'ListenPort', 10051), 'DebugLevel' => 4, 'LogFileSize' => 0, 'LogFile' => self::getLogPath(self::COMPONENT_AGENT), 'PidFile' => PHPUNIT_COMPONENT_DIR.'zabbix_agent.pid' ] ]; } public function checkTriggersCreate() { $response = $this->callUntilDataIsPresent('host.get', ['filter' => ['host' => self::HOST_NAME]], 10, 2); $this->assertArrayHasKey(0, $response['result'], json_encode($response, JSON_PRETTY_PRINT)); $this->assertArrayHasKey('host', $response['result'][0]); $response = $this->callUntilDataIsPresent('trigger.get', [ 'selectTags' => 'extend', 'filter' => [ 'host' => self::HOST_NAME ], 'output' => [ 'triggerid', 'description', 'priority', 'status', 'templateid', 'comments', 'url', 'url_name', 'type', 'flags', 'recovery_mode', 'correlation_mode', 'correlation_tag', 'manual_close', 'opdata', 'discover', 'event_name', 'functions', 'expression', 'recovery_expression' ], 'selectFunctions' => 'extend', 'sortfield' => 'description' ], 10, 2); $this->assertEquals(self::NUMBER_OF_TEMPLATES * self::NUMBER_OF_TRIGGERS_PER_TEMPLATE, count($response['result'])); $i = 0; foreach ($response['result'] as $entry) { $ep = json_encode($entry, JSON_PRETTY_PRINT); $this->assertArrayHasKey('tags', $entry, $ep); $this->assertArrayHasKey(0, $entry['tags'], $ep); $this->assertArrayHasKey('tag', $entry['tags'][0], $ep); $this->assertEquals(self::TAG_NAME_PRE . "_" . self::$stringids[$i], $entry['tags'][0]['tag'], $ep); $this->assertEquals($entry['description'], self::TRIGGER_DESCRIPTION_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['priority'], self::TRIGGER_PRIORITY, $ep); $this->assertEquals($entry['status'], self::TRIGGER_STATUS, $ep); $this->assertEquals($entry['comments'], self::TRIGGER_COMMENTS_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['url'], self::TRIGGER_URL_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['url_name'], self::TRIGGER_URL_NAME_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['type'], self::TRIGGER_TYPE, $ep); $this->assertEquals($entry['recovery_mode'], self::TRIGGER_RECOVERY_MODE, $ep); $this->assertEquals($entry['correlation_mode'], self::TRIGGER_CORRELATION_MODE, $ep); $this->assertEquals($entry['correlation_tag'], self::TRIGGER_CORRELATION_TAG_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['manual_close'], self::TRIGGER_MANUAL_CLOSE, $ep); $this->assertEquals($entry['opdata'], self::TRIGGER_OPDATA_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['event_name'], self::TRIGGER_EVENT_NAME_PRE . "_" . self::$stringids[$i], $ep); $this->assertEquals($entry['functions'][0]['parameter'], '$', $ep); $this->assertEquals($entry['functions'][0]['function'], 'last', $ep); $this->assertEquals($entry['expression'], "{{$entry['functions'][0]['functionid']}}=2", $ep); $this->assertEquals($entry['recovery_expression'], "{{$entry['functions'][0]['functionid']}}=3", $ep); $i++; } } /** * Test trigger linking cases. * * @configurationDataProvider agentConfigurationProvider * @required-components server, agent */ public function testTriggerLinking_checkMe() { $this->reloadConfigurationCache(); $this->checkTriggersCreate(); } }