fix: try to properly append YAML in correct places in argoapplication #80
| @ -213,7 +213,7 @@ spec: | |||||||
|         let mut yaml_value: Value = |         let mut yaml_value: Value = | ||||||
|             serde_yaml::from_str(yaml_str.as_str()).expect("couldn't parse string to YAML"); |             serde_yaml::from_str(yaml_str.as_str()).expect("couldn't parse string to YAML"); | ||||||
| 
 | 
 | ||||||
|         let spec = yaml_value |         let mut spec = yaml_value | ||||||
|             .get_mut("spec") |             .get_mut("spec") | ||||||
|             .expect("couldn't get spec from yaml") |             .expect("couldn't get spec from yaml") | ||||||
|             .as_mapping_mut() |             .as_mapping_mut() | ||||||
| @ -226,10 +226,19 @@ spec: | |||||||
|         let revision_history_limit = serde_yaml::to_value(&self.revision_history_limit) |         let revision_history_limit = serde_yaml::to_value(&self.revision_history_limit) | ||||||
|             .expect("couldn't serialize revision_history_limit to value"); |             .expect("couldn't serialize revision_history_limit to value"); | ||||||
| 
 | 
 | ||||||
|         spec.entry(source.clone()).or_insert(source); |         spec.insert( | ||||||
|         spec.entry(sync_policy.clone()).or_insert(sync_policy); |             serde_yaml::to_value("source").expect("string to value failed"), | ||||||
|         spec.entry(revision_history_limit.clone()) |             source, | ||||||
|             .or_insert(revision_history_limit); |         ); | ||||||
|  |         spec.insert( | ||||||
|  |             serde_yaml::to_value("syncPolicy").expect("string to value failed"), | ||||||
|  |             sync_policy, | ||||||
|  |         ); | ||||||
|  |         spec.insert( | ||||||
|  |             serde_yaml::to_value("revisionHistoryLimit") | ||||||
|  |                 .expect("couldn't convert str to yaml value"), | ||||||
|  |             revision_history_limit, | ||||||
|  |         ); | ||||||
| 
 | 
 | ||||||
|         debug!("spec: {}", serde_yaml::to_string(spec).unwrap()); |         debug!("spec: {}", serde_yaml::to_string(spec).unwrap()); | ||||||
|         debug!( |         debug!( | ||||||
| @ -237,7 +246,112 @@ spec: | |||||||
|             serde_yaml::to_string(&yaml_value).unwrap() |             serde_yaml::to_string(&yaml_value).unwrap() | ||||||
|         ); |         ); | ||||||
| 
 | 
 | ||||||
|         debug!("yaml serialize of :\n{yaml_str}"); |         yaml_value | ||||||
|         serde_yaml::from_str(&yaml_str).expect("Couldn't parse YAML") |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[cfg(test)] | ||||||
|  | mod tests { | ||||||
|  |     use url::Url; | ||||||
|  | 
 | ||||||
|  |     use crate::modules::application::features::{ | ||||||
|  |         ArgoApplication, Automated, Backoff, Helm, Retry, Source, SyncPolicy, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     #[test] | ||||||
|  |     fn test_argo_application_to_yaml_happy_path() { | ||||||
|  |         let app = ArgoApplication { | ||||||
|  |             name: "test".to_string(), | ||||||
|  |             namespace: Some("test-ns".to_string()), | ||||||
|  |             project: "test-project".to_string(), | ||||||
|  |             source: Source { | ||||||
|  |                 repo_url: Url::parse("http://test").unwrap(), | ||||||
|  |                 target_revision: None, | ||||||
|  |                 chart: "test-chart".to_string(), | ||||||
|  |                 helm: Helm { | ||||||
|  |                     pass_credentials: None, | ||||||
|  |                     parameters: vec![], | ||||||
|  |                     file_parameters: vec![], | ||||||
|  |                     release_name: Some("test-release-neame".to_string()), | ||||||
|  |                     value_files: vec![], | ||||||
|  |                     ignore_missing_value_files: None, | ||||||
|  |                     values: None, | ||||||
|  |                     values_object: None, | ||||||
|  |                     skip_crds: None, | ||||||
|  |                     skip_schema_validation: None, | ||||||
|  |                     version: None, | ||||||
|  |                     kube_version: None, | ||||||
|  |                     api_versions: vec![], | ||||||
|  |                     namespace: None, | ||||||
|  |                 }, | ||||||
|  |             }, | ||||||
|  |             sync_policy: SyncPolicy { | ||||||
|  |                 automated: Automated { | ||||||
|  |                     prune: false, | ||||||
|  |                     self_heal: false, | ||||||
|  |                     allow_empty: false, | ||||||
|  |                 }, | ||||||
|  |                 sync_options: vec![], | ||||||
|  |                 retry: Retry { | ||||||
|  |                     limit: 5, | ||||||
|  |                     backoff: Backoff { | ||||||
|  |                         duration: "5s".to_string(), | ||||||
|  |                         factor: 2, | ||||||
|  |                         max_duration: "3m".to_string(), | ||||||
|  |                     }, | ||||||
|  |                 }, | ||||||
|  |             }, | ||||||
|  |             revision_history_limit: 10, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         let expected_yaml_output = r#"apiVersion: argoproj.io/v1alpha1
 | ||||||
|  | kind: Application | ||||||
|  | metadata: | ||||||
|  |   name: test | ||||||
|  |   namespace: test-ns | ||||||
|  | spec: | ||||||
|  |   project: test-project | ||||||
|  |   destination: | ||||||
|  |     server: https://kubernetes.default.svc
 | ||||||
|  |     namespace: test-ns | ||||||
|  |   source: | ||||||
|  |     repoUrl: http://test/
 | ||||||
|  |     targetRevision: null | ||||||
|  |     chart: test-chart | ||||||
|  |     helm: | ||||||
|  |       passCredentials: null | ||||||
|  |       parameters: [] | ||||||
|  |       fileParameters: [] | ||||||
|  |       releaseName: test-release-neame | ||||||
|  |       valueFiles: [] | ||||||
|  |       ignoreMissingValueFiles: null | ||||||
|  |       values: null | ||||||
|  |       valuesObject: null | ||||||
|  |       skipCrds: null | ||||||
|  |       skipSchemaValidation: null | ||||||
|  |       version: null | ||||||
|  |       kubeVersion: null | ||||||
|  |       apiVersions: [] | ||||||
|  |       namespace: null | ||||||
|  |   syncPolicy: | ||||||
|  |     automated: | ||||||
|  |       prune: false | ||||||
|  |       selfHeal: false | ||||||
|  |       allowEmpty: false | ||||||
|  |     syncOptions: [] | ||||||
|  |     retry: | ||||||
|  |       limit: 5 | ||||||
|  |       backoff: | ||||||
|  |         duration: 5s | ||||||
|  |         factor: 2 | ||||||
|  |         maxDuration: 3m | ||||||
|  |   revisionHistoryLimit: 10"#;
 | ||||||
|  | 
 | ||||||
|  |         assert_eq!( | ||||||
|  |             expected_yaml_output.trim(), | ||||||
|  |             serde_yaml::to_string(&app.clone().to_yaml()) | ||||||
|  |                 .unwrap() | ||||||
|  |                 .trim() | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user