config_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package envconfig
  2. import (
  3. "fmt"
  4. "math"
  5. "net"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestConfig(t *testing.T) {
  12. t.Setenv("OLLAMA_DEBUG", "")
  13. require.False(t, Debug())
  14. t.Setenv("OLLAMA_DEBUG", "false")
  15. require.False(t, Debug())
  16. t.Setenv("OLLAMA_DEBUG", "1")
  17. require.True(t, Debug())
  18. t.Setenv("OLLAMA_FLASH_ATTENTION", "1")
  19. LoadConfig()
  20. require.True(t, FlashAttention)
  21. t.Setenv("OLLAMA_KEEP_ALIVE", "")
  22. LoadConfig()
  23. require.Equal(t, 5*time.Minute, KeepAlive)
  24. t.Setenv("OLLAMA_KEEP_ALIVE", "3")
  25. LoadConfig()
  26. require.Equal(t, 3*time.Second, KeepAlive)
  27. t.Setenv("OLLAMA_KEEP_ALIVE", "1h")
  28. LoadConfig()
  29. require.Equal(t, 1*time.Hour, KeepAlive)
  30. t.Setenv("OLLAMA_KEEP_ALIVE", "-1s")
  31. LoadConfig()
  32. require.Equal(t, time.Duration(math.MaxInt64), KeepAlive)
  33. t.Setenv("OLLAMA_KEEP_ALIVE", "-1")
  34. LoadConfig()
  35. require.Equal(t, time.Duration(math.MaxInt64), KeepAlive)
  36. }
  37. func TestClientFromEnvironment(t *testing.T) {
  38. type testCase struct {
  39. value string
  40. expect string
  41. err error
  42. }
  43. hostTestCases := map[string]*testCase{
  44. "empty": {value: "", expect: "127.0.0.1:11434"},
  45. "only address": {value: "1.2.3.4", expect: "1.2.3.4:11434"},
  46. "only port": {value: ":1234", expect: ":1234"},
  47. "address and port": {value: "1.2.3.4:1234", expect: "1.2.3.4:1234"},
  48. "hostname": {value: "example.com", expect: "example.com:11434"},
  49. "hostname and port": {value: "example.com:1234", expect: "example.com:1234"},
  50. "zero port": {value: ":0", expect: ":0"},
  51. "too large port": {value: ":66000", err: ErrInvalidHostPort},
  52. "too small port": {value: ":-1", err: ErrInvalidHostPort},
  53. "ipv6 localhost": {value: "[::1]", expect: "[::1]:11434"},
  54. "ipv6 world open": {value: "[::]", expect: "[::]:11434"},
  55. "ipv6 no brackets": {value: "::1", expect: "[::1]:11434"},
  56. "ipv6 + port": {value: "[::1]:1337", expect: "[::1]:1337"},
  57. "extra space": {value: " 1.2.3.4 ", expect: "1.2.3.4:11434"},
  58. "extra quotes": {value: "\"1.2.3.4\"", expect: "1.2.3.4:11434"},
  59. "extra space+quotes": {value: " \" 1.2.3.4 \" ", expect: "1.2.3.4:11434"},
  60. "extra single quotes": {value: "'1.2.3.4'", expect: "1.2.3.4:11434"},
  61. }
  62. for k, v := range hostTestCases {
  63. t.Run(k, func(t *testing.T) {
  64. t.Setenv("OLLAMA_HOST", v.value)
  65. LoadConfig()
  66. oh, err := getOllamaHost()
  67. if err != v.err {
  68. t.Fatalf("expected %s, got %s", v.err, err)
  69. }
  70. if err == nil {
  71. host := net.JoinHostPort(oh.Host, oh.Port)
  72. assert.Equal(t, v.expect, host, fmt.Sprintf("%s: expected %s, got %s", k, v.expect, host))
  73. }
  74. })
  75. }
  76. }