config_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. LoadConfig()
  18. require.True(t, FlashAttention)
  19. t.Setenv("OLLAMA_KEEP_ALIVE", "")
  20. LoadConfig()
  21. require.Equal(t, 5*time.Minute, KeepAlive)
  22. t.Setenv("OLLAMA_KEEP_ALIVE", "3")
  23. LoadConfig()
  24. require.Equal(t, 3*time.Second, KeepAlive)
  25. t.Setenv("OLLAMA_KEEP_ALIVE", "1h")
  26. LoadConfig()
  27. require.Equal(t, 1*time.Hour, KeepAlive)
  28. t.Setenv("OLLAMA_KEEP_ALIVE", "-1s")
  29. LoadConfig()
  30. require.Equal(t, time.Duration(math.MaxInt64), KeepAlive)
  31. t.Setenv("OLLAMA_KEEP_ALIVE", "-1")
  32. LoadConfig()
  33. require.Equal(t, time.Duration(math.MaxInt64), KeepAlive)
  34. }
  35. func TestHost(t *testing.T) {
  36. cases := map[string]struct {
  37. value string
  38. expect string
  39. }{
  40. "empty": {"", "127.0.0.1:11434"},
  41. "only address": {"1.2.3.4", "1.2.3.4:11434"},
  42. "only port": {":1234", ":1234"},
  43. "address and port": {"1.2.3.4:1234", "1.2.3.4:1234"},
  44. "hostname": {"example.com", "example.com:11434"},
  45. "hostname and port": {"example.com:1234", "example.com:1234"},
  46. "zero port": {":0", ":0"},
  47. "too large port": {":66000", ":11434"},
  48. "too small port": {":-1", ":11434"},
  49. "ipv6 localhost": {"[::1]", "[::1]:11434"},
  50. "ipv6 world open": {"[::]", "[::]:11434"},
  51. "ipv6 no brackets": {"::1", "[::1]:11434"},
  52. "ipv6 + port": {"[::1]:1337", "[::1]:1337"},
  53. "extra space": {" 1.2.3.4 ", "1.2.3.4:11434"},
  54. "extra quotes": {"\"1.2.3.4\"", "1.2.3.4:11434"},
  55. "extra space+quotes": {" \" 1.2.3.4 \" ", "1.2.3.4:11434"},
  56. "extra single quotes": {"'1.2.3.4'", "1.2.3.4:11434"},
  57. }
  58. for name, tt := range cases {
  59. t.Run(name, func(t *testing.T) {
  60. t.Setenv("OLLAMA_HOST", tt.value)
  61. if host := Host(); host.Host != tt.expect {
  62. t.Errorf("%s: expected %s, got %s", name, tt.expect, host.Host)
  63. }
  64. })
  65. }
  66. }
  67. func TestOrigins(t *testing.T) {
  68. cases := []struct {
  69. value string
  70. expect []string
  71. }{
  72. {"", []string{
  73. "http://localhost",
  74. "https://localhost",
  75. "http://localhost:*",
  76. "https://localhost:*",
  77. "http://127.0.0.1",
  78. "https://127.0.0.1",
  79. "http://127.0.0.1:*",
  80. "https://127.0.0.1:*",
  81. "http://0.0.0.0",
  82. "https://0.0.0.0",
  83. "http://0.0.0.0:*",
  84. "https://0.0.0.0:*",
  85. "app://*",
  86. "file://*",
  87. "tauri://*",
  88. }},
  89. {"http://10.0.0.1", []string{
  90. "http://10.0.0.1",
  91. "http://localhost",
  92. "https://localhost",
  93. "http://localhost:*",
  94. "https://localhost:*",
  95. "http://127.0.0.1",
  96. "https://127.0.0.1",
  97. "http://127.0.0.1:*",
  98. "https://127.0.0.1:*",
  99. "http://0.0.0.0",
  100. "https://0.0.0.0",
  101. "http://0.0.0.0:*",
  102. "https://0.0.0.0:*",
  103. "app://*",
  104. "file://*",
  105. "tauri://*",
  106. }},
  107. {"http://172.16.0.1,https://192.168.0.1", []string{
  108. "http://172.16.0.1",
  109. "https://192.168.0.1",
  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. }},
  126. {"http://totally.safe,http://definitely.legit", []string{
  127. "http://totally.safe",
  128. "http://definitely.legit",
  129. "http://localhost",
  130. "https://localhost",
  131. "http://localhost:*",
  132. "https://localhost:*",
  133. "http://127.0.0.1",
  134. "https://127.0.0.1",
  135. "http://127.0.0.1:*",
  136. "https://127.0.0.1:*",
  137. "http://0.0.0.0",
  138. "https://0.0.0.0",
  139. "http://0.0.0.0:*",
  140. "https://0.0.0.0:*",
  141. "app://*",
  142. "file://*",
  143. "tauri://*",
  144. }},
  145. }
  146. for _, tt := range cases {
  147. t.Run(tt.value, func(t *testing.T) {
  148. t.Setenv("OLLAMA_ORIGINS", tt.value)
  149. if diff := cmp.Diff(Origins(), tt.expect); diff != "" {
  150. t.Errorf("%s: mismatch (-want +got):\n%s", tt.value, diff)
  151. }
  152. })
  153. }
  154. }