node_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package pricingmodel
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/model/shared"
  5. )
  6. func TestNodeKeyRoundtrip(t *testing.T) {
  7. cases := []struct {
  8. name string
  9. key NodeKey
  10. }{
  11. {
  12. name: "full GPU key",
  13. key: NodeKey{
  14. Provider: shared.ProviderGCP,
  15. Region: "us-central1",
  16. NodeType: "n2-standard-4",
  17. UsageType: shared.UsageTypeOnDemand,
  18. Family: "n2",
  19. DeviceType: "nvidia-tesla-t4",
  20. PricingType: NodePricingTypeDevice,
  21. },
  22. },
  23. {
  24. name: "on-demand CPU key",
  25. key: NodeKey{
  26. Provider: shared.ProviderAWS,
  27. Region: "us-east-1",
  28. NodeType: "m5.xlarge",
  29. UsageType: shared.UsageTypeOnDemand,
  30. Family: "m5",
  31. PricingType: NodePricingTypeCPUCore,
  32. },
  33. },
  34. {
  35. name: "spot total key",
  36. key: NodeKey{
  37. Provider: shared.ProviderAzure,
  38. Region: "eastus",
  39. NodeType: "Standard_D4s_v3",
  40. UsageType: shared.UsageTypeSpot,
  41. PricingType: NodePricingTypeTotal,
  42. },
  43. },
  44. {
  45. name: "RAM key",
  46. key: NodeKey{
  47. Provider: shared.ProviderGCP,
  48. Region: "europe-west1",
  49. Family: "n1",
  50. UsageType: shared.UsageTypeOnDemand,
  51. PricingType: NodePricingTypeRamGB,
  52. },
  53. },
  54. {
  55. name: "empty key",
  56. key: NodeKey{},
  57. },
  58. }
  59. for _, tc := range cases {
  60. t.Run(tc.name, func(t *testing.T) {
  61. data, err := tc.key.MarshalBinary()
  62. if err != nil {
  63. t.Fatalf("MarshalBinary() error: %v", err)
  64. }
  65. var got NodeKey
  66. if err := got.UnmarshalBinary(data); err != nil {
  67. t.Fatalf("UnmarshalBinary() error: %v", err)
  68. }
  69. if got != tc.key {
  70. t.Errorf("roundtrip mismatch:\n got %+v\n want %+v", got, tc.key)
  71. }
  72. })
  73. }
  74. }
  75. func TestNodePricingRoundtrip(t *testing.T) {
  76. cases := []float64{0, 0.048, 0.192, 1.5, 2.0, 99.99}
  77. for _, rate := range cases {
  78. np := NodePricing{HourlyRate: rate}
  79. data, err := np.MarshalBinary()
  80. if err != nil {
  81. t.Fatalf("MarshalBinary(%v) error: %v", rate, err)
  82. }
  83. var got NodePricing
  84. if err := got.UnmarshalBinary(data); err != nil {
  85. t.Fatalf("UnmarshalBinary(%v) error: %v", rate, err)
  86. }
  87. if got.HourlyRate != np.HourlyRate {
  88. t.Errorf("HourlyRate roundtrip: got %v, want %v", got.HourlyRate, np.HourlyRate)
  89. }
  90. }
  91. }