|
| 1 | +package bundle |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/docker/model-runner/pkg/distribution/types" |
| 11 | +) |
| 12 | + |
| 13 | +func TestParse_NoModelWeights(t *testing.T) { |
| 14 | + // Create a temporary directory for the test bundle |
| 15 | + tempDir := t.TempDir() |
| 16 | + |
| 17 | + // Create model subdirectory |
| 18 | + modelDir := filepath.Join(tempDir, ModelSubdir) |
| 19 | + if err := os.MkdirAll(modelDir, 0755); err != nil { |
| 20 | + t.Fatalf("Failed to create model directory: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + // Create a valid config.json at bundle root |
| 24 | + cfg := types.Config{ |
| 25 | + Format: types.FormatGGUF, |
| 26 | + } |
| 27 | + configPath := filepath.Join(tempDir, "config.json") |
| 28 | + f, err := os.Create(configPath) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to create config.json: %v", err) |
| 31 | + } |
| 32 | + if err := json.NewEncoder(f).Encode(cfg); err != nil { |
| 33 | + f.Close() |
| 34 | + t.Fatalf("Failed to encode config: %v", err) |
| 35 | + } |
| 36 | + f.Close() |
| 37 | + |
| 38 | + // Try to parse the bundle - should fail because no model weights are present |
| 39 | + _, err = Parse(tempDir) |
| 40 | + if err == nil { |
| 41 | + t.Fatal("Expected error when parsing bundle without model weights, got nil") |
| 42 | + } |
| 43 | + |
| 44 | + expectedErrMsg := "no supported model weights found (neither GGUF nor safetensors)" |
| 45 | + if !strings.Contains(err.Error(), expectedErrMsg) { |
| 46 | + t.Errorf("Expected error message to contain %q, got: %v", expectedErrMsg, err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestParse_WithGGUF(t *testing.T) { |
| 51 | + // Create a temporary directory for the test bundle |
| 52 | + tempDir := t.TempDir() |
| 53 | + |
| 54 | + // Create model subdirectory |
| 55 | + modelDir := filepath.Join(tempDir, ModelSubdir) |
| 56 | + if err := os.MkdirAll(modelDir, 0755); err != nil { |
| 57 | + t.Fatalf("Failed to create model directory: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Create a dummy GGUF file |
| 61 | + ggufPath := filepath.Join(modelDir, "model.gguf") |
| 62 | + if err := os.WriteFile(ggufPath, []byte("dummy gguf content"), 0644); err != nil { |
| 63 | + t.Fatalf("Failed to create GGUF file: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + // Create a valid config.json at bundle root |
| 67 | + cfg := types.Config{ |
| 68 | + Format: types.FormatGGUF, |
| 69 | + } |
| 70 | + configPath := filepath.Join(tempDir, "config.json") |
| 71 | + f, err := os.Create(configPath) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Failed to create config.json: %v", err) |
| 74 | + } |
| 75 | + if err := json.NewEncoder(f).Encode(cfg); err != nil { |
| 76 | + f.Close() |
| 77 | + t.Fatalf("Failed to encode config: %v", err) |
| 78 | + } |
| 79 | + f.Close() |
| 80 | + |
| 81 | + // Parse the bundle - should succeed |
| 82 | + bundle, err := Parse(tempDir) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Expected successful parse with GGUF file, got error: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if bundle.ggufFile != "model.gguf" { |
| 88 | + t.Errorf("Expected ggufFile to be 'model.gguf', got: %s", bundle.ggufFile) |
| 89 | + } |
| 90 | + |
| 91 | + if bundle.safetensorsFile != "" { |
| 92 | + t.Errorf("Expected safetensorsFile to be empty, got: %s", bundle.safetensorsFile) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestParse_WithSafetensors(t *testing.T) { |
| 97 | + // Create a temporary directory for the test bundle |
| 98 | + tempDir := t.TempDir() |
| 99 | + |
| 100 | + // Create model subdirectory |
| 101 | + modelDir := filepath.Join(tempDir, ModelSubdir) |
| 102 | + if err := os.MkdirAll(modelDir, 0755); err != nil { |
| 103 | + t.Fatalf("Failed to create model directory: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + // Create a dummy safetensors file |
| 107 | + safetensorsPath := filepath.Join(modelDir, "model.safetensors") |
| 108 | + if err := os.WriteFile(safetensorsPath, []byte("dummy safetensors content"), 0644); err != nil { |
| 109 | + t.Fatalf("Failed to create safetensors file: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Create a valid config.json at bundle root |
| 113 | + cfg := types.Config{ |
| 114 | + Format: types.FormatSafetensors, |
| 115 | + } |
| 116 | + configPath := filepath.Join(tempDir, "config.json") |
| 117 | + f, err := os.Create(configPath) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("Failed to create config.json: %v", err) |
| 120 | + } |
| 121 | + if err := json.NewEncoder(f).Encode(cfg); err != nil { |
| 122 | + f.Close() |
| 123 | + t.Fatalf("Failed to encode config: %v", err) |
| 124 | + } |
| 125 | + f.Close() |
| 126 | + |
| 127 | + // Parse the bundle - should succeed |
| 128 | + bundle, err := Parse(tempDir) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Expected successful parse with safetensors file, got error: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + if bundle.safetensorsFile != "model.safetensors" { |
| 134 | + t.Errorf("Expected safetensorsFile to be 'model.safetensors', got: %s", bundle.safetensorsFile) |
| 135 | + } |
| 136 | + |
| 137 | + if bundle.ggufFile != "" { |
| 138 | + t.Errorf("Expected ggufFile to be empty, got: %s", bundle.ggufFile) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestParse_WithBothFormats(t *testing.T) { |
| 143 | + // Create a temporary directory for the test bundle |
| 144 | + tempDir := t.TempDir() |
| 145 | + |
| 146 | + // Create model subdirectory |
| 147 | + modelDir := filepath.Join(tempDir, ModelSubdir) |
| 148 | + if err := os.MkdirAll(modelDir, 0755); err != nil { |
| 149 | + t.Fatalf("Failed to create model directory: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + // Create both GGUF and safetensors files |
| 153 | + ggufPath := filepath.Join(modelDir, "model.gguf") |
| 154 | + if err := os.WriteFile(ggufPath, []byte("dummy gguf content"), 0644); err != nil { |
| 155 | + t.Fatalf("Failed to create GGUF file: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + safetensorsPath := filepath.Join(modelDir, "model.safetensors") |
| 159 | + if err := os.WriteFile(safetensorsPath, []byte("dummy safetensors content"), 0644); err != nil { |
| 160 | + t.Fatalf("Failed to create safetensors file: %v", err) |
| 161 | + } |
| 162 | + |
| 163 | + // Create a valid config.json at bundle root |
| 164 | + cfg := types.Config{ |
| 165 | + Format: types.FormatGGUF, |
| 166 | + } |
| 167 | + configPath := filepath.Join(tempDir, "config.json") |
| 168 | + f, err := os.Create(configPath) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("Failed to create config.json: %v", err) |
| 171 | + } |
| 172 | + if err := json.NewEncoder(f).Encode(cfg); err != nil { |
| 173 | + f.Close() |
| 174 | + t.Fatalf("Failed to encode config: %v", err) |
| 175 | + } |
| 176 | + f.Close() |
| 177 | + |
| 178 | + // Parse the bundle - should succeed with both files present |
| 179 | + bundle, err := Parse(tempDir) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Expected successful parse with both formats, got error: %v", err) |
| 182 | + } |
| 183 | + |
| 184 | + if bundle.ggufFile != "model.gguf" { |
| 185 | + t.Errorf("Expected ggufFile to be 'model.gguf', got: %s", bundle.ggufFile) |
| 186 | + } |
| 187 | + |
| 188 | + if bundle.safetensorsFile != "model.safetensors" { |
| 189 | + t.Errorf("Expected safetensorsFile to be 'model.safetensors', got: %s", bundle.safetensorsFile) |
| 190 | + } |
| 191 | +} |
0 commit comments