config_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package envconfig
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/google/go-cmp/cmp"
  7. )
  8. func TestHost(t *testing.T) {
  9. cases := map[string]struct {
  10. value string
  11. expect string
  12. }{
  13. "empty": {"", "http://127.0.0.1:11434"},
  14. "only address": {"1.2.3.4", "http://1.2.3.4:11434"},
  15. "only port": {":1234", "http://:1234"},
  16. "address and port": {"1.2.3.4:1234", "http://1.2.3.4:1234"},
  17. "hostname": {"example.com", "http://example.com:11434"},
  18. "hostname and port": {"example.com:1234", "http://example.com:1234"},
  19. "zero port": {":0", "http://:0"},
  20. "too large port": {":66000", "http://:11434"},
  21. "too small port": {":-1", "http://:11434"},
  22. "ipv6 localhost": {"[::1]", "http://[::1]:11434"},
  23. "ipv6 world open": {"[::]", "http://[::]:11434"},
  24. "ipv6 no brackets": {"::1", "http://[::1]:11434"},
  25. "ipv6 + port": {"[::1]:1337", "http://[::1]:1337"},
  26. "extra space": {" 1.2.3.4 ", "http://1.2.3.4:11434"},
  27. "extra quotes": {"\"1.2.3.4\"", "http://1.2.3.4:11434"},
  28. "extra space+quotes": {" \" 1.2.3.4 \" ", "http://1.2.3.4:11434"},
  29. "extra single quotes": {"'1.2.3.4'", "http://1.2.3.4:11434"},
  30. "http": {"http://1.2.3.4", "http://1.2.3.4:80"},
  31. "http port": {"http://1.2.3.4:4321", "http://1.2.3.4:4321"},
  32. "https": {"https://1.2.3.4", "https://1.2.3.4:443"},
  33. "https port": {"https://1.2.3.4:4321", "https://1.2.3.4:4321"},
  34. "proxy path": {"https://example.com/ollama", "https://example.com:443/ollama"},
  35. }
  36. for name, tt := range cases {
  37. t.Run(name, func(t *testing.T) {
  38. t.Setenv("OLLAMA_HOST", tt.value)
  39. if host := Host(); host.String() != tt.expect {
  40. t.Errorf("%s: expected %s, got %s", name, tt.expect, host.String())
  41. }
  42. })
  43. }
  44. }
  45. func TestOrigins(t *testing.T) {
  46. cases := []struct {
  47. value string
  48. expect []string
  49. }{
  50. {"", []string{
  51. "http://localhost",
  52. "https://localhost",
  53. "http://localhost:*",
  54. "https://localhost:*",
  55. "http://127.0.0.1",
  56. "https://127.0.0.1",
  57. "http://127.0.0.1:*",
  58. "https://127.0.0.1:*",
  59. "http://0.0.0.0",
  60. "https://0.0.0.0",
  61. "http://0.0.0.0:*",
  62. "https://0.0.0.0:*",
  63. "app://*",
  64. "file://*",
  65. "tauri://*",
  66. "vscode-webview://*",
  67. }},
  68. {"http://10.0.0.1", []string{
  69. "http://10.0.0.1",
  70. "http://localhost",
  71. "https://localhost",
  72. "http://localhost:*",
  73. "https://localhost:*",
  74. "http://127.0.0.1",
  75. "https://127.0.0.1",
  76. "http://127.0.0.1:*",
  77. "https://127.0.0.1:*",
  78. "http://0.0.0.0",
  79. "https://0.0.0.0",
  80. "http://0.0.0.0:*",
  81. "https://0.0.0.0:*",
  82. "app://*",
  83. "file://*",
  84. "tauri://*",
  85. "vscode-webview://*",
  86. }},
  87. {"http://172.16.0.1,https://192.168.0.1", []string{
  88. "http://172.16.0.1",
  89. "https://192.168.0.1",
  90. "http://localhost",
  91. "https://localhost",
  92. "http://localhost:*",
  93. "https://localhost:*",
  94. "http://127.0.0.1",
  95. "https://127.0.0.1",
  96. "http://127.0.0.1:*",
  97. "https://127.0.0.1:*",
  98. "http://0.0.0.0",
  99. "https://0.0.0.0",
  100. "http://0.0.0.0:*",
  101. "https://0.0.0.0:*",
  102. "app://*",
  103. "file://*",
  104. "tauri://*",
  105. "vscode-webview://*",
  106. }},
  107. {"http://totally.safe,http://definitely.legit", []string{
  108. "http://totally.safe",
  109. "http://definitely.legit",
  110. "http://localhost",
  111. "https://localhost",
  112. "http://localhost:*",
  113. "https://localhost:*",
  114. "http://127.0.0.1",
  115. "https://127.0.0.1",
  116. "http://127.0.0.1:*",
  117. "https://127.0.0.1:*",
  118. "http://0.0.0.0",
  119. "https://0.0.0.0",
  120. "http://0.0.0.0:*",
  121. "https://0.0.0.0:*",
  122. "app://*",
  123. "file://*",
  124. "tauri://*",
  125. "vscode-webview://*",
  126. }},
  127. }
  128. for _, tt := range cases {
  129. t.Run(tt.value, func(t *testing.T) {
  130. t.Setenv("OLLAMA_ORIGINS", tt.value)
  131. if diff := cmp.Diff(Origins(), tt.expect); diff != "" {
  132. t.Errorf("%s: mismatch (-want +got):\n%s", tt.value, diff)
  133. }
  134. })
  135. }
  136. }
  137. func TestBool(t *testing.T) {
  138. cases := map[string]bool{
  139. "": false,
  140. "true": true,
  141. "false": false,
  142. "1": true,
  143. "0": false,
  144. // invalid values
  145. "random": true,
  146. "something": true,
  147. }
  148. for k, v := range cases {
  149. t.Run(k, func(t *testing.T) {
  150. t.Setenv("OLLAMA_BOOL", k)
  151. if b := Bool("OLLAMA_BOOL")(); b != v {
  152. t.Errorf("%s: expected %t, got %t", k, v, b)
  153. }
  154. })
  155. }
  156. }
  157. func TestUint(t *testing.T) {
  158. cases := map[string]uint{
  159. "0": 0,
  160. "1": 1,
  161. "1337": 1337,
  162. // default values
  163. "": 11434,
  164. "-1": 11434,
  165. "0o10": 11434,
  166. "0x10": 11434,
  167. "string": 11434,
  168. }
  169. for k, v := range cases {
  170. t.Run(k, func(t *testing.T) {
  171. t.Setenv("OLLAMA_UINT", k)
  172. if i := Uint("OLLAMA_UINT", 11434)(); i != v {
  173. t.Errorf("%s: expected %d, got %d", k, v, i)
  174. }
  175. })
  176. }
  177. }
  178. func TestKeepAlive(t *testing.T) {
  179. cases := map[string]time.Duration{
  180. "": 5 * time.Minute,
  181. "1s": time.Second,
  182. "1m": time.Minute,
  183. "1h": time.Hour,
  184. "5m0s": 5 * time.Minute,
  185. "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second,
  186. "0": time.Duration(0),
  187. "60": 60 * time.Second,
  188. "120": 2 * time.Minute,
  189. "3600": time.Hour,
  190. "-0": time.Duration(0),
  191. "-1": time.Duration(math.MaxInt64),
  192. "-1m": time.Duration(math.MaxInt64),
  193. // invalid values
  194. " ": 5 * time.Minute,
  195. "???": 5 * time.Minute,
  196. "1d": 5 * time.Minute,
  197. "1y": 5 * time.Minute,
  198. "1w": 5 * time.Minute,
  199. }
  200. for tt, expect := range cases {
  201. t.Run(tt, func(t *testing.T) {
  202. t.Setenv("OLLAMA_KEEP_ALIVE", tt)
  203. if actual := KeepAlive(); actual != expect {
  204. t.Errorf("%s: expected %s, got %s", tt, expect, actual)
  205. }
  206. })
  207. }
  208. }
  209. func TestLoadTimeout(t *testing.T) {
  210. defaultTimeout := 5 * time.Minute
  211. cases := map[string]time.Duration{
  212. "": defaultTimeout,
  213. "1s": time.Second,
  214. "1m": time.Minute,
  215. "1h": time.Hour,
  216. "5m0s": defaultTimeout,
  217. "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second,
  218. "0": time.Duration(math.MaxInt64),
  219. "60": 60 * time.Second,
  220. "120": 2 * time.Minute,
  221. "3600": time.Hour,
  222. "-0": time.Duration(math.MaxInt64),
  223. "-1": time.Duration(math.MaxInt64),
  224. "-1m": time.Duration(math.MaxInt64),
  225. // invalid values
  226. " ": defaultTimeout,
  227. "???": defaultTimeout,
  228. "1d": defaultTimeout,
  229. "1y": defaultTimeout,
  230. "1w": defaultTimeout,
  231. }
  232. for tt, expect := range cases {
  233. t.Run(tt, func(t *testing.T) {
  234. t.Setenv("OLLAMA_LOAD_TIMEOUT", tt)
  235. if actual := LoadTimeout(); actual != expect {
  236. t.Errorf("%s: expected %s, got %s", tt, expect, actual)
  237. }
  238. })
  239. }
  240. }
  241. func TestVar(t *testing.T) {
  242. cases := map[string]string{
  243. "value": "value",
  244. " value ": "value",
  245. " 'value' ": "value",
  246. ` "value" `: "value",
  247. " ' value ' ": " value ",
  248. ` " value " `: " value ",
  249. }
  250. for k, v := range cases {
  251. t.Run(k, func(t *testing.T) {
  252. t.Setenv("OLLAMA_VAR", k)
  253. if s := Var("OLLAMA_VAR"); s != v {
  254. t.Errorf("%s: expected %q, got %q", k, v, s)
  255. }
  256. })
  257. }
  258. }