config_test.go 5.5 KB

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