Skip to content

Commit

Permalink
chore: add test for mapping to sinker container
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebond committed Jul 25, 2023
1 parent 6c740da commit fcd3027
Showing 1 changed file with 93 additions and 18 deletions.
111 changes: 93 additions & 18 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,18 @@ mod tests {
#[tokio::test]
async fn test_get_ar_from_subtree() {
let subtree = &serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": { "name": "test-sinker-container" },
"spec": {
"dummykey": "dummyvalue",
},
});
let ar = get_ar_from_subtree(subtree).unwrap();
assert_eq!(ar.group, "sinker.tubernetes.io");
assert_eq!(ar.group, "sinker.influxdata.io");
assert_eq!(ar.version, "v1alpha1");
assert_eq!(ar.kind, "SinkerContainer");
assert_eq!(ar.api_version, "sinker.tubernetes.io/v1alpha1");
assert_eq!(ar.api_version, "sinker.influxdata.io/v1alpha1");
}

#[tokio::test]
Expand Down Expand Up @@ -468,15 +468,15 @@ mod tests {
}],
source: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.tubernetes.io/v1alpha1".to_string(),
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container-1".to_string(),
},
cluster: None,
},
target: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.tubernetes.io/v1alpha1".to_string(),
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container-2".to_string(),
},
Expand All @@ -486,7 +486,7 @@ mod tests {
);
let dynamic_sc: DynamicObject = serde_json::from_str(
&serde_json::to_string(&serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": { "name": "test-sinker-container-1" },
"spec": {
Expand All @@ -499,7 +499,7 @@ mod tests {
)
.unwrap();
let expected = serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": {
"name": "test-sinker-container-2",
Expand All @@ -512,7 +512,7 @@ mod tests {
},
});
let ar = ApiResource::from_gvk(&GroupVersionKind {
group: "sinker.tubernetes.io".to_string(),
group: "sinker.influxdata.io".to_string(),
version: "v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
});
Expand Down Expand Up @@ -547,15 +547,15 @@ mod tests {
],
source: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.tubernetes.io/v1alpha1".to_string(),
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container-1".to_string(),
},
cluster: None,
},
target: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.tubernetes.io/v1alpha1".to_string(),
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container-2".to_string(),
},
Expand All @@ -565,7 +565,7 @@ mod tests {
);
let dynamic_sc: DynamicObject = serde_json::from_str(
&serde_json::to_string(&serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": {
"labels": { "key": "value" },
Expand All @@ -581,7 +581,7 @@ mod tests {
)
.unwrap();
let expected = serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": {
"labels": { "key": "value" },
Expand All @@ -595,7 +595,7 @@ mod tests {
},
});
let ar = ApiResource::from_gvk(&GroupVersionKind {
group: "sinker.tubernetes.io".to_string(),
group: "sinker.influxdata.io".to_string(),
version: "v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
});
Expand Down Expand Up @@ -624,7 +624,7 @@ mod tests {
}],
source: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.tubernetes.io/v1alpha1".to_string(),
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container".to_string(),
},
Expand All @@ -641,9 +641,9 @@ mod tests {
},
);
let ar = ApiResource::from_gvk(&GroupVersionKind {
group: "sinker.tubernetes.io".to_string(),
version: "v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
group: "".to_string(),
version: "v1".to_string(),
kind: "ConfigMap".to_string(),
});
let expected = serde_json::json!({
"apiVersion": "v1",
Expand All @@ -664,7 +664,7 @@ mod tests {
});
let dynamic_sc: DynamicObject = serde_json::from_str(
&serde_json::to_string(&serde_json::json!({
"apiVersion": "sinker.tubernetes.io/v1alpha1",
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": { "name": "test-sinker-container" },
"spec": {
Expand Down Expand Up @@ -696,4 +696,79 @@ mod tests {
serde_json::to_string(&expected).unwrap(),
);
}

#[tokio::test]
async fn test_apply_mappings_to_sinkercontainer() {
let resource_sync = ResourceSync::new(
"sinker-test",
ResourceSyncSpec {
mappings: vec![Mapping {
from_field_path: None,
to_field_path: Some("spec".to_string()),
}],
source: ClusterResourceRef {
resource_ref: GVKN {
api_version: "v1".to_string(),
kind: "Deployment".to_string(),
name: "test-deployment".to_string(),
},
cluster: None,
},
target: ClusterResourceRef {
resource_ref: GVKN {
api_version: "sinker.influxdata.io/v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
name: "test-sinker-container".to_string(),
},
cluster: None,
},
},
);
let ar = ApiResource::from_gvk(&GroupVersionKind {
group: "sinker.influxdata.io".to_string(),
version: "v1alpha1".to_string(),
kind: "SinkerContainer".to_string(),
});
let source_dep = serde_json::json!({
"apiVersion": "v1",
"kind": "Deployment",
"metadata": {
"annotations": {
"key1": "value1",
},
"labels": {
"key2": "value2",
},
"name": "test-deployment",
"namespace": "default",
},
"spec": {
"dummykey": "dummyvalue",
},
"status": {
"isgood": true,
},
});
let expected = serde_json::json!({
"apiVersion": "sinker.influxdata.io/v1alpha1",
"kind": "SinkerContainer",
"metadata": { "name": "test-sinker-container", "namespace": "default" },
"spec": source_dep,
});
let dynamic_sc: DynamicObject =
serde_json::from_str(&serde_json::to_string(&serde_json::json!(source_dep)).unwrap())
.unwrap();
let target = apply_mappings(
&dynamic_sc,
&resource_sync.spec.target.resource_ref,
"default",
&ar,
&resource_sync,
)
.unwrap();
assert_eq!(
serde_json::to_string(&target).unwrap(),
serde_json::to_string(&expected).unwrap(),
);
}
}

0 comments on commit fcd3027

Please sign in to comment.