<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");
$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");
$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");
$_testns = 'wh_cloud';
$rg_cache_enable = TRUE;
$rg_cache_debug = TRUE;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";
$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_log('');
rg_log("Creating a user...");
rg_test_create_user($db, $rg_ui);
rg_log('');
rg_log_enter('Login...');
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
rg_log("Cannot login!");
exit(1);
}
rg_log_exit();
rg_log('');
rg_log_enter('Registering webhook...');
$extra = array(
'wh::htype' => 'cloud',
'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, $good_sid, 'cloud', $extra);
rg_log_exit();
rg_log('');
rg_log_enter('Finding out the hook id...');
for ($i = 0; $i < 10; $i++) {
$r = rg_cache_get('wh' . '::' . $rg_ui['uid']);
if ($r !== FALSE)
break;
sleep(1);
}
if ($r === FALSE) {
rg_log('Cannot get id from cache');
exit(1);
}
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_log('');
rg_log_enter('Creating and upload a ssh key...');
rg_test_upload_ssh_key($db, $rg_ui, "wh_cloud", $good_sid);
rg_log_exit();
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);
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');
for ($i = 0; $i < 10; $i++) {
$key = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $wh_id;
$r = rg_cache_get($key);
rg_log_ml('cache: ' . $r);
if ($r !== FALSE)
break;
sleep(1);
}
if ($r === FALSE) {
rg_log('Seems the event did not set the cache!');
exit(1);
}
if (!strstr($r, '"deploymentId":"d-') && !strstr($r, 'is already deploying')) {
rg_log_ml('r: ' . print_r($r, 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::htype' => 'cloud',
'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, $good_sid, 'cloud', $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'] = 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', '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!");
?>