-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.php
238 lines (193 loc) · 7.67 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
*
* @author Daniel Kesler
* @version 0.10.0
* @license https://opensource.org/licenses/GPL-3.0
*
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Plugin_fab_diagnostic extends FAB_Controller {
function __construct()
{
parent::__construct();
session_write_close(); //avoid freezing page
}
private function getTestCases($test)
{
$this->load->helper('plugin_helper');
$test_case_file = plugin_path() . '/config/test_cases/' . $test . '.php';
if(file_exists($test_case_file))
{
require_once $test_case_file;
return isset($config) ? $config : array();
}
return array();
}
public function index($tab = '')
{
$this->load->library('smart');
$this->load->helper(array('form', 'fabtotum_helper', 'plugin_helper'));
//load plugin translation
loadPluginTranslation('fab_diagnostic', 'fab_diagnostic');
$data = array();
$widgetOptions = array(
'sortable' => false, 'fullscreenbutton' => true, 'refreshbutton' => false, 'togglebutton' => false,
'deletebutton' => false, 'editbutton' => false, 'colorbutton' => false, 'collapsed' => false
);
// Tab php filenames are generated by 'key'+'_tab'
// Tab content id is generated by 'key'+'-tab'
$tabs = array(
'selftests' => dgettext('fab_diagnostic', 'Self-tests'),
'network' => dgettext('fab_diagnostic', 'Network'),
'firmware' => dgettext('fab_diagnostic', 'Firmware'),
'sensors' => dgettext('fab_diagnostic', 'Sensors'),
'fabui' => dgettext('fab_diagnostic', 'FabUI'),
'os' => dgettext('fab_diagnostic', 'OS')
);
$headerToolbar = '<ul class="nav nav-tabs pull-right">';
$tabs_content = '';
if($tab == '')
$tab = 'selftests';
$test_case_js = '';
foreach($tabs as $key => $title)
{
$is_active = ($tab == $key);
$headerToolbar .= '<li '.($is_active?'class="active"':'').'><a data-toggle="tab" href="#'.$key.'-tab"> '._($title).'</a></li>';
$tab_content = '';
$test_cases = $this->getTestCases($key);
foreach($test_cases as $tc_key => $tc_info)
{
$test_case_js_file = plugin_path() . '/scripts/js/'.$key.'_'.$tc_info['test'].'.js';
if( file_exists($test_case_js_file) )
{
$test_case_js .= file_get_contents($test_case_js_file);
}
$run_file = '/tmp/fabui/testcase/'.$key.'_'.$tc_info['test'].'/run_log.json';
$has_run = file_exists($run_file);
$run_content = '';
if($has_run)
{
$run_info = json_decode( file_get_contents($run_file), 1 );
if(file_exists($run_info['log']))
$run_info['log'] = file_get_contents($run_info['log']);
else
$run_info['log'] = '';
if(file_exists($run_info['result']))
$data['result'] = file_get_contents($run_info['result']);
else
$data['result'] = '';
if($run_info['test'] == 'passed')
$run_content ='<button class="btn btn-success test-action" data-subsystem="'.$key.'" data-test-case="'.$tc_info['test'].'" data-action="show-log"><i class="fa fa-check-circle" aria-hidden="true"></i> '.dgettext('fab_diagnostic', 'Passed').'</button>';
else if ($run_info['test'] == 'skipped')
$run_content ='<button class="btn btn-warning test-action" data-subsystem="'.$key.'" data-test-case="'.$tc_info['test'].'" data-action="show-log"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> '.dgettext('fab_diagnostic', 'Skipped').'</button>';
else
$run_content ='<button class="btn btn-danger test-action" data-subsystem="'.$key.'" data-test-case="'.$tc_info['test'].'" data-action="show-log"><i class="fa fa-times-circle" aria-hidden="true"></i> '.dgettext('fab_diagnostic', 'Failed').'</button>';
}
$tab_content .= '
<div class="row">
<div class="col col-md-6">
<dl class="dl-horizontal">
<dt>Test:</dt>
<dd>'.$tc_info['title'].'</dd>
<dt>Description:</dt>
<dd>'.$tc_info['desc'].'</dd>
</dl>
</div>
<div class="col col-md-6">
<button class="btn test-action" data-subsystem="'.$key.'" data-test-case="'.$tc_info['test'].'" data-action="run"><i class="fa fa-play" aria-hidden="true"></i></button>
<span id="'.$key.'_'.$tc_info['test'].'-result">'.$run_content.'</span>
</div>
</div>
<hr class="simple">';
}
$tab_data = array();
$tab_data['tab_active'] = $is_active;
$tab_data['tab_id'] = $key;
$tab_data['tab_content'] = $tab_content;
$tabs_content .= $this->load->view(plugin_url($key.'_tab'), $tab_data, true );
}
$headerToolbar .= '</ul>';
$data['tabs_content'] = $tabs_content;
$widgeFooterButtons = '';
$widget = $this->smart->create_widget($widgetOptions);
$widget->id = 'main-widget-head-installation';
$widget->header = array('icon' => 'fa fa-heartbeat', "title" => "<h2>" . _("Diagnostic tools") . "</h2>", 'toolbar' => $headerToolbar);
$widget->body = array('content' => $this->load->view(plugin_url('main_widget'), $data, true ), 'class'=>'no-padding', 'footer'=>$widgeFooterButtons);
$this->addJsInLine($this->load->view(plugin_url('js'), $data, true));
$this->addJsInLine($test_case_js);
$this->addJSFile( plugin_assets_url('js/ansi_up.js'));
$this->addJSFile('/assets/js/plugin/flot/jquery.flot.cust.min.js'); //chart
$this->addJSFile('/assets/js/plugin/flot/jquery.flot.resize.min.js'); //chart
$this->addJSFile('/assets/js/plugin/flot/jquery.flot.fillbetween.min.js'); //chart
$this->addJSFile('/assets/js/plugin/flot/jquery.flot.time.min.js'); //chart
$this->addJSFile('/assets/js/plugin/flot/jquery.flot.tooltip.min.js'); //chart
$this->content = $widget->print_html(true);
$this->view();
}
public function runTestCase($subsystem, $test_case)
{
$this->load->helper(array('plugin_helper', 'fabtotum_helper'));
$this->config->load('fabtotum');
$extPath = plugin_path() . '/scripts/bash/';
$result = shell_exec('sudo sh '.$extPath . $subsystem . '_' . $test_case . '.sh');
$data = json_decode($result, 1);
if($data)
{
if(file_exists($data['log']))
$data['log'] = file_get_contents($data['log']);
else
$data['log'] = '';
if(file_exists($data['result']))
$data['result'] = file_get_contents($data['result']);
else
$data['result'] = '';
}
else
{
$tmpdir = '/tmp/fabui/testcase/'.$subsystem .'_'.$test_case;
$logfile = $tmpdir.'/testcase.log';
$runfile = $tmpdir.'/run_log.json';
shell_exec('sudo mkdir -p '.$tmpdir);
file_put_contents('/tmp/fabui/testcase.log', '== Test case script failure =='.PHP_EOL);
$data = array(
'log' => $logfile,
'test' => 'failed',
'result' => ''
);
file_put_contents('/tmp/fabui/run_log.json', json_encode($data));
$result = shell_exec('sudo sh '.$extPath . $subsystem . '_' . $test_case . '.sh >> /tmp/fabui/testcase.log 2>&1');
shell_exec('sudo mv /tmp/fabui/testcase.log '.$logfile);
shell_exec('sudo mv /tmp/fabui/run_log.json '.$runfile);
}
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
/**
*
*/
public function getTestCaseLog($subsystem, $test_case)
{
$logfile = '/tmp/fabui/testcase/'.$subsystem .'_'.$test_case.'/testcase.log';
if(file_exists($logfile))
{
echo file_get_contents($logfile);
}
}
/**
*
*/
public function downloadTestCaseLog($subsystem, $test_case)
{
$this->load->helper(array('download_helper'));
$logfile = '/tmp/fabui/testcase/'.$subsystem .'_'.$test_case.'/testcase.log';
if(file_exists($logfile))
{
$data = file_get_contents($logfile);
$name = $subsystem."_".$test_case.'.txt';
force_download($name, $data);
}else{
show_404();
}
}
}
?>