config_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. }},
  67. {"http://10.0.0.1", []string{
  68. "http://10.0.0.1",
  69. "http://localhost",
  70. "https://localhost",
  71. "http://localhost:*",
  72. "https://localhost:*",
  73. "http://127.0.0.1",
  74. "https://127.0.0.1",
  75. "http://127.0.0.1:*",
  76. "https://127.0.0.1:*",
  77. "http://0.0.0.0",
  78. "https://0.0.0.0",
  79. "http://0.0.0.0:*",
  80. "https://0.0.0.0:*",
  81. "app://*",
  82. "file://*",
  83. "tauri://*",
  84. }},
  85. {"http://172.16.0.1,https://192.168.0.1", []string{
  86. "http://172.16.0.1",
  87. "https://192.168.0.1",
  88. "http://localhost",
  89. "https://localhost",
  90. "http://localhost:*",
  91. "https://localhost:*",
  92. "http://127.0.0.1",
  93. "https://127.0.0.1",
  94. "http://127.0.0.1:*",
  95. "https://127.0.0.1:*",
  96. "http://0.0.0.0",
  97. "https://0.0.0.0",
  98. "http://0.0.0.0:*",
  99. "https://0.0.0.0:*",
  100. "app://*",
  101. "file://*",
  102. "tauri://*",
  103. }},
  104. {"http://totally.safe,http://definitely.legit", []string{
  105. "http://totally.safe",
  106. "http://definitely.legit",
  107. "http://localhost",
  108. "https://localhost",
  109. "http://localhost:*",
  110. "https://localhost:*",
  111. "http://127.0.0.1",
  112. "https://127.0.0.1",
  113. "http://127.0.0.1:*",
  114. "https://127.0.0.1:*",
  115. "http://0.0.0.0",
  116. "https://0.0.0.0",
  117. "http://0.0.0.0:*",
  118. "https://0.0.0.0:*",
  119. "app://*",
  120. "file://*",
  121. "tauri://*",
  122. }},
  123. }
  124. for _, tt := range cases {
  125. t.Run(tt.value, func(t *testing.T) {
  126. t.Setenv("OLLAMA_ORIGINS", tt.value)
  127. if diff := cmp.Diff(Origins(), tt.expect); diff != "" {
  128. t.Errorf("%s: mismatch (-want +got):\n%s", tt.value, diff)
  129. }
  130. })
  131. }
  132. }
  133. func TestBool(t *testing.T) {
  134. cases := map[string]bool{
  135. "": false,
  136. "true": true,
  137. "false": false,
  138. "1": true,
  139. "0": false,
  140. // invalid values
  141. "random": true,
  142. "something": true,
  143. }
  144. for k, v := range cases {
  145. t.Run(k, func(t *testing.T) {
  146. t.Setenv("OLLAMA_BOOL", k)
  147. if b := Bool("OLLAMA_BOOL")(); b != v {
  148. t.Errorf("%s: expected %t, got %t", k, v, b)
  149. }
  150. })
  151. }
  152. }
  153. func TestUint(t *testing.T) {
  154. cases := map[string]uint{
  155. "0": 0,
  156. "1": 1,
  157. "1337": 1337,
  158. // default values
  159. "": 11434,
  160. "-1": 11434,
  161. "0o10": 11434,
  162. "0x10": 11434,
  163. "string": 11434,
  164. }
  165. for k, v := range cases {
  166. t.Run(k, func(t *testing.T) {
  167. t.Setenv("OLLAMA_UINT", k)
  168. if i := Uint("OLLAMA_UINT", uint(11434))(); i != v {
  169. t.Errorf("%s: expected %d, got %d", k, v, i)
  170. }
  171. })
  172. }
  173. }
  174. func TestKeepAlive(t *testing.T) {
  175. cases := map[string]time.Duration{
  176. "": 5 * time.Minute,
  177. "1s": time.Second,
  178. "1m": time.Minute,
  179. "1h": time.Hour,
  180. "5m0s": 5 * time.Minute,
  181. "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second,
  182. "0": time.Duration(0),
  183. "60": 60 * time.Second,
  184. "120": 2 * time.Minute,
  185. "3600": time.Hour,
  186. "-0": time.Duration(0),
  187. "-1": time.Duration(math.MaxInt64),
  188. "-1m": time.Duration(math.MaxInt64),
  189. // invalid values
  190. " ": 5 * time.Minute,
  191. "???": 5 * time.Minute,
  192. "1d": 5 * time.Minute,
  193. "1y": 5 * time.Minute,
  194. "1w": 5 * time.Minute,
  195. }
  196. for tt, expect := range cases {
  197. t.Run(tt, func(t *testing.T) {
  198. t.Setenv("OLLAMA_KEEP_ALIVE", tt)
  199. if actual := KeepAlive(); actual != expect {
  200. t.Errorf("%s: expected %s, got %s", tt, expect, actual)
  201. }
  202. })
  203. }
  204. }
  205. func TestLoadTimeout(t *testing.T) {
  206. defaultTimeout := 5 * time.Minute
  207. cases := map[string]time.Duration{
  208. "": defaultTimeout,
  209. "1s": time.Second,
  210. "1m": time.Minute,
  211. "1h": time.Hour,
  212. "5m0s": defaultTimeout,
  213. "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second,
  214. "0": time.Duration(math.MaxInt64),
  215. "60": 60 * time.Second,
  216. "120": 2 * time.Minute,
  217. "3600": time.Hour,
  218. "-0": time.Duration(math.MaxInt64),
  219. "-1": time.Duration(math.MaxInt64),
  220. "-1m": time.Duration(math.MaxInt64),
  221. // invalid values
  222. " ": defaultTimeout,
  223. "???": defaultTimeout,
  224. "1d": defaultTimeout,
  225. "1y": defaultTimeout,
  226. "1w": defaultTimeout,
  227. }
  228. for tt, expect := range cases {
  229. t.Run(tt, func(t *testing.T) {
  230. t.Setenv("OLLAMA_LOAD_TIMEOUT", tt)
  231. if actual := LoadTimeout(); actual != expect {
  232. t.Errorf("%s: expected %s, got %s", tt, expect, actual)
  233. }
  234. })
  235. }
  236. }
  237. func TestVar(t *testing.T) {
  238. cases := map[string]string{
  239. "value": "value",
  240. " value ": "value",
  241. " 'value' ": "value",
  242. ` "value" `: "value",
  243. " ' value ' ": " value ",
  244. ` " value " `: " value ",
  245. }
  246. for k, v := range cases {
  247. t.Run(k, func(t *testing.T) {
  248. t.Setenv("OLLAMA_VAR", k)
  249. if s := Var("OLLAMA_VAR"); s != v {
  250. t.Errorf("%s: expected %q, got %q", k, v, s)
  251. }
  252. })
  253. }
  254. }