예제 #1
0
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     if (self::isSkippedFunctionalTest(self::TEST_TYPE_UI)) {
         return;
     }
     $db = \Scalr::getDb();
     self::deleteTestVariables();
     $envId = \Scalr::config('scalr.phpunit.envid');
     if (!$envId) {
         return;
     }
     $env = \Scalr_Environment::init()->loadById($envId);
     self::$vars[ScopeInterface::SCOPE_SCALR] = new Scalr_Scripting_GlobalVariables();
     self::$vars[ScopeInterface::SCOPE_ACCOUNT] = new Scalr_Scripting_GlobalVariables($env->clientId, 0, ScopeInterface::SCOPE_ACCOUNT);
     self::$vars[ScopeInterface::SCOPE_ENVIRONMENT] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ENVIRONMENT);
     self::$args[ScopeInterface::SCOPE_SCALR] = self::$args[ScopeInterface::SCOPE_ACCOUNT] = self::$args[ScopeInterface::SCOPE_ENVIRONMENT] = [0, 0, 0, ''];
     /* @var $farm Farm */
     $farm = Farm::findOne([['envId' => $env->id]]);
     if ($farm) {
         self::$vars[ScopeInterface::SCOPE_FARM] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARM);
         self::$args[ScopeInterface::SCOPE_FARM] = [0, $farm->id, 0, ''];
         /* @var $farmRole FarmRole */
         $farmRole = FarmRole::findOne([['farmId' => $farm->id]]);
         if ($farmRole) {
             self::$vars[ScopeInterface::SCOPE_ROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ROLE);
             self::$args[ScopeInterface::SCOPE_ROLE] = [$farmRole->roleId, 0, 0, ''];
             self::$vars[ScopeInterface::SCOPE_FARMROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARMROLE);
             self::$args[ScopeInterface::SCOPE_FARMROLE] = [$farmRole->roleId, $farm->id, $farmRole->id, ''];
         }
     }
 }
예제 #2
0
 /**
  * @test
  * @functional
  */
 public function testFarmRoleGlobalVariables()
 {
     $db = \Scalr::getDb();
     $testName = str_replace('-', '', $this->getTestName());
     $farm = Farm::findOne([['envId' => static::$testEnvId]]);
     /* @var $farm Farm */
     $farmRole = FarmRole::findOne([['farmId' => $farm->id]]);
     /* @var $farmRole FarmRole */
     $roleId = $farmRole->id;
     $uri = static::getUserApiUrl("farm-roles/{$roleId}/global-variables");
     $variables = null;
     $declaredNotInRole = null;
     do {
         $query = [];
         if (isset($variables->pagination->next)) {
             $parts = parse_url($variables->pagination->next);
             parse_str($parts['query'], $query);
         }
         $query[ApiController::QUERY_PARAM_MAX_RESULTS] = 2;
         $describe = $this->request($uri, Request::METHOD_GET, $query);
         $this->assertDescribeResponseNotEmpty($describe);
         $this->assertNotEmpty($describe->getBody());
         $variables = $describe->getBody();
         $this->assertLessThanOrEqual(2, count($variables->data));
         foreach ($variables->data as $variable) {
             $this->assertVariableObjectNotEmpty($variable);
             if (empty($declaredNotInRole) && $variable->declaredIn !== ScopeInterface::SCOPE_FARMROLE && !$variable->hidden) {
                 $declaredNotInRole = $variable->name;
             }
             if (strpos($variable->name, $testName) !== false) {
                 $delete = $this->request($uri . '/' . $variable->name, Request::METHOD_DELETE);
                 $this->assertEquals(200, $delete->response->getStatus());
             }
         }
     } while (!empty($variables->pagination->next));
     $this->assertNotNull($declaredNotInRole);
     $notFoundRoleId = 10 + $db->GetOne("SELECT MAX(f.id) FROM farm_roles f");
     $describe = $this->request(static::getUserApiUrl("/farm-roles/{$notFoundRoleId}/global-variables"), Request::METHOD_GET);
     $this->assertErrorMessageContains($describe, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     $create = $this->request($uri, Request::METHOD_POST, [], ['invalid' => 'value']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set');
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => 'invalid val--ue']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE, 'Name should contain only letters, numbers and underscores, start with letter and be from 2 to 128 chars long');
     //test invalid category name
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => 'TestName', 'category' => 'invalid category']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE);
     $create = $this->request($uri, Request::METHOD_POST);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Invalid body');
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => $testName, 'value' => $testName, 'description' => $testName]);
     $this->assertEquals(201, $create->response->getStatus());
     $this->assertFetchResponseNotEmpty($create);
     $createBody = $create->getBody();
     $this->assertNotEmpty($createBody);
     $this->assertVariableObjectNotEmpty($createBody->data);
     $this->assertEquals($testName, $createBody->data->name);
     $this->assertEquals($testName, $createBody->data->value);
     $this->assertEquals($testName, $createBody->data->description);
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => $testName]);
     $this->assertErrorMessageContains($create, 409, ErrorMessage::ERR_UNICITY_VIOLATION, 'Variable with name');
     $fetch = $this->request($uri . '/' . $testName, Request::METHOD_GET);
     $this->assertEquals(200, $fetch->response->getStatus());
     $this->assertFetchResponseNotEmpty($fetch);
     $fetchBody = $fetch->getBody();
     $this->assertNotEmpty($fetchBody);
     $this->assertVariableObjectNotEmpty($fetchBody->data);
     $this->assertEquals($testName, $fetchBody->data->name);
     $this->assertEquals($testName, $fetchBody->data->value);
     $modify = $this->request($uri . '/' . $testName, Request::METHOD_PATCH, [], ['value' => '']);
     $this->assertEquals(200, $modify->response->getStatus());
     $this->assertFetchResponseNotEmpty($modify);
     $modifyBody = $modify->getBody();
     $this->assertNotEmpty($modifyBody);
     $this->assertVariableObjectNotEmpty($modifyBody->data);
     $this->assertEquals($testName, $modifyBody->data->name);
     $this->assertEquals('', $modifyBody->data->value);
     $modify = $this->request($uri . '/' . $testName . 'notFound', Request::METHOD_PATCH, [], ['value' => '']);
     $this->assertEquals(404, $modify->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_OBJECT_NOT_FOUND, $modify);
     $modify = $this->request($uri . '/' . $testName, Request::METHOD_PATCH, [], ['name' => '']);
     $this->assertErrorMessageContains($modify, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set');
     $modify = $this->request($uri . '/' . $declaredNotInRole, Request::METHOD_PATCH, [], ['hidden' => 1]);
     $this->assertEquals(403, $modify->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_SCOPE_VIOLATION, $modify);
     $delete = $this->request($uri . '/' . $declaredNotInRole, Request::METHOD_DELETE);
     $this->assertEquals(403, $delete->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_SCOPE_VIOLATION, $delete);
     $delete = $this->request($uri . '/' . $testName . 'notfound', Request::METHOD_DELETE);
     $this->assertEquals(404, $delete->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_OBJECT_NOT_FOUND, $delete);
     $delete = $this->request($uri . '/' . $testName, Request::METHOD_DELETE);
     $this->assertEquals(200, $delete->response->getStatus());
 }
예제 #3
0
 /**
  * @test
  */
 public function testComplex()
 {
     /* @var Script $script */
     $script = static::generateScripts([['os' => 'linux']])[0];
     /* @var ScriptVersion $version */
     $version = static::generateVersions($script, [['content' => '#!/bin/sh']])[0];
     $adapter = $this->getAdapter('OrchestrationRules\\FarmRoleScript');
     /* @var User $user */
     $user = $this->getUser();
     $environment = $this->getEnvironment();
     /* @var $farm Farm */
     $farm = static::createEntity(new Farm(), ['changedById' => $user->getId(), 'name' => "{$this->uuid}-farm", 'description' => "{$this->uuid}-description", 'envId' => $environment->id, 'accountId' => $user->getAccountId(), 'createdById' => $user->getId()]);
     $farmRole = $this->createTestFarmRole($farm);
     static::createEntity(new FarmRoleScript(), ['farmRoleId' => $farmRole->id, 'scriptId' => $script->id, 'farmId' => $farm->id]);
     //test get endpoint
     $filterable = $adapter->getRules()[ApiEntityAdapter::RULE_TYPE_FILTERABLE];
     $rules = $this->listRules($farmRole->id);
     foreach ($rules as $rule) {
         foreach ($filterable as $property) {
             $filterValue = $rule->{$property};
             $listResult = $this->listRules($farmRole->id, [$property => $filterValue]);
             if (!static::isRecursivelyEmpty($filterValue)) {
                 foreach ($listResult as $filtered) {
                     $this->assertEquals($filterValue, $filtered->{$property}, "Property '{$property}' mismatch");
                 }
             }
         }
         $response = $this->getRule($farmRole->id, $rule->id);
         $this->assertEquals(200, $response->status, $this->printResponseError($response));
         $dbRule = FarmRoleScript::findPk($rule->id);
         $this->assertObjectEqualsEntity($response->getBody()->data, $dbRule, $adapter);
     }
     $scalrFRScriptData = ['trigger' => ['triggerType' => FarmRoleScriptAdapter::TRIGGER_SINGLE_EVENT, 'event' => ['id' => 'HostInit']], 'target' => ['targetType' => FarmRoleScriptAdapter::TARGET_NAME_TRIGGERING_SERVER], 'action' => ['actionType' => FarmRoleScriptAdapter::ACTION_SCRIPT, 'scriptVersion' => ['script' => ['id' => $script->id], 'version' => $version->version]]];
     $localFRScriptData = ['trigger' => ['triggerType' => FarmRoleScriptAdapter::TRIGGER_ALL_EVENTS], 'target' => ['targetType' => FarmRoleScriptAdapter::TARGET_NAME_NULL], 'action' => ['actionType' => FarmRoleScriptAdapter::ACTION_URI, 'path' => 'https://example.com']];
     //post scalr rule
     $response = $this->postRule($farmRole->id, $scalrFRScriptData);
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $ruleId = $response->getBody()->data->id;
     /* @var $rule FarmRoleScript */
     $rule = FarmRoleScript::findPk($ruleId);
     $this->assertNotEmpty($rule);
     $this->ruleToDelete($ruleId);
     $this->assertObjectEqualsEntity($scalrFRScriptData, $rule, $adapter);
     //post local rule
     $response = $this->postRule($farmRole->id, $localFRScriptData);
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $ruleId = $response->getBody()->data->id;
     /* @var $rule FarmRoleScript */
     $rule = FarmRoleScript::findPk($ruleId);
     $this->assertNotEmpty($rule);
     $this->ruleToDelete($ruleId);
     $this->assertObjectEqualsEntity($localFRScriptData, $rule, $adapter);
     //post rule already existing
     $data = $scalrFRScriptData;
     $data['id'] = $ruleId;
     $response = $this->postRule($farmRole->id, $data);
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $ruleId = $response->getBody()->data->id;
     $this->ruleToDelete($ruleId);
     $this->assertNotEquals($data['id'], $ruleId);
     //post rule with script that does not exists
     $data = $scalrFRScriptData;
     $data['action']['scriptVersion']['script']['id'] = Script::findOne([], null, ['id' => true])->id + 1;
     $response = $this->postRule($farmRole->id, $data);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     //post rule with version that does not exists
     $data = $scalrFRScriptData;
     $data['action']['scriptVersion']['version'] = Script::findPk($data['action']['scriptVersion']['script']['id'])->getLatestVersion()->version + 1;
     $response = $this->postRule($farmRole->id, $data);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     //post rule with properties that not existing
     $data = $scalrFRScriptData;
     $data['foo'] = 'bar';
     $response = $this->postRule($farmRole->id, $data);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //post rule without required fields
     $data = $localFRScriptData;
     unset($data['action']);
     $response = $this->postRule($farmRole->id, $data);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //post rule with invalid field
     $data = $localFRScriptData;
     $data['action'] = '';
     $response = $this->postRule($farmRole->id, $data);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //modify rule
     //TODO::ape add modify rule
     //fetch rule
     $response = $this->getRule($farmRole->id, $rule->id);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $this->assertObjectEqualsEntity($response->getBody()->data, $rule, $adapter);
     //fetch rule that doe not exists
     $response = $this->getRule($farmRole->id, FarmRoleScript::findOne([], null, ['id' => false])->id + 1);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     //fetch rule with missmatch farm role id
     $response = $this->getRule(FarmRole::findOne([], null, ['id' => false])->id + 1, $rule->id);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_VALUE);
     //test have access to all listed rules
     $rules = $this->listRules($farmRole->id);
     foreach ($rules as $rule) {
         $this->assertTrue(FarmRoleScript::findPk($rule->id)->hasAccessPermissions($user));
     }
     //test invalid filters
     $url = self::getUserApiUrl("/farm-roles/{$farmRole->id}/orchestration-rules/");
     $response = $this->request($url, Request::METHOD_GET, ['foo' => 'bar']);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     $response = $this->request($url, Request::METHOD_GET, ['scope' => 'foobar']);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //delete script
     /* @var $rule FarmRoleScript */
     $rule = static::createEntity(new FarmRoleScript(), ['farmRoleId' => $farmRole->id, 'scriptId' => $script->id, 'farmId' => $farm->id]);
     $response = $this->deleteRule($farmRole->id, $rule->id);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     //delete script that does not exists
     $response = $this->deleteRule($farmRole->id, FarmRoleScript::findOne([], null, ['id' => false])->id + 1);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
 }
예제 #4
0
파일: ApiTest.php 프로젝트: scalr/scalr
 /**
  * Get GV
  *
  * @param string $criteria search criteria
  * @param array $params query params
  *
  * @return array
  */
 public function getGlobalVariable($criteria, $params)
 {
     $roleId = 0;
     $farmId = 0;
     $farmRoleId = 0;
     $serverId = 0;
     $scope = ScopeInterface::SCOPE_ENVIRONMENT;
     if (isset($params['farmId'])) {
         $scope = ScopeInterface::SCOPE_FARM;
         $farmId = $params['farmId'];
     } else {
         if (isset($params['farmRoleId'])) {
             /* @var  $farmRole FarmRole */
             $farmRole = FarmRole::findOne([['id' => $params['farmRoleId']]]);
             $farmRoleId = $farmRole->id;
             $roleId = $farmRole->roleId;
             $farmId = $farmRole->farmId;
             $scope = ScopeInterface::SCOPE_FARMROLE;
         } else {
             if (isset($params['roleId'])) {
                 $roleId = $params['roleId'];
                 $scope = ScopeInterface::SCOPE_ROLE;
             }
         }
     }
     $variableScopeIdentity = [$roleId, $farmId, $farmRoleId, $serverId];
     $varName = $criteria;
     $gv = new Scalr_Scripting_GlobalVariables($this->getUser()->getAccountId(), $this->getEnvironment()->id, $scope);
     $variable = [];
     $list = $gv->getValues(...$variableScopeIdentity);
     foreach ($list as $var) {
         if (!empty($var['current']['name']) && $var['current']['name'] == $varName || !empty($var['default']['name']) && $var['default']['name'] == $varName) {
             $variable = $var;
             break;
         }
     }
     return $variable;
 }