示例#1
0
 public function createVm($vmName, $osName, $amiId, $instanceType, $securityGroup)
 {
     // what are we doing?
     $log = usingLog()->startAction("start EC2 VM '{$vmName}', running guest OS '{$osName}', using AMI ID '{$amiId}' and security group '{$securityGroup}'");
     // get the aws settings
     $awsSettings = fromConfig()->getModuleSetting('aws');
     // put the details into an array
     $vmDetails = new Ec2VmDetails();
     $vmDetails->hostId = $vmName;
     $vmDetails->environment = $this->st->getTestEnvironmentName();
     $vmDetails->osName = $osName;
     $vmDetails->amiId = $amiId;
     $vmDetails->type = 'Ec2Vm';
     $vmDetails->instanceType = $instanceType;
     $vmDetails->securityGroup = $securityGroup;
     $vmDetails->keyPairName = $awsSettings->ec2->keyPairName;
     $vmDetails->sshUsername = $awsSettings->ec2->sshUsername;
     $vmDetails->sshKeyFile = $awsSettings->ec2->sshKeyFile;
     $vmDetails->sshOptions = array("-i '" . $awsSettings->ec2->sshKeyFile . "'");
     $vmDetails->scpOptions = array("-i '" . $awsSettings->ec2->sshKeyFile . "'");
     // create our host adapter
     $host = HostLib::getHostAdapter($this->st, $vmDetails->type);
     // create our virtual machine
     $host->createHost($vmDetails);
     // all done
     $log->endAction();
 }
示例#2
0
 public function getDataFor($metric, $startTime, $endTime)
 {
     // when are we looking for?
     $humanStartTime = date('Y-m-d H:i:s', $startTime);
     $humanEndTime = date('Y-m-d H:i:s', $endTime);
     // what are we doing?
     $log = usingLog()->startAction("get raw data from graphite for '{$metric}' between '{$humanStartTime}' and '{$humanEndTime}'");
     // find out where graphite is
     $graphiteUrl = fromConfig()->getModuleSetting('graphite.url');
     if (substr($graphiteUrl, -1, 1) !== '/') {
         $graphiteUrl .= '/';
     }
     // get the requested data
     $response = fromHttp()->get("{$graphiteUrl}render?format=json&target={$metric}&from={$startTime}&until={$endTime}");
     // are there any stats in the response?
     assertsArray($response->chunks)->isExpectedType();
     assertsArray($response->chunks)->isNotEmpty();
     // assemble the raw chunks into one string to decode
     $rawStats = implode("", $response->chunks);
     assertsString($rawStats)->isValidJson();
     $stats = json_decode($rawStats);
     // all done
     $log->endAction();
     return $stats;
 }
示例#3
0
 public function getAwsClientFactory()
 {
     // what are we doing?
     $log = usingLog()->startAction("create AWS client factory using official SDK");
     // get the settings for Aws
     $awsSettings = fromConfig()->getModuleSetting('aws');
     // create the AWS client factory
     $awsFactory = Aws::factory(array('key' => $awsSettings->key, 'secret' => $awsSettings->secret, 'region' => $awsSettings->region));
     // all done
     $log->endAction();
     return $awsFactory;
 }
示例#4
0
 public function get($url, $params = array(), $headers = array(), $timeout = null)
 {
     // create the full URL
     if (count($params) > 0) {
         $url = $url . '?' . http_build_query($params);
     }
     // what are we doing?
     $log = usingLog()->startAction("HTTP GET '{$url}'");
     // build the HTTP request
     $request = new HttpClientRequest($url);
     $request->withUserAgent("Storyplayer")->asGetRequest();
     foreach ($headers as $key => $value) {
         $request->withExtraHeader($key, $value);
     }
     // special case - do we validate SSL certificates in this
     // test environment?
     $httpAddress = $request->getAddress();
     if ($httpAddress->scheme == "https") {
         $validateSsl = fromConfig()->getModuleSetting("http.validateSsl");
         if (null === $validateSsl) {
             // default to TRUE if no setting present
             $validateSsl = true;
         }
         if (!$validateSsl) {
             $request->disableSslCertificateValidation();
         }
     }
     if ($timeout !== null) {
         $request->setReadTimeout($timeout);
     }
     // make the call
     $client = new HttpClient();
     $response = $client->newRequest($request);
     // is this a valid response?
     if (!$response instanceof HttpClientResponse) {
         throw new E5xx_ActionFailed(__METHOD__);
     }
     // all done
     $log->endAction($response);
     return $response;
 }
 /**
  *
  * @param  StoryTeller $st
  * @return void
  */
 public function start(StoryTeller $st)
 {
     // are we using browsermob-proxy?
     $useProxy = false;
     if (fromConfig()->hasModuleSetting('device.browsermob.enable')) {
         $useProxy = fromConfig()->getModuleSetting('device.browsermob.enable');
     }
     // do we have a URL to use?
     $browserUrl = 'http://localhost:4444/wd/hub';
     if (fromConfig()->hasModuleSetting('device.localwebdriver.url')) {
         $browserUrl = fromConfig()->getModuleSetting('device.localwebdriver.url');
     }
     try {
         // by default, we have no proxy session
         $this->proxySession = null;
         // start the proxy if we want it
         if ($useProxy) {
             $httpProxy = new BrowserMobProxyClient();
             $httpProxy->enableFeature('enhancedReplies');
             $this->proxySession = $httpProxy->createProxy();
             // start recording
             $this->proxySession->startHAR();
         }
         // build our requirements for Selenium
         $desiredCapabilities = $this->browserDetails->desiredCapabilities;
         if (is_object($this->proxySession)) {
             $desiredCapabilities['proxy'] = $this->proxySession->getWebDriverProxyConfig();
         }
         // create the browser session
         $webDriver = new WebDriverClient($browserUrl);
         $this->browserSession = $webDriver->newSession($this->browserDetails->browser, $desiredCapabilities);
     } catch (Exception $e) {
         // something went wrong
         throw new E5xx_CannotStartDevice();
     }
 }
示例#6
0
 /**
  * @param string $verb
  */
 protected function makeHttpRequest($url, $verb, $params, $body, $headers = array(), $timeout = null)
 {
     // create the full URL
     if (count($params) > 0) {
         $url = $url . '?' . http_build_query($params);
     }
     // what are we doing?
     $logMsg = ["HTTP " . strtoupper($verb) . " '{$url}'"];
     if ($body != null) {
         $logMsg[] = $body;
     }
     if (count($headers) > 0) {
         $logMsg[] = $headers;
     }
     $log = usingLog()->startAction($logMsg);
     // build the HTTP request
     $request = new HttpClientRequest($url);
     $request->withUserAgent("Storyplayer")->withHttpVerb($verb);
     if (is_array($headers)) {
         foreach ($headers as $key => $value) {
             $request->withExtraHeader($key, $value);
         }
     }
     if (is_array($body)) {
         foreach ($body as $key => $value) {
             $request->addData($key, $value);
         }
     } else {
         $request->setPayload($body);
     }
     // special case - do we validate SSL certificates in this
     // test environment?
     $validateSsl = fromConfig()->getModuleSetting("http.validateSsl");
     if (null === $validateSsl) {
         // default to TRUE if no setting present
         $validateSsl = true;
     }
     if (!$validateSsl) {
         $request->disableSslCertificateValidation();
     }
     if ($timeout !== null) {
         $request->setReadTimeout($timeout);
     }
     // make the call
     $client = new HttpClient();
     $response = $client->newRequest($request);
     // is this a valid response?
     if (!$response instanceof HttpClientResponse) {
         throw new E5xx_ActionFailed(__METHOD__);
     }
     // all done
     $log->endAction($response);
     return $response;
 }
 /**
  * @param string $inventoryFolder
  */
 protected function getHostVarsFilename($inventoryFolder, $hostId)
 {
     // what are we doing?
     $log = usingLog()->startAction("determine host_vars filename for host '{$hostId}'");
     // get our ansible settings
     $ansibleSettings = fromConfig()->getModuleSetting('ansible');
     // get our inventory folder
     $invFolder = $this->getInventoryFolder($ansibleSettings, $inventoryFolder);
     // what is the path to the file?
     $filename = $invFolder . DIRECTORY_SEPARATOR . 'host_vars' . DIRECTORY_SEPARATOR . $hostId;
     // all done
     $log->endAction("filename is: " . $filename);
     return $filename;
 }
<?php

// ========================================================================
//
// STORY DETAILS
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Config')->called('Can merge system-under-test params into test environment config');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // nothing to do
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $expected = fromConfig()->get("systemundertest.roles.0.params.filename");
    $actual = fromConfig()->get("target.groups.0.details.machines.default.params.filename");
    assertsString($actual)->equals($expected);
});
 public function provisionHosts(ProvisioningDefinition $hosts, $provConfig)
 {
     // what are we doing?
     $log = usingLog()->startAction("use dsbuild to provision host(s)");
     // the params file that we are going to output
     $dsbuildParams = new BaseObject();
     // build up the list of settings to write out
     foreach ($hosts as $hostId => $hostProps) {
         // what is the host's IP address?
         $ipAddress = fromHost($hostId)->getIpAddress();
         $propName = $hostId . '_ipv4Address';
         $dsbuildParams->{$propName} = $ipAddress;
         if (isset($hostProps->params)) {
             $dsbuildParams->mergeFrom($hostProps->params);
         }
     }
     // add in all the config settings that we know about
     $dsbuildParams->storyplayer_ipv4Address = fromConfig()->get('storyplayer.ipAddress');
     $dsbuildParams->mergeFrom($this->flattenData($this->st->getActiveConfig()->getData('')));
     // write them out
     $this->writeDsbuildParamsShellFile((array) $dsbuildParams);
     $this->writeDsbuildParamsYamlFile((array) $dsbuildParams);
     // at this point, we are ready to attempt provisioning
     //
     // provision each host in the order that they're listed
     foreach ($hosts as $hostId => $hostProps) {
         // which dsbuildfile are we going to run?
         $hostDir = fromHost($hostId)->getLocalFolder();
         $dsbuildFilename = $this->getDsbuildFilename($hostDir, $provConfig, $hostId);
         if ($dsbuildFilename === null) {
             // there is no dsbuildfile at all to run
             $log->endAction("cannot find dsbuildfile to run :(");
             throw new E5xx_ActionFailed(__METHOD__, "no dsbuildfile to run");
         }
         // at this point, we are ready to provision
         $commandRunner = new CommandRunner();
         // copy the dsbuildparams files to the target machine using scp
         // NOTE: the "vagrant rsync" command seems not working with some Vagrant provisioners (e.g. OpenStack)
         $command = 'scp' . ' ' . $dsbuildParams->{'hosts_' . $hostId . '_scpOptions_0'} . ' ' . $dsbuildParams->{'hosts_' . $hostId . '_scpOptions_1'} . ' dsbuildparams.*' . ' ' . $dsbuildParams->{'hosts_' . $hostId . '_sshUsername'} . '@' . $dsbuildParams->{'hosts_' . $hostId . '_ipAddress'} . ':/vagrant/';
         $result = $commandRunner->runSilently($command);
         if (!$result->didCommandSucceed()) {
             // try to rsync folders in case of scp fail
             $command = 'vagrant rsync ' . $hostId;
             $commandRunner->runSilently($command);
         }
         // provision
         $command = 'sudo bash /vagrant/' . $dsbuildFilename;
         $result = usingHost($hostId)->runCommand($command);
         // what happened?
         if (!$result->didCommandSucceed()) {
             throw new E5xx_ActionFailed(__METHOD__, "provisioning failed");
         }
     }
     // all done
     $log->endAction();
 }
 /**
  * makeGraphApiRequest
  *
  * Make a request to the Graph API, including a user access token
  *
  * @param string $path URL to call in the graph API
  *
  * @return stdClass
  */
 private function makeGraphApiRequest($path)
 {
     $access_token = fromConfig()->getModuleSetting('facebook.access_token');
     // GET $path/?access_token=$access_token
     $resp = fromCurl()->get($this->base_path . $path . '?access_token=' . $access_token);
     // Make sure it's well formed
     $log = usingLog()->startAction("make sure we have the 'data' key in the response");
     if (!isset($resp->data)) {
         // if it was an access token error, remove it from the runtime config
         if (isset($resp->error->message) && strpos($resp->error->message, "Error validating access token") !== false) {
             // Remove the access token from the runtime config
             $config = $this->st->getRuntimeConfig();
             unset($config->facebookAccessToken);
             $this->st->saveRuntimeConfig();
         }
         $respString = json_encode($resp);
         $log->endAction("no data key found. payload is '{$respString}'");
         throw new E5xx_ActionFailed(__METHOD__, "Key 'data' was not found in response");
     }
     $log->endAction();
     return $resp->data;
 }