Skip to content

Commit 3ca265e

Browse files
authored
fix: add ut for backend runtime. (#428)
* fix: add ut for backend runtime. * fix: fix test case structure in backend parser. Signed-off-by: X1aoZEOuO <[email protected]> --------- Signed-off-by: X1aoZEOuO <[email protected]> Co-authored-by: X1aoZEOuO <[email protected]>
1 parent 24913b1 commit 3ca265e

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

pkg/controller_helper/backendruntime/backendruntime_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ import (
2020
"testing"
2121

2222
"github.com/google/go-cmp/cmp"
23+
coreapi "github.com/inftyai/llmaz/api/core/v1alpha1"
24+
"github.com/inftyai/llmaz/test/util/wrapper"
25+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/api/resource"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
29+
inferenceapi "github.com/inftyai/llmaz/api/inference/v1alpha1"
2330
)
2431

2532
func TestRenderFlags(t *testing.T) {
@@ -89,3 +96,154 @@ func TestRenderFlags(t *testing.T) {
8996
})
9097
}
9198
}
99+
100+
func TestBackendRuntimeParser_BasicFields(t *testing.T) {
101+
type want struct {
102+
cmd []string
103+
args []string
104+
envs []corev1.EnvVar
105+
lifecycle *corev1.Lifecycle
106+
image string
107+
version string
108+
resources *inferenceapi.ResourceRequirements
109+
shm *resource.Quantity
110+
}
111+
112+
testCases := []struct {
113+
name string
114+
parser *BackendRuntimeParser
115+
want want
116+
}{
117+
{
118+
name: "normal case has recommendConfig",
119+
parser: func() *BackendRuntimeParser {
120+
cmd := []string{"python", "serve.py"}
121+
args := []string{"--port=8080"}
122+
envs := []corev1.EnvVar{{Name: "MODE", Value: "release"}}
123+
lc := &corev1.Lifecycle{
124+
PostStart: &corev1.LifecycleHandler{
125+
Exec: &corev1.ExecAction{Command: []string{"echo", "started"}},
126+
},
127+
}
128+
shm := resource.NewQuantity(1*1024*1024*1024, resource.BinarySI)
129+
res := &inferenceapi.ResourceRequirements{}
130+
131+
backend := &inferenceapi.BackendRuntime{
132+
ObjectMeta: metav1.ObjectMeta{Name: "default"},
133+
Spec: inferenceapi.BackendRuntimeSpec{
134+
Command: cmd,
135+
Envs: envs,
136+
Lifecycle: lc,
137+
Image: "inftyai/llama",
138+
Version: "v0.1.0",
139+
RecommendedConfigs: []inferenceapi.RecommendedConfig{
140+
{
141+
Name: "default",
142+
Args: args,
143+
Resources: res,
144+
SharedMemorySize: shm,
145+
},
146+
},
147+
},
148+
}
149+
150+
models := []*coreapi.OpenModel{}
151+
qwenModel := wrapper.MakeModel("qwen2-0--5b").FamilyName("qwen2").ModelSourceWithURI("ollama://qwen2:0.5b").Obj()
152+
models = append(models, qwenModel)
153+
154+
playground := wrapper.MakePlayground("qwen2-0--5b", corev1.NamespaceDefault).ModelClaim("qwen2-0--5b").BackendRuntime("llmaz-ollama").BackendRuntimeEnv("OLLAMA_HOST", "0.0.0.0:8080").Replicas(1).Obj()
155+
156+
return NewBackendRuntimeParser(backend, models, playground)
157+
}(),
158+
want: want{
159+
cmd: []string{"python", "serve.py"},
160+
args: []string{"--port=8080"},
161+
envs: []corev1.EnvVar{{Name: "MODE", Value: "release"}},
162+
lifecycle: &corev1.Lifecycle{
163+
PostStart: &corev1.LifecycleHandler{
164+
Exec: &corev1.ExecAction{Command: []string{"echo", "started"}},
165+
},
166+
},
167+
image: "inftyai/llama:v0.1.0",
168+
version: "v0.1.0",
169+
resources: &inferenceapi.ResourceRequirements{},
170+
shm: resource.NewQuantity(1*1024*1024*1024, resource.BinarySI),
171+
},
172+
},
173+
{
174+
name: "recommendConfigName not found and resources and SharedMemorySize return nil",
175+
parser: func() *BackendRuntimeParser {
176+
backend := &inferenceapi.BackendRuntime{
177+
ObjectMeta: metav1.ObjectMeta{Name: "default"},
178+
Spec: inferenceapi.BackendRuntimeSpec{
179+
Command: []string{"some"},
180+
Image: "repo/img",
181+
Version: "latest",
182+
RecommendedConfigs: []inferenceapi.RecommendedConfig{
183+
{
184+
Name: "default",
185+
},
186+
},
187+
},
188+
}
189+
190+
models := []*coreapi.OpenModel{}
191+
qwenModel := wrapper.MakeModel("qwen2-0--5b").FamilyName("qwen2").ModelSourceWithURI("ollama://qwen2:0.5b").Obj()
192+
models = append(models, qwenModel)
193+
194+
playground := wrapper.MakePlayground("qwen2-0--5b", corev1.NamespaceDefault).ModelClaim("qwen2-0--5b").BackendRuntime("llmaz-ollama").BackendRuntimeEnv("OLLAMA_HOST", "0.0.0.0:8080").Replicas(1).Obj()
195+
196+
return NewBackendRuntimeParser(backend, models, playground)
197+
}(),
198+
want: want{
199+
cmd: []string{"some"},
200+
image: "repo/img:latest",
201+
version: "latest",
202+
args: []string{},
203+
resources: nil,
204+
shm: nil,
205+
},
206+
},
207+
}
208+
209+
for _, tc := range testCases {
210+
tc := tc // capture range variable
211+
t.Run(tc.name, func(t *testing.T) {
212+
p := tc.parser
213+
if diff := cmp.Diff(tc.want.cmd, p.Command()); diff != "" {
214+
t.Fatalf("Command() mismatch (-want +got):\n%s", diff)
215+
}
216+
args, err := p.Args()
217+
if err != nil {
218+
t.Fatal(err)
219+
}
220+
if diff := cmp.Diff(tc.want.args, args); diff != "" {
221+
t.Fatalf("Args() mismatch (-want +got):\n%s", diff)
222+
}
223+
224+
if diff := cmp.Diff(tc.want.envs, p.Envs()); diff != "" {
225+
t.Fatalf("Envs() mismatch (-want +got):\n%s", diff)
226+
}
227+
228+
if diff := cmp.Diff(tc.want.lifecycle, p.Lifecycle()); diff != "" {
229+
t.Fatalf("Lifecycle() mismatch (-want +got):\n%s", diff)
230+
}
231+
232+
if got := p.Image(p.Version()); got != tc.want.image {
233+
t.Fatalf("Image() = %s, want %s", got, tc.want.image)
234+
}
235+
236+
if got := p.Version(); got != tc.want.version {
237+
t.Fatalf("Version() = %s, want %s", got, tc.want.version)
238+
}
239+
240+
if diff := cmp.Diff(tc.want.resources, p.Resources()); diff != "" {
241+
t.Fatalf("Resources() mismatch (-want +got):\n%s", diff)
242+
}
243+
244+
if diff := cmp.Diff(tc.want.shm, p.SharedMemorySize()); diff != "" {
245+
t.Fatalf("SharedMemorySize() mismatch (-want +got):\n%s", diff)
246+
}
247+
})
248+
}
249+
}

0 commit comments

Comments
 (0)