1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Unit_Test controller.
5 class Ninja_unit_test_Controller
extends Ninja_Controller
{
7 * Run unit tests and show results!
9 public function index()
14 private function prereq() {
15 if(date
::utc_offset() != 3600 * (1 +
date("I"))) {
16 echo "Aborting: Report tests require CET as timezone (date.timezone = \"CET\" in php.ini, for example). Got ".date
::utc_offset()."\n";
24 public function reports()
28 $test_results = array();
29 $config = Kohana
::config('database.default');
30 if (isset($config['connection'])) {
31 $connection = $config['connection'];
32 if (isset($connection['database']) && $connection['database'])
33 $db_name = $connection['database'];
34 if (isset($connection['user']) && $connection['user'])
35 $db_user = $connection['user'];
36 if (isset($connection['pass']) && $connection['pass'])
37 $db_pass = $connection['pass'];
38 if (isset($connection['host']) && $connection['host'])
39 $db_host = $connection['host'];
40 if (isset($connection['type']) && $connection['type'])
41 $db_type = $connection['type'];
43 $importer = '$(rpm --eval %{_libdir})/merlin/import --nagios-cfg=/tmp/ninja-test/nagios.cfg';
45 $argv = isset($argv) ?
$argv : $GLOBALS['argv'];
46 $argc = isset($argc) ?
$argc : $GLOBALS['argc'];
47 for ($i = 1; $i < $argc; $i++
) {
50 $importer = $argv[$i +
1];
53 $db_name = $argv[$i +
1];
56 $db_user = $argv[$i +
1];
59 $db_user = $argv[$i +
1];
62 if (substr($argv[$i], 0, 2) == '--') {
67 if (is_file($argv[$i])) {
68 echo "Adding $argv[$i] to testfiles\n";
69 $test_file[] = $argv[$i];
74 foreach ($test_file as $tfile) {
75 $test = new Ninja_Reports_Test($tfile);
76 $test->importer
= $importer;
77 $test->db_name
= $db_name;
78 $test->db_user
= $db_user;
79 $test->db_pass
= $db_pass;
80 $test->db_type
= $db_type;
81 $test->db_host
= $db_host;
82 if ($test->run_test_series() === -1) {
83 echo " $tfile : '$test->description' failed to run\n";
87 $passed +
= $test->passed
;
88 $failed +
= $test->failed
;
91 echo "Failed test-cases:\n";
92 foreach ($all as $test) {
93 if (!empty($test->failed
))
94 echo " " . $test->test_file
. ", '" . $test->description
. "'\n";
98 echo "Error: No tests found\n";
101 echo "$passed/".($passed+
$failed)." tests passed.".($failed==0?
" Hooray!":"")."\n";
105 exit(0); //removing this causes ninja to print a 302 to tac
110 Drops all tables in Database() which "appear to be"
113 public function wipe_tables()
115 $db = Database
::instance();
116 $tlist = $db->list_tables();
117 if( !count($tlist) ) {
118 throw new Exception("Query failed!");
120 foreach( $tlist as $table ) {
121 #echo "Checking table $table...\n";
122 if (strlen($table) >= 40) {
123 echo "Dropping table $table...\n";
124 if (!$db->query("drop table $table")) {
125 echo "Failed to drop table $table:" . $db->error_message() . "\n";