| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | package server | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 02:46:12 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetBlobsPath(t *testing.T) { | 
					
						
							|  |  |  | 	// GetBlobsPath expects an actual directory to exist
 | 
					
						
							|  |  |  | 	dir, err := os.MkdirTemp("", "ollama-test") | 
					
						
							|  |  |  | 	assert.Nil(t, err) | 
					
						
							|  |  |  | 	defer os.RemoveAll(dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name     string | 
					
						
							|  |  |  | 		digest   string | 
					
						
							|  |  |  | 		expected string | 
					
						
							|  |  |  | 		err      error | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"empty digest", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			filepath.Join(dir, "blobs"), | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"valid with colon", | 
					
						
							|  |  |  | 			"sha256:456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9", | 
					
						
							|  |  |  | 			filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"), | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"valid with dash", | 
					
						
							|  |  |  | 			"sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9", | 
					
						
							|  |  |  | 			filepath.Join(dir, "blobs", "sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9"), | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"digest too short", | 
					
						
							|  |  |  | 			"sha256-45640291", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			ErrInvalidDigestFormat, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"digest too long", | 
					
						
							|  |  |  | 			"sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7aad9aaaaaaaaaa", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			ErrInvalidDigestFormat, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"digest invalid chars", | 
					
						
							|  |  |  | 			"../sha256-456402914e838a953e0cf80caa6adbe75383d9e63584a964f504a7bbb8f7a", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			ErrInvalidDigestFormat, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tc := range tests { | 
					
						
							|  |  |  | 		t.Run(tc.name, func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Setenv("OLLAMA_MODELS", dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			got, err := GetBlobsPath(tc.digest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.ErrorIs(t, tc.err, err, tc.name) | 
					
						
							|  |  |  | 			assert.Equal(t, tc.expected, got, tc.name) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestParseModelPath(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							| 
									
										
										
										
											2023-09-29 01:00:34 +08:00
										 |  |  | 		name string | 
					
						
							|  |  |  | 		arg  string | 
					
						
							|  |  |  | 		want ModelPath | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"full path https", | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"https://example.com/ns/repo:tag", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "https", | 
					
						
							|  |  |  | 				Registry:       "example.com", | 
					
						
							|  |  |  | 				Namespace:      "ns", | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            "tag", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"full path http", | 
					
						
							|  |  |  | 			"http://example.com/ns/repo:tag", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "http", | 
					
						
							|  |  |  | 				Registry:       "example.com", | 
					
						
							|  |  |  | 				Namespace:      "ns", | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            "tag", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"no protocol", | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"example.com/ns/repo:tag", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "https", | 
					
						
							|  |  |  | 				Registry:       "example.com", | 
					
						
							|  |  |  | 				Namespace:      "ns", | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            "tag", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"no registry", | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"ns/repo:tag", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "https", | 
					
						
							|  |  |  | 				Registry:       DefaultRegistry, | 
					
						
							|  |  |  | 				Namespace:      "ns", | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            "tag", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"no namespace", | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"repo:tag", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "https", | 
					
						
							|  |  |  | 				Registry:       DefaultRegistry, | 
					
						
							|  |  |  | 				Namespace:      DefaultNamespace, | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            "tag", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"no tag", | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			"repo", | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 			ModelPath{ | 
					
						
							|  |  |  | 				ProtocolScheme: "https", | 
					
						
							|  |  |  | 				Registry:       DefaultRegistry, | 
					
						
							|  |  |  | 				Namespace:      DefaultNamespace, | 
					
						
							|  |  |  | 				Repository:     "repo", | 
					
						
							|  |  |  | 				Tag:            DefaultTag, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range tests { | 
					
						
							|  |  |  | 		t.Run(tc.name, func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-08-23 00:39:42 +08:00
										 |  |  | 			got := ParseModelPath(tc.arg) | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if got != tc.want { | 
					
						
							|  |  |  | 				t.Errorf("got: %q want: %q", got, tc.want) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |