2
0

mock.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. )
  7. type MockConfig struct {
  8. }
  9. func (mc *MockConfig) Validate() error {
  10. return nil
  11. }
  12. func (mc *MockConfig) Equals(config cloud.Config) bool {
  13. _, ok := config.(*MockConfig)
  14. return ok
  15. }
  16. func (mc *MockConfig) Sanitize() cloud.Config {
  17. return &MockConfig{}
  18. }
  19. // MockKeyedConfig implements KeyedConfig it only requires a key to be valid, there is an additional property allowing
  20. // MockKeyedConfig with the same key to not be equal
  21. type MockKeyedConfig struct {
  22. key string
  23. property string
  24. valid bool
  25. }
  26. func NewMockKeyedConfig(key, property string, valid bool) cloud.KeyedConfig {
  27. return &MockKeyedConfig{
  28. key: key,
  29. property: property,
  30. valid: valid,
  31. }
  32. }
  33. func (mkc *MockKeyedConfig) Validate() error {
  34. if !mkc.valid {
  35. return fmt.Errorf("MockKeyedConfig: set to invalid")
  36. }
  37. if mkc.key == "" {
  38. return fmt.Errorf("MockKeyedConfig: missing key")
  39. }
  40. return nil
  41. }
  42. func (mkc *MockKeyedConfig) Equals(config cloud.Config) bool {
  43. that, ok := config.(*MockKeyedConfig)
  44. if !ok {
  45. return false
  46. }
  47. if mkc.key != that.key {
  48. return false
  49. }
  50. if mkc.property != that.property {
  51. return false
  52. }
  53. if mkc.valid != that.valid {
  54. return false
  55. }
  56. return true
  57. }
  58. func (mkc *MockKeyedConfig) Sanitize() cloud.Config {
  59. return &MockKeyedConfig{
  60. key: mkc.key,
  61. property: mkc.property,
  62. valid: mkc.valid,
  63. }
  64. }
  65. func (mkc *MockKeyedConfig) Key() string {
  66. return mkc.key
  67. }
  68. func (mkc *MockKeyedConfig) Provider() string {
  69. return opencost.CustomProvider
  70. }
  71. type MockKeyedConfigWatcher struct {
  72. Integrations []cloud.KeyedConfig
  73. }
  74. func (mkcw *MockKeyedConfigWatcher) GetConfigs() []cloud.KeyedConfig {
  75. return mkcw.Integrations
  76. }