config_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package envconfig
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestSmoke(t *testing.T) {
  10. t.Setenv("OLLAMA_DEBUG", "")
  11. require.False(t, Debug())
  12. t.Setenv("OLLAMA_DEBUG", "false")
  13. require.False(t, Debug())
  14. t.Setenv("OLLAMA_DEBUG", "1")
  15. require.True(t, Debug())
  16. t.Setenv("OLLAMA_FLASH_ATTENTION", "1")
  17. require.True(t, FlashAttention())
  18. }
  19. func TestHost(t *testing.T) {
  20. cases := map[string]struct {
  21. value string
  22. expect string
  23. }{
  24. "empty": {"", "127.0.0.1:11434"},
  25. "only address": {"1.2.3.4", "1.2.3.4:11434"},
  26. "only port": {":1234", ":1234"},
  27. "address and port": {"1.2.3.4:1234", "1.2.3.4:1234"},
  28. "hostname": {"example.com", "example.com:11434"},
  29. "hostname and port": {"example.com:1234", "example.com:1234"},
  30. "zero port": {":0", ":0"},
  31. "too large port": {":66000", ":11434"},
  32. "too small port": {":-1", ":11434"},
  33. "ipv6 localhost": {"[::1]", "[::1]:11434"},
  34. "ipv6 world open": {"[::]", "[::]:11434"},
  35. "ipv6 no brackets": {"::1", "[::1]:11434"},
  36. "ipv6 + port": {"[::1]:1337", "[::1]:1337"},
  37. "extra space": {" 1.2.3.4 ", "1.2.3.4:11434"},
  38. "extra quotes": {"\"1.2.3.4\"", "1.2.3.4:11434"},
  39. "extra space+quotes": {" \" 1.2.3.4 \" ", "1.2.3.4:11434"},
  40. "extra single quotes": {"'1.2.3.4'", "1.2.3.4:11434"},
  41. }
  42. for name, tt := range cases {
  43. t.Run(name, func(t *testing.T) {
  44. t.Setenv("OLLAMA_HOST", tt.value)
  45. if host := Host(); host.Host != tt.expect {
  46. t.Errorf("%s: expected %s, got %s", name, tt.expect, host.Host)
  47. }
  48. })
  49. }
  50. }
  51. func TestOrigins(t *testing.T) {
  52. cases := []struct {
  53. value string
  54. expect []string
  55. }{
  56. {"", []string{
  57. "http://localhost",
  58. "https://localhost",
  59. "http://localhost:*",
  60. "https://localhost:*",
  61. "http://127.0.0.1",
  62. "https://127.0.0.1",
  63. "http://127.0.0.1:*",
  64. "https://127.0.0.1:*",
  65. "http://0.0.0.0",
  66. "https://0.0.0.0",
  67. "http://0.0.0.0:*",
  68. "https://0.0.0.0:*",
  69. "app://*",
  70. "file://*",
  71. "tauri://*",
  72. }},
  73. {"http://10.0.0.1", []string{
  74. "http://10.0.0.1",
  75. "http://localhost",
  76. "https://localhost",
  77. "http://localhost:*",
  78. "https://localhost:*",
  79. "http://127.0.0.1",
  80. "https://127.0.0.1",
  81. "http://127.0.0.1:*",
  82. "https://127.0.0.1:*",
  83. "http://0.0.0.0",
  84. "https://0.0.0.0",
  85. "http://0.0.0.0:*",
  86. "https://0.0.0.0:*",
  87. "app://*",
  88. "file://*",
  89. "tauri://*",
  90. }},
  91. {"http://172.16.0.1,https://192.168.0.1", []string{
  92. "http://172.16.0.1",
  93. "https://192.168.0.1",
  94. "http://localhost",
  95. "https://localhost",
  96. "http://localhost:*",
  97. "https://localhost:*",
  98. "http://127.0.0.1",
  99. "https://127.0.0.1",
  100. "http://127.0.0.1:*",
  101. "https://127.0.0.1:*",
  102. "http://0.0.0.0",
  103. "https://0.0.0.0",
  104. "http://0.0.0.0:*",
  105. "https://0.0.0.0:*",
  106. "app://*",
  107. "file://*",
  108. "tauri://*",
  109. }},
  110. {"http://totally.safe,http://definitely.legit", []string{
  111. "http://totally.safe",
  112. "http://definitely.legit",
  113. "http://localhost",
  114. "https://localhost",
  115. "http://localhost:*",
  116. "https://localhost:*",
  117. "http://127.0.0.1",
  118. "https://127.0.0.1",
  119. "http://127.0.0.1:*",
  120. "https://127.0.0.1:*",
  121. "http://0.0.0.0",
  122. "https://0.0.0.0",
  123. "http://0.0.0.0:*",
  124. "https://0.0.0.0:*",
  125. "app://*",
  126. "file://*",
  127. "tauri://*",
  128. }},
  129. }
  130. for _, tt := range cases {
  131. t.Run(tt.value, func(t *testing.T) {
  132. t.Setenv("OLLAMA_ORIGINS", tt.value)
  133. if diff := cmp.Diff(Origins(), tt.expect); diff != "" {
  134. t.Errorf("%s: mismatch (-want +got):\n%s", tt.value, diff)
  135. }
  136. })
  137. }
  138. }
  139. func TestBool(t *testing.T) {
  140. cases := map[string]struct {
  141. value string
  142. expect bool
  143. }{
  144. "empty": {"", false},
  145. "true": {"true", true},
  146. "false": {"false", false},
  147. "1": {"1", true},
  148. "0": {"0", false},
  149. "random": {"random", true},
  150. "something": {"something", true},
  151. }
  152. for name, tt := range cases {
  153. t.Run(name, func(t *testing.T) {
  154. t.Setenv("OLLAMA_BOOL", tt.value)
  155. if b := Bool("OLLAMA_BOOL"); b() != tt.expect {
  156. t.Errorf("%s: expected %t, got %t", name, tt.expect, b())
  157. }
  158. })
  159. }
  160. }
  161. func TestKeepAlive(t *testing.T) {
  162. cases := map[string]time.Duration{
  163. "": 5 * time.Minute,
  164. "1s": time.Second,
  165. "1m": time.Minute,
  166. "1h": time.Hour,
  167. "5m0s": 5 * time.Minute,
  168. "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second,
  169. "0": time.Duration(0),
  170. "60": 60 * time.Second,
  171. "120": 2 * time.Minute,
  172. "3600": time.Hour,
  173. "-0": time.Duration(0),
  174. "-1": time.Duration(math.MaxInt64),
  175. "-1m": time.Duration(math.MaxInt64),
  176. // invalid values
  177. " ": 5 * time.Minute,
  178. "???": 5 * time.Minute,
  179. "1d": 5 * time.Minute,
  180. "1y": 5 * time.Minute,
  181. "1w": 5 * time.Minute,
  182. }
  183. for tt, expect := range cases {
  184. t.Run(tt, func(t *testing.T) {
  185. t.Setenv("OLLAMA_KEEP_ALIVE", tt)
  186. if actual := KeepAlive(); actual != expect {
  187. t.Errorf("%s: expected %s, got %s", tt, expect, actual)
  188. }
  189. })
  190. }
  191. }