opencost_test.go 865 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package env
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/opencost/opencost/core/pkg/env"
  7. )
  8. func TestGetAPIPort(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. want int
  12. pre func()
  13. }{
  14. {
  15. name: "Ensure the default API port '9003'",
  16. want: 9003,
  17. },
  18. {
  19. name: fmt.Sprintf("Ensure the default API port '9003' when %s is set to ''", env.APIPortEnvVar),
  20. want: 9003,
  21. pre: func() {
  22. os.Setenv(env.APIPortEnvVar, "")
  23. },
  24. },
  25. {
  26. name: fmt.Sprintf("Ensure the API port '9004' when %s is set to '9004'", env.APIPortEnvVar),
  27. want: 9004,
  28. pre: func() {
  29. os.Setenv(env.APIPortEnvVar, "9004")
  30. },
  31. },
  32. }
  33. for _, tt := range tests {
  34. if tt.pre != nil {
  35. tt.pre()
  36. }
  37. t.Run(tt.name, func(t *testing.T) {
  38. if got := GetOpencostAPIPort(); got != tt.want {
  39. t.Errorf("GetAPIPort() = %v, want %v", got, tt.want)
  40. }
  41. })
  42. }
  43. }