Skip to content

Commit e08d837

Browse files
committed
Emit cargo test json output
This commit uses cargo test flags to emit test results in JSON format, then use cargo2junit tooling to convert JSON output into junit xml file. The cargo test flags are unstable and work only on nightly, but api_generator and yaml_test_runner require nightly channel because rustfmt-nightly is used. Determine branch/commit, test suite and version from running Elasticsearch instance.
1 parent 6396d0e commit e08d837

File tree

5 files changed

+61
-49
lines changed

5 files changed

+61
-49
lines changed

.ci/DockerFile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ RUN set -eux; \
3636
rustup --version; \
3737
cargo --version; \
3838
rustc --version; \
39+
cargo install cargo2junit; \
3940
\
4041
apt-get remove -y --auto-remove \
4142
wget \

.ci/run-repository.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ source $script_path/functions/imports.sh
1313
set -euo pipefail
1414

1515
RUST_TOOLCHAIN=${RUST_TOOLCHAIN-nightly-2020-06-09}
16-
BRANCH=${BRANCH-master}
1716
TOKEN=${TOKEN-}
1817
ELASTICSEARCH_URL=${ELASTICSEARCH_URL-"$elasticsearch_url"}
1918
elasticsearch_container=${elasticsearch_container-}
@@ -22,7 +21,6 @@ echo -e "\033[34;1mINFO:\033[0m VERSION ${STACK_VERSION}\033[0m"
2221
echo -e "\033[34;1mINFO:\033[0m TEST_SUITE ${TEST_SUITE}\033[0m"
2322
echo -e "\033[34;1mINFO:\033[0m URL ${ELASTICSEARCH_URL}\033[0m"
2423
echo -e "\033[34;1mINFO:\033[0m CONTAINER ${elasticsearch_container}\033[0m"
25-
echo -e "\033[34;1mINFO:\033[0m BRANCH ${BRANCH}\033[0m"
2624
echo -e "\033[34;1mINFO:\033[0m RUST_TOOLCHAIN ${RUST_TOOLCHAIN}\033[0m"
2725

2826
echo -e "\033[1m>>>>> Build [elastic/elasticsearch-rs container] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"
@@ -36,12 +34,14 @@ repo=$(realpath $(dirname $(realpath -s $0))/../)
3634
docker run \
3735
--network=${network_name} \
3836
--env "ES_TEST_SERVER=${ELASTICSEARCH_URL}" \
39-
--env "ELASTICSEARCH_VERSION=${elasticsearch_container}" \
4037
--env "TOKEN=${TOKEN}" \
4138
--name test-runner \
4239
--volume ${repo}/test_results:/usr/src/elasticsearch-rs/test_results \
4340
--rm \
4441
elastic/elasticsearch-rs \
4542
/bin/bash -c \
46-
"cargo run -p yaml_test_runner -- --branch \"${BRANCH}\" --path \"/usr/src/elasticsearch-rs/api_generator/rest_specs\"; cargo test -p yaml_test_runner -- --test-threads=1"
43+
"cargo run -p yaml_test_runner -- -u \"${ELASTICSEARCH_URL}\" -p \"api_generator/rest_specs\"; \\
44+
mkdir -p test_results; \\
45+
cargo test -p yaml_test_runner -- --test-threads=1 -Z unstable-options --format json | tee test_results/results.json; \\
46+
cat test_results/results.json | cargo2junit > test_results/junit.xml"
4747

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* text=auto
2+
3+
*.sh text eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ yaml_test_runner/yaml/
99
yaml_test_runner/tests/oss
1010
yaml_test_runner/tests/xpack
1111
yaml_test_runner/tests/mod.rs
12+
test_results/

yaml_test_runner/src/main.rs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern crate simple_logger;
2828

2929
use clap::{App, Arg};
3030
use log::Level;
31+
use serde_json::Value;
3132
use std::{fs, path::PathBuf, process::exit};
3233

3334
mod generator;
@@ -42,21 +43,20 @@ fn main() -> Result<(), failure::Error> {
4243

4344
let matches = App::new(env!("CARGO_PKG_NAME"))
4445
.about(env!("CARGO_PKG_DESCRIPTION"))
45-
.arg(Arg::with_name("branch")
46-
.short("b")
47-
.long("branch")
48-
.value_name("BRANCH")
49-
.help("The git branch in the Elasticsearch repository from which to download yaml tests")
50-
.required(true)
51-
.default_value("master")
52-
.takes_value(true))
5346
.arg(Arg::with_name("token")
5447
.short("t")
5548
.long("token")
5649
.value_name("TOKEN")
5750
.help("The GitHub access token. Increases the rate limit to be able to download all yaml tests. Must be specified if not passed through environment variable")
5851
.required(false)
5952
.takes_value(true))
53+
.arg(Arg::with_name("url")
54+
.short("u")
55+
.long("url")
56+
.value_name("ELASTICSEARCH_URL")
57+
.help("The url of a running Elasticsearch cluster. Used to determine the version, test suite and branch to use to compile tests")
58+
.required(true)
59+
.takes_value(true))
6060
.arg(Arg::with_name("path")
6161
.short("p")
6262
.long("path")
@@ -66,39 +66,8 @@ fn main() -> Result<(), failure::Error> {
6666
.takes_value(true))
6767
.get_matches();
6868

69-
// Get the version from ELASTICSEARCH_VERSION environment variable, if set.
70-
// any prerelease part needs to be trimmed because the semver crate only allows
71-
// a version with a prerelease to match against predicates, if at least one predicate
72-
// has a prerelease. See
73-
// https://github.com/steveklabnik/semver/blob/afa5fc853cb4d6d2b1329579e5528f86f3b550f9/src/version_req.rs#L319-L331
74-
let (suite, version) = match std::env::var("ELASTICSEARCH_VERSION") {
75-
Ok(v) => {
76-
let suite = if v.contains("oss") {
77-
TestSuite::Oss
78-
} else {
79-
TestSuite::XPack
80-
};
81-
82-
let v = v
83-
.split(':')
84-
.next_back()
85-
.unwrap()
86-
.trim_end_matches(|c: char| c.is_alphabetic() || c == '-');
87-
88-
(suite, semver::Version::parse(v)?)
89-
}
90-
Err(_) => {
91-
error!("ELASTICSEARCH_VERSION environment variable must be set to compile tests");
92-
exit(1);
93-
}
94-
};
95-
96-
info!("Using version {:?} to compile tests", &version);
97-
98-
let branch = matches
99-
.value_of("branch")
100-
.expect("missing 'branch' argument");
101-
69+
let url = matches.value_of("url").expect("missing 'url' argument");
70+
let path = matches.value_of("path").expect("missing 'path' argument");
10271
let token = match std::env::var("TOKEN") {
10372
Ok(v) => v,
10473
Err(_) => match matches.value_of("token") {
@@ -109,12 +78,27 @@ fn main() -> Result<(), failure::Error> {
10978
}
11079
},
11180
};
112-
let path = matches.value_of("path").expect("missing 'path' argument");
81+
82+
let (branch, suite, version) = match branch_suite_and_version_from_elasticsearch(url) {
83+
Ok(v) => v,
84+
Err(e) => {
85+
error!(
86+
"Problem getting values from Elasticsearch at {}. {:?}",
87+
url, e
88+
);
89+
exit(1);
90+
}
91+
};
92+
93+
info!("Using version {}", &version.to_string());
94+
info!("Using branch {}", &branch);
95+
info!("Using test_suite {:?}", &suite);
96+
11397
let rest_specs_dir = PathBuf::from(path);
11498
let download_dir = PathBuf::from(format!("./{}/yaml", env!("CARGO_PKG_NAME")));
11599
let generated_dir = PathBuf::from(format!("./{}/tests", env!("CARGO_PKG_NAME")));
116100

117-
github::download_test_suites(&token, branch, &download_dir)?;
101+
github::download_test_suites(&token, &branch, &download_dir)?;
118102

119103
let mut last_downloaded_rest_spec_branch = rest_specs_dir.clone();
120104
last_downloaded_rest_spec_branch.push("last_downloaded_version");
@@ -134,10 +118,10 @@ fn main() -> Result<(), failure::Error> {
134118
}
135119

136120
if download_rest_specs {
137-
api_generator::rest_spec::download_specs(branch, &rest_specs_dir)?;
121+
api_generator::rest_spec::download_specs(&branch, &rest_specs_dir)?;
138122
}
139123

140-
let api = api_generator::generator::read_api(branch, &rest_specs_dir)?;
124+
let api = api_generator::generator::read_api(&branch, &rest_specs_dir)?;
141125

142126
// delete everything under the generated_dir except common dir
143127
if generated_dir.exists() {
@@ -168,3 +152,26 @@ fn main() -> Result<(), failure::Error> {
168152

169153
Ok(())
170154
}
155+
156+
fn branch_suite_and_version_from_elasticsearch(
157+
url: &str,
158+
) -> Result<(String, TestSuite, semver::Version), failure::Error> {
159+
let mut response = reqwest::get(url)?;
160+
let json: Value = response.json()?;
161+
let branch = json["version"]["build_hash"].as_str().unwrap().to_string();
162+
let suite = match json["version"]["build_flavor"].as_str().unwrap() {
163+
"oss" => TestSuite::Oss,
164+
_ => TestSuite::XPack,
165+
};
166+
167+
// any prerelease part needs to be trimmed because the semver crate only allows
168+
// a version with a prerelease to match against predicates, if at least one predicate
169+
// has a prerelease. See
170+
// https://github.com/steveklabnik/semver/blob/afa5fc853cb4d6d2b1329579e5528f86f3b550f9/src/version_req.rs#L319-L331
171+
let version = json["version"]["number"]
172+
.as_str()
173+
.unwrap()
174+
.trim_end_matches(|c: char| c.is_alphabetic() || c == '-');
175+
176+
Ok((branch, suite, semver::Version::parse(version)?))
177+
}

0 commit comments

Comments
 (0)