Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Issue-Id: #267
  • Loading branch information
christoph-hamm committed Sep 17, 2024
1 parent e9b4217 commit 43bf139
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 17 deletions.
2 changes: 2 additions & 0 deletions agent/src/runtime_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,8 @@ mod tests {
runtime: Some("runtime1".to_string()),
runtime_config: Some("generalOptions: [\"--version\"]\ncommandOptions: [\"--network=host\"]\nimage: alpine:latest\ncommandArgs: [\"bash\"]\n".to_string()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: Default::default()})
})];
let mut complete_state = test_utils::generate_test_proto_complete_state(&workloads);
complete_state.workload_states = Some(ank_base::WorkloadStatesMap {
Expand Down
62 changes: 59 additions & 3 deletions ank/src/cli_commands/server_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ mod tests {
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some(String::default()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
},
)]),
),
Expand All @@ -449,7 +456,14 @@ mod tests {
}),
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into()
})
},
)])
.into())
Expand Down Expand Up @@ -526,6 +540,13 @@ mod tests {
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
},
)]),
)),
Expand Down Expand Up @@ -554,6 +575,13 @@ mod tests {
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
},
)]),
),
Expand All @@ -577,7 +605,14 @@ mod tests {
}),
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into()
})
},
)])
.into())
Expand Down Expand Up @@ -619,6 +654,13 @@ mod tests {
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
},
)]),
),
Expand All @@ -642,7 +684,14 @@ mod tests {
}),
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into()
})
},
)])
.into())
Expand Down Expand Up @@ -789,6 +838,13 @@ mod tests {
restart_policy: Some(ank_base::RestartPolicy::Never as i32),
runtime_config: Some("".to_string()),
control_interface_access: None,
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
},
)]),
)),
Expand Down
1 change: 1 addition & 0 deletions api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)
// .type_attribute("ConfigItem", "#[serde(try_from = \"serde_yaml::Value\")]")
.field_attribute("Workload.tags", "#[serde(flatten)]")
.field_attribute("Workload.configs", "#[serde(flatten)]")
.field_attribute("Workload.dependencies", "#[serde(flatten)]")
.field_attribute("WorkloadStatesMap.agentStateMap", "#[serde(flatten)]")
.field_attribute(
Expand Down
9 changes: 9 additions & 0 deletions api/proto/ank_base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ message Workload {
optional string runtime = 5; /// The name of the runtime e.g. podman.
optional string runtimeConfig = 6; /// The configuration information specific to the runtime.
ControlInterfaceAccess controlInterfaceAccess = 7;
ConfigMappings configs = 8;
}

/**
Expand Down Expand Up @@ -319,6 +320,14 @@ enum ReadWriteEnum {
RW_READ_WRITE = 5; // Allow read and write
}

/**
* This is a workaround for proto not supporing optional maps
*/
message ConfigMappings {
map<string, string> configs = 1;
}


/**
* This is a workaround for proto not supporing optional maps
*/
Expand Down
17 changes: 15 additions & 2 deletions common/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ mod tests {

mod ank_base {
pub use api::ank_base::{
request::RequestContent, CompleteState, CompleteStateRequest, Dependencies, Request,
RestartPolicy, State, Tag, Tags, UpdateStateRequest, Workload, WorkloadMap,
request::RequestContent, CompleteState, CompleteStateRequest, ConfigMappings,
Dependencies, Request, RestartPolicy, State, Tag, Tags, UpdateStateRequest, Workload,
WorkloadMap,
};
}

Expand Down Expand Up @@ -276,6 +277,13 @@ mod tests {
}],
}),
control_interface_access: Default::default(),
configs: Some(ank_base::ConfigMappings {
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}),
}
};
(ankaios) => {
Expand All @@ -290,6 +298,11 @@ mod tests {
runtime: RUNTIME.to_string(),
runtime_config: RUNTIME_CONFIG.to_string(),
control_interface_access: Default::default(),
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}
};
}
Expand Down
12 changes: 12 additions & 0 deletions common/src/objects/stored_workload_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct StoredWorkloadSpec {
pub runtime_config: String,
#[serde(default)]
pub control_interface_access: ControlInterfaceAccess,
#[serde(default)]
pub configs: HashMap<String, String>,
}

impl TryFrom<ank_base::Workload> for StoredWorkloadSpec {
Expand Down Expand Up @@ -67,6 +69,7 @@ impl TryFrom<ank_base::Workload> for StoredWorkloadSpec {
.control_interface_access
.unwrap_or_default()
.try_into()?,
configs: value.configs.ok_or("Missing field configs")?.configs,
})
}
}
Expand All @@ -89,6 +92,9 @@ impl From<StoredWorkloadSpec> for ank_base::Workload {
tags: workload.tags.into_iter().map(|x| x.into()).collect(),
}),
control_interface_access: workload.control_interface_access.into(),
configs: Some(ank_base::ConfigMappings {
configs: workload.configs,
}),
}
}
}
Expand Down Expand Up @@ -121,6 +127,7 @@ impl From<WorkloadSpec> for StoredWorkloadSpec {
tags: value.tags,
runtime_config: value.runtime_config,
control_interface_access: value.control_interface_access,
configs: Default::default(),
}
}
}
Expand Down Expand Up @@ -153,6 +160,11 @@ pub fn generate_test_stored_workload_spec_with_config(
}],
runtime_config: runtime_config.into(),
control_interface_access: Default::default(),
configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions common/src/state_manipulation/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,12 @@ mod tests {
Mapping::default()
.entry("allowRules", vec![] as Vec<Value>)
.entry("denyRules", vec![] as Vec<Value>),
)
.entry(
"configs",
Mapping::default()
.entry("ref1", "config.path.1")
.entry("ref2", "config.path.2")
),
),
)
Expand Down
41 changes: 29 additions & 12 deletions common/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

use std::collections::HashMap;

use api::ank_base::{self, Dependencies, Tags, WorkloadMap};
use api::ank_base::{self, ConfigMappings, Dependencies, Tags, WorkloadMap};
use serde::{Serialize, Serializer};

use crate::objects::{
generate_test_workload_spec_with_param, DeleteCondition, DeletedWorkload, State,
WorkloadInstanceName, WorkloadSpec,
generate_test_runtime_config, generate_test_stored_workload_spec_with_config, DeleteCondition,
DeletedWorkload, State, StoredWorkloadSpec, WorkloadInstanceName, WorkloadSpec,
};

const RUNTIME_NAME: &str = "runtime";
Expand All @@ -33,7 +33,16 @@ pub fn generate_test_state_from_workloads(workloads: Vec<WorkloadSpec>) -> State
api_version: API_VERSION.into(),
workloads: workloads
.into_iter()
.map(|v| (v.instance_name.workload_name().to_owned(), v.into()))
.map(|v| {
let name = v.instance_name.workload_name().to_owned();
let mut w = StoredWorkloadSpec::from(v);
w.configs = [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
]
.into();
(name, w)
})
.collect(),
configs: HashMap::new(),
}
Expand Down Expand Up @@ -86,20 +95,20 @@ pub fn generate_test_state() -> State {

let mut ankaios_workloads = HashMap::new();

let workload_1 = generate_test_workload_spec_with_param(
let workload_1 = generate_test_stored_workload_spec_with_config(
AGENT_NAME.to_owned(),
WORKLOAD_1_NAME.to_owned(),
RUNTIME_NAME.to_owned(),
generate_test_runtime_config(),
);

let workload_2 = generate_test_workload_spec_with_param(
let workload_2 = generate_test_stored_workload_spec_with_config(
AGENT_NAME.to_owned(),
WORKLOAD_2_NAME.to_owned(),
RUNTIME_NAME.to_owned(),
generate_test_runtime_config(),
);

ankaios_workloads.insert(workload_name_1, workload_1.into());
ankaios_workloads.insert(workload_name_2, workload_2.into());
ankaios_workloads.insert(workload_name_1, workload_1);
ankaios_workloads.insert(workload_name_2, workload_2);

State {
api_version: API_VERSION.into(),
Expand Down Expand Up @@ -161,7 +170,11 @@ pub fn generate_test_proto_workload_with_param(
key: "key".into(),
value: "value".into(),
}]}),
control_interface_access: Default::default()
control_interface_access: Default::default(),
configs: Some(ConfigMappings{configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
].into()})
}
}

Expand All @@ -177,7 +190,11 @@ pub fn generate_test_proto_workload() -> ank_base::Workload {
key: "key".into(),
value: "value".into(),
}]}),
control_interface_access: Default::default()
control_interface_access: Default::default(),
configs: Some(ConfigMappings{configs: [
("ref1".into(), "config.path.1".into()),
("ref2".into(), "config.path.2".into()),
].into()})
}
}

Expand Down
2 changes: 2 additions & 0 deletions server/src/ankaios_server/server_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ mod tests {
runtime: None,
runtime_config: None,
control_interface_access: None,
configs: None,
},
),
(
Expand All @@ -475,6 +476,7 @@ mod tests {
runtime: Some(w1.runtime.clone()),
runtime_config: Some(w1.runtime_config.clone()),
control_interface_access: w1.control_interface_access.into(),
configs: Some(Default::default()),
},
),
];
Expand Down

0 comments on commit 43bf139

Please sign in to comment.