Skip to content

Commit 5e3e1a0

Browse files
test: Add unit tests for Port
1 parent bb5e4af commit 5e3e1a0

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

rust/operator-binary/src/controller/validate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,6 @@ mod tests {
993993
spec: listener::v1alpha1::ListenerSpec::default(),
994994
status: Some(listener::v1alpha1::ListenerStatus {
995995
ingress_addresses: Some(vec![listener::v1alpha1::ListenerIngress {
996-
// TODO Check if it is the FQDN
997996
address: "my-opensearch.default.svc.cluster.local".to_owned(),
998997
address_type: listener::v1alpha1::AddressType::Hostname,
999998
ports: [("http".to_owned(), 9200)].into(),

rust/operator-binary/src/framework/controller_utils.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ mod tests {
8787
type DynamicType = ();
8888

8989
fn kind(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
90-
"".into()
90+
"TestResource".into()
9191
}
9292

9393
fn group(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
94-
todo!()
94+
"stackable.tech".into()
9595
}
9696

9797
fn version(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
98-
todo!()
98+
"v1".into()
9999
}
100100

101101
fn plural(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
102-
todo!()
102+
"testresources".into()
103103
}
104104

105105
fn name(&self) -> Option<std::borrow::Cow<'_, str>> {
@@ -111,7 +111,7 @@ mod tests {
111111
}
112112

113113
fn resource_version(&self) -> Option<std::borrow::Cow<'_, str>> {
114-
todo!()
114+
Some("1".into())
115115
}
116116

117117
fn uid(&self) -> Option<std::borrow::Cow<'_, str>> {

rust/operator-binary/src/framework/types/common.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Common types that do not belong (yet) to a more specific module
12
use snafu::{ResultExt, Snafu};
23
use strum::{EnumDiscriminants, IntoStaticStr};
34

@@ -8,6 +9,7 @@ pub enum Error {
89
ConvertToPortNumber { source: std::num::TryFromIntError },
910
}
1011

12+
/// A port number
1113
#[derive(Clone, Debug, Eq, PartialEq)]
1214
pub struct Port(pub u16);
1315

@@ -32,3 +34,29 @@ impl From<Port> for i32 {
3234
value.0 as i32
3335
}
3436
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
41+
use super::{ErrorDiscriminants, Port};
42+
43+
#[test]
44+
fn test_port_fmt() {
45+
assert_eq!("0".to_owned(), Port(0).to_string());
46+
assert_eq!("65535".to_owned(), Port(65535).to_string());
47+
}
48+
49+
#[test]
50+
fn test_port_try_from_i32() {
51+
assert_eq!(Some(Port(0)), Port::try_from(0).ok());
52+
assert_eq!(Some(Port(65535)), Port::try_from(65535).ok());
53+
assert_eq!(
54+
Err(ErrorDiscriminants::ConvertToPortNumber),
55+
Port::try_from(-1).map_err(ErrorDiscriminants::from)
56+
);
57+
assert_eq!(
58+
Err(ErrorDiscriminants::ConvertToPortNumber),
59+
Port::try_from(65536).map_err(ErrorDiscriminants::from)
60+
);
61+
}
62+
}

0 commit comments

Comments
 (0)