From f1536800d71fd0402caff56af235a754f09737f5 Mon Sep 17 00:00:00 2001 From: micafer Date: Mon, 13 Nov 2017 17:13:46 +0100 Subject: [PATCH 01/29] Add Unit tests --- db.php | 2 +- test/unit/bootstrap.php | 8 ++++ test/unit/phpunit.xml | 13 +++++++ test/unit/test_cred.php | 52 ++++++++++++++++++++++++++ test/unit/test_db.php | 41 ++++++++++++++++++++ test/unit/test_grp.php | 17 +++++++++ test/unit/test_http.php | 40 ++++++++++++++++++++ test/unit/test_user.php | 83 +++++++++++++++++++++++++++++++++++++++++ user.php | 4 +- 9 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 test/unit/bootstrap.php create mode 100644 test/unit/phpunit.xml create mode 100644 test/unit/test_cred.php create mode 100644 test/unit/test_db.php create mode 100644 test/unit/test_grp.php create mode 100644 test/unit/test_http.php create mode 100644 test/unit/test_user.php diff --git a/db.php b/db.php index 9ecb82d..cc50ddd 100644 --- a/db.php +++ b/db.php @@ -21,7 +21,7 @@ class IMDB extends SQLite3 { function __construct() { - include('config.php'); + include('config.php'); $this->open($im_db); } diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php new file mode 100644 index 0000000..c747f0b --- /dev/null +++ b/test/unit/bootstrap.php @@ -0,0 +1,8 @@ + diff --git a/test/unit/phpunit.xml b/test/unit/phpunit.xml new file mode 100644 index 0000000..6be7397 --- /dev/null +++ b/test/unit/phpunit.xml @@ -0,0 +1,13 @@ + + + + + . + + + + + ../../ + + + diff --git a/test/unit/test_cred.php b/test/unit/test_cred.php new file mode 100644 index 0000000..f86f97b --- /dev/null +++ b/test/unit/test_cred.php @@ -0,0 +1,52 @@ +assertEquals($res, ""); + + $res = get_credentials($user); + $this->assertEquals("cred", $res[0]["id"]); + + $rowid = $res[0]["rowid"]; + $res = get_credential($rowid); + $this->assertEquals("InfrastructureManager", $res["type"]); + + $res = edit_credential($rowid, "crednew", "InfrastructureManager", "host", "user", "impass", + "token_type", "project", "proxy", "public_key", "private_key", "certificate", + "tenant", "subscription_id", "auth_version", "domain", "service_region", "base_url"); + $this->assertEquals($res, ""); + + $res = get_credential($rowid); + $this->assertEquals("InfrastructureManager", $res["type"]); + $this->assertEquals("crednew", $res["id"]); + $this->assertEquals(true, $res["enabled"]); + + $res = enable_credential($rowid, 0); + $this->assertEquals($res, ""); + $res = get_credential($rowid); + $this->assertEquals(false, $res["enabled"]); + + $res = insert_credential($user, "cred2", "InfrastructureManager", "host", "user", "impass", + "token_type", "project", "proxy", "public_key", "private_key", "certificate", + "tenant", "subscription_id", "auth_version", "domain", "service_region", "base_url"); + $this->assertEquals($res, ""); + $res = change_order($rowid, $user, 0, 1); + $this->assertEquals($res, ""); + $res = get_credential($rowid); + $this->assertEquals(1, $res["ord"]); + + $res = delete_credential($rowid); + $this->assertEquals($res, ""); + $res = get_credential($rowid); + $this->assertEquals(NULL, $res); + } +} diff --git a/test/unit/test_db.php b/test/unit/test_db.php new file mode 100644 index 0000000..b71874a --- /dev/null +++ b/test/unit/test_db.php @@ -0,0 +1,41 @@ +direct_query("select * from user"); + + $this->assertEquals("admin",$res[0]['username']); + + $res = $db->get_items_from_table("user"); + $this->assertEquals("admin",$res[0]['username']); + + $username = uniqid(); + $fields = array(); + $fields[] = "'" . $username . "'"; + $fields[] = "'passwd'"; + $fields[] = "'0'"; + $res = $db->insert_item_into_table("user",$fields); + $this->assertEquals("",$res); + + $res = $db->get_items_from_table("user", array("username" => "'" . $username . "'")); + $this->assertEquals(1,count($res)); + + $fields = array(); + $fields["password"] = "'somepass'"; + $where = array("username" => "'" . $username . "'"); + $res = $db->edit_item_from_table("user",$fields,$where); + $this->assertEquals("",$res); + + $res = $db->get_items_from_table("user", array("username" => "'" . $username . "'")); + $this->assertEquals("somepass",$res[0]["password"]); + + $db->close(); + } + +} diff --git a/test/unit/test_grp.php b/test/unit/test_grp.php new file mode 100644 index 0000000..5614e2a --- /dev/null +++ b/test/unit/test_grp.php @@ -0,0 +1,17 @@ +assertEquals($res, ""); + + $res = get_groups(); + $this->assertEquals("users", $res[0]["name"]); + } +} diff --git a/test/unit/test_http.php b/test/unit/test_http.php new file mode 100644 index 0000000..e46c79b --- /dev/null +++ b/test/unit/test_http.php @@ -0,0 +1,40 @@ +assertInstanceOf( + Http::class, + Http::connect('www.google.es') + ); + } + + public function testExec() + { + $headers = array("Authorization: Basic"); + $res = Http::connect('www.google.es', 443, 'https') + ->setHeaders($headers) + ->exec('GET', '/'); + + $status = $res->getStatus(); + $output = $res->getOutput(); + + $this->assertEquals(200, $status); + $this->assertStringStartsWith('', $output); + + $methods = array('DELETE','PUT', 'POST'); + foreach ($methods as $method) { + $res = Http::connect('www.google.es', 80, 'http') + ->setHeaders($headers) + ->exec($method, '/'); + + $status = $res->getStatus(); + $this->assertEquals(405, $status); + } + } + +} diff --git a/test/unit/test_user.php b/test/unit/test_user.php new file mode 100644 index 0000000..3eb3413 --- /dev/null +++ b/test/unit/test_user.php @@ -0,0 +1,83 @@ +assertEquals( + false, + check_session_user() + ); + + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $this->assertEquals( + true, + check_session_user() + ); + + $username = uniqid(); + $res = insert_user($username, "pass", array('users'), ''); + $this->assertEquals($res, ""); + + $_SESSION = array("user"=>$username, "password"=>"pass"); + $this->assertEquals( + false, + check_admin_user() + ); + } + + public function testGetUsers() + { + $res = get_users(); + $this->assertGreaterThanOrEqual(1, count($res)); + $this->assertEquals('admin', $res[0]['username']); + + $res = get_user("admin"); + $this->assertEquals('admin', $res['username']); + + $res = get_user_groups("admin"); + $this->assertEquals('users', $res[0]['grpname']); + } + + public function testManageUsers() + { + $res = change_password("admin", "test"); + $this->assertEquals('', $res); + + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $this->assertEquals( + false, + check_session_user() + ); + + $_SESSION = array("user"=>"admin", "password"=>"test"); + $this->assertEquals( + true, + check_session_user() + ); + + $res = change_password("admin", "admin"); + + $username = uniqid(); + $res = insert_user($username, "pass", array('users'), ''); + $this->assertEquals($res, ""); + + $res = edit_user($username, $username, "pass", array(), "1"); + $this->assertEquals($res, ""); + + $_SESSION = array("user"=>$username, "password"=>"pass"); + $this->assertEquals( + true, + check_session_user() + ); + + $res = delete_user($username); + $this->assertEquals($res, ""); + + $res = get_user($username); + $this->assertEquals("", $res); + } +} diff --git a/user.php b/user.php index cffa163..b8fb3d7 100644 --- a/user.php +++ b/user.php @@ -60,7 +60,9 @@ function check_admin_user() { if (count($res) > 0) { $res = check_password($password, $res[0]["password"]); - } + } else { + $res = false; + } return $res; } From 9fe8ea8d9ee9a29cb02109a2304b76011774cc60 Mon Sep 17 00:00:00 2001 From: micafer Date: Mon, 13 Nov 2017 18:25:21 +0100 Subject: [PATCH 02/29] Add Unit tests --- format.php | 10 +++++----- test/unit/bootstrap.php | 1 + test/unit/test_grp.php | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/format.php b/format.php index 32e3c21..b2f59e2 100644 --- a/format.php +++ b/format.php @@ -26,7 +26,7 @@ function formatState($state) { if ($state == "failed") $res = "failed"; if ($state == "unknown") $res = "unknown"; if ($state == "running") $res = "configuring"; - if ($state == "configured") $res = "" . $state . ""; + if ($state == "configured") $res = "configured"; return $res; } @@ -154,13 +154,13 @@ function getOutPorts($radl) { else if ($key == 'outports') $ports = $value; } - + if ($public and strlen($ports) > 0) { $port_parts = explode(",", $ports); foreach ($port_parts as $port_pair) { - $port_pair_parts = explode("-", $port_pair); - if (count($port_pair_parts)>1 and $port_pair_parts[0] != $port_pair_parts[1]) { - $res[$port_pair_parts[0]] = $port_pair_parts[1]; + $port_pair_parts = explode("-", $port_pair); + if (count($port_pair_parts)>1) { + $res[$port_pair_parts[0]] = $port_pair_parts[1]; } } } diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php index c747f0b..bd5f362 100644 --- a/test/unit/bootstrap.php +++ b/test/unit/bootstrap.php @@ -5,4 +5,5 @@ include_once('../../db.php'); include_once('../../cred.php'); include_once('../../group.php'); +include_once('../../format.php'); ?> diff --git a/test/unit/test_grp.php b/test/unit/test_grp.php index 5614e2a..78b9ee1 100644 --- a/test/unit/test_grp.php +++ b/test/unit/test_grp.php @@ -13,5 +13,20 @@ public function testManageGroups() $res = get_groups(); $this->assertEquals("users", $res[0]["name"]); + + $res = get_group($grp); + $this->assertEquals("desc", $res["description"]); + + $newgrp = uniqid(); + $res = edit_group($grp, $newgrp, "newdesc"); + $this->assertEquals($res, ""); + + $res = get_group($newgrp); + $this->assertEquals("newdesc", $res["description"]); + + $res = delete_group($newgrp); + $this->assertEquals($res, ""); + $res = get_group($newgrp); + $this->assertEquals(NULL, $res); } } From 6df949b2aaef5711c7af8fa56e44c4a08a22aafd Mon Sep 17 00:00:00 2001 From: micafer Date: Tue, 14 Nov 2017 10:12:02 +0100 Subject: [PATCH 03/29] Add Unit tests and improve code --- README.md | 2 +- config.php | 3 +- getcontmsg.php | 4 +- getoutputs.php | 2 +- getvminfo.php | 2 +- im-rest.php | 369 ++++++++++---------- im-xml-rpc.php | 718 +++++++++++++++++++------------------- im.php | 18 +- list.php | 6 +- operate.php | 14 +- radlinfo.php | 4 +- test/unit/bootstrap.php | 2 + test/unit/test_format.php | 144 ++++++++ test/unit/test_rest.php | 23 ++ test/unit/test_xml.php | 31 ++ 15 files changed, 791 insertions(+), 551 deletions(-) create mode 100644 test/unit/test_format.php create mode 100644 test/unit/test_rest.php create mode 100644 test/unit/test_xml.php diff --git a/README.md b/README.md index 657465c..ef4d20b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ fully functional infrastructure. IM web interface is based on PHP, so a web server with PHP support must be installed. -Also the mcrypt PHP module must be installed and enabled. +Also the mcrypt PHP modules must be installed and enabled. It is also required to install the PHP module to access SQLite databases. diff --git a/config.php b/config.php index cce6cfa..6d42729 100644 --- a/config.php +++ b/config.php @@ -23,7 +23,8 @@ $im_host="localhost"; $im_port=8899; $im_method='http'; -$im_db="/home/www-data/im.db"; +#$im_db="/home/www-data/im.db"; +$im_db="/tmp/im.db"; # To use that feature the IM recipes file must accesible to the web server #$recipes_db="/usr/local/im/contextualization/recipes_ansible.db"; # If not set "" diff --git a/getcontmsg.php b/getcontmsg.php index 3bc35f2..14b6165 100644 --- a/getcontmsg.php +++ b/getcontmsg.php @@ -29,9 +29,9 @@ $id = $_GET['id']; if (isset($_GET['vmid'])) { $vmid = $_GET['vmid']; - $cont_msg = GetVMContMsg($im_host,$im_port,$im_method,$id,$vmid); + $cont_msg = GetIM()->GetVMContMsg($id,$vmid); } else { - $cont_msg = GetInfrastructureContMsg($im_host,$im_port,$im_method,$id); + $cont_msg = GetIM()->GetInfrastructureContMsg($id); } } diff --git a/getoutputs.php b/getoutputs.php index e8b26f6..ddaed96 100644 --- a/getoutputs.php +++ b/getoutputs.php @@ -27,7 +27,7 @@ include('im.php'); include('config.php'); $id = $_GET['id']; - $outputs = GetOutputs($im_host,$im_port,$id); + $outputs = GetIM()->GetOutputs($id); } ?> diff --git a/getvminfo.php b/getvminfo.php index cb463a9..483bb59 100644 --- a/getvminfo.php +++ b/getvminfo.php @@ -29,7 +29,7 @@ include_once('im.php'); include_once('config.php'); - $res = GetVMInfo($im_host,$im_port,$im_method, $id, $vmid); + $res = GetIM()->GetVMInfo($id, $vmid); if (is_string($res) && strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); diff --git a/im-rest.php b/im-rest.php index f4f0273..f322035 100644 --- a/im-rest.php +++ b/im-rest.php @@ -1,217 +1,232 @@ 0) { - $value = str_replace("\n",$AUTH_NEW_LINE_SEPARATOR, $cred[$field]); - if ($field == "certificate") { - $auth = $auth . "password = " . $value . "; "; - } else { - $auth = $auth . $field ." = " . $value . "; "; - } - } - } - $auth = substr( $auth, 0, strlen($auth)-2 ) . $AUTH_LINE_SEPARATOR; - } - } - } - - return $auth; -} +class IMRest +{ + static public function connect($host, $port) + { + return new self($host, $port); + } -function GetErrorMessage($output) { - return $output; -} + public function __construct($host = "localhost", $port = 8800) + { + $this->_host = $host; + $this->_port = $port; + } -function BasicRESTCall($verb, $host, $port, $path, $extra_headers=array(), $params=array()) { - include('config.php'); - $auth = get_auth_data(); - $headers = array("Authorization:" . $auth); - $headers = array_merge($headers, $extra_headers); + public function get_auth_data() { + + $fields = array("id","type","host","username","password","proxy","token_type","project","public_key", + "private_key", "certificate", "tenant", "project", "subscription_id", "auth_version", "domain", + "service_region", "base_url"); + $user = $_SESSION['user']; + $password = $_SESSION['password']; + // esto por si usamos la autorizacion del servidor web + //$user = $_SERVER['PHP_AUTH_USER'] + //$password = $_SERVER['PHP_AUTH_PW'] + + // Same values as defined in IM REST API + // Combination of chars used to separate the lines in the AUTH header + $AUTH_LINE_SEPARATOR = '\\n'; + // Combination of chars used to separate the lines inside the auth data (i.e. in a certificate) + $AUTH_NEW_LINE_SEPARATOR = '\\\\n'; + + $auth = NULL; + $creds = get_credentials($user, $password); + if (!is_null($creds)) { + $auth = ""; + foreach ($creds as $cred) { + if ($cred['enabled']) { + foreach ($fields as $field) { + if (!is_null($cred[$field]) && strlen(trim($cred[$field])) > 0) { + $value = str_replace("\n",$AUTH_NEW_LINE_SEPARATOR, $cred[$field]); + if ($field == "certificate") { + $auth = $auth . "password = " . $value . "; "; + } else { + $auth = $auth . $field ." = " . $value . "; "; + } + } + } + $auth = substr( $auth, 0, strlen($auth)-2 ) . $AUTH_LINE_SEPARATOR; + } + } + } - if ($im_use_ssl) { - $protocol = 'https'; - } else { - $protocol = 'http'; - } - - try { - $res = Http::connect($host, $port, $protocol) - ->setHeaders($headers) - ->exec($verb, $path, $params); - - $status = $res->getStatus(); - $output = $res->getOutput(); - } catch (Exception $e) { - $status = 600; - $output = "Exception: " . $e->getMessage(); + return $auth; } - $res = $output; - if ($status != 200) { - $res = 'Error: Code: ' . strval($status) . '. ' . GetErrorMessage($output); + public function GetErrorMessage($output) { + return $output; } - return new Http_response($status, $res); -} + public function BasicRESTCall($verb, $path, $extra_headers=array(), $params=array()) { + include('config.php'); + $auth = $this->get_auth_data(); + $headers = array("Authorization:" . $auth); + $headers = array_merge($headers, $extra_headers); -function GetInfrastructureList($host, $port, $method) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures', $headers); + if ($im_use_ssl) { + $protocol = 'https'; + } else { + $protocol = 'http'; + } + + try { + $res = Http::connect($this->_host, $this->_port, $protocol) + ->setHeaders($headers) + ->exec($verb, $path, $params); + + $status = $res->getStatus(); + $output = $res->getOutput(); + } catch (Exception $e) { + $status = 600; + $output = "Exception: " . $e->getMessage(); + } - if ($res->getStatus() != 200) { - return $res->getOutput(); - } else { - $inf_urls = explode("\n", $res->getOutput()); - $inf_ids = array(); - foreach ($inf_urls as $inf_url) { - $inf_id = trim(basename(parse_url($inf_url, PHP_URL_PATH))); - if (strlen($inf_id) > 0) { - $inf_ids[] = $inf_id; + $res = $output; + if ($status != 200) { + $res = 'Error: Code: ' . strval($status) . '. ' . $this->GetErrorMessage($output); + } + + return new Http_response($status, $res); + } + + public function GetInfrastructureList() { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures', $headers); + + if ($res->getStatus() != 200) { + return $res->getOutput(); + } else { + $inf_urls = explode("\n", $res->getOutput()); + $inf_ids = array(); + foreach ($inf_urls as $inf_url) { + $inf_id = trim(basename(parse_url($inf_url, PHP_URL_PATH))); + if (strlen($inf_id) > 0) { + $inf_ids[] = $inf_id; + } } + return $inf_ids; } - return $inf_ids; } -} -function GetInfrastructureInfo($host, $port, $method, $id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/'.$id, $headers); - - if ($res->getStatus() != 200) { - return 'Error: Code: ' . strval($res->getStatus()) . '. ' . GetErrorMessage($output); - } else { - $vm_urls = explode("\n", $res->getOutput()); - $vm_ids = array(); - foreach ($vm_urls as $vm_url) { - $vm_id = trim(basename(parse_url($vm_url, PHP_URL_PATH))); - if (strlen($vm_id) > 0) { - $vm_ids[] = $vm_id; + public function GetInfrastructureInfo($id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures/'.$id, $headers); + + if ($res->getStatus() != 200) { + return 'Error: Code: ' . strval($res->getStatus()) . '. ' . GetErrorMessage($output); + } else { + $vm_urls = explode("\n", $res->getOutput()); + $vm_ids = array(); + foreach ($vm_urls as $vm_url) { + $vm_id = trim(basename(parse_url($vm_url, PHP_URL_PATH))); + if (strlen($vm_id) > 0) { + $vm_ids[] = $vm_id; + } } + return $vm_ids; } - return $vm_ids; } -} -function GetInfrastructureState($host, $port, $method, $id) { - $headers = array('Accept: application/json'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/'.$id.'/state', $headers); + public function GetInfrastructureState($id) { + $headers = array('Accept: application/json'); + $res = $this->BasicRESTCall("GET", '/infrastructures/'.$id.'/state', $headers); - if ($res->getStatus() != 200) { - return $res->getOutput(); - } else { - return json_decode($res->getOutput())->state->state; + if ($res->getStatus() != 200) { + return $res->getOutput(); + } else { + return json_decode($res->getOutput())->state->state; + } } -} -function DestroyInfrastructure($host, $port, $method, $id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("DELETE", $host, $port, '/infrastructures/'.$id, $headers); - - if ($res->getStatus() != 200) { + public function DestroyInfrastructure($id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("DELETE", '/infrastructures/'.$id, $headers); + + if ($res->getStatus() != 200) { + return $res->getOutput(); + } else { + return ""; + } + } + + public function GetVMInfo($inf_id, $vm_id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures/' . $inf_id . '/vms/' . $vm_id, $headers); return $res->getOutput(); - } else { - return ""; } -} -function GetVMInfo($host, $port, $method, $inf_id, $vm_id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id, $headers); - return $res->getOutput(); -} + public function GetInfrastructureContMsg($id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures/'.$id.'/contmsg', $headers); + return $res->getOutput(); + } -function GetInfrastructureContMsg($host, $port, $method, $id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/'.$id.'/contmsg', $headers); - return $res->getOutput(); -} + public function GetVMProperty($inf_id, $vm_id, $property) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/" . $property, $headers); + return $res->getOutput(); + } -function GetVMProperty($host, $port, $method, $inf_id, $vm_id, $property) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/" . $property, $headers); - return $res->getOutput(); -} + public function GetVMContMsg($inf_id, $vm_id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("GET", '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/contmsg", $headers); + return $res->getOutput(); + } -function GetVMContMsg($host, $port, $method, $inf_id, $vm_id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/contmsg", $headers); - return $res->getOutput(); -} + public function GetContentType($content) { + if (strpos($content,"tosca_definitions_version") !== false) { + return 'text/yaml'; + } elseif (substr(trim($content),0,1) == "[") { + return 'application/json'; + } else { + return 'text/plain'; + } + } -function GetContentType($content) { - if (strpos($content,"tosca_definitions_version") !== false) { - return 'text/yaml'; - } elseif (substr(trim($content),0,1) == "[") { - return 'application/json'; - } else { - return 'text/plain'; + public function CreateInfrastructure($radl) { + $headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl)); + $res = $this->BasicRESTCall("POST", '/infrastructures', $headers, $radl); + return $res->getOutput(); } -} -function CreateInfrastructure($host, $port, $method, $radl) { - $headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl)); - $res = BasicRESTCall("POST", $host, $port, '/infrastructures', $headers, $radl); - return $res->getOutput(); -} + public function StartVM($inf_id, $vm_id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("PUT", '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/start", $headers); + return $res->getOutput(); + } -function StartVM($host, $port, $method, $inf_id, $vm_id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("PUT", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/start", $headers); - return $res->getOutput(); -} + public function StopVM($inf_id, $vm_id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("PUT", '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/stop", $headers); + return $res->getOutput(); + } -function StopVM($host, $port, $method, $inf_id, $vm_id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("PUT", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id . "/stop", $headers); - return $res->getOutput(); -} + public function AddResource($inf_id, $radl) { + $headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl)); + $res = $this->BasicRESTCall("POST", '/infrastructures/' . $inf_id, $headers, $radl); + return $res->getOutput(); + } -function AddResource($host, $port, $method, $inf_id, $radl) { - $headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl)); - $res = BasicRESTCall("POST", $host, $port, '/infrastructures/' . $inf_id, $headers, $radl); - return $res->getOutput(); -} + public function RemoveResource($inf_id, $vm_id) { + $headers = array('Accept: text/*'); + $res = $this->BasicRESTCall("DELETE",'/infrastructures/' . $inf_id . '/vms/' . $vm_id, $headers); + return $res->getOutput(); + } -function RemoveResource($host, $port, $method, $inf_id, $vm_id) { - $headers = array('Accept: text/*'); - $res = BasicRESTCall("DELETE", $host, $port, '/infrastructures/' . $inf_id . '/vms/' . $vm_id, $headers); - return $res->getOutput(); -} + public function Reconfigure($inf_id, $radl) { + $headers = array('Accept: text/*', 'Content-Type: text/plain', 'Content-Length: ' . strlen($radl)); + $res = $this->BasicRESTCall("PUT", '/infrastructures/' . $inf_id . '/reconfigure', $headers, $radl); + return $res->getOutput(); + } -function Reconfigure($host, $port, $method, $inf_id, $radl) { - $headers = array('Accept: text/*', 'Content-Type: text/plain', 'Content-Length: ' . strlen($radl)); - $res = BasicRESTCall("PUT", $host, $port, '/infrastructures/' . $inf_id . '/reconfigure', $headers, $radl); - return $res->getOutput(); -} + public function GetOutputs($inf_id) { + $headers = array('Accept: application/json'); + $res = $this->BasicRESTCall("GET", '/infrastructures/' . $inf_id . '/outputs', $headers); + return json_decode($res->getOutput(), true)["outputs"]; + } -function GetOutputs($host, $port, $inf_id) { - $headers = array('Accept: application/json'); - $res = BasicRESTCall("GET", $host, $port, '/infrastructures/' . $inf_id . '/outputs', $headers); - return json_decode($res->getOutput(), true)["outputs"]; } ?> diff --git a/im-xml-rpc.php b/im-xml-rpc.php index a513beb..b57dab5 100644 --- a/im-xml-rpc.php +++ b/im-xml-rpc.php @@ -1,424 +1,438 @@ 0) { - $auth_cloud['id'] = new xmlrpcval($cred['id']); - } - if (!is_null($cred['host']) && strlen(trim($cred['host'])) > 0) { - $auth_cloud['host'] = new xmlrpcval($cred['host']); - } - if (!is_null($cred['username']) && strlen(trim($cred['username'])) > 0) { - $auth_cloud['username'] = new xmlrpcval($cred['username']); - } - if (!is_null($cred['password']) && strlen(trim($cred['password'])) > 0) { - $auth_cloud['password'] = new xmlrpcval($cred['password']); - } - if (!is_null($cred['proxy']) && strlen(trim($cred['proxy'])) > 0) { - $auth_cloud['proxy'] = new xmlrpcval($cred['proxy']); - } - if (!is_null($cred['token_type']) && strlen(trim($cred['token_type'])) > 0) { - $auth_cloud['token_type'] = new xmlrpcval($cred['token_type']); - } - if (!is_null($cred['project']) && strlen(trim($cred['project'])) > 0) { - $auth_cloud['project'] = new xmlrpcval($cred['project']); - } - if (!is_null($cred['public_key']) && strlen(trim($cred['public_key'])) > 0) { - $auth_cloud['public_key'] = new xmlrpcval($cred['public_key']); - } - if (!is_null($cred['private_key']) && strlen(trim($cred['private_key'])) > 0) { - $auth_cloud['private_key'] = new xmlrpcval($cred['private_key']); - } - if (!is_null($cred['certificate']) && strlen(trim($cred['certificate'])) > 0) { - $auth_cloud['password'] = new xmlrpcval($cred['certificate']); - } - if (!is_null($cred['tenant']) && strlen(trim($cred['tenant'])) > 0) { - $auth_cloud['tenant'] = new xmlrpcval($cred['tenant']); - } - if (!is_null($cred['project']) && strlen(trim($cred['project'])) > 0) { - $auth_cloud['project'] = new xmlrpcval($cred['project']); - } - if (!is_null($cred['subscription_id']) && strlen(trim($cred['subscription_id'])) > 0) { - $auth_cloud['subscription_id'] = new xmlrpcval($cred['subscription_id']); - } - if (!is_null($cred['auth_version']) && strlen(trim($cred['auth_version'])) > 0) { - $auth_cloud['auth_version'] = new xmlrpcval($cred['auth_version']); - } - if (!is_null($cred['domain']) && strlen(trim($cred['domain'])) > 0) { - $auth_cloud['domain'] = new xmlrpcval($cred['domain']); - } - if (!is_null($cred['service_region']) && strlen(trim($cred['service_region'])) > 0) { - $auth_cloud['service_region'] = new xmlrpcval($cred['service_region']); - } - if (!is_null($cred['base_url']) && strlen(trim($cred['base_url'])) > 0) { - $auth_cloud['base_url'] = new xmlrpcval($cred['base_url']); + static public function connect($host, $port, $method) + { + return new self($host, $port, $method); + } + + public function __construct($host = "localhost", $port = 8800, $method = "http") + { + $this->_host = $host; + $this->_port = $port; + $this->_method = $method; + } + + public function get_auth_data() { + + $user = $_SESSION['user']; + $password = $_SESSION['password']; + // esto por si usamos la autorizacion del servidor web + //$user = $_SERVER['PHP_AUTH_USER'] + //$password = $_SERVER['PHP_AUTH_PW'] + + $auth = NULL; + $creds = get_credentials($user, $password); + if (!is_null($creds)) { + $auth = array(); + foreach ($creds as $cred) { + if ($cred['enabled']) { + $auth_cloud = array(); + $auth_cloud['type'] = new xmlrpcval($cred['type']); + if (!is_null($cred['id']) && strlen(trim($cred['id'])) > 0) { + $auth_cloud['id'] = new xmlrpcval($cred['id']); + } + if (!is_null($cred['host']) && strlen(trim($cred['host'])) > 0) { + $auth_cloud['host'] = new xmlrpcval($cred['host']); + } + if (!is_null($cred['username']) && strlen(trim($cred['username'])) > 0) { + $auth_cloud['username'] = new xmlrpcval($cred['username']); + } + if (!is_null($cred['password']) && strlen(trim($cred['password'])) > 0) { + $auth_cloud['password'] = new xmlrpcval($cred['password']); + } + if (!is_null($cred['proxy']) && strlen(trim($cred['proxy'])) > 0) { + $auth_cloud['proxy'] = new xmlrpcval($cred['proxy']); + } + if (!is_null($cred['token_type']) && strlen(trim($cred['token_type'])) > 0) { + $auth_cloud['token_type'] = new xmlrpcval($cred['token_type']); + } + if (!is_null($cred['project']) && strlen(trim($cred['project'])) > 0) { + $auth_cloud['project'] = new xmlrpcval($cred['project']); + } + if (!is_null($cred['public_key']) && strlen(trim($cred['public_key'])) > 0) { + $auth_cloud['public_key'] = new xmlrpcval($cred['public_key']); + } + if (!is_null($cred['private_key']) && strlen(trim($cred['private_key'])) > 0) { + $auth_cloud['private_key'] = new xmlrpcval($cred['private_key']); + } + if (!is_null($cred['certificate']) && strlen(trim($cred['certificate'])) > 0) { + $auth_cloud['password'] = new xmlrpcval($cred['certificate']); + } + if (!is_null($cred['tenant']) && strlen(trim($cred['tenant'])) > 0) { + $auth_cloud['tenant'] = new xmlrpcval($cred['tenant']); + } + if (!is_null($cred['project']) && strlen(trim($cred['project'])) > 0) { + $auth_cloud['project'] = new xmlrpcval($cred['project']); + } + if (!is_null($cred['subscription_id']) && strlen(trim($cred['subscription_id'])) > 0) { + $auth_cloud['subscription_id'] = new xmlrpcval($cred['subscription_id']); + } + if (!is_null($cred['auth_version']) && strlen(trim($cred['auth_version'])) > 0) { + $auth_cloud['auth_version'] = new xmlrpcval($cred['auth_version']); + } + if (!is_null($cred['domain']) && strlen(trim($cred['domain'])) > 0) { + $auth_cloud['domain'] = new xmlrpcval($cred['domain']); + } + if (!is_null($cred['service_region']) && strlen(trim($cred['service_region'])) > 0) { + $auth_cloud['service_region'] = new xmlrpcval($cred['service_region']); + } + if (!is_null($cred['base_url']) && strlen(trim($cred['base_url'])) > 0) { + $auth_cloud['base_url'] = new xmlrpcval($cred['base_url']); + } + $auth[] = new xmlrpcval($auth_cloud, "struct"); } - $auth[] = new xmlrpcval($auth_cloud, "struct"); } } + + return new xmlrpcval($auth, "array"); } - - return new xmlrpcval($auth, "array"); -} -function GetInfrastructureList($host, $port, $method) { - $auth = get_auth_data(); - - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureList', array($auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $list = $res[1]; + // helper function to make easier mocking + public function send_xmlrpc_call($xmlrpc_msg) { + return $xmlrpc_client->send($xmlrpc_msg); + } + + public function GetInfrastructureList() { + $auth = $this->get_auth_data(); + + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureList', array($auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($success) { - return $list; - } else { - return 'Error: ' . $inf_id; - } -} + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $list = $res[1]; + + if ($success) { + return $list; + } else { + return 'Error: ' . $inf_id; + } + } -function CreateInfrastructure($host, $port, $method, $radl) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('CreateInfrastructure', array(new xmlrpcval($radl, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $inf_id = $res[1]; + public function CreateInfrastructure($radl) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('CreateInfrastructure', array(new xmlrpcval($radl, "string"), $auth)); - if ($success) { - return $inf_id; - } else { - return 'Error: ' . $inf_id; - } -} + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $inf_id = $res[1]; + + if ($success) { + return $inf_id; + } else { + return 'Error: ' . $inf_id; + } + } -function DestroyInfrastructure($host, $port, $method, $id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('DestroyInfrastructure', array(new xmlrpcval($id, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $inf_id = $res[1]; + public function DestroyInfrastructure($id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('DestroyInfrastructure', array(new xmlrpcval($id, "string"), $auth)); - if ($success) { - return ""; - } else { - return 'Error: ' . $inf_id; - } -} + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $inf_id = $res[1]; + + if ($success) { + return ""; + } else { + return 'Error: ' . $inf_id; + } + } -function GetInfrastructureInfo($host, $port, $method, $id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureInfo', array(new xmlrpcval($id, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $inf_info = $res[1]; + public function GetInfrastructureInfo($id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureInfo', array(new xmlrpcval($id, "string"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $inf_info = $res[1]; + + if ($success) { + return $inf_info; + } else { + return 'Error'; + } + } + + public function GetInfrastructureContMsg($id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureContMsg', array(new xmlrpcval($id, "string"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $cont_msg = $res[1]; + if ($success) { - return $inf_info; + return $cont_msg; } else { return 'Error'; } -} - -function GetInfrastructureContMsg($host, $port, $method, $id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureContMsg', array(new xmlrpcval($id, "string"), $auth)); + } - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + public function GetVMInfo($inf_id, $vm_id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetVMInfo', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; + + if ($success) { + return $info; + } else { + return 'Error'; + } + } - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $cont_msg = $res[1]; + public function GetVMProperty($inf_id, $vm_id, $property) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetVMProperty', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), new xmlrpcval($property, "string"), $auth)); - if ($success) { - return $cont_msg; - } else { - return 'Error'; - } -} + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); -function GetVMInfo($host, $port, $method, $inf_id, $vm_id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetVMInfo', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); $success = $res[0]; $info = $res[1]; - + if ($success) { return $info; } else { return 'Error'; } -} + } -function GetVMProperty($host, $port, $method, $inf_id, $vm_id, $property) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetVMProperty', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), new xmlrpcval($property, "string"), $auth)); + public function GetVMContMsg($inf_id, $vm_id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetVMContMsg', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; - if ($success) { - return $info; - } else { - return 'Error'; - } -} + if ($success) { + return $info; + } else { + return 'Error'; + } + } -function GetVMContMsg($host, $port, $method, $inf_id, $vm_id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetVMContMsg', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); + public function StartVM($inf_id, $vm_id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('StartVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; - if ($success) { - return $info; - } else { - return 'Error'; - } -} + if ($success) { + return $info; + } else { + return 'Error'; + } + } -function StartVM($host, $port, $method, $inf_id, $vm_id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('StartVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); + public function StopVM($inf_id, $vm_id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('StopVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; - if ($success) { - return $info; - } else { - return 'Error'; - } -} + if ($success) { + return $info; + } else { + return 'Error'; + } + } -function StopVM($host, $port, $method, $inf_id, $vm_id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('StopVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth)); + public function AddResource($inf_id, $radl) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('AddResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; + + if ($success) { + return "OK"; + } else { + return 'Error'; + } + } - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + public function RemoveResource($inf_id, $vm_list) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('RemoveResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_list, "string"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; + + if ($success) { + return $info; + } else { + return 'Error'; + } + } - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + public function Reconfigure($inf_id, $radl) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('Reconfigure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth)); - if ($success) { - return $info; - } else { - return 'Error'; - } -} + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); -function AddResource($host, $port, $method, $inf_id, $radl) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('AddResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); $success = $res[0]; $info = $res[1]; - + if ($success) { - return "OK"; + return $info; } else { return 'Error'; } -} + } -function RemoveResource($host, $port, $method, $inf_id, $vm_list) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('RemoveResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_list, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); + public function ExportInfrastructure($inf_id, $delete) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('ExportInfrastructure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($delete, "boolean"), $auth)); + + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); + + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); $success = $res[0]; $info = $res[1]; - + if ($success) { return $info; } else { return 'Error'; } -} - -function Reconfigure($host, $port, $method, $inf_id, $radl) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('Reconfigure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; - - if ($success) { - return $info; - } else { - return 'Error'; - } -} - -function ExportInfrastructure($host, $port, $method, $inf_id, $delete) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('ExportInfrastructure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($delete, "boolean"), $auth)); + } - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + public function ImportInfrastructure($inf_str) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('ImportInfrastructure', array(new xmlrpcval($inf_str, "string"), $auth)); - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($success) { - return $info; - } else { - return 'Error'; - } -} + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $info = $res[1]; -function ImportInfrastructure($host, $port, $method, $inf_str) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('ImportInfrastructure', array(new xmlrpcval($inf_str, "string"), $auth)); + if ($success) { + return $info; + } else { + return 'Error'; + } + } - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); + public function GetInfrastructureState($id) { + $auth = $this->get_auth_data(); + $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method); + $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureState', array(new xmlrpcval($id, "string"), $auth)); - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $info = $res[1]; + $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg); - if ($success) { - return $info; - } else { - return 'Error'; - } -} + if ($xmlrpc_resp->faultCode()) + return 'Error: ' . $xmlrpc_resp->faultString(); + else + $res = php_xmlrpc_decode($xmlrpc_resp->value()); + $success = $res[0]; + $state = $res[1]['state']; -function GetInfrastructureState($host, $port, $method, $id) { - $auth = get_auth_data(); - $xmlrpc_client = new xmlrpc_client('/',$host,$port,$method); - $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureState', array(new xmlrpcval($id, "string"), $auth)); - - $xmlrpc_resp = $xmlrpc_client->send($xmlrpc_msg); - - if ($xmlrpc_resp->faultCode()) - return 'Error: ' . $xmlrpc_resp->faultString(); - else - $res = php_xmlrpc_decode($xmlrpc_resp->value()); - $success = $res[0]; - $state = $res[1]['state']; - - if ($success) { - return $state; - } else { - return 'Error'; - } + if ($success) { + return $state; + } else { + return 'Error'; + } + } } ?> diff --git a/im.php b/im.php index 33d9991..81ba055 100644 --- a/im.php +++ b/im.php @@ -1,9 +1,19 @@ diff --git a/list.php b/list.php index 8cb829a..b2956dc 100644 --- a/list.php +++ b/list.php @@ -30,7 +30,7 @@ } else { include('im.php'); include('config.php'); - $res = GetInfrastructureList($im_host,$im_port,$im_method); + $res = GetIM()->GetInfrastructureList(); if (is_string($res) and strpos($res, "Error") !== false) { header('Location: error.php?msg=' . $res); @@ -169,13 +169,13 @@ function confirm_delete(url, id) { GetInfrastructureInfo($im_method,$inf); if (is_string($vm_list) || count($vm_list) == 0) { $vm_list = array("N/A"); } - $state = GetInfrastructureState($im_host,$im_port,$im_method,$inf); + $state = GetIM()->GetInfrastructureState($im_method,$inf); $status = "N/A"; if (!(is_string($state) && strpos($state, "Error") !== false)) { $status = formatState($state); diff --git a/operate.php b/operate.php index e405668..1ee9977 100644 --- a/operate.php +++ b/operate.php @@ -38,7 +38,7 @@ if ($op == "create") { $radl = $_POST['radl']; - $res = CreateInfrastructure($im_host,$im_port,$im_method,$radl); + $res = GetIM()->CreateInfrastructure($radl); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -48,7 +48,7 @@ } elseif ($op == "destroy") { if (isset($_GET['id'])) { $id = $_GET['id']; - $res = DestroyInfrastructure($im_host,$im_port,$im_method,$id); + $res = GetIM()->DestroyInfrastructure($id); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -63,7 +63,7 @@ $infid = $_GET['infid']; $vmid = $_GET['vmid']; - $res = RemoveResource($im_host,$im_port,$im_method,$infid, $vmid); + $res = GetIM()->RemoveResource($infid, $vmid); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -78,7 +78,7 @@ $infid = $_GET['infid']; $vmid = $_GET['vmid']; - $res = StopVM($im_host,$im_port,$im_method,$infid, $vmid); + $res = GetIM()->StopVM($infid, $vmid); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -93,7 +93,7 @@ $infid = $_GET['infid']; $vmid = $_GET['vmid']; - $res = StartVM($im_host,$im_port,$im_method,$infid, $vmid); + $res = GetIM()->StartVM($infid, $vmid); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -109,7 +109,7 @@ if (isset($_POST['infid'])) { $infid = $_POST['infid']; - $res = AddResource($im_host,$im_port,$im_method,$infid, $radl); + $res = GetIM()->AddResource($infid, $radl); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); @@ -123,7 +123,7 @@ if (isset($_GET['infid'])) { $infid = $_GET['infid']; - $res = Reconfigure($im_host,$im_port,$im_method,$infid, ""); + $res = GetIM()->Reconfigure($infid, ""); if (strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); diff --git a/radlinfo.php b/radlinfo.php index 4b82efa..5732f84 100644 --- a/radlinfo.php +++ b/radlinfo.php @@ -121,7 +121,7 @@ // tenemos todos los parametros, asi que lanzamos el RADL substituido if ($params_ok) { - $res = CreateInfrastructure($im_host,$im_port,$im_method,$radl['radl']); + $res = GetIM()->CreateInfrastructure($radl['radl']); if (strpos($res, "Error") === False) { header('Location: list.php'); @@ -135,7 +135,7 @@ } } else { // no tenemos parametros - $res = CreateInfrastructure($im_host,$im_port,$im_method,$radl['radl']); + $res = GetIM()->CreateInfrastructure($radl['radl']); if (strpos($res, "Error") === False) { header('Location: list.php'); diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php index bd5f362..5b2574f 100644 --- a/test/unit/bootstrap.php +++ b/test/unit/bootstrap.php @@ -6,4 +6,6 @@ include_once('../../cred.php'); include_once('../../group.php'); include_once('../../format.php'); +include_once('../../im-rest.php'); +include_once('../../im-xml-rpc.php'); ?> diff --git a/test/unit/test_format.php b/test/unit/test_format.php new file mode 100644 index 0000000..c551d11 --- /dev/null +++ b/test/unit/test_format.php @@ -0,0 +1,144 @@ +assertEquals($res, "configuring"); + $res = formatState("unknown"); + $this->assertEquals($res, "unknown"); + $res = formatState("failed"); + $this->assertEquals($res, "failed"); + $res = formatState("configured"); + $this->assertEquals($res, "configured"); + } + + public function testFormatCloud() + { + $tokens = array("provider.type"=>"EC2","provider.host"=>"host"); + $res = formatCloud($tokens); + $this->assertEquals($res, "EC2"); + + $tokens = array("provider.type"=>"OpenNebula","provider.host"=>"host","provider.port"=>"2633"); + $res = formatCloud($tokens); + $this->assertEquals($res, "OpenNebula
host:2633
"); + } + + public function testFormatIPs() + { + $tokens = array("net_interface.0.ip"=>"10.0.0.1", "net_interface.1.ip"=>"10.0.0.2"); + $res = formatIPs($tokens); + $this->assertEquals("0 => 10.0.0.1
1 => 10.0.0.2
", $res); + } + + public function testFormatApps() + { + $res = formatAplication("(name = 'tomcat' and version = '7.0' and path = '/var/lib/tomcat')"); + $this->assertEquals("tomcat v. 7.0 (/var/lib/tomcat)", $res); + } + + public function testFormatOutPorts() + { + $radl = "network publica (outbound = 'yes' and outports = '8080-8080,22-22')\n"; + $outports = getOutPorts($radl); + $this->assertEquals(array("8080"=>"8080","22"=>"22"),$outports); + $res = formatOutPorts($outports); + $this->assertEquals("8080 => 8080
\n22 => 22
\n", $res); + } + + public function testFormatRADL() + { + $radl = "" . + "network publica (outbound = 'yes') " . + "network privada ( ) " . + "system front ( " . + "cpu.arch='x86_64' and " . + "cpu.count>=1 and " . + "memory.size>=512m and " . + "net_interface.1.connection = 'publica' and " . + "net_interface.0.connection = 'privada' and " . + "net_interface.0.dns_name = 'front' and " . + "disk.0.os.flavour='centos' and " . + "disk.0.os.version>='7' and " . + "disk.0.os.name = 'linux' and " . + "disk.0.applications contains (name = 'ansible.modules.grycap.octave') and " . + "disk.0.applications contains (name = 'gmetad') and " . + "disk.0.os.credentials.private_key = 'priv' and " . + "disk.1.size=1GB and " . + "disk.1.device='hdb' and " . + "disk.1.fstype='ext4' and " . + "disk.1.mount_path='/mnt/disk'" . + ")"; + + $expected_res = " +cpu.arch='x86_64' +

+
+
+cpu.count>=1
+

+
+
+memory.size>=512m
+

+
+
+disk.0.os.flavour='centos'
+

+
+
+disk.0.os.version>='7'
+

+
+
+disk.0.os.name
+
linux
+ + +disk.0.applications +
gmetad
+ + +disk.0.os.credentials.private_key +Download + + +disk.1.size=1GB +

+
+
+disk.1.dev
+

+
+";
+
+        $res = parseRADL($radl);
+        $res = formatRADL($res);
+        $this->assertEquals($expected_res, $res);
+    }
+    
+    
+}
diff --git a/test/unit/test_rest.php b/test/unit/test_rest.php
new file mode 100644
index 0000000..932a4a5
--- /dev/null
+++ b/test/unit/test_rest.php
@@ -0,0 +1,23 @@
+"admin", "password"=>"admin");
+
+        $resp = new Http_response(200, "infid1\ninfid2");
+
+        $im = $this->getMockBuilder(IMRest::class)
+            ->setMethods(['BasicRESTCall'])
+            ->getMock();
+        $im->method('BasicRESTCall')
+            ->willReturn($resp);
+
+        $res = $im->GetInfrastructureList();
+        $this->assertEquals(array("infid1","infid2"), $res);
+    }
+}
diff --git a/test/unit/test_xml.php b/test/unit/test_xml.php
new file mode 100644
index 0000000..57e8b4b
--- /dev/null
+++ b/test/unit/test_xml.php
@@ -0,0 +1,31 @@
+"admin", "password"=>"admin");
+
+        $val1 = new xmlrpcval(true);
+        $val2 = new xmlrpcval(array(new xmlrpcval("infid1"), new xmlrpcval("infid2")), "array");
+        $value = new xmlrpcval(array($val1, $val2), "array");
+
+        $resp = $this->createMock(xmlrpcresp::class);
+        $resp->method('faultCode')
+            ->willReturn(false);
+        $resp->method('value')
+            ->willReturn($value);
+
+        $im = $this->getMockBuilder(IMXML::class)
+            ->setMethods(['send_xmlrpc_call'])
+            ->getMock();
+        $im->method('send_xmlrpc_call')
+            ->willReturn($resp);
+
+        $res = $im->GetInfrastructureList();
+        $this->assertEquals(array("infid1","infid2"), $res);
+    }
+}

From 8793eeaaba37b538572d36218573df12f25ba76c Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 10:18:44 +0100
Subject: [PATCH 04/29] Fix error

---
 im-xml-rpc.php | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/im-xml-rpc.php b/im-xml-rpc.php
index b57dab5..b7d9142 100644
--- a/im-xml-rpc.php
+++ b/im-xml-rpc.php
@@ -95,13 +95,13 @@ public function get_auth_data() {
 
     // helper function to make easier mocking
     public function send_xmlrpc_call($xmlrpc_msg) {
+        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
         return $xmlrpc_client->send($xmlrpc_msg);
     }
 
     public function GetInfrastructureList() {
         $auth = $this->get_auth_data();
 
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
         $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureList', array($auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -122,7 +122,7 @@ public function GetInfrastructureList() {
 
     public function CreateInfrastructure($radl) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('CreateInfrastructure', array(new xmlrpcval($radl, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -143,7 +143,7 @@ public function CreateInfrastructure($radl) {
 
     public function DestroyInfrastructure($id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('DestroyInfrastructure', array(new xmlrpcval($id, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -164,7 +164,7 @@ public function DestroyInfrastructure($id) {
 
     public function GetInfrastructureInfo($id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureInfo', array(new xmlrpcval($id, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -185,7 +185,7 @@ public function GetInfrastructureInfo($id) {
 
     public function GetInfrastructureContMsg($id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureContMsg', array(new xmlrpcval($id, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -206,7 +206,7 @@ public function GetInfrastructureContMsg($id) {
 
     public function GetVMInfo($inf_id, $vm_id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetVMInfo', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -227,7 +227,7 @@ public function GetVMInfo($inf_id, $vm_id) {
 
     public function GetVMProperty($inf_id, $vm_id, $property) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetVMProperty', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), new xmlrpcval($property, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -248,7 +248,7 @@ public function GetVMProperty($inf_id, $vm_id, $property) {
 
     public function GetVMContMsg($inf_id, $vm_id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetVMContMsg', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -269,7 +269,7 @@ public function GetVMContMsg($inf_id, $vm_id) {
 
     public function StartVM($inf_id, $vm_id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('StartVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -290,7 +290,7 @@ public function StartVM($inf_id, $vm_id) {
 
     public function StopVM($inf_id, $vm_id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('StopVM', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_id, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -311,7 +311,7 @@ public function StopVM($inf_id, $vm_id) {
 
     public function AddResource($inf_id, $radl) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('AddResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -332,7 +332,7 @@ public function AddResource($inf_id, $radl) {
 
     public function RemoveResource($inf_id, $vm_list) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('RemoveResource', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($vm_list, "string"), $auth));
         
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -353,7 +353,7 @@ public function RemoveResource($inf_id, $vm_list) {
 
     public function Reconfigure($inf_id, $radl) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('Reconfigure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($radl, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -374,7 +374,7 @@ public function Reconfigure($inf_id, $radl) {
 
     public function ExportInfrastructure($inf_id, $delete) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('ExportInfrastructure', array(new xmlrpcval($inf_id, "string"), new xmlrpcval($delete, "boolean"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -395,7 +395,7 @@ public function ExportInfrastructure($inf_id, $delete) {
 
     public function ImportInfrastructure($inf_str) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('ImportInfrastructure', array(new xmlrpcval($inf_str, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);
@@ -416,7 +416,7 @@ public function ImportInfrastructure($inf_str) {
 
     public function GetInfrastructureState($id) {
         $auth = $this->get_auth_data();
-        $xmlrpc_client = new xmlrpc_client('/',$this->_host,$this->_port,$this->_method);
+        
         $xmlrpc_msg = new xmlrpcmsg('GetInfrastructureState', array(new xmlrpcval($id, "string"), $auth));
 
         $xmlrpc_resp = $this->send_xmlrpc_call($xmlrpc_msg);

From e14e1d439b2707fc8af06d4226dc4c80a24622e9 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 10:41:11 +0100
Subject: [PATCH 05/29] Fix error

---
 im.php | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/im.php b/im.php
index 81ba055..6f42f4c 100644
--- a/im.php
+++ b/im.php
@@ -1,7 +1,7 @@
 
+?>
\ No newline at end of file

From 1ce5c9d3198278a96dba66b776e09d8966c98f7b Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 10:47:22 +0100
Subject: [PATCH 06/29] Fix error

---
 im.php | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/im.php b/im.php
index 6f42f4c..9eedeb2 100644
--- a/im.php
+++ b/im.php
@@ -1,12 +1,13 @@
 
Date: Tue, 14 Nov 2017 10:58:54 +0100
Subject: [PATCH 07/29] Fix error

---
 list.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/list.php b/list.php
index b2956dc..5b7d726 100644
--- a/list.php
+++ b/list.php
@@ -169,13 +169,13 @@ function confirm_delete(url, id) {
     GetInfrastructureInfo($im_method,$inf);
+                    $vm_list = GetIM()->GetInfrastructureInfo($inf);
 
                     if (is_string($vm_list) || count($vm_list) == 0) {
 						$vm_list = array("N/A");
 					}
 
-					$state = GetIM()->GetInfrastructureState($im_method,$inf);
+					$state = GetIM()->GetInfrastructureState($inf);
 					$status = "N/A";
                    	if (!(is_string($state) && strpos($state, "Error") !== false)) {
 						$status = formatState($state);

From 9cb18e9fc8cefb41a4d0a0554c1feb9f29b1a7ab Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 11:09:37 +0100
Subject: [PATCH 08/29] Increase tests

---
 test/unit/test_rest.php | 50 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/test/unit/test_rest.php b/test/unit/test_rest.php
index 932a4a5..2c7c081 100644
--- a/test/unit/test_rest.php
+++ b/test/unit/test_rest.php
@@ -5,7 +5,7 @@
 final class RESTTest extends TestCase
 {
 
-    public function testREST()
+    public function testGetInfrastructureList()
     {
         $_SESSION = array("user"=>"admin", "password"=>"admin");
 
@@ -20,4 +20,52 @@ public function testREST()
         $res = $im->GetInfrastructureList();
         $this->assertEquals(array("infid1","infid2"), $res);
     }
+
+    public function testGetInfrastructureInfo()
+    {
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+
+        $resp = new Http_response(200, "vmid1\nvmid2");
+
+        $im = $this->getMockBuilder(IMRest::class)
+            ->setMethods(['BasicRESTCall'])
+            ->getMock();
+        $im->method('BasicRESTCall')
+            ->willReturn($resp);
+
+        $res = $im->GetInfrastructureInfo("infid1");
+        $this->assertEquals(array("vmid1","vmid2"), $res);
+    }
+
+    public function testGetInfrastructureState()
+    {
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+
+        $resp = new Http_response(200, '{"state":{"state": "running", "vm_states" : {"vmid1": "running", "vmid2": "running"}}}');
+
+        $im = $this->getMockBuilder(IMRest::class)
+            ->setMethods(['BasicRESTCall'])
+            ->getMock();
+        $im->method('BasicRESTCall')
+            ->willReturn($resp);
+
+        $res = $im->GetInfrastructureState("infid1");
+        $this->assertEquals("running", $res);
+    }
+
+    public function testDestroyInfrastructure()
+    {
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+
+        $resp = new Http_response(200, '');
+
+        $im = $this->getMockBuilder(IMRest::class)
+            ->setMethods(['BasicRESTCall'])
+            ->getMock();
+        $im->method('BasicRESTCall')
+            ->willReturn($resp);
+
+        $res = $im->DestroyInfrastructure("infid1");
+        $this->assertEquals("", $res);
+    }
 }

From 27e5363847389028245c99877ad30334864d0da9 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 12:17:49 +0100
Subject: [PATCH 09/29] Fix error

---
 im-rest.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/im-rest.php b/im-rest.php
index f322035..907e010 100644
--- a/im-rest.php
+++ b/im-rest.php
@@ -187,7 +187,7 @@ public function GetContentType($content) {
 	}
 
 	public function CreateInfrastructure($radl) {
-		$headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl));
+		$headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . $this->GetContentType($radl));
 		$res = $this->BasicRESTCall("POST", '/infrastructures', $headers, $radl);
 		return $res->getOutput();
 	}
@@ -205,7 +205,7 @@ public function StopVM($inf_id, $vm_id) {
 	}
 
 	public function AddResource($inf_id, $radl) {
-		$headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . GetContentType($radl));
+		$headers = array('Accept: text/*', 'Content-Length: ' . strlen($radl), 'Content-Type: ' . $this->GetContentType($radl));
 		$res = $this->BasicRESTCall("POST", '/infrastructures/' . $inf_id, $headers, $radl);
 		return $res->getOutput();
 	}

From 6366adf874ec5bfd63ab9a63dab70227f5aa47e9 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 12:17:55 +0100
Subject: [PATCH 10/29] Increase tests

---
 test/unit/test_rest.php | 135 +++++++++++++++++++++++++++++++---------
 1 file changed, 104 insertions(+), 31 deletions(-)

diff --git a/test/unit/test_rest.php b/test/unit/test_rest.php
index 2c7c081..b0d0362 100644
--- a/test/unit/test_rest.php
+++ b/test/unit/test_rest.php
@@ -5,67 +5,140 @@
 final class RESTTest extends TestCase
 {
 
-    public function testGetInfrastructureList()
-    {
+    private function getIM($value, $status = 200) {
         $_SESSION = array("user"=>"admin", "password"=>"admin");
 
-        $resp = new Http_response(200, "infid1\ninfid2");
-
+        $resp = new Http_response($status, $value);
+        
         $im = $this->getMockBuilder(IMRest::class)
             ->setMethods(['BasicRESTCall'])
             ->getMock();
         $im->method('BasicRESTCall')
             ->willReturn($resp);
 
-        $res = $im->GetInfrastructureList();
-        $this->assertEquals(array("infid1","infid2"), $res);
+        return $im;        
     }
 
-    public function testGetInfrastructureInfo()
+    public function test_get_auth_data()
     {
         $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $im = IMRest::connect("","");
+        $res = $im->get_auth_data();
+        $this->assertEquals("type = InfrastructureManager; username = admin; password = admin\\ntype = VMRC; host = http://servproject.i3m.upv.es:8080/vmrc/vmrc; username = micafer; password = ttt25\\n", $res);
+    }
 
-        $resp = new Http_response(200, "vmid1\nvmid2");
+    public function testBasicRESTCall()
+    {
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $im = IMRest::connect("localhost","8899");
+        $headers = array('Accept: text/*');
+        $res = $im->BasicRESTCall("GET", '/infrastructures', $headers);
+        $this->assertEquals(0, $res->getStatus());
+    }
 
-        $im = $this->getMockBuilder(IMRest::class)
-            ->setMethods(['BasicRESTCall'])
-            ->getMock();
-        $im->method('BasicRESTCall')
-            ->willReturn($resp);
+    public function testGetInfrastructureList()
+    {
+        $im = $this->getIM("infid1\ninfid2");
+        $res = $im->GetInfrastructureList();
+        $this->assertEquals(array("infid1","infid2"), $res);
+    }
 
+    public function testGetInfrastructureInfo()
+    {
+        $im = $this->getIM("vmid1\nvmid2");
         $res = $im->GetInfrastructureInfo("infid1");
         $this->assertEquals(array("vmid1","vmid2"), $res);
     }
 
     public function testGetInfrastructureState()
     {
-        $_SESSION = array("user"=>"admin", "password"=>"admin");
-
-        $resp = new Http_response(200, '{"state":{"state": "running", "vm_states" : {"vmid1": "running", "vmid2": "running"}}}');
-
-        $im = $this->getMockBuilder(IMRest::class)
-            ->setMethods(['BasicRESTCall'])
-            ->getMock();
-        $im->method('BasicRESTCall')
-            ->willReturn($resp);
-
+        $im = $this->getIM('{"state":{"state": "running", "vm_states" : {"vmid1": "running", "vmid2": "running"}}}');
         $res = $im->GetInfrastructureState("infid1");
         $this->assertEquals("running", $res);
     }
 
     public function testDestroyInfrastructure()
     {
-        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $im = $this->getIM("");
+        $res = $im->DestroyInfrastructure("infid1");
+        $this->assertEquals("", $res);
+    }
 
-        $resp = new Http_response(200, '');
+    public function testGetVMInfo()
+    {
+        $im = $this->getIM("vminfo");
+        $res = $im->GetVMInfo("infid1", "vmid1");
+        $this->assertEquals("vminfo", $res);
+    }
 
-        $im = $this->getMockBuilder(IMRest::class)
-            ->setMethods(['BasicRESTCall'])
-            ->getMock();
-        $im->method('BasicRESTCall')
-            ->willReturn($resp);
+    public function testGetInfrastructureContMsg()
+    {
+        $im = $this->getIM("contmsg");
+        $res = $im->GetInfrastructureContMsg("infid1");
+        $this->assertEquals("contmsg", $res);
+    }
 
-        $res = $im->DestroyInfrastructure("infid1");
+    public function testGetVMProperty()
+    {
+        $im = $this->getIM("vmprop");
+        $res = $im->GetVMProperty("infid1", "vmid1", "prop");
+        $this->assertEquals("vmprop", $res);
+    }
+
+    public function testGetVMContMsg()
+    {
+        $im = $this->getIM("contmsg");
+        $res = $im->GetVMContMsg("infid1", "vmid1");
+        $this->assertEquals("contmsg", $res);
+    }
+
+    public function testCreateInfrastructure()
+    {
+        $im = $this->getIM("infid");
+        $res = $im->CreateInfrastructure("radl");
+        $this->assertEquals("infid", $res);
+    }
+
+    public function testStartVM()
+    {
+        $im = $this->getIM("");
+        $res = $im->StartVM("infid", "vmid");
         $this->assertEquals("", $res);
     }
+
+    public function testStopVM()
+    {
+        $im = $this->getIM("");
+        $res = $im->StopVM("infid", "vmid");
+        $this->assertEquals("", $res);
+    }
+
+    public function testAddResource()
+    {
+        $im = $this->getIM("vmid1\nvmid2");
+        $res = $im->AddResource("infid", "tosca_definitions_version: ");
+        $this->assertEquals("vmid1\nvmid2", $res);
+    }
+
+    public function testRemoveResource()
+    {
+        $im = $this->getIM("");
+        $res = $im->RemoveResource("infid", "vmid");
+        $this->assertEquals("", $res);
+    }
+
+    public function testReconfigure()
+    {
+        $im = $this->getIM("");
+        $res = $im->Reconfigure("infid", "vmid");
+        $this->assertEquals("", $res);
+    }
+
+    public function testGetOutputs()
+    {
+        $im = $this->getIM('{"outputs": {"key": "value"}}');
+        $res = $im->GetOutputs("infid");
+        $this->assertEquals(array("key"=>"value"), $res);
+    }
+    
 }

From 0a11aa57b04217f1bd38ec10c4d61709831506f2 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 12:37:46 +0100
Subject: [PATCH 11/29] Fix error

---
 test/unit/test_cred.php   | 1 +
 test/unit/test_db.php     | 1 +
 test/unit/test_format.php | 1 +
 test/unit/test_grp.php    | 1 +
 test/unit/test_http.php   | 1 +
 test/unit/test_rest.php   | 1 +
 test/unit/test_user.php   | 1 +
 7 files changed, 7 insertions(+)

diff --git a/test/unit/test_cred.php b/test/unit/test_cred.php
index f86f97b..519ab7c 100644
--- a/test/unit/test_cred.php
+++ b/test/unit/test_cred.php
@@ -50,3 +50,4 @@ public function testManageCreds()
         $this->assertEquals(NULL, $res);
     }
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_db.php b/test/unit/test_db.php
index b71874a..abc98eb 100644
--- a/test/unit/test_db.php
+++ b/test/unit/test_db.php
@@ -39,3 +39,4 @@ public function testDB()
     }
 
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_format.php b/test/unit/test_format.php
index c551d11..defc5ac 100644
--- a/test/unit/test_format.php
+++ b/test/unit/test_format.php
@@ -142,3 +142,4 @@ function createDownloadLink(anchorSelector, str, fileName){
     
     
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_grp.php b/test/unit/test_grp.php
index 78b9ee1..2266095 100644
--- a/test/unit/test_grp.php
+++ b/test/unit/test_grp.php
@@ -30,3 +30,4 @@ public function testManageGroups()
         $this->assertEquals(NULL, $res);
     }
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_http.php b/test/unit/test_http.php
index e46c79b..46026a7 100644
--- a/test/unit/test_http.php
+++ b/test/unit/test_http.php
@@ -38,3 +38,4 @@ public function testExec()
     }
 
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_rest.php b/test/unit/test_rest.php
index b0d0362..cefff2d 100644
--- a/test/unit/test_rest.php
+++ b/test/unit/test_rest.php
@@ -142,3 +142,4 @@ public function testGetOutputs()
     }
     
 }
+?>
\ No newline at end of file
diff --git a/test/unit/test_user.php b/test/unit/test_user.php
index 3eb3413..4f8c8e4 100644
--- a/test/unit/test_user.php
+++ b/test/unit/test_user.php
@@ -81,3 +81,4 @@ public function testManageUsers()
         $this->assertEquals("", $res);
     }
 }
+?>
\ No newline at end of file

From b8adc1fcaae691a408a126320a9251dca6f2fcce Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 12:37:53 +0100
Subject: [PATCH 12/29] Increase tests

---
 test/unit/test_xml.php | 139 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 133 insertions(+), 6 deletions(-)

diff --git a/test/unit/test_xml.php b/test/unit/test_xml.php
index 57e8b4b..bb96244 100644
--- a/test/unit/test_xml.php
+++ b/test/unit/test_xml.php
@@ -4,13 +4,11 @@
 
 final class XMLTest extends TestCase
 {
-
-    public function testXML()
-    {
+    private function getIM($value, $success=true) {
         $_SESSION = array("user"=>"admin", "password"=>"admin");
-
-        $val1 = new xmlrpcval(true);
-        $val2 = new xmlrpcval(array(new xmlrpcval("infid1"), new xmlrpcval("infid2")), "array");
+        
+        $val1 = new xmlrpcval($success);
+        $val2 = $value;
         $value = new xmlrpcval(array($val1, $val2), "array");
 
         $resp = $this->createMock(xmlrpcresp::class);
@@ -25,7 +23,136 @@ public function testXML()
         $im->method('send_xmlrpc_call')
             ->willReturn($resp);
 
+        return $im;        
+    }
+
+    public function testGetInfrastructureList()
+    {
+        $value = new xmlrpcval(array(new xmlrpcval("infid1"), new xmlrpcval("infid2")), "array");
+        $im = $this->getIM($value);
         $res = $im->GetInfrastructureList();
         $this->assertEquals(array("infid1","infid2"), $res);
     }
+
+    public function testCreateInfrastructure()
+    {
+        $value = new xmlrpcval("infid1");
+        $im = $this->getIM($value);
+        $res = $im->CreateInfrastructure("radl");
+        $this->assertEquals("infid1", $res);
+    }
+
+    public function testDestroyInfrastructure()
+    {
+        $value = new xmlrpcval("");
+        $im = $this->getIM($value);
+        $res = $im->DestroyInfrastructure("radl");
+        $this->assertEquals("", $res);
+    }
+
+    public function testGetInfrastructureInfo()
+    {
+        $value = new xmlrpcval(array(new xmlrpcval("vmid1"), new xmlrpcval("vmid2")), "array");
+        $im = $this->getIM($value);
+        $res = $im->GetInfrastructureInfo("radl");
+        $this->assertEquals(array("vmid1","vmid2"), $res);
+    }
+
+    public function testGetInfrastructureContMsg()
+    {
+        $value = new xmlrpcval("contmsg");
+        $im = $this->getIM($value);
+        $res = $im->GetInfrastructureContMsg("radl");
+        $this->assertEquals("contmsg", $res);
+    }
+
+    public function testGetVMInfo()
+    {
+        $value = new xmlrpcval("vminfo");
+        $im = $this->getIM($value);
+        $res = $im->GetVMInfo("infid", "vmid");
+        $this->assertEquals("vminfo", $res);
+    }
+
+    public function testGetVMProperty()
+    {
+        $value = new xmlrpcval("vmprop");
+        $im = $this->getIM($value);
+        $res = $im->GetVMProperty("infid", "vmid", "prop");
+        $this->assertEquals("vmprop", $res);
+    }
+
+    public function testGetVMContMsg()
+    {
+        $value = new xmlrpcval("contmsg");
+        $im = $this->getIM($value);
+        $res = $im->GetVMContMsg("infid", "vmid");
+        $this->assertEquals("contmsg", $res);
+    }
+
+    public function testStartVM()
+    {
+        $value = new xmlrpcval("");
+        $im = $this->getIM($value);
+        $res = $im->StartVM("infid", "vmid");
+        $this->assertEquals("", $res);
+    }
+
+    public function testStopVM()
+    {
+        $value = new xmlrpcval("");
+        $im = $this->getIM($value);
+        $res = $im->StopVM("infid", "vmid");
+        $this->assertEquals("", $res);
+    }
+
+    public function testAddResource()
+    {
+        $value = new xmlrpcval("");
+        $im = $this->getIM($value);
+        $res = $im->AddResource("infid", "radl");
+        $this->assertEquals("OK", $res);
+    }
+
+    public function testRemoveResource()
+    {
+        $value = new xmlrpcval(1);
+        $im = $this->getIM($value);
+        $res = $im->RemoveResource("infid", "vmid");
+        $this->assertEquals(1, $res);
+    }
+
+    public function testReconfigure()
+    {
+        $value = new xmlrpcval("");
+        $im = $this->getIM($value);
+        $res = $im->Reconfigure("infid", "radl");
+        $this->assertEquals("", $res);
+    }
+
+    public function testExportInfrastructure()
+    {
+        $value = new xmlrpcval("ok");
+        $im = $this->getIM($value);
+        $res = $im->ExportInfrastructure("infid", true);
+        $this->assertEquals("ok", $res);
+    }
+
+    public function testImportInfrastructure()
+    {
+        $value = new xmlrpcval("ok");
+        $im = $this->getIM($value);
+        $res = $im->ImportInfrastructure("str_inf");
+        $this->assertEquals("ok", $res);
+    }
+
+    public function testGetInfrastructureState()
+    {
+        $run = new xmlrpcval("running");
+        $value = new xmlrpcval(array("state"=>$run),"struct");
+        $im = $this->getIM($value);
+        $res = $im->GetInfrastructureState("infid");
+        $this->assertEquals("running", $res);
+    }
 }
+?>
\ No newline at end of file

From 782822d15b730208b7b0e0658ca592fa5868bb03 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 13:04:45 +0100
Subject: [PATCH 13/29] Fix error

---
 operate.php | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/operate.php b/operate.php
index 1ee9977..d6a4ace 100644
--- a/operate.php
+++ b/operate.php
@@ -17,12 +17,12 @@
  along with this program.  If not, see .
 */
 
-    include('im.php');
-    include('config.php');
+    include_once('im.php');
+    include_once('config.php');
  
     if(!isset($_SESSION)) session_start();   
     
-    include('user.php');
+    include_once('user.php');
     if (!check_session_user()) {
 	header('Location: index.php?error=Invalid User');
     } else {
@@ -32,8 +32,7 @@
         } elseif (isset($_GET['op'])) {
             $op = $_GET['op'];
         }
-        
-        
+
         if (strlen($op) > 0) {
             if ($op == "create") {
                 $radl = $_POST['radl'];

From aa3b2e63003c1758d7422ab35f20ed84ebd69591 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 13:04:53 +0100
Subject: [PATCH 14/29] Increase tests

---
 test/unit/test_operate.php | 104 +++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
 create mode 100644 test/unit/test_operate.php

diff --git a/test/unit/test_operate.php b/test/unit/test_operate.php
new file mode 100644
index 0000000..4d4064a
--- /dev/null
+++ b/test/unit/test_operate.php
@@ -0,0 +1,104 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreate()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"create");
+        $_POST = array("radl"=>"radl");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testDestroy()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"destroy", "id"=>"id");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testDestroyVM()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"destroyvm", "infid"=>"id", "vmid"=>"vid");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testStopVM()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"stopvm", "infid"=>"id", "vmid"=>"vid");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testStartVM()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"startvm", "infid"=>"id", "vmid"=>"vid");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testAddResource()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"addresource");
+        $_POST = array("radl"=>"radl", "infid"=>"id");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testReconfigure()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"reconfigure", "infid"=>"id");
+        include('../../operate.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+}
+?>
\ No newline at end of file

From d3dbe2086a6da4995fcbf237b5c6523b1654ca35 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 13:26:21 +0100
Subject: [PATCH 15/29] Increase tests

---
 test/unit/bootstrap.php |  1 +
 test/unit/test_radl.php | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 test/unit/test_radl.php

diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php
index 5b2574f..81975e6 100644
--- a/test/unit/bootstrap.php
+++ b/test/unit/bootstrap.php
@@ -8,4 +8,5 @@
 include_once('../../format.php');
 include_once('../../im-rest.php');
 include_once('../../im-xml-rpc.php');
+include_once('../../radl.php');
 ?>
diff --git a/test/unit/test_radl.php b/test/unit/test_radl.php
new file mode 100644
index 0000000..11662c9
--- /dev/null
+++ b/test/unit/test_radl.php
@@ -0,0 +1,41 @@
+assertEquals("", $res);
+
+        $res = get_radls("admin");
+        $rowid = $res[0]["rowid"];
+        $this->assertEquals("radltest", $res[0]["name"]);
+
+        $username = uniqid();
+        $res = insert_user($username, "pass", array('users'), '');
+        $this->assertEquals($res, "");
+
+        $res = radl_user_can($rowid, $username, "r");
+        $this->assertEquals(true, $res);
+
+        $res = edit_radl($rowid, "newname", "radldesc", "radlbody", '0', '0', '0', '0', '0', '0', '0');
+        $this->assertEquals("", $res);
+
+        $res = radl_user_can($rowid, $username, "r");
+        $this->assertEquals(false, $res);
+
+        $res = delete_user($username);
+        $this->assertEquals($res, "");
+
+        $res = delete_radl($rowid);
+        $this->assertEquals("", $res);
+
+        $res = get_radl($rowid);
+        $this->assertEquals(NULL, $res);
+    }
+
+}
+?>
\ No newline at end of file

From a8baa486d96b856cb3b83bf9d7eb81689d8fe1e8 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 14:55:48 +0100
Subject: [PATCH 16/29] Increase tests

---
 test/unit/bootstrap.php   |  1 +
 test/unit/test_recipe.php | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 test/unit/test_recipe.php

diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php
index 81975e6..2d4fd9e 100644
--- a/test/unit/bootstrap.php
+++ b/test/unit/bootstrap.php
@@ -9,4 +9,5 @@
 include_once('../../im-rest.php');
 include_once('../../im-xml-rpc.php');
 include_once('../../radl.php');
+include_once('../../recipe.php');
 ?>
diff --git a/test/unit/test_recipe.php b/test/unit/test_recipe.php
new file mode 100644
index 0000000..0fe33f0
--- /dev/null
+++ b/test/unit/test_recipe.php
@@ -0,0 +1,48 @@
+exec('CREATE TABLE "recipes" (
+            name VARCHAR(256) NOT NULL,
+            version VARCHAR(256) NOT NULL,
+            module VARCHAR(256) NOT NULL,
+            recipe VARCHAR(500) NOT NULL,
+            isapp BOOLEAN NOT NULL,
+            galaxy_module VARCHAR(256) NOT NULL,
+            description VARCHAR(500) NOT NULL,
+            requirements VARCHAR(500) NOT NULL
+            );
+            CREATE UNIQUE INDEX pk_index ON "recipes"("name","version");');
+        $db->close();
+    }
+
+    public static function tearDownAfterClass() {
+        unlink("/tmp/recipe.db");
+    }
+
+    public function testRecipe()
+    {
+        $res = insert_recipe("recname", "recver", "recdesc", "module", "recipe", "galaxy_module", "requirements");
+        $this->assertEquals("", $res);
+
+        $res = get_recipes();
+        $rowid = $res[0]["rowid"];
+        $this->assertEquals("recname", $res[0]["name"]);
+
+        $res = get_recipe($rowid);
+        $this->assertEquals("module", $res["module"]);
+
+        $res = delete_recipe($rowid);
+        $this->assertEquals("", $res);
+
+        $res = get_recipe($rowid);
+        $this->assertEquals(NULL, $res);
+    }
+
+}
+?>
\ No newline at end of file

From 623240e5874cfe4960ee34aa8916f473ea60f3ce Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 16:23:50 +0100
Subject: [PATCH 17/29] Increase tests

---
 create_tar.sh               |   4 +-
 credinfo.php                |   2 +-
 test/unit/test_credinfo.php | 138 ++++++++++++++++++++++++++++++++++++
 test/unit/test_recipe.php   |  20 ++----
 4 files changed, 148 insertions(+), 16 deletions(-)
 create mode 100644 test/unit/test_credinfo.php

diff --git a/create_tar.sh b/create_tar.sh
index 4a713cd..236a68c 100755
--- a/create_tar.sh
+++ b/create_tar.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-VERSION=1.5.3
+VERSION=1.5.4
 
 if [ ! -d dist ]
 then
@@ -8,4 +8,4 @@ then
 fi
 
 rm -f dist/IM-web-${VERSION}.tar.gz
-tar --exclude=create_tar.sh --exclude=docker --exclude=doc --exclude=dist --exclude=.git  -czf dist/IM-web-${VERSION}.tar.gz *
+tar --exclude=create_tar.sh --exclude=docker --exclude=doc --exclude=dist --exclude=.git --exclude=test -czf dist/IM-web-${VERSION}.tar.gz *
diff --git a/credinfo.php b/credinfo.php
index befd106..34808bc 100644
--- a/credinfo.php
+++ b/credinfo.php
@@ -22,7 +22,7 @@
  
     if(!isset($_SESSION)) session_start();   
     
-    include('user.php');
+    include_once('user.php');
     if (!check_session_user()) {
 	header('Location: index.php?error=Invalid User');
     } else {    
diff --git a/test/unit/test_credinfo.php b/test/unit/test_credinfo.php
new file mode 100644
index 0000000..b21a53b
--- /dev/null
+++ b/test/unit/test_credinfo.php
@@ -0,0 +1,138 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreate()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("type"=>"EC2", "id"=>"ec2",
+                    "username"=>"user", "password"=>"pass");
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+
+        $res = get_credentials("admin");
+        $this->assertEquals("ec2", end($res)['id']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testEdit()
+    {
+        $this->expectOutputString('');
+
+        $res = get_credentials("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"edit");
+        $_POST = array("type"=>"EC2", "id"=>"ec2", "rowid"=>$rowid,
+                    "service_region"=>"region");
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+
+        $res = get_credentials("admin");
+        $this->assertEquals("ec2", end($res)['id']);
+        $this->assertEquals("region", end($res)['service_region']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testEnable()
+    {
+        $this->expectOutputString('');
+
+        $res = get_credentials("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"enable", "id"=>$rowid);
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+
+        $res = get_credentials("admin");
+        $this->assertEquals("1", end($res)['enabled']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testDisable()
+    {
+        $this->expectOutputString('');
+
+        $res = get_credentials("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"disable", "id"=>$rowid);
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+
+        $res = get_credential($rowid);
+        $this->assertEquals("0", $res['enabled']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testOrder()
+    {
+        $this->expectOutputString('');
+
+        $res = get_credentials("admin");
+        $rowid = end($res)['rowid'];
+        $order = end($res)['ord'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"order", "id"=>$rowid, "order"=>$order, "new_order"=>intval($order)-1);
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+
+        $res = get_credential($rowid);
+        $this->assertEquals(intval($order)-1, $res['ord']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testDelete()
+    {
+        $this->expectOutputString('');
+
+        $db = new IMDB();
+        $res = $db->direct_query("select max(rowid) from credentials");
+        $db->close();
+        $rowid = $res[0][0];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"delete", "id"=>$rowid);
+        include('../../credinfo.php');
+        $this->assertEquals(array('Location: credentials.php'),xdebug_get_headers());
+    }
+}
+?>
\ No newline at end of file
diff --git a/test/unit/test_recipe.php b/test/unit/test_recipe.php
index 0fe33f0..ef61c26 100644
--- a/test/unit/test_recipe.php
+++ b/test/unit/test_recipe.php
@@ -4,10 +4,10 @@
 
 final class RecipeTest extends TestCase
 {
-
-    public static function setUpBeforeClass() {
-        $db = new SQLite3("/tmp/recipe.db"); 
-        $res = $db->exec('CREATE TABLE "recipes" (
+    public function testRecipe()
+    {
+        $db = new SQLite3("/tmp/recipes.db"); 
+        $res = $db->exec('CREATE TABLE IF NOT EXISTS "recipes" (
             name VARCHAR(256) NOT NULL,
             version VARCHAR(256) NOT NULL,
             module VARCHAR(256) NOT NULL,
@@ -16,17 +16,9 @@ public static function setUpBeforeClass() {
             galaxy_module VARCHAR(256) NOT NULL,
             description VARCHAR(500) NOT NULL,
             requirements VARCHAR(500) NOT NULL
-            );
-            CREATE UNIQUE INDEX pk_index ON "recipes"("name","version");');
+            );');
         $db->close();
-    }
 
-    public static function tearDownAfterClass() {
-        unlink("/tmp/recipe.db");
-    }
-
-    public function testRecipe()
-    {
         $res = insert_recipe("recname", "recver", "recdesc", "module", "recipe", "galaxy_module", "requirements");
         $this->assertEquals("", $res);
 
@@ -42,6 +34,8 @@ public function testRecipe()
 
         $res = get_recipe($rowid);
         $this->assertEquals(NULL, $res);
+
+        unlink("/tmp/recipes.db");
     }
 
 }

From c46aad28a47cf14a87fa4f07c41e8c030d1d4b1d Mon Sep 17 00:00:00 2001
From: micafer 
Date: Tue, 14 Nov 2017 16:41:48 +0100
Subject: [PATCH 18/29] Increase tests

---
 test/unit/test_groupinfo.php | 68 ++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 test/unit/test_groupinfo.php

diff --git a/test/unit/test_groupinfo.php b/test/unit/test_groupinfo.php
new file mode 100644
index 0000000..01fde8a
--- /dev/null
+++ b/test/unit/test_groupinfo.php
@@ -0,0 +1,68 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../groupinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testAdd()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("name"=>"grpname", "description"=>"description");
+        include('../../groupinfo.php');
+        $this->assertEquals(array('Location: group_list.php'),xdebug_get_headers());
+
+        $res = get_group("grpname");
+        $this->assertEquals("description", $res["description"]);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testAdd
+     */
+    public function testEdit()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"edit");
+        $_POST = array("id"=>"grpname", "name"=>"newgrpname", "description"=>"newdescription");
+        include('../../groupinfo.php');
+        $this->assertEquals(array('Location: group_list.php'),xdebug_get_headers());
+
+        $res = get_group("newgrpname");
+        $this->assertEquals("newdescription", $res["description"]);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testAdd
+     */
+    public function testDelete()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"delete", "id"=>"newgrpname");
+        include('../../groupinfo.php');
+        $this->assertEquals(array('Location: group_list.php'),xdebug_get_headers());
+
+        $res = get_group("newgrpname");
+        $this->assertEquals(NULL, $res);
+    }
+
+}
+?>
\ No newline at end of file

From 11a544af2aef12814df151f38d47dd9454da642b Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 08:19:30 +0100
Subject: [PATCH 19/29] Increase tests

---
 radlinfo.php                |   2 +-
 test/unit/test_radlinfo.php | 108 ++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 1 deletion(-)
 create mode 100644 test/unit/test_radlinfo.php

diff --git a/radlinfo.php b/radlinfo.php
index 5732f84..64f755c 100644
--- a/radlinfo.php
+++ b/radlinfo.php
@@ -22,7 +22,7 @@
     
     if(!isset($_SESSION)) session_start();
     
-    include('user.php');
+    include_once('user.php');
     if (!check_session_user()) {
 	header('Location: index.php?error=Invalid User');
     } else {
diff --git a/test/unit/test_radlinfo.php b/test/unit/test_radlinfo.php
new file mode 100644
index 0000000..d11533b
--- /dev/null
+++ b/test/unit/test_radlinfo.php
@@ -0,0 +1,108 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreate()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("name"=>"radltest", "description"=>"radldesc",
+                    "radl"=>"radlbody", "group"=>"users");
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: radl_list.php'),xdebug_get_headers());
+
+        $res = get_radls("admin");
+        $this->assertEquals("radltest", end($res)['name']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testEdit()
+    {
+        $this->expectOutputString('');
+
+        $res = get_radls("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"edit");
+        $_POST = array("name"=>"radltest", "id"=>$rowid, "description"=>"newradldesc",
+                    "radl"=>"radlbody\n@input.wn@", "group"=>"users");
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: radl_list.php'),xdebug_get_headers());
+
+        $res = get_radl($rowid);
+        $this->assertEquals("newradldesc", $res['description']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testLaunchWithoutParams()
+    {
+        $this->expectOutputString('');
+
+        $res = get_radls("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"launch", "id"=>$rowid);
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: radl_list.php?parameters=' . $rowid),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testLaunch()
+    {
+        $this->expectOutputString('');
+
+        $res = get_radls("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"launch", "id"=>$rowid, "parameters"=>"1", "wn"=>"2");
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testDelete()
+    {
+        $this->expectOutputString('');
+
+        $res = get_radls("admin");
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"delete", "id"=>$rowid);
+        include('../../radlinfo.php');
+        $this->assertEquals(array('Location: radl_list.php'),xdebug_get_headers());
+    }
+}
+?>
\ No newline at end of file

From bcd9811c6e65e5bd9d3dd508a8a7212b600b32de Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 08:58:56 +0100
Subject: [PATCH 20/29] Increase tests

---
 recipeinfo.php                |  2 +-
 test/unit/test_recipeinfo.php | 92 +++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 test/unit/test_recipeinfo.php

diff --git a/recipeinfo.php b/recipeinfo.php
index fb77d1b..6955fae 100644
--- a/recipeinfo.php
+++ b/recipeinfo.php
@@ -32,7 +32,7 @@
         } elseif (isset($_GET['op'])) {
             $op = $_GET['op'];
         }
-        
+
         if (strlen($op) > 0) {
             if ($op == "delete") {
                 if (isset($_GET['id'])) {
diff --git a/test/unit/test_recipeinfo.php b/test/unit/test_recipeinfo.php
new file mode 100644
index 0000000..68306e2
--- /dev/null
+++ b/test/unit/test_recipeinfo.php
@@ -0,0 +1,92 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../recipeinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreate()
+    {
+        $db = new SQLite3("/tmp/recipes.db"); 
+        $res = $db->exec('CREATE TABLE IF NOT EXISTS "recipes" (
+            name VARCHAR(256) NOT NULL,
+            version VARCHAR(256) NOT NULL,
+            module VARCHAR(256) NOT NULL,
+            recipe VARCHAR(500) NOT NULL,
+            isapp BOOLEAN NOT NULL,
+            galaxy_module VARCHAR(256) NOT NULL,
+            description VARCHAR(500) NOT NULL,
+            requirements VARCHAR(500) NOT NULL
+            );');
+        $db->close();
+
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("name"=>"radltest", "version"=>"version",
+                    "module"=>"module", "recipe"=>"recipe", "galaxy_module"=>"galaxy_module",
+                    "description"=>"description", "requirements"=>"requirements");
+        include('../../recipeinfo.php');
+        $this->assertEquals(array('Location: recipe_list.php'),xdebug_get_headers());
+
+        $res = get_recipes();
+        $this->assertEquals("radltest", end($res)['name']);
+        $this->assertEquals(1, end($res)['rowid']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testEdit()
+    {
+        $this->expectOutputString('');
+
+        $res = get_recipes();
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"edit");
+        $_POST = array("id"=>$rowid, "name"=>"radltest", "version"=>"version",
+                    "module"=>"newmodule", "recipe"=>"recipe", "galaxy_module"=>"galaxy_module",
+                    "description"=>"description", "requirements"=>"requirements");
+        include('../../recipeinfo.php');
+        $this->assertEquals(array('Location: recipe_list.php'),xdebug_get_headers());
+
+        $res = get_recipe($rowid);
+        $this->assertEquals("newmodule", $res['module']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testDelete()
+    {
+        $this->expectOutputString('');
+
+        $res = get_recipes();
+        $rowid = end($res)['rowid'];
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"delete", "id"=>$rowid);
+        include('../../recipeinfo.php');
+        $this->assertEquals(array('Location: recipe_list.php'),xdebug_get_headers());
+
+        unlink("/tmp/recipes.db");
+    }
+}
+?>
\ No newline at end of file

From b18680a15a0c7ee028d16a26a016392c5bd969be Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 09:36:46 +0100
Subject: [PATCH 21/29] Fix #40

---
 userinfo.php | 84 ++++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/userinfo.php b/userinfo.php
index d124d4d..654e058 100644
--- a/userinfo.php
+++ b/userinfo.php
@@ -48,20 +48,20 @@
                     header('Location: error.php?msg=No id');
                 }
             } elseif ($op == "password") {
-		$username = $_SESSION["user"];
+		        $username = $_SESSION["user"];
                 $password = $_POST['password'];
                 $password2 = $_POST['password2'];
 
-		$err = "";
-		if (strlen(trim($password)) > 0) {
-			if (trim($password) != trim($password2)) {
-				$err = "The passwords are not equal.";
-			}
-		}
-                
-		if ($err == "") {
-			$err = change_password($username, $password);
-		}
+                $err = "";
+                if (strlen(trim($password)) > 0) {
+                    if (trim($password) != trim($password2)) {
+                        $err = "The passwords are not equal.";
+                    }
+                }
+                        
+                if ($err == "") {
+                    $err = change_password($username, $password);
+                }
                 if (strlen($err) > 0) {
                     header('Location: index.php?error=' . $err);
                 } else {
@@ -73,18 +73,18 @@
                 $password = $_POST['password'];
                 $password2 = $_POST['password2'];
 
-		$err = "";
-		if (strlen(trim($password)) > 0) {
-			if (trim($password) != trim($password2)) {
-				$err = "The passwords are not equal.";
-			}
-		}
-                
-		if ($err == "") {
-	                $err = insert_user($username, $password, array('users'), 0);
-			$err = insert_credential($username, "", "InfrastructureManager", "", $username, $password, '', '', '', '', '', '', '', '');
-			$err = insert_credential($username, "", "VMRC", "http://servproject.i3m.upv.es:8080/vmrc/vmrc", "micafer", "ttt25", '', '', '', '', '', '', '', '');
-		}
+                $err = "";
+                if (strlen(trim($password)) > 0) {
+                    if (trim($password) != trim($password2)) {
+                        $err = "The passwords are not equal.";
+                    }
+                }
+                        
+                if ($err == "") {
+                    $err = insert_user($username, $password, array('users'), 0);
+                    $err = insert_credential($username, "", "InfrastructureManager", "", $username, $password, '', '', '', '', '', '', '', '', '', '', '', '');
+                    $err = insert_credential($username, "", "VMRC", "http://servproject.i3m.upv.es:8080/vmrc/vmrc", "micafer", "ttt25", '', '', '', '', '', '', '', '', '', '', '', '');
+                }
                 if (strlen($err) > 0) {
                     header('Location: index.php?error=' . $err);
                 } else {
@@ -94,19 +94,19 @@
                 $username = $_POST['username'];
                 $password = $_POST['password'];
                 $password2 = $_POST['password2'];
-                $groups = $_POST['user_groups'];
+                $groups = explode(",", $_POST['user_groups']);
                 $permissions = $_POST['permissions'];
 
-		$err = "";
-		if (strlen(trim($password)) > 0) {
-			if (trim($password) != trim($password2)) {
-				$err = "The passwords are not equal.";
-			}
-		}
-                
-		if ($err == "") {
-	                $err = insert_user($username, $password, $groups, $permissions);
-		}
+                $err = "";
+                if (strlen(trim($password)) > 0) {
+                    if (trim($password) != trim($password2)) {
+                        $err = "The passwords are not equal.";
+                    }
+                }
+                        
+                if ($err == "") {
+                            $err = insert_user($username, $password, $groups, $permissions);
+                }
                 if (strlen($err) > 0) {
                     header('Location: error.php?msg=' . urlencode($err));
                 } else {
@@ -118,19 +118,19 @@
                     $new_username = $_POST['username'];
                     $password = $_POST['password'];
                     $password2 = $_POST['password2'];
-                    $groups = $_POST['user_groups'];
+                    $groups = explode(",", $_POST['user_groups']);
                     $permissions = $_POST['permissions'];
 
-		    $err = "";
+                    $err = "";
                     if (strlen(trim($password)) > 0) {
-			if (trim($password) != trim($password2)) {
-				$err = "The passwords are not equal.";
-			}
+                        if (trim($password) != trim($password2)) {
+                            $err = "The passwords are not equal.";
+                        }
                     }
                     
-		    if ($err == "") {
-                         $err = edit_user($username, $new_username, $password, $groups, $permissions);
-		    }
+                    if ($err == "") {
+                                $err = edit_user($username, $new_username, $password, $groups, $permissions);
+                    }
                     if (strlen($err) > 0) {
                         header('Location: error.php?msg=' . urlencode($err));
                     } else {

From 16752b4424b2f3156e921ca26169629c72170ba4 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 09:37:03 +0100
Subject: [PATCH 22/29] Set correct ver num

---
 create_tar.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/create_tar.sh b/create_tar.sh
index 236a68c..2b3b680 100755
--- a/create_tar.sh
+++ b/create_tar.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-VERSION=1.5.4
+VERSION=1.5.3
 
 if [ ! -d dist ]
 then

From 5bc0061ab4c15979d7b338f1d9fc48b2400fc8d4 Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 09:38:04 +0100
Subject: [PATCH 23/29] Increase tests

---
 test/unit/test_userinfo.php | 126 ++++++++++++++++++++++++++++++++++++
 1 file changed, 126 insertions(+)
 create mode 100644 test/unit/test_userinfo.php

diff --git a/test/unit/test_userinfo.php b/test/unit/test_userinfo.php
new file mode 100644
index 0000000..8e9c626
--- /dev/null
+++ b/test/unit/test_userinfo.php
@@ -0,0 +1,126 @@
+expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=No op'),xdebug_get_headers());
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreateIncorrectPass()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("username"=>"userinfotest", "password"=>"passwordtest",
+                    "password2"=>"password", "user_groups"=>"users", "permissions"=>"0");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: error.php?msg=The+passwords+are+not+equal.'),xdebug_get_headers());
+
+        $res = get_user("userinfotest");
+        $this->assertEquals(NULL, $res);
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCreate()
+    {
+        $this->expectOutputString('');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"add");
+        $_POST = array("username"=>"userinfotest", "password"=>"passwordtest",
+                    "password2"=>"passwordtest", "user_groups"=>"users", "permissions"=>"0");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: user_list.php'),xdebug_get_headers());
+
+        $res = get_user("userinfotest");
+        $this->assertEquals("0", $res['permissions']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testEdit()
+    {
+        $this->expectOutputString('');
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"edit");
+        $_POST = array("id"=>"userinfotest", "username"=>"newuserinfotest", "password"=>"passwordtest",
+                    "password2"=>"passwordtest", "user_groups"=>"users", "permissions"=>"0");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: user_list.php'),xdebug_get_headers());
+
+        $res = get_user("newuserinfotest");
+        $this->assertEquals("0", $res['permissions']);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testChangePassword()
+    {
+        $this->expectOutputString('');
+
+        $_SESSION = array("user"=>"newuserinfotest", "password"=>"passwordtest");
+        $_GET = array("op"=>"password");
+        $_POST = array("password"=>"npasswordtest", "password2"=>"npasswordtest");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: list.php'),xdebug_get_headers());
+
+        $res = get_user("newuserinfotest");
+        $res = check_password("npasswordtest", $res["password"]);
+        $this->assertEquals(true, $res);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testRegister()
+    {
+        $this->expectOutputString('');
+
+        $_SESSION = array("user"=>"newuserinfotest", "password"=>"passwordtest");
+        $_GET = array("op"=>"register");
+        $_POST = array("username"=>"userinfotest2", "password"=>"npasswordtest", "password2"=>"npasswordtest");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: index.php?info=User added successfully'),xdebug_get_headers());
+
+        $res = get_user("userinfotest2");
+        $res = check_password("npasswordtest", $res["password"]);
+        $this->assertEquals(true, $res);
+
+        $err = delete_user("userinfotest2");
+        $this->assertEquals("", $err);
+    }
+
+    /**
+     * @runInSeparateProcess
+     * @depends testCreate
+     */
+    public function testDelete()
+    {
+        $this->expectOutputString('');
+
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("op"=>"delete", "id"=>"newuserinfotest");
+        include('../../userinfo.php');
+        $this->assertEquals(array('Location: user_list.php'),xdebug_get_headers());
+    }
+}
+?>
\ No newline at end of file

From 468dc39faf363bf043f378af45724ba02de789ea Mon Sep 17 00:00:00 2001
From: micafer 
Date: Wed, 15 Nov 2017 10:47:01 +0100
Subject: [PATCH 24/29] Increase tests

---
 credentials.php              |  2 +-
 credform.php                 |  4 ++--
 test/unit/test_credpages.php | 31 ++++++++++++++++++++++++++
 test/unit/test_userpages.php | 43 ++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 test/unit/test_credpages.php
 create mode 100644 test/unit/test_userpages.php

diff --git a/credentials.php b/credentials.php
index c51b2ef..2f02221 100644
--- a/credentials.php
+++ b/credentials.php
@@ -19,7 +19,7 @@
 
     if(!isset($_SESSION)) session_start();
 
-    include('user.php');
+    include_once('user.php');
     if (!check_session_user()) {
 	header('Location: index.php?error=Invalid User');
     } else {
diff --git a/credform.php b/credform.php
index ee1298f..ef33e3c 100644
--- a/credform.php
+++ b/credform.php
@@ -20,8 +20,8 @@
     if(!isset($_SESSION)) session_start();
  
     include('config.php');   
-    include('user.php');
-    include('cred.php');
+    include_once('user.php');
+    include_once('cred.php');
     if (!check_session_user()) {
 	header('Location: index.php?error=Invalid User');
     } else {
diff --git a/test/unit/test_credpages.php b/test/unit/test_credpages.php
new file mode 100644
index 0000000..1090764
--- /dev/null
+++ b/test/unit/test_credpages.php
@@ -0,0 +1,31 @@
+expectOutputRegex('/.*input type="text" name="username" value="admin".*/');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("id"=>"1");
+        include('../../credform.php');
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testCredList()
+    {
+        $this->expectOutputRegex('/.*IMRow\.png.*/');
+        $this->expectOutputRegex('/.*credinfo.php\?op=delete&id=2.*/');
+        $this->expectOutputRegex('/.*http:\/\/servproject.i3m.upv.es:8080\/vmrc\/vmrc.*/');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        include('../../credentials.php');
+    }
+}
+?>
diff --git a/test/unit/test_userpages.php b/test/unit/test_userpages.php
new file mode 100644
index 0000000..99e6dc5
--- /dev/null
+++ b/test/unit/test_userpages.php
@@ -0,0 +1,43 @@
+expectOutputRegex('/.*admin is connected.*/');
+        $_SESSION = array("user"=>"admin");
+        include('../../user_connected.php');
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testUserForm()
+    {
+        $this->expectOutputRegex('/.*input type="hidden" name="id" value="admin".*/');
+        $this->expectOutputRegex('/.*input type="text" name="username" value="admin".*/');
+        $this->expectOutputRegex('/.*option value="1"[\\n\n ]+selected="selected" +>Administrator.*/');
+        $_SESSION = array("user"=>"admin", "password"=>"admin");
+        $_GET = array("id"=>"admin");
+        include('../../userform.php');
+    }
+
+    /**
+     * @runInSeparateProcess
+     */
+    public function testUserList()
+    {
+        $this->expectOutputRegex('/.*[ \n\\n]+admin[ \n\\n]+<\/td>.*/');
+        $this->expectOutputRegex('/.*[ \n\\n]+users
[ \n\\n]+<\/td>.*/'); + $this->expectOutputRegex('/.*[ \n\\n]+Administrator[ \n\\n]+<\/td>.*/'); + $this->expectOutputRegex('/.*userform.php\?id=admin.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + include('../../user_list.php'); + } +} +?> From dd076f68432c50f0dd57c93a5e3401cb62b3d60c Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 15 Nov 2017 12:28:44 +0100 Subject: [PATCH 25/29] Increase tests --- getcontmsg.php | 6 +- getoutputs.php | 6 +- getvminfo.php | 2 +- im.php | 5 ++ list.php | 2 +- test/unit/test_infpages.php | 152 ++++++++++++++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 test/unit/test_infpages.php diff --git a/getcontmsg.php b/getcontmsg.php index 14b6165..9b98013 100644 --- a/getcontmsg.php +++ b/getcontmsg.php @@ -19,12 +19,12 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { if (isset($_GET['id'])) { - include('im.php'); + include_once('im.php'); include('config.php'); $id = $_GET['id']; if (isset($_GET['vmid'])) { @@ -53,7 +53,7 @@ - + diff --git a/getoutputs.php b/getoutputs.php index ddaed96..780682d 100644 --- a/getoutputs.php +++ b/getoutputs.php @@ -19,12 +19,12 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { if (isset($_GET['id'])) { - include('im.php'); + include_once('im.php'); include('config.php'); $id = $_GET['id']; $outputs = GetIM()->GetOutputs($id); @@ -48,7 +48,7 @@ - + diff --git a/getvminfo.php b/getvminfo.php index 483bb59..474b7c1 100644 --- a/getvminfo.php +++ b/getvminfo.php @@ -34,7 +34,7 @@ if (is_string($res) && strpos($res, "Error") !== false) { header('Location: error.php?msg=' . urlencode($res)); } else { - $radl_tokens = parseRADL($res); + $radl_tokens = parseRADL($res); $outports = getOutPorts($res); ?> diff --git a/im.php b/im.php index 9eedeb2..68b30e5 100644 --- a/im.php +++ b/im.php @@ -5,6 +5,11 @@ function GetIM() { include('config.php'); + // for the unit tests + if (isset($GLOBALS['mock_im'])) { + return $GLOBALS['mock_im']; + } + if ($im_use_rest) { return IMRest::connect($im_host,$im_port); } else { diff --git a/list.php b/list.php index 5b7d726..5e24458 100644 --- a/list.php +++ b/list.php @@ -24,7 +24,7 @@ $_SESSION['user'] = $_POST['username']; include_once('format.php'); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { diff --git a/test/unit/test_infpages.php b/test/unit/test_infpages.php new file mode 100644 index 0000000..680bf5c --- /dev/null +++ b/test/unit/test_infpages.php @@ -0,0 +1,152 @@ +expectOutputRegex('/.*infid1.*/'); + $this->expectOutputRegex("/.*style='color:green'>configuring.*/"); + $this->expectOutputRegex("/.*getvminfo.php\?id=infid1&vmid=vmid1.*/"); + $this->expectOutputRegex("/.*getvminfo.php\?id=infid1&vmid=vmid2.*/"); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + + $im = $this->getMockBuilder(IMXML::class) + ->setMethods(['GetInfrastructureList', 'GetInfrastructureInfo', 'GetInfrastructureState']) + ->getMock(); + $im->method('GetInfrastructureList') + ->willReturn(array("infid1")); + $im->method('GetInfrastructureInfo') + ->willReturn(array("vmid1","vmid2")); + $im->method('GetInfrastructureState') + ->willReturn("running"); + + $GLOBALS['mock_im'] = $im; + + include('../../list.php'); + unset($GLOBALS['mock_im']); + } + + /** + * @runInSeparateProcess + */ + public function testGetVMInfo() + { + $this->expectOutputRegex("/.*style='color:green'>configured.*/"); + $this->expectOutputRegex("/.*0 => 10.0.0.2
1 => 10.0.0.1.*/"); + $this->expectOutputRegex("/.*getcontmsg.php\?id=infid&ivmd=vmid.*/"); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + + $radl = "" . + "network publica (outbound = 'yes') " . + "network privada ( ) " . + "system front ( " . + "state = 'configured' and " . + "provider.type = 'EC2' and " . + "cpu.arch = 'x86_64' and " . + "cpu.count >= 1 and " . + "memory.size >= 512m and " . + "net_interface.1.connection = 'publica' and " . + "net_interface.1.ip = '10.0.0.1' and " . + "net_interface.0.connection = 'privada' and " . + "net_interface.0.dns_name = 'front' and " . + "net_interface.0.ip = '10.0.0.2' and " . + "disk.0.os.flavour = 'centos' and " . + "disk.0.os.version >='7' and " . + "disk.0.os.name = 'linux' and " . + "disk.0.applications contains (name = 'ansible.modules.grycap.octave') and " . + "disk.0.applications contains (name = 'gmetad') and " . + "disk.0.os.credentials.private_key = 'priv' and " . + "disk.1.size = 1GB and " . + "disk.1.device = 'hdb' and " . + "disk.1.fstype = 'ext4' and " . + "disk.1.mount_path = '/mnt/disk'" . + ")"; + $im = $this->getMockBuilder(IMXML::class) + ->setMethods(['GetVMInfo']) + ->getMock(); + $im->method('GetVMInfo') + ->willReturn($radl); + + $_GET = array('vmid'=>'vmid', 'id'=>'infid'); + $GLOBALS['mock_im'] = $im; + + include('../../getvminfo.php'); + unset($GLOBALS['mock_im']); + } + + /** + * @runInSeparateProcess + */ + public function testGetVMContMsg() + { + $this->expectOutputRegex("/.*vmcontmsg.*/"); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + + $im = $this->getMockBuilder(IMXML::class) + ->setMethods(['GetVMContMsg','GetInfrastructureContMsg']) + ->getMock(); + $im->method('GetVMContMsg') + ->willReturn("vmcontmsg"); + $im->method('GetInfrastructureContMsg') + ->willReturn("infcontmsg"); + + $_GET = array('vmid'=>'vmid', 'id'=>'infid'); + $GLOBALS['mock_im'] = $im; + + include('../../getcontmsg.php'); + unset($GLOBALS['mock_im']); + } + + /** + * @runInSeparateProcess + */ + public function testGetContMsg() + { + $this->expectOutputRegex("/.*infcontmsg.*/"); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + + $im = $this->getMockBuilder(IMXML::class) + ->setMethods(['GetVMContMsg','GetInfrastructureContMsg']) + ->getMock(); + $im->method('GetVMContMsg') + ->willReturn("vmcontmsg"); + $im->method('GetInfrastructureContMsg') + ->willReturn("infcontmsg"); + + $_GET = array('id'=>'infid'); + $GLOBALS['mock_im'] = $im; + + include('../../getcontmsg.php'); + unset($GLOBALS['mock_im']); + } + + /** + * @runInSeparateProcess + */ + public function testGetOutputs() + { + $this->expectOutputRegex("/.*infcontmsg.*/"); + $this->expectOutputRegex('/.*[ \n\\n\t]+key[ \n\\n\t]+<\/td>[ \n\\n\t]*[ \n\\n\t]+value[ \n\\n\t]+<\/td>.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['GetOutputs']) + ->getMock(); + $im->method('GetOutputs') + ->willReturn(array("key"=>"value")); + + $_GET = array('id'=>'infid'); + $GLOBALS['mock_im'] = $im; + + include('../../getoutputs.php'); + unset($GLOBALS['mock_im']); + } +} +?> + From e4c0a8625ab8388069f535e46c713f227a8a111e Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 15 Nov 2017 12:41:26 +0100 Subject: [PATCH 26/29] Improve tests --- test/unit/test_operate.php | 77 ++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/test/unit/test_operate.php b/test/unit/test_operate.php index 4d4064a..43b2648 100644 --- a/test/unit/test_operate.php +++ b/test/unit/test_operate.php @@ -24,8 +24,17 @@ public function testCreate() $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"create"); $_POST = array("radl"=>"radl"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['CreateInfrastructure']) + ->getMock(); + $im->method('CreateInfrastructure') + ->willReturn("infid"); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: list.php'),xdebug_get_headers()); } /** @@ -36,8 +45,17 @@ public function testDestroy() $this->expectOutputString(''); $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"destroy", "id"=>"id"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['DestroyInfrastructure']) + ->getMock(); + $im->method('DestroyInfrastructure') + ->willReturn("infid"); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: list.php'),xdebug_get_headers()); } /** @@ -48,8 +66,17 @@ public function testDestroyVM() $this->expectOutputString(''); $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"destroyvm", "infid"=>"id", "vmid"=>"vid"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['RemoveResource']) + ->getMock(); + $im->method('RemoveResource') + ->willReturn("1"); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: list.php'),xdebug_get_headers()); } /** @@ -60,8 +87,17 @@ public function testStopVM() $this->expectOutputString(''); $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"stopvm", "infid"=>"id", "vmid"=>"vid"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['StopVM']) + ->getMock(); + $im->method('StopVM') + ->willReturn(""); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: getvminfo.php?id=id&vmid=vid'),xdebug_get_headers()); } /** @@ -72,8 +108,17 @@ public function testStartVM() $this->expectOutputString(''); $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"startvm", "infid"=>"id", "vmid"=>"vid"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['StartVM']) + ->getMock(); + $im->method('StartVM') + ->willReturn(""); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: getvminfo.php?id=id&vmid=vid'),xdebug_get_headers()); } /** @@ -85,8 +130,17 @@ public function testAddResource() $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"addresource"); $_POST = array("radl"=>"radl", "infid"=>"id"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['AddResource']) + ->getMock(); + $im->method('AddResource') + ->willReturn("vmid"); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: list.php'),xdebug_get_headers()); } /** @@ -97,8 +151,17 @@ public function testReconfigure() $this->expectOutputString(''); $_SESSION = array("user"=>"admin", "password"=>"admin"); $_GET = array("op"=>"reconfigure", "infid"=>"id"); + + $im = $this->getMockBuilder(IMRest::class) + ->setMethods(['Reconfigure']) + ->getMock(); + $im->method('Reconfigure') + ->willReturn(""); + + $GLOBALS['mock_im'] = $im; include('../../operate.php'); - $this->assertEquals(array('Location: error.php?msg=Error%3A+Connect+error%3A+Connection+refused+%28111%29'),xdebug_get_headers()); + unset($GLOBALS['mock_im']); + $this->assertEquals(array('Location: list.php'),xdebug_get_headers()); } } ?> \ No newline at end of file From ed1dddbc0214c8ed8bf7c44d7fd7d6abaa01ea35 Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 15 Nov 2017 13:11:21 +0100 Subject: [PATCH 27/29] Improve tests --- group_list.php | 4 +-- groupform.php | 4 +-- radl_list.php | 2 +- radlform.php | 4 +-- recipe_list.php | 4 +-- recipeform.php | 4 +-- test/unit/test_groupages.php | 30 ++++++++++++++++++ test/unit/test_radlpages.php | 41 ++++++++++++++++++++++++ test/unit/test_recipespages.php | 55 +++++++++++++++++++++++++++++++++ 9 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 test/unit/test_groupages.php create mode 100644 test/unit/test_radlpages.php create mode 100644 test/unit/test_recipespages.php diff --git a/group_list.php b/group_list.php index 35df1f7..0c386f7 100644 --- a/group_list.php +++ b/group_list.php @@ -19,11 +19,11 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user() || !check_admin_user()) { header('Location: index.php?error=Invalid User'); } else { - include('group.php'); + include_once('group.php'); $groups = get_groups(); ?> diff --git a/groupform.php b/groupform.php index 7ad2be0..e4a8060 100644 --- a/groupform.php +++ b/groupform.php @@ -19,7 +19,7 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user() || !check_admin_user()) { header('Location: index.php?error=Invalid User'); } else { @@ -41,7 +41,7 @@ - +
diff --git a/radl_list.php b/radl_list.php index 54688e2..72812a5 100644 --- a/radl_list.php +++ b/radl_list.php @@ -19,7 +19,7 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { diff --git a/radlform.php b/radlform.php index 6140b8c..e259d6b 100644 --- a/radlform.php +++ b/radlform.php @@ -19,7 +19,7 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { @@ -48,7 +48,7 @@ - + diff --git a/recipe_list.php b/recipe_list.php index 7bc34ba..0ed9f20 100644 --- a/recipe_list.php +++ b/recipe_list.php @@ -19,11 +19,11 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { - include('recipe.php'); + include_once('recipe.php'); $recipes = get_recipes(); ?> diff --git a/recipeform.php b/recipeform.php index 51807f7..4766ed9 100644 --- a/recipeform.php +++ b/recipeform.php @@ -19,7 +19,7 @@ if(!isset($_SESSION)) session_start(); - include('user.php'); + include_once('user.php'); if (!check_session_user()) { header('Location: index.php?error=Invalid User'); } else { @@ -41,7 +41,7 @@ - +
diff --git a/test/unit/test_groupages.php b/test/unit/test_groupages.php new file mode 100644 index 0000000..27d09eb --- /dev/null +++ b/test/unit/test_groupages.php @@ -0,0 +1,30 @@ +expectOutputRegex('/.*input type="text" name="name" value="users".*/'); + $this->expectOutputRegex('/.*input maxlength="256" size="200" type="descr" name="description" value="Grupo general de usuarios".*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $_GET = array("id"=>"users"); + include('../../groupform.php'); + } + + /** + * @runInSeparateProcess + */ + public function testGroupList() + { + $this->expectOutputRegex('/.*groupform.php?id=users.*/'); + $this->expectOutputRegex('/.*[ \n\\n\t]+users[ \n\\n\t]+<\/td>.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + include('../../group_list.php'); + } +} +?> diff --git a/test/unit/test_radlpages.php b/test/unit/test_radlpages.php new file mode 100644 index 0000000..eb02c81 --- /dev/null +++ b/test/unit/test_radlpages.php @@ -0,0 +1,41 @@ +assertEquals("", $res); + $res = get_radls("admin"); + $rowid = $res[0]["rowid"]; + + $this->expectOutputRegex('/.*input type="hidden" name="id" value="' . $rowid . '".*/'); + $this->expectOutputRegex('/.* + diff --git a/test/unit/test_errorpage.php b/test/unit/test_errorpage.php new file mode 100644 index 0000000..5ac4cf2 --- /dev/null +++ b/test/unit/test_errorpage.php @@ -0,0 +1,40 @@ +expectOutputRegex('/.*error msg.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $_GET = array("msg"=>"error msg"); + include('../../error.php'); + } + + /** + * @runInSeparateProcess + */ + public function testIndexErrorPage() + { + $this->expectOutputRegex('/.*error msg.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $_GET = array("error"=>"error msg"); + include('../../index.php'); + } + + /** + * @runInSeparateProcess + */ + public function testIndexInfoPage() + { + $this->expectOutputRegex('/.*info msg.*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $_GET = array("info"=>"info msg"); + include('../../index.php'); + } +} +?> diff --git a/test/unit/test_radlpages.php b/test/unit/test_radlpages.php index eb02c81..a93c577 100644 --- a/test/unit/test_radlpages.php +++ b/test/unit/test_radlpages.php @@ -21,6 +21,20 @@ public function testRADLForm() include('../../radlform.php'); } + /** + * @runInSeparateProcess + */ + public function testAddForm() + { + $res = get_radls("admin"); + $rowid = $res[0]["rowid"]; + + $this->expectOutputRegex('/.*input type="hidden" name="infid" value="' . $rowid . '".*/'); + $_SESSION = array("user"=>"admin", "password"=>"admin"); + $_GET = array("id"=>$rowid); + include('../../form.php'); + } + /** * @runInSeparateProcess */ From 937df5270e5db2830b53397ec752120850d527c5 Mon Sep 17 00:00:00 2001 From: micafer Date: Wed, 15 Nov 2017 13:29:53 +0100 Subject: [PATCH 29/29] Fix config --- config.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config.php b/config.php index 6d42729..cce6cfa 100644 --- a/config.php +++ b/config.php @@ -23,8 +23,7 @@ $im_host="localhost"; $im_port=8899; $im_method='http'; -#$im_db="/home/www-data/im.db"; -$im_db="/tmp/im.db"; +$im_db="/home/www-data/im.db"; # To use that feature the IM recipes file must accesible to the web server #$recipes_db="/usr/local/im/contextualization/recipes_ansible.db"; # If not set ""