namespace.rs 7.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use dynamo_llm::{
    discovery::ModelEntry,
    model_type::ModelType,
    namespace::{GLOBAL_NAMESPACE, is_global_namespace},
};
use dynamo_runtime::protocols::EndpointId;

#[test]
fn test_is_global_namespace_with_global_string() {
    assert!(is_global_namespace(GLOBAL_NAMESPACE));
    assert!(is_global_namespace("dynamo"));
}

#[test]
fn test_is_global_namespace_with_empty_string() {
    assert!(is_global_namespace(""));
}

#[test]
fn test_is_global_namespace_with_specific_namespace() {
    assert!(!is_global_namespace("test-namespace"));
    assert!(!is_global_namespace("my-custom-namespace"));
}

#[test]
fn test_is_global_namespace_with_whitespace() {
    // Whitespace should not be considered global
    assert!(!is_global_namespace(" "));
    assert!(!is_global_namespace("  "));
    assert!(!is_global_namespace("\t"));
    assert!(!is_global_namespace("\n"));
}

#[test]
fn test_is_global_namespace_case_sensitivity() {
    // Should be case sensitive
    assert!(!is_global_namespace("Dynamo"));
    assert!(!is_global_namespace("DYNAMO"));
}

#[test]
fn test_global_namespace_constant() {
    assert_eq!(GLOBAL_NAMESPACE, "dynamo");
}

// Helper function to create a test ModelEntry
fn create_test_model_entry(
    name: &str,
    namespace: &str,
    component: &str,
    endpoint_name: &str,
    model_type: ModelType,
) -> ModelEntry {
    ModelEntry {
        name: name.to_string(),
        endpoint_id: EndpointId {
            namespace: namespace.to_string(),
            component: component.to_string(),
            name: endpoint_name.to_string(),
        },
        model_type,
        runtime_config: None,
    }
}

#[test]
fn test_model_entry_creation_with_different_namespaces() {
    // Test creating ModelEntry with specific namespace
    let model_vllm = create_test_model_entry(
        "test-model-1",
        "vllm-agg",
        "backend",
        "generate",
        ModelType::Chat,
    );

    assert_eq!(model_vllm.name, "test-model-1");
    assert_eq!(model_vllm.endpoint_id.namespace, "vllm-agg");
    assert_eq!(model_vllm.endpoint_id.component, "backend");
    assert_eq!(model_vllm.endpoint_id.name, "generate");
    assert_eq!(model_vllm.model_type, ModelType::Chat);

    // Test creating ModelEntry with global namespace
    let model_global = create_test_model_entry(
        "test-model-2",
        "dynamo",
        "frontend",
        "http",
        ModelType::Completion,
    );

    assert_eq!(model_global.name, "test-model-2");
    assert_eq!(model_global.endpoint_id.namespace, "dynamo");
    assert_eq!(model_global.endpoint_id.component, "frontend");
    assert_eq!(model_global.endpoint_id.name, "http");
    assert_eq!(model_global.model_type, ModelType::Completion);
}

#[test]
fn test_namespace_filtering_logic() {
    // Test the core logic that would be used in namespace filtering
    let models = vec![
        create_test_model_entry(
            "model-1",
            "vllm-agg",
            "backend",
            "generate",
            ModelType::Chat,
        ),
        create_test_model_entry(
            "model-2",
            "sglang-prod",
            "backend",
            "generate",
            ModelType::Chat,
        ),
        create_test_model_entry("model-3", "dynamo", "backend", "generate", ModelType::Chat),
        create_test_model_entry("model-4", "", "backend", "generate", ModelType::Chat),
    ];

    // Test filtering for specific namespace "vllm-agg"
    let target_namespace = "vllm-agg";
    let global_namespace = is_global_namespace(target_namespace);
    let filtered_vllm: Vec<&ModelEntry> = models
        .iter()
        .filter(|model| global_namespace || model.endpoint_id.namespace == target_namespace)
        .collect();

    assert_eq!(filtered_vllm.len(), 1);
    assert_eq!(filtered_vllm[0].name, "model-1");
    assert_eq!(filtered_vllm[0].endpoint_id.namespace, "vllm-agg");

    // Test filtering for global namespace (should include all)
    let target_namespace = "dynamo";
    let global_namespace = is_global_namespace(target_namespace);
    let filtered_global: Vec<&ModelEntry> = models
        .iter()
        .filter(|model| global_namespace || model.endpoint_id.namespace == target_namespace)
        .collect();

    assert_eq!(filtered_global.len(), 4); // All models should be included

    // Test filtering for empty namespace (should include all, treated as global)
    let target_namespace = "";
    let global_namespace = is_global_namespace(target_namespace);
    let filtered_empty: Vec<&ModelEntry> = models
        .iter()
        .filter(|model| global_namespace || model.endpoint_id.namespace == target_namespace)
        .collect();

    assert_eq!(filtered_empty.len(), 4); // All models should be included

    // Test filtering for non-existent namespace
    let target_namespace = "non-existent";
    let global_namespace = is_global_namespace(target_namespace);
    let filtered_none: Vec<&ModelEntry> = models
        .iter()
        .filter(|model| global_namespace || model.endpoint_id.namespace == target_namespace)
        .collect();

    assert_eq!(filtered_none.len(), 0); // No models should match
}

#[test]
fn test_model_entry_serialization() {
    // Test that ModelEntry can be serialized and deserialized (important for etcd storage)
    let model = create_test_model_entry(
        "test-model",
        "vllm-agg",
        "backend",
        "generate",
        ModelType::Chat,
    );

    // Serialize to JSON
    let json = serde_json::to_string(&model).expect("Failed to serialize ModelEntry");
    assert!(json.contains("test-model"));
    assert!(json.contains("vllm-agg"));
    assert!(json.contains("backend"));
    assert!(json.contains("generate"));

    // Deserialize from JSON
    let deserialized: ModelEntry =
        serde_json::from_str(&json).expect("Failed to deserialize ModelEntry");
    assert_eq!(deserialized.name, model.name);
    assert_eq!(
        deserialized.endpoint_id.namespace,
        model.endpoint_id.namespace
    );
    assert_eq!(
        deserialized.endpoint_id.component,
        model.endpoint_id.component
    );
    assert_eq!(deserialized.endpoint_id.name, model.endpoint_id.name);
    assert_eq!(deserialized.model_type, model.model_type);
}

#[test]
fn test_endpoint_namespace_parsing() {
    // Test Endpoint creation from string with namespace
    let endpoint1 = EndpointId::from("vllm-agg.backend.generate");
    assert_eq!(endpoint1.namespace, "vllm-agg");
    assert_eq!(endpoint1.component, "backend");
    assert_eq!(endpoint1.name, "generate");

    let endpoint2 = EndpointId::from("global.frontend.http");
    assert_eq!(endpoint2.namespace, "global");
    assert_eq!(endpoint2.component, "frontend");
    assert_eq!(endpoint2.name, "http");

    // Test with forward slash separator
    let endpoint3 = EndpointId::from("sglang-prod/backend/generate");
    assert_eq!(endpoint3.namespace, "sglang-prod");
    assert_eq!(endpoint3.component, "backend");
    assert_eq!(endpoint3.name, "generate");
}