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
//! Module for invoking [IGT GPU Tools](https://gitlab.freedesktop.org/drm/igt-gpu-tools) tests.
use crate::parse::ResultParser;
use crate::parse_igt::{
    igt_parse_testcases_from_caselist, igt_parse_testcases_from_subtests, igt_parse_testlist_file,
    read_testlist_file, IgtResultParser,
};
use crate::timeout::{TimeoutChildStdout, Timer};
use crate::{
    runner_results::*, FailCounter, SingleBinaryTestCommand, SingleTestCommand, SubRunConfig,
    TestConfiguration,
};
use crate::{CaselistResult, TestResult, TestStatus};
use crate::{TestCase, TestCommand};
use anyhow::{Context, Result};
use log::*;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str;
use structopt::StructOpt;

pub struct IgtCommand {
    pub config: TestConfiguration,
    pub igt_folder: PathBuf,
}

// Common structure for configuring a igt on deqp-runner Suite (multiple Runs)
#[derive(Debug, Deserialize, StructOpt)]
pub struct IgtRunConfig {
    #[structopt(long, help = "path to folder containing the IGT test binaries")]
    pub igt_folder: PathBuf,
}

#[derive(Deserialize)]
pub struct IgtTomlConfig {
    pub caselists: Vec<PathBuf>,

    #[serde(flatten)]
    pub sub_config: SubRunConfig,

    #[serde(flatten)]
    pub igt_config: IgtRunConfig,
}

fn igt_get_subtests(test_folder: &Path, binary: &str) -> Result<Vec<String>> {
    let mut command = Command::new(test_folder.join(Path::new(&binary)));
    let output = command
        .current_dir(test_folder)
        .args(&["--list-subtests".to_string()])
        .output()?;
    let s = str::from_utf8(&output.stdout)?
        .lines()
        .map(str::to_string)
        .collect();
    Ok(s)
}

fn igt_get_test_list_from_binaries(
    test_folder: &Path,
    binaries_list: Vec<String>,
) -> Result<Vec<TestCase>> {
    let mut tests: Vec<TestCase> = Vec::new();
    for bin in binaries_list {
        let subtests = igt_get_subtests(test_folder, &bin)?;
        let mut testcases = igt_parse_testcases_from_subtests(&bin, subtests)?;
        tests.append(&mut testcases);
    }
    Ok(tests)
}

fn igt_get_test_list(caselists: &[PathBuf], test_folder: &Path) -> Result<Vec<TestCase>> {
    let test_list = igt_parse_testcases_from_caselist(caselists)?;
    if !test_list.is_empty() {
        return Ok(test_list);
    }
    let text = read_testlist_file(test_folder)?;
    let binaries_list = igt_parse_testlist_file(&text)?
        .iter()
        .map(|i| i.to_string())
        .collect();
    let test_list = igt_get_test_list_from_binaries(test_folder, binaries_list)?;
    Ok(test_list)
}

impl IgtTomlConfig {
    pub fn test_groups<'d>(
        &self,
        igt: &'d IgtCommand,
        filters: &[String],
    ) -> Result<Vec<(&'d dyn TestCommand, Vec<TestCase>)>> {
        assert!(
            rayon::current_num_threads() == 1,
            "igt tests can't be run on more than one thread, use --jobs 1"
        );

        let test_folder = &self.igt_config.igt_folder;
        let tests: Vec<TestCase> = igt_get_test_list(&self.caselists, test_folder)
            .with_context(|| "Collecting IGT tests")?;

        igt.test_groups(&self.sub_config, filters, tests)
    }
}

impl SingleTestCommand for IgtCommand {}
impl SingleBinaryTestCommand for IgtCommand {}

impl TestCommand for IgtCommand {
    fn name(&self) -> &str {
        "Igt"
    }

    fn prepare(&self, _caselist_state: &CaselistState, tests: &[&TestCase]) -> Result<Command> {
        let test = self.current_test(tests);
        let bin_path = self.igt_folder.clone();

        let mut command = Command::new(bin_path.join(Path::new(&test.binary)));
        command
            .current_dir(&self.igt_folder)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .stdin(Stdio::null())
            .args(&test.args)
            .envs(self.config.env.iter());

        debug!("Begin test {}", test.name);
        Ok(command)
    }

    fn clean(
        &self,
        _caselist_state: &CaselistState,
        tests: &[&TestCase],
        _results: &[RunnerResult],
    ) -> Result<()> {
        let test = self.current_test(tests);
        debug!("End test {}", test.name);
        Ok(())
    }

    fn handle_exit_status(&self, code: Option<i32>, some_result: Option<&mut TestResult>) {
        const IGT_EXIT_SUCCESS: i32 = 0;
        const IGT_EXIT_INVALID: i32 = 79;
        const IGT_EXIT_FAILURE: i32 = 98;
        const IGT_EXIT_SKIP: i32 = 77;

        if let Some(result) = some_result {
            result.status = match code {
                Some(IGT_EXIT_SUCCESS) => TestStatus::Pass,
                Some(IGT_EXIT_INVALID) => TestStatus::Skip,
                Some(IGT_EXIT_FAILURE) => TestStatus::Fail,
                Some(IGT_EXIT_SKIP) => TestStatus::Skip,
                _ => {
                    if result.status != TestStatus::Timeout {
                        TestStatus::Crash
                    } else {
                        result.status
                    }
                }
            }
        }
    }

    fn parse_results(
        &self,
        _caselist_state: &CaselistState,
        tests: &[&TestCase],
        stdout: TimeoutChildStdout,
        timer: Option<Timer>,
        fail_counter: Option<FailCounter>,
    ) -> Result<CaselistResult> {
        let test = self.current_test(tests);
        let parser = IgtResultParser::new(&test.name);
        parser.parse_with_timer(stdout, timer, fail_counter)
    }

    fn should_save_log(&self, _caselist_state: &CaselistState, tests: &[&TestCase]) -> bool {
        let _ = tests;
        true
    }

    fn log_path(&self, _caselist_state: &CaselistState, tests: &[&TestCase]) -> Result<PathBuf> {
        let test = self.current_test(tests);
        Ok(self
            .config
            .output_dir
            .join(format!("igt.{}.log", str::replace(&test.name, "/", "_")).as_str()))
    }

    fn see_more(&self, test_name: &str, _caselist_state: &CaselistState) -> String {
        let log_path = self
            .config
            .output_dir
            .join(format!("igt.{}.log", str::replace(test_name, "/", "_")).as_str());
        format!("See {:?}", log_path)
    }

    fn config(&self) -> &TestConfiguration {
        &self.config
    }
}