Skip to content

Commit

Permalink
Merge pull request #21 from opencomputeproject/fea/spec_validation
Browse files Browse the repository at this point in the history
fea/spec validation
  • Loading branch information
mimir-d authored Oct 14, 2024
2 parents a59bc8d + 7a360d5 commit e25600d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 12 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,29 @@ jobs:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
env_vars: OS,RUSTVER

spec:
runs-on: ubuntu-latest
name: examples / spec validation
steps:
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
- name: pull validator
run: git clone https://github.com/opencomputeproject/ocp-diag-core.git --depth=1
- name: Install go
uses: actions/setup-go@v2
with:
go-version: "1.17.6"
- name: run validator against examples
run: |
ROOT="$(pwd)"
cd ocp-diag-core/validators/spec_validator
cargo metadata --manifest-path $ROOT/Cargo.toml --format-version 1 |
jq -r '.["packages"][] | select(.name == "ocptv") | .targets[] | select(.kind[0] == "example") | .name' |
xargs -I{} bash -c "
echo validating output of example {}... &&
cargo run --manifest-path $ROOT/Cargo.toml --example {} |
tee /dev/stderr |
go run . -schema ../../json_spec/output/root.json -
"
10 changes: 4 additions & 6 deletions examples/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum ExtensionType {
}

#[derive(Serialize)]
struct ComplexExtension {
struct Extension {
#[serde(rename = "@type")]
ext_type: ExtensionType,

Expand All @@ -25,13 +25,11 @@ struct ComplexExtension {
}

async fn step0(s: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
s.add_extension("simple", "extension_identifier").await?;

s.add_extension(
"complex",
ComplexExtension {
"ext0",
Extension {
ext_type: ExtensionType::Example,
field: "demo".to_owned(),
field: "example".to_owned(),
subtypes: vec![1, 42],
},
)
Expand Down
62 changes: 62 additions & 0 deletions examples/measurement_concurrency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// (c) Meta Platforms, Inc. and affiliates.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

use std::{sync::Arc, time::Duration};

use anyhow::Result;
use rand;

use ocptv::output as tv;
use tv::{DutInfo, TestResult, TestRun, TestRunOutcome, TestStatus};

/// While the general recommendation is to run test steps sequentially, the specification does not
/// mandate for this to happen. This example shows multiple steps running in parallel, each
/// emitting their own measurements.
#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() -> Result<()> {
let dut = DutInfo::builder("dut0").build();

TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
let run = Arc::new(r);

let tasks = (0..5)
.map(|i| {
tokio::spawn({
let r = Arc::clone(&run);
async move {
r.add_step(&format!("step{}", i))
.scope(move |s| async move {
let offset = rand::random::<u64>() % 10000;
tokio::time::sleep(Duration::from_micros(offset)).await;

let fan_speed = 1000 + 100 * i;
s.add_measurement(&format!("fan{}", i), fan_speed)
.await
.unwrap();

Ok(TestStatus::Complete)
})
.await
}
})
})
.collect::<Vec<_>>();

for t in tasks {
t.await.map_err(|e| tv::OcptvError::Other(Box::new(e)))??;
}

Ok(TestRunOutcome {
status: TestStatus::Complete,
result: TestResult::Pass,
})
})
.await?;

Ok(())
}
1 change: 0 additions & 1 deletion examples/measurement_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
#![allow(warnings)]

use anyhow::Result;
use chrono::Duration;
Expand Down
1 change: 0 additions & 1 deletion examples/measurement_subcomponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
// #![allow(warnings)]

use anyhow::Result;

Expand Down
1 change: 0 additions & 1 deletion examples/measurement_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
// #![allow(warnings)]

use anyhow::Result;

Expand Down
3 changes: 3 additions & 0 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ pub enum OcptvError {

#[error("failed to format input object")]
Format(Box<dyn std::error::Error + Send + Sync + 'static>), // opaque type so we don't leak impl

#[error("other error")]
Other(Box<dyn std::error::Error + Send + Sync + 'static>),
}
2 changes: 1 addition & 1 deletion src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ pub struct Diagnosis {
#[derive(Debug, Serialize, PartialEq, Clone)]
#[serde(rename = "file")]
pub struct File {
#[serde(rename = "name")]
#[serde(rename = "displayName")]
pub name: String,

#[serde(rename = "uri")]
Expand Down
4 changes: 2 additions & 2 deletions tests/output/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn test_step_with_file() -> Result<()> {
"testStepArtifact": {
"testStepId": "step0",
"file": {
"name": "name",
"displayName": "name",
"uri": uri.clone().as_str().to_owned(),
"isSnapshot": false
}
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn test_step_with_file_builder() -> Result<()> {
"testStepArtifact": {
"testStepId": "step0",
"file": {
"name": "name",
"displayName": "name",
"uri": uri.clone().as_str().to_owned(),
"isSnapshot": false,
"contentType": "text/plain",
Expand Down

0 comments on commit e25600d

Please sign in to comment.