fn test_handle_endpoint_slice_event()

in nfm-controller/src/kubernetes/kubernetes_metadata_collector.rs [858:906]


    fn test_handle_endpoint_slice_event() {
        let collector = KubernetesMetadataCollector::new();
        let adress = IpAddr::from_str("192.168.1.1").unwrap();
        let slice = create_test_slice(adress);

        // add the endpoint
        let event = Ok(Event::Apply(slice.clone()));
        KubernetesMetadataCollector::handle_endpoint_slice_event(
            event,
            collector.pod_info_arc.clone(),
        );

        // keep these scoped so that they unlock after using, otherwise the next "handle_endpoint_slice will wait for lock forever"
        {
            let pod_info = collector.pod_info_arc.lock().unwrap();
            assert_eq!(
                pod_info.get(&adress).unwrap().get(&TEST_POD_PORT),
                Some(&PodInfo {
                    name: adress_to_podname(adress),
                    namespace: "default".to_string(),
                    service_name: "test-service".to_string(),
                })
            );
        }

        // now delete it
        let event = Ok(Event::Delete(slice));
        KubernetesMetadataCollector::handle_endpoint_slice_event(
            event,
            collector.pod_info_arc.clone(),
        );

        {
            let pod_info = collector.pod_info_arc.lock().unwrap();
            assert!(!pod_info.contains_key(&adress));
        }

        // and do a final test to see if it is still empty after a no-op event
        KubernetesMetadataCollector::handle_endpoint_slice_event(
            Err(watcher::Error::NoResourceVersion),
            collector.pod_info_arc.clone(),
        );

        // Verify that the function returns early and doesn't modify the maps
        {
            let pod_info = collector.pod_info_arc.lock().unwrap();
            assert!(pod_info.is_empty());
        }
    }