|
| 1 | +/* |
| 2 | +Copyright 2024 The InftyAI Team. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package modelSource |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + |
| 25 | + "github.com/inftyai/llmaz/pkg" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_ModelHubProvider_InjectModelLoader(t *testing.T) { |
| 29 | + fileName := "weights.gguf" |
| 30 | + revision := "v1.2" |
| 31 | + allowPatterns := []string{"*.gguf", "*.json"} |
| 32 | + ignorePatterns := []string{"*.tmp"} |
| 33 | + |
| 34 | + testCases := []struct { |
| 35 | + name string |
| 36 | + provider *ModelHubProvider |
| 37 | + index int |
| 38 | + expectMainModel bool |
| 39 | + expectEnvContains []string |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "inject full modelhub with fileName, revision, allow/ignore", |
| 43 | + provider: &ModelHubProvider{ |
| 44 | + modelName: "llama3", |
| 45 | + modelID: "meta/llama-3", |
| 46 | + modelHub: "Huggingface", |
| 47 | + fileName: &fileName, |
| 48 | + modelRevision: &revision, |
| 49 | + modelAllowPatterns: allowPatterns, |
| 50 | + modelIgnorePatterns: ignorePatterns, |
| 51 | + }, |
| 52 | + index: 0, |
| 53 | + expectMainModel: true, |
| 54 | + expectEnvContains: []string{ |
| 55 | + "MODEL_SOURCE_TYPE", "MODEL_ID", "MODEL_HUB_NAME", "MODEL_FILENAME", |
| 56 | + "REVISION", "MODEL_ALLOW_PATTERNS", "MODEL_IGNORE_PATTERNS", |
| 57 | + "HUGGING_FACE_HUB_TOKEN", "HF_TOKEN", |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "inject with index > 0 skips volume/container mount", |
| 62 | + provider: &ModelHubProvider{ |
| 63 | + modelName: "sub-model", |
| 64 | + modelID: "some/model", |
| 65 | + modelHub: "Huggingface", |
| 66 | + }, |
| 67 | + index: 1, |
| 68 | + expectMainModel: false, |
| 69 | + expectEnvContains: []string{ |
| 70 | + "MODEL_SOURCE_TYPE", "MODEL_ID", "MODEL_HUB_NAME", |
| 71 | + "HUGGING_FACE_HUB_TOKEN", "HF_TOKEN", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tc := range testCases { |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + template := &corev1.PodTemplateSpec{ |
| 79 | + Spec: corev1.PodSpec{ |
| 80 | + Containers: []corev1.Container{ |
| 81 | + { |
| 82 | + Name: MODEL_RUNNER_CONTAINER_NAME, |
| 83 | + Env: []corev1.EnvVar{ |
| 84 | + {Name: "HTTP_PROXY", Value: "http://1.1.1.1"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + tc.provider.InjectModelLoader(template, tc.index) |
| 92 | + |
| 93 | + assert.Len(t, template.Spec.InitContainers, 1) |
| 94 | + initContainer := template.Spec.InitContainers[0] |
| 95 | + expectedName := MODEL_LOADER_CONTAINER_NAME |
| 96 | + if tc.index != 0 { |
| 97 | + expectedName += "-" + string(rune('0'+tc.index)) |
| 98 | + } |
| 99 | + assert.Equal(t, expectedName, initContainer.Name) |
| 100 | + assert.Equal(t, pkg.LOADER_IMAGE, initContainer.Image) |
| 101 | + |
| 102 | + // Check env vars exist |
| 103 | + for _, key := range tc.expectEnvContains { |
| 104 | + found := false |
| 105 | + for _, env := range initContainer.Env { |
| 106 | + if env.Name == key { |
| 107 | + found = true |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + assert.True(t, found, "expected env %s not found", key) |
| 112 | + } |
| 113 | + |
| 114 | + // Main model should inject volume & container mount |
| 115 | + if tc.expectMainModel { |
| 116 | + // Volume should be present |
| 117 | + foundVol := false |
| 118 | + for _, v := range template.Spec.Volumes { |
| 119 | + if v.Name == MODEL_VOLUME_NAME { |
| 120 | + foundVol = true |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + assert.True(t, foundVol, "volume not injected") |
| 125 | + |
| 126 | + // Runner container mount should exist |
| 127 | + foundMount := false |
| 128 | + for _, m := range template.Spec.Containers[0].VolumeMounts { |
| 129 | + if m.Name == MODEL_VOLUME_NAME && m.ReadOnly && m.MountPath == CONTAINER_MODEL_PATH { |
| 130 | + foundMount = true |
| 131 | + } |
| 132 | + } |
| 133 | + assert.True(t, foundMount, "volume mount not injected to runner") |
| 134 | + } else { |
| 135 | + // No volumes or mounts should be injected |
| 136 | + assert.Empty(t, template.Spec.Volumes) |
| 137 | + assert.Empty(t, template.Spec.Containers[0].VolumeMounts) |
| 138 | + } |
| 139 | + |
| 140 | + // Should always carry over container env |
| 141 | + assert.Contains(t, initContainer.Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: "http://1.1.1.1"}) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments