@@ -21,7 +21,7 @@ pub mod versioned {
2121 ) ,
2222 namespaced
2323 ) ) ]
24- #[ derive( Clone , Debug , PartialEq , CustomResource , Deserialize , Serialize , JsonSchema ) ]
24+ #[ derive( Clone , Debug , PartialEq , Eq , CustomResource , Deserialize , Serialize , JsonSchema ) ]
2525 pub struct ScalerSpec {
2626 /// Desired replica count.
2727 ///
@@ -40,7 +40,7 @@ pub mod versioned {
4040}
4141
4242/// Status of a StackableScaler.
43- #[ derive( Clone , Debug , Deserialize , Serialize , JsonSchema ) ]
43+ #[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize , JsonSchema ) ]
4444#[ serde( rename_all = "camelCase" ) ]
4545pub struct ScalerStatus {
4646 /// The current total number of replicas targeted by the managed StatefulSet.
@@ -64,14 +64,14 @@ pub struct ScalerStatus {
6464// and others to be typed as objects. We therefore encode the variant data in a separate details
6565// key/object. With this, all variants can be encoded as strings, while the status can still contain
6666// additional data in an extra field when needed.
67- #[ derive( Clone , Debug , Deserialize , Serialize , strum:: Display ) ]
67+ #[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize , strum:: Display ) ]
6868#[ serde(
6969 tag = "state" ,
7070 content = "details" ,
71- rename_all = "camelCase " ,
71+ rename_all = "PascalCase " ,
7272 rename_all_fields = "camelCase"
7373) ]
74- #[ strum( serialize_all = "camelCase " ) ]
74+ #[ strum( serialize_all = "PascalCase " ) ]
7575pub enum ScalerState {
7676 /// No scaling operation is in progress.
7777 Idle ,
@@ -140,8 +140,8 @@ impl JsonSchema for ScalerState {
140140}
141141
142142/// In which state the scaling operation failed.
143- #[ derive( Clone , Debug , Deserialize , Serialize , JsonSchema ) ]
144- #[ serde( rename_all = "camelCase " ) ]
143+ #[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize , JsonSchema ) ]
144+ #[ serde( rename_all = "PascalCase " ) ]
145145pub enum FailedInState {
146146 /// The `pre_scale` hook returned an error.
147147 PreScaling ,
@@ -152,3 +152,51 @@ pub enum FailedInState {
152152 /// The `post_scale` hook returned an error.
153153 PostScaling ,
154154}
155+
156+ #[ cfg( test) ]
157+ mod tests {
158+ use rstest:: rstest;
159+
160+ use super :: * ;
161+ use crate :: utils:: yaml_from_str_singleton_map;
162+
163+ #[ rstest]
164+ #[ case:: idle( "state: Idle" , ScalerState :: Idle { } ) ]
165+ #[ case:: pre_scaling( "state: PreScaling" , ScalerState :: PreScaling { } ) ]
166+ #[ case:: scaling( "state: Scaling
167+ details:
168+ previousReplicas: 42" , ScalerState :: Scaling { previous_replicas: 42 } ) ]
169+ #[ case:: post_scaling( "state: PostScaling
170+ details:
171+ previousReplicas: 42" , ScalerState :: PostScaling { previous_replicas: 42 } ) ]
172+ #[ case:: failed( "state: Failed
173+ details:
174+ failedIn: PreScaling
175+ reason: bruh moment" , ScalerState :: Failed {
176+ failed_in: FailedInState :: PreScaling ,
177+ reason: "bruh moment" . to_owned( )
178+ } ) ]
179+ fn parse_state ( #[ case] input : & str , #[ case] expected : ScalerState ) {
180+ let parsed: ScalerState =
181+ yaml_from_str_singleton_map ( input) . expect ( "invalid test YAML input" ) ;
182+ assert_eq ! ( parsed, expected) ;
183+ }
184+
185+ #[ rstest]
186+ #[ case:: idle( ScalerState :: Idle { } , "state: Idle\n " ) ]
187+ #[ case:: pre_scaling( ScalerState :: PreScaling { } , "state: PreScaling\n " ) ]
188+ #[ case:: scaling( ScalerState :: Scaling { previous_replicas: 42 } , "state: Scaling
189+ details:
190+ previousReplicas: 42\n " ) ]
191+ #[ case:: post_scaling( ScalerState :: PostScaling { previous_replicas: 42 } , "state: PostScaling
192+ details:
193+ previousReplicas: 42\n " ) ]
194+ #[ case:: failed( ScalerState :: Failed { failed_in: FailedInState :: PreScaling , reason: "bruh moment" . to_owned( ) } , "state: Failed
195+ details:
196+ failedIn: PreScaling
197+ reason: bruh moment\n " ) ]
198+ fn serialize_state ( #[ case] input : ScalerState , #[ case] expected : & str ) {
199+ let serialized = serde_yaml:: to_string ( & input) . expect ( "serialization always passes" ) ;
200+ assert_eq ! ( serialized, expected) ;
201+ }
202+ }
0 commit comments