<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$rg_cache_debug = TRUE;
$test_normal = TRUE;
$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/user.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");
rg_log_set_file("wh_cloud.log");
require_once("common.php");
$_testns = 'wh_cloud';
$home = getenv('HOME');
if (!file_exists($home . '/.aws/conf.php')) {
rg_log('no ~/.aws.conf.php file!');
exit(0);
}
// this will load $a array
include($home . '/.aws/conf.php');
$rg_ui = array('confirmed' => 20);
rg_test_create_user($db, $rg_ui);
$info = array('id' => $rg_ui['username']);
prepare_http($info);
rg_log('');
rg_log_enter('Login...');
$r = test_login($test_url, $rg_ui);
if ($r === FALSE) {
rg_log("Cannot login!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Registering webhook...');
$extra = array(
'wh::description' => 'description1 <xss> codedeploy',
'wh::repo' => '',
'wh::refname' => '',
'wh::idata::events[P]' => 'on',
'wh::idata::access_key_id' => $a['access_key_id'],
'wh::idata::secret_access_key' => $a['secret_access_key'],
'wh::idata::region' => 'eu-central-1',
'wh::idata::application_name' => 'DemoApplication-##repo##',
'wh::idata::deployment_group_name' => 'DemoFleet-##repo##-##branch##',
'wh::idata::deployment_config_name' => '',
'wh::idata::bucket' => 'test-deploy-500-##repo##-##branch##',
'wh::idata::file' =>
'##repo##-##branch##-##hook_id##'
. '-##commit##-##date##-##time##-##ip##-##timestamp##.zip'
);
rg_test_wh_add_edit($db, $rg_ui, 'cloud', 'generic', $extra);
rg_log_exit();
rg_log('');
rg_log_enter('Finding out the hook id...');
$r = test_wait_cache('wh' . '::' . $rg_ui['uid'], 50);
rg_log_ml('r=' . print_r($r, TRUE));
$t = array_keys($r['list']);
if (count($t) != 1) {
rg_log('We do not have 1 id!');
exit(1);
}
$wh_id = $t[0];
rg_log('wh_id=' . $wh_id);
rg_log_exit();
rg_test_upload_ssh_key($db, $rg_ui, 'wh_cloud', $kn);
rg_log('');
rg_log_enter('Creating a repo and pushing it');
$repo = array('name' => 'wh-cloud');
rg_test_create_repo($db, $rg_ui, $repo);
$repo_url = 'ssh://rocketgit@' . $rg_ssh_host . ':' . $rg_ssh_port
. '/user/' . $rg_ui['username'] . '/' . $repo['name'];
rg_log('repo_url=' . escapeshellarg($repo_url));
$r = rg_exec('./wh_cloud.git.sh ' . escapeshellarg($repo_url),
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log('Could not create local git repo: ' . $r['errmsg'] . '!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing if hook executed with success');
$key = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $wh_id;
$r = test_wait_cache($key, 50);
$a = @json_decode($r, TRUE);
if ($a === NULL) {
rg_log('Cannot decode JSON: ' . $r);
exit(1);
}
if (!isset($a['deploymentId'])
&& !(isset($a['message']) && strstr($a['message'], 'is already deploying'))) {
rg_log_ml('a: ' . print_r($a, TRUE));
rg_log('Seems hook did not executed correctly!');
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Testing the edit of webhook...');
$extra = array(
'wh::id' => $wh_id,
'wh::repo' => '.*',
'wh::refname' => '..*',
'wh::description' => 'desc2 <xss>',
'wh::flags' => 'D',
'wh::idata::access_key_id' => 'aaa',
'wh::idata::secret_access_key' => 'bbb',
'wh::idata::region' => 'ccc',
'wh::idata::application_name' => 'xxx',
'wh::idata::deployment_group_name' => 'yyy',
'wh::idata::deployment_config_name' => 'zzz',
'wh::idata::bucket' => 'buck',
'wh::idata::file' => 'rrr',
);
rg_test_wh_add_edit($db, $rg_ui, 'cloud', 'generic', $extra);
$sql = "SELECT * FROM webhooks WHERE uid = " . $rg_ui['uid']
. " AND id = " . $wh_id;
$res = rg_sql_query($db, $sql);
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$row['idata'] = rg_unserialize($row['idata']);
$key = 'wh' . '::' . $rg_ui['uid'] . '::' . 'list' . '::' . $wh_id;
rg_cache_core_unset($key); // else we will get previous copy!
$c = rg_cache_get($key);
$list = array('htype' => 'cloud', 'hsubtype' => 'generic',
'repo' => '.*', 'refname' => '..*',
'description' => 'desc2 <xss>', 'flags' => 'D',
'access_key_id' => 'aaa',
'secret_access_key' => 'bbb', 'region' => 'ccc',
'application_name' => 'xxx', 'deployment_group_name' => 'yyy',
'deployment_config_name' => 'zzz', 'bucket' => 'buck',
'file' => 'rrr');
foreach ($list as $k => $v) {
if (isset($row[$k]))
$a = $row[$k];
else if (isset($row['idata'][$k]))
$a = $row['idata'][$k];
else {
rg_log('Key [' . $k . '] not found! Bad!');
exit(1);
}
if (strcmp($a, $v) != 0) {
rg_log_ml('row: ' . print_r($row, TRUE));
rg_log("db: Seems that [$k] has not been updated"
. " [" . $a . "] != [" . $v . "]!");
exit(1);
}
if (isset($c[$k]))
$a = $c[$k];
else
$a = $c['idata'][$k];
if (strcmp($a, $v) != 0) {
rg_log_ml('c: ' . print_r($c, TRUE));
rg_log("cache: Seems that [$k] has not been updated"
. " [" . $a . "] != [" . $v . "]!");
exit(1);
}
}
rg_log_exit();
rg_log('OK!');