opencost_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package env
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/env"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestGetAPIPort(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. want int
  15. pre func()
  16. }{
  17. {
  18. name: "Ensure the default API port '9003'",
  19. want: 9003,
  20. },
  21. {
  22. name: fmt.Sprintf("Ensure the default API port '9003' when %s is set to ''", env.APIPortEnvVar),
  23. want: 9003,
  24. pre: func() {
  25. os.Setenv(env.APIPortEnvVar, "")
  26. },
  27. },
  28. {
  29. name: fmt.Sprintf("Ensure the API port '9004' when %s is set to '9004'", env.APIPortEnvVar),
  30. want: 9004,
  31. pre: func() {
  32. os.Setenv(env.APIPortEnvVar, "9004")
  33. },
  34. },
  35. }
  36. for _, tt := range tests {
  37. if tt.pre != nil {
  38. tt.pre()
  39. }
  40. t.Run(tt.name, func(t *testing.T) {
  41. if got := GetOpencostAPIPort(); got != tt.want {
  42. t.Errorf("GetAPIPort() = %v, want %v", got, tt.want)
  43. }
  44. })
  45. }
  46. }
  47. func TestGetMCPQueryTimeout_Default(t *testing.T) {
  48. // Ensure env var is not set
  49. os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  50. timeout := GetMCPQueryTimeout()
  51. assert.Equal(t, 60*time.Second, timeout, "Default timeout should be 60 seconds")
  52. }
  53. func TestGetMCPQueryTimeout_CustomValue(t *testing.T) {
  54. // Set custom timeout
  55. err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "120")
  56. require.NoError(t, err)
  57. defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  58. timeout := GetMCPQueryTimeout()
  59. assert.Equal(t, 120*time.Second, timeout, "Custom timeout should be 120 seconds")
  60. }
  61. func TestGetMCPQueryTimeout_InvalidValue(t *testing.T) {
  62. // Set invalid value (should fall back to default)
  63. err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "invalid")
  64. require.NoError(t, err)
  65. defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  66. timeout := GetMCPQueryTimeout()
  67. assert.Equal(t, 60*time.Second, timeout, "Invalid value should fall back to default 60 seconds")
  68. }
  69. func TestGetMCPQueryTimeout_ZeroValue(t *testing.T) {
  70. // Set zero value - should fall back to minimum of 1 second
  71. err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "0")
  72. require.NoError(t, err)
  73. defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  74. timeout := GetMCPQueryTimeout()
  75. assert.Equal(t, 1*time.Second, timeout, "Zero value should use minimum of 1 second")
  76. }
  77. func TestGetMCPQueryTimeout_NegativeValue(t *testing.T) {
  78. // Set negative value - should fall back to minimum of 1 second
  79. err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "-10")
  80. require.NoError(t, err)
  81. defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  82. timeout := GetMCPQueryTimeout()
  83. assert.Equal(t, 1*time.Second, timeout, "Negative value should use minimum of 1 second")
  84. }
  85. func TestGetMCPQueryTimeout_LargeValue(t *testing.T) {
  86. // Set large timeout value
  87. err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "3600")
  88. require.NoError(t, err)
  89. defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
  90. timeout := GetMCPQueryTimeout()
  91. assert.Equal(t, 3600*time.Second, timeout, "Large timeout should be accepted (1 hour)")
  92. }