| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package env
- import (
- "fmt"
- "os"
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/env"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestGetAPIPort(t *testing.T) {
- tests := []struct {
- name string
- want int
- pre func()
- }{
- {
- name: "Ensure the default API port '9003'",
- want: 9003,
- },
- {
- name: fmt.Sprintf("Ensure the default API port '9003' when %s is set to ''", env.APIPortEnvVar),
- want: 9003,
- pre: func() {
- os.Setenv(env.APIPortEnvVar, "")
- },
- },
- {
- name: fmt.Sprintf("Ensure the API port '9004' when %s is set to '9004'", env.APIPortEnvVar),
- want: 9004,
- pre: func() {
- os.Setenv(env.APIPortEnvVar, "9004")
- },
- },
- }
- for _, tt := range tests {
- if tt.pre != nil {
- tt.pre()
- }
- t.Run(tt.name, func(t *testing.T) {
- if got := GetOpencostAPIPort(); got != tt.want {
- t.Errorf("GetAPIPort() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestGetMCPQueryTimeout_Default(t *testing.T) {
- // Ensure env var is not set
- os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 60*time.Second, timeout, "Default timeout should be 60 seconds")
- }
- func TestGetMCPQueryTimeout_CustomValue(t *testing.T) {
- // Set custom timeout
- err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "120")
- require.NoError(t, err)
- defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 120*time.Second, timeout, "Custom timeout should be 120 seconds")
- }
- func TestGetMCPQueryTimeout_InvalidValue(t *testing.T) {
- // Set invalid value (should fall back to default)
- err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "invalid")
- require.NoError(t, err)
- defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 60*time.Second, timeout, "Invalid value should fall back to default 60 seconds")
- }
- func TestGetMCPQueryTimeout_ZeroValue(t *testing.T) {
- // Set zero value - should fall back to minimum of 1 second
- err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "0")
- require.NoError(t, err)
- defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 1*time.Second, timeout, "Zero value should use minimum of 1 second")
- }
- func TestGetMCPQueryTimeout_NegativeValue(t *testing.T) {
- // Set negative value - should fall back to minimum of 1 second
- err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "-10")
- require.NoError(t, err)
- defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 1*time.Second, timeout, "Negative value should use minimum of 1 second")
- }
- func TestGetMCPQueryTimeout_LargeValue(t *testing.T) {
- // Set large timeout value
- err := os.Setenv(MCPQueryTimeoutSecondsEnvVar, "3600")
- require.NoError(t, err)
- defer os.Unsetenv(MCPQueryTimeoutSecondsEnvVar)
- timeout := GetMCPQueryTimeout()
- assert.Equal(t, 3600*time.Second, timeout, "Large timeout should be accepted (1 hour)")
- }
|