|
| 1 | +/* |
| 2 | +Copyright 2025 The InftyAI Team. |
| 3 | +
|
| 4 | +Licensed under the Apache License, |
| 5 | + Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package modelSource |
| 19 | + |
| 20 | +import ( |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + "k8s.io/utils/ptr" |
| 30 | + |
| 31 | + "github.com/inftyai/llmaz/pkg" |
| 32 | +) |
| 33 | + |
| 34 | +func Test_ModelHubProvider_InjectModelLoader(t *testing.T) { |
| 35 | + fileName := "weights.gguf" |
| 36 | + revision := "v1.2" |
| 37 | + allowPatterns := []string{"*.gguf", "*.json"} |
| 38 | + ignorePatterns := []string{"*.tmp"} |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + provider *ModelHubProvider |
| 43 | + index int |
| 44 | + expectMainModel bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "inject full modelhub with fileName, revision, allow/ignore", |
| 48 | + provider: &ModelHubProvider{ |
| 49 | + modelName: "llama3", |
| 50 | + modelID: "meta/llama-3", |
| 51 | + modelHub: "Huggingface", |
| 52 | + fileName: &fileName, |
| 53 | + modelRevision: &revision, |
| 54 | + modelAllowPatterns: allowPatterns, |
| 55 | + modelIgnorePatterns: ignorePatterns, |
| 56 | + }, |
| 57 | + index: 0, |
| 58 | + expectMainModel: true, |
| 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 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + envSortOpt := cmpopts.SortSlices(func(a, b corev1.EnvVar) bool { |
| 73 | + return a.Name < b.Name |
| 74 | + }) |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.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 | + tt.provider.InjectModelLoader(template, tt.index) |
| 92 | + |
| 93 | + assert.Len(t, template.Spec.InitContainers, 1) |
| 94 | + initContainer := template.Spec.InitContainers[0] |
| 95 | + |
| 96 | + expectedName := MODEL_LOADER_CONTAINER_NAME |
| 97 | + if tt.index != 0 { |
| 98 | + expectedName += "-" + strconv.Itoa(tt.index) |
| 99 | + } |
| 100 | + assert.Equal(t, expectedName, initContainer.Name) |
| 101 | + assert.Equal(t, pkg.LOADER_IMAGE, initContainer.Image) |
| 102 | + |
| 103 | + wantEnv := buildExpectedEnv(tt.provider) |
| 104 | + if diff := cmp.Diff(wantEnv, initContainer.Env, envSortOpt); diff != "" { |
| 105 | + t.Errorf("InitContainer.Env mismatch (-want +got):\n%s", diff) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func buildExpectedEnv(p *ModelHubProvider) []corev1.EnvVar { |
| 112 | + envs := make([]corev1.EnvVar, 0, 10) |
| 113 | + |
| 114 | + envs = append(envs, corev1.EnvVar{Name: "HTTP_PROXY", Value: "http://1.1.1.1"}) |
| 115 | + |
| 116 | + envs = append(envs, |
| 117 | + corev1.EnvVar{Name: "MODEL_SOURCE_TYPE", Value: MODEL_SOURCE_MODELHUB}, |
| 118 | + corev1.EnvVar{Name: "MODEL_ID", Value: p.modelID}, |
| 119 | + corev1.EnvVar{Name: "MODEL_HUB_NAME", Value: p.modelHub}, |
| 120 | + ) |
| 121 | + |
| 122 | + if p.fileName != nil { |
| 123 | + envs = append(envs, corev1.EnvVar{Name: "MODEL_FILENAME", Value: *p.fileName}) |
| 124 | + } |
| 125 | + if p.modelRevision != nil { |
| 126 | + envs = append(envs, corev1.EnvVar{Name: "REVISION", Value: *p.modelRevision}) |
| 127 | + } |
| 128 | + if p.modelAllowPatterns != nil { |
| 129 | + envs = append(envs, corev1.EnvVar{ |
| 130 | + Name: "MODEL_ALLOW_PATTERNS", |
| 131 | + Value: strings.Join(p.modelAllowPatterns, ","), |
| 132 | + }) |
| 133 | + } |
| 134 | + if p.modelIgnorePatterns != nil { |
| 135 | + envs = append(envs, corev1.EnvVar{ |
| 136 | + Name: "MODEL_IGNORE_PATTERNS", |
| 137 | + Value: strings.Join(p.modelIgnorePatterns, ","), |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + for _, tokenName := range []string{"HUGGING_FACE_HUB_TOKEN", "HF_TOKEN"} { |
| 142 | + envs = append(envs, corev1.EnvVar{ |
| 143 | + Name: tokenName, |
| 144 | + ValueFrom: &corev1.EnvVarSource{ |
| 145 | + SecretKeyRef: &corev1.SecretKeySelector{ |
| 146 | + LocalObjectReference: corev1.LocalObjectReference{Name: MODELHUB_SECRET_NAME}, |
| 147 | + Key: HUGGING_FACE_TOKEN_KEY, |
| 148 | + Optional: ptr.To(true), |
| 149 | + }, |
| 150 | + }, |
| 151 | + }) |
| 152 | + } |
| 153 | + |
| 154 | + return envs |
| 155 | +} |
0 commit comments