config_test.go 2.7 KB

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