call('host.get', [ 'selectTags' => ['tag', 'value'] ]); $this->assertArrayHasKey('result', $response, 'Failed to autoregister host before timeout'); $this->assertCount(1, $response['result'], 'Failed to autoregister host before timeout'); $this->assertArrayHasKey('tags', $response['result'][0], 'Failed to autoregister host before timeout'); $autoregHost = $response['result'][0]; $this->assertArrayHasKey('hostid', $autoregHost, 'Failed to get host ID of the autoregistered host'); $tags = $autoregHost['tags']; $this->assertCount(count($expectedTags), $tags, 'Unexpected tags count was detected'); foreach ($expectedTags as $tag) { $this->assertContains($tag, $tags); } break; } catch (Exception $e) { if ($i == $max_attempts - 1) throw $e; else sleep($sleep_time); } } } /** * Component configuration provider for agent related tests. * * @return array */ public function agentConfigurationProvider() { return [ self::COMPONENT_AGENT => [ 'Hostname' => self::COMPONENT_AGENT, 'ServerActive' => '127.0.0.1:'.self::getConfigurationValue(self::COMPONENT_SERVER, 'ListenPort'), 'HostMetadata' => self::$HOST_METADATA ] ]; } /** * @inheritdoc */ public function prepareData() { $response = $this->call('host.get', []); $hostids = array(); foreach ($response['result'] as $host) { $hostids[] = $host['hostid']; } $this->call('host.delete', $hostids); $response = $this->call('host.get', []); $this->assertArrayHasKey('result', $response, 'Failed to clear existing hosts during test setup'); $this->assertCount(0, $response['result'], 'Failed to clear existing hosts during test setup'); $response = $this->call('action.create', [ [ 'name' => self::AUTOREG_ACTION_NAME1, 'eventsource' => EVENT_SOURCE_AUTOREGISTRATION, 'status' => ACTION_STATUS_ENABLED, 'filter' => [ 'conditions' => [ [ 'conditiontype' => CONDITION_TYPE_HOST_NAME, 'operator' => CONDITION_OPERATOR_LIKE, 'value' => self::COMPONENT_AGENT ], [ 'conditiontype' => CONDITION_TYPE_HOST_METADATA, 'operator' => CONDITION_OPERATOR_LIKE, 'value' => self::HOST_METADATA1 ] ], 'evaltype' => CONDITION_EVAL_TYPE_AND_OR ], 'operations' => [ /* OPERATION_TYPE_HOST_ADD is intentionally missing. It is expected to be run by */ /* Zabbix server, because OPERATION_TYPE_HOST_TAGS_ADD is present. */ [ 'operationtype' => OPERATION_TYPE_HOST_TAGS_ADD, 'optag' => [ [ 'tag' => 'a1', 'value' => 'autoreg 1' ], [ 'tag' => 'tag1', 'value' => 'value 1' ] ] ], [ 'operationtype' => OPERATION_TYPE_HOST_TAGS_REMOVE, 'optag' => [ [ 'tag' => 'a2', 'value' => 'autoreg 2' ], [ 'tag' => 'tag2', 'value' => 'value 2' ] ] ] ] ], [ 'name' => self::AUTOREG_ACTION_NAME2, 'eventsource' => EVENT_SOURCE_AUTOREGISTRATION, 'status' => ACTION_STATUS_ENABLED, 'filter' => [ 'conditions' => [ [ 'conditiontype' => CONDITION_TYPE_HOST_NAME, 'operator' => CONDITION_OPERATOR_LIKE, 'value' => self::COMPONENT_AGENT ], [ 'conditiontype' => CONDITION_TYPE_HOST_METADATA, 'operator' => CONDITION_OPERATOR_LIKE, 'value' => self::HOST_METADATA2 ] ], 'evaltype' => CONDITION_EVAL_TYPE_AND_OR ], 'operations' => [ /* OPERATION_TYPE_HOST_ADD is intentionally missing. It is expected to be run by */ /* Zabbix server, because OPERATION_TYPE_HOST_TAGS_ADD is present. */ [ 'operationtype' => OPERATION_TYPE_HOST_TAGS_ADD, 'optag' => [ [ 'tag' => 'a2', 'value' => 'autoreg 2' ], [ 'tag' => 'tag2', 'value' => 'value 2' ] ] ], [ 'operationtype' => OPERATION_TYPE_HOST_TAGS_REMOVE, 'optag' => [ [ 'tag' => 'a1', 'value' => 'autoreg 1' ], [ 'tag' => 'tag1', 'value' => 'value 1' ] ] ] ] ]]); $this->assertArrayHasKey('result', $response, 'Failed to create an autoregistration action'); $this->assertArrayHasKey('actionids', $response['result'], 'Failed to create an autoregistration action'); $actionids = $response['result']['actionids']; $this->assertCount(2, $actionids, 'Failed to create an autoregistration action'); } /** * @required-components agent * @configurationDataProvider agentConfigurationProvider */ public function testAutoregistration_autoregHost1FirstTime() { $this->waitForAutoreg([ ['tag' => 'a1', 'value' => 'autoreg 1'], ['tag' => 'tag1', 'value' => 'value 1'] ]); self::$HOST_METADATA = self::HOST_METADATA2; } /** * @required-components agent * @configurationDataProvider agentConfigurationProvider * @depends testAutoregistration_autoregHost1FirstTime */ public function testAutoregistration_autoregHost2() { $this->waitForAutoreg([ ['tag' => 'a2', 'value' => 'autoreg 2'], ['tag' => 'tag2', 'value' => 'value 2'] ]); self::$HOST_METADATA = self::HOST_METADATA1; } /** * @required-components agent * @configurationDataProvider agentConfigurationProvider * @depends testAutoregistration_autoregHost2 */ public function testAutoregistration_autoregHost1SecondTime() { $this->waitForAutoreg([ ['tag' => 'a1', 'value' => 'autoreg 1'], ['tag' => 'tag1', 'value' => 'value 1'] ]); } }