Skip to content

Commit

Permalink
Rework storage tests to test both encoding formats
Browse files Browse the repository at this point in the history
Fix tests failing if the default encoding is switched to legacy in the
code; make the tests not rely on what the coded default is.

Signed-off-by: J Robert Ray <jrray@jrray.org>
  • Loading branch information
jrray committed Jun 17, 2024
1 parent 59195e7 commit eb783fe
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 110 deletions.
58 changes: 24 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ sentry-tracing = { version = "0.32.2" }
serde = "1.0"
serde_json = "1.0"
serde_yaml = "0.9.25"
serial_test = "3.1"
shellexpand = "3.1.0"
spfs = { path = "crates/spfs" }
spfs-cli-common = { path = "crates/spfs-cli/common" }
Expand Down
2 changes: 1 addition & 1 deletion crates/spfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ tonic-build = { workspace = true }
[dev-dependencies]
criterion = { version = "0.3", features = ["async_tokio", "html_reports"] }
rstest = { version = "0.15.0", default_features = false }
serial_test = "0.8.0"
serial_test = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
static_assertions = { workspace = true }

Expand Down
6 changes: 3 additions & 3 deletions crates/spfs/src/bootstrap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::runtime;
case("tcsh", "test.csh", "echo hi; setenv TEST_VALUE 'spfs-test-value'")
)]
#[tokio::test]
#[serial_test::serial] // env and config manipulation must be reliable
#[serial_test::serial(env)] // env and config manipulation must be reliable
async fn test_shell_initialization_startup_scripts(
shell: &str,
startup_script: &str,
Expand Down Expand Up @@ -99,7 +99,7 @@ async fn test_shell_initialization_startup_scripts(

#[rstest(shell, case("bash"), case("tcsh"))]
#[tokio::test]
#[serial_test::serial] // env and config manipulation must be reliable
#[serial_test::serial(env)] // env and config manipulation must be reliable
async fn test_shell_initialization_no_startup_scripts(shell: &str, tmpdir: tempfile::TempDir) {
init_logging();
let shell_path = match which(shell) {
Expand Down Expand Up @@ -154,7 +154,7 @@ async fn test_shell_initialization_no_startup_scripts(shell: &str, tmpdir: tempf
#[cfg(unix)]
#[rstest(shell, case("bash"), case("tcsh"))]
#[tokio::test]
#[serial_test::serial] // env manipulation must be reliable
#[serial_test::serial(env)] // env manipulation must be reliable
async fn test_find_alternate_bash(shell: &str, tmpdir: tempfile::TempDir) {
init_logging();
let original_path = std::env::var("PATH").unwrap_or_default();
Expand Down
77 changes: 58 additions & 19 deletions crates/spfs/src/graph/layer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use rstest::rstest;

use super::Layer;
use crate::encoding;
use crate::encoding::prelude::*;
use crate::graph::object::EncodingFormat;
use crate::graph::object::{DigestStrategy, EncodingFormat};
use crate::graph::{AnnotationValue, Object};
use crate::{encoding, Config};

#[rstest]
fn test_layer_encoding_manifest_only() {
Expand All @@ -22,34 +22,62 @@ fn test_layer_encoding_manifest_only() {
assert_eq!(actual.digest().unwrap(), expected.digest().unwrap())
}

#[rstest]
fn test_layer_encoding_annotation_only() {
#[rstest(
write_encoding_format => [EncodingFormat::Legacy, EncodingFormat::FlatBuffers],
write_digest_strategy => [DigestStrategy::Legacy, DigestStrategy::WithKindAndSalt],
)]
#[serial_test::serial(config)]
fn test_layer_encoding_annotation_only(
write_encoding_format: EncodingFormat,
write_digest_strategy: DigestStrategy,
) {
let mut config = Config::default();
config.storage.encoding_format = write_encoding_format;
config.storage.digest_strategy = write_digest_strategy;
config.make_current().unwrap();

let expected = Layer::new_with_annotation(
"key".to_string(),
AnnotationValue::String("value".to_string()),
);
tracing::error!("Expected: {:?}", expected);

let mut stream = Vec::new();
expected.encode(&mut stream).unwrap();

let decoded = Object::decode(&mut stream.as_slice());
if EncodingFormat::default() == EncodingFormat::Legacy {
if decoded.is_ok() {
panic!("This test should fail when EncodingFormat::Legacy is the default")
match expected.encode(&mut stream) {
Ok(_) if write_encoding_format == EncodingFormat::Legacy => {
panic!("Encode should fail if encoding format is legacy")
}
Ok(_) => {}
Err(_) if write_encoding_format == EncodingFormat::Legacy => {
// This error is expected
return;
}
Err(e) => {
panic!("Error encoding layer: {e}")
}
// Don't run the rest of the test when EncodingFormat::Legacy is used
return;
};

let decoded = Object::decode(&mut stream.as_slice());

let actual = decoded.unwrap().into_layer().unwrap();
println!(" Actual: {:?}", actual);

assert_eq!(actual.digest().unwrap(), expected.digest().unwrap())
}

#[rstest]
fn test_layer_encoding_manifest_and_annotations() {
#[rstest(
write_encoding_format => [EncodingFormat::Legacy, EncodingFormat::FlatBuffers],
write_digest_strategy => [DigestStrategy::Legacy, DigestStrategy::WithKindAndSalt],
)]
#[serial_test::serial(config)]
fn test_layer_encoding_manifest_and_annotations(
write_encoding_format: EncodingFormat,
write_digest_strategy: DigestStrategy,
) {
let mut config = Config::default();
config.storage.encoding_format = write_encoding_format;
config.storage.digest_strategy = write_digest_strategy;
config.make_current().unwrap();

let expected = Layer::new_with_manifest_and_annotations(
encoding::EMPTY_DIGEST.into(),
vec![(
Expand All @@ -60,18 +88,29 @@ fn test_layer_encoding_manifest_and_annotations() {
println!("Expected: {:?}", expected);

let mut stream = Vec::new();
expected.encode(&mut stream).unwrap();
match expected.encode(&mut stream) {
Ok(_) if write_encoding_format == EncodingFormat::Legacy => {
panic!("Encode should fail if encoding format is legacy")
}
Ok(_) => {}
Err(_) if write_encoding_format == EncodingFormat::Legacy => {
// This error is expected
return;
}
Err(e) => {
panic!("Error encoding layer: {e}")
}
};

let actual = Object::decode(&mut stream.as_slice())
.unwrap()
.into_layer()
.unwrap();
println!(" Actual: {:?}", actual);

match EncodingFormat::default() {
match write_encoding_format {
EncodingFormat::Legacy => {
// Legacy encoding does not support annotaion data, so these won't match
assert_ne!(actual.digest().unwrap(), expected.digest().unwrap())
unreachable!();
}
EncodingFormat::FlatBuffers => {
// Under flatbuffers encoding both will contain the
Expand Down
Loading

0 comments on commit eb783fe

Please sign in to comment.