awspricingsource_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package aws
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/pricing"
  5. )
  6. func TestNewAWSPricingSource(t *testing.T) {
  7. config := AWSPricingSourceConfig{
  8. CurrencyCode: "USD",
  9. }
  10. source := NewAWSPricingSource(config)
  11. if source == nil {
  12. t.Fatal("NewAWSPricingSource() returned nil")
  13. }
  14. if source.config.CurrencyCode != "USD" {
  15. t.Errorf("CurrencyCode = %v, want USD", source.config.CurrencyCode)
  16. }
  17. }
  18. func TestUsageTypeRegex(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. usageType string
  22. wantMatch bool
  23. wantGroup string
  24. }{
  25. {
  26. name: "Standard EBS usage",
  27. usageType: "USE1-EBS:VolumeUsage.gp3",
  28. wantMatch: true,
  29. wantGroup: "EBS:VolumeUsage.gp3",
  30. },
  31. {
  32. name: "GP2 volume",
  33. usageType: "USW2-EBS:VolumeUsage.gp2",
  34. wantMatch: true,
  35. wantGroup: "EBS:VolumeUsage.gp2",
  36. },
  37. {
  38. name: "Standard volume",
  39. usageType: "USE1-EBS:VolumeUsage",
  40. wantMatch: true,
  41. wantGroup: "EBS:VolumeUsage",
  42. },
  43. {
  44. name: "IO1 volume",
  45. usageType: "USE1-EBS:VolumeUsage.piops",
  46. wantMatch: true,
  47. wantGroup: "EBS:VolumeUsage.piops",
  48. },
  49. {
  50. name: "No region prefix",
  51. usageType: "EBS:VolumeUsage.gp3",
  52. wantMatch: true,
  53. wantGroup: "EBS:VolumeUsage.gp3",
  54. },
  55. {
  56. name: "Non-EBS usage",
  57. usageType: "BoxUsage:t3.medium",
  58. wantMatch: false,
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. matches := usageTypeRegex.FindStringSubmatch(tt.usageType)
  64. if tt.wantMatch {
  65. if len(matches) == 0 {
  66. t.Errorf("usageTypeRegex did not match %q", tt.usageType)
  67. return
  68. }
  69. // The last group should contain the EBS usage type
  70. actualGroup := matches[len(matches)-1]
  71. if actualGroup != tt.wantGroup {
  72. t.Errorf("usageTypeRegex matched %q, want %q", actualGroup, tt.wantGroup)
  73. }
  74. } else {
  75. if len(matches) > 0 {
  76. t.Errorf("usageTypeRegex unexpectedly matched %q", tt.usageType)
  77. }
  78. }
  79. })
  80. }
  81. }
  82. func TestAWSVolumeTypes(t *testing.T) {
  83. tests := []struct {
  84. usageType string
  85. expectedType pricing.VolumeType
  86. shouldExist bool
  87. }{
  88. {
  89. usageType: "EBS:VolumeUsage.gp2",
  90. expectedType: pricing.VolumeTypeGP2,
  91. shouldExist: true,
  92. },
  93. {
  94. usageType: "EBS:VolumeUsage.gp3",
  95. expectedType: pricing.VolumeTypeGP3,
  96. shouldExist: true,
  97. },
  98. {
  99. usageType: "EBS:VolumeUsage",
  100. expectedType: pricing.VolumeTypeStandard,
  101. shouldExist: true,
  102. },
  103. {
  104. usageType: "EBS:VolumeUsage.sc1",
  105. expectedType: pricing.VolumeTypeSC1,
  106. shouldExist: true,
  107. },
  108. {
  109. usageType: "EBS:VolumeP-IOPS.piops",
  110. expectedType: pricing.VolumeTypeIO1,
  111. shouldExist: true,
  112. },
  113. {
  114. usageType: "EBS:VolumeUsage.st1",
  115. expectedType: pricing.VolumeTypeST1,
  116. shouldExist: true,
  117. },
  118. {
  119. usageType: "EBS:VolumeUsage.piops",
  120. expectedType: pricing.VolumeTypeIO1,
  121. shouldExist: true,
  122. },
  123. {
  124. usageType: "EBS:VolumeUsage.io2",
  125. expectedType: pricing.VolumeTypeIO2,
  126. shouldExist: true,
  127. },
  128. {
  129. usageType: "EBS:VolumeUsage.unknown",
  130. shouldExist: false,
  131. },
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.usageType, func(t *testing.T) {
  135. volumeType, exists := awsVolumeTypes[tt.usageType]
  136. if exists != tt.shouldExist {
  137. t.Errorf("awsVolumeTypes[%q] exists = %v, want %v", tt.usageType, exists, tt.shouldExist)
  138. return
  139. }
  140. if tt.shouldExist && volumeType != tt.expectedType {
  141. t.Errorf("awsVolumeTypes[%q] = %v, want %v", tt.usageType, volumeType, tt.expectedType)
  142. }
  143. })
  144. }
  145. }
  146. func TestOnDemandRateCodes(t *testing.T) {
  147. // Test that expected rate codes exist
  148. expectedCodes := []string{"JRTCKXETXF"}
  149. for _, code := range expectedCodes {
  150. if _, exists := OnDemandRateCodes[code]; !exists {
  151. t.Errorf("OnDemandRateCodes missing expected code: %s", code)
  152. }
  153. }
  154. // Test that we have at least one code
  155. if len(OnDemandRateCodes) == 0 {
  156. t.Error("OnDemandRateCodes is empty")
  157. }
  158. }
  159. func TestOnDemandRateCodesCn(t *testing.T) {
  160. // Test that expected China rate codes exist
  161. expectedCodes := []string{"99YE2YK9UR", "5Y9WH78GDR", "KW44MY7SZN"}
  162. for _, code := range expectedCodes {
  163. if _, exists := OnDemandRateCodesCn[code]; !exists {
  164. t.Errorf("OnDemandRateCodesCn missing expected code: %s", code)
  165. }
  166. }
  167. // Test that we have at least one code
  168. if len(OnDemandRateCodesCn) == 0 {
  169. t.Error("OnDemandRateCodesCn is empty")
  170. }
  171. }
  172. func TestHourlyRateCodes(t *testing.T) {
  173. if HourlyRateCode == "" {
  174. t.Error("HourlyRateCode is empty")
  175. }
  176. if HourlyRateCodeCn == "" {
  177. t.Error("HourlyRateCodeCn is empty")
  178. }
  179. if HourlyRateCode == HourlyRateCodeCn {
  180. t.Error("HourlyRateCode and HourlyRateCodeCn should be different")
  181. }
  182. }
  183. func TestPriceListEC2PricePerUnit_ForCurrency(t *testing.T) {
  184. tests := []struct {
  185. name string
  186. price PriceListEC2PricePerUnit
  187. currency string
  188. expected string
  189. }{
  190. {
  191. name: "USD currency",
  192. price: PriceListEC2PricePerUnit{
  193. USD: "0.0416",
  194. CNY: "0.2800",
  195. },
  196. currency: "USD",
  197. expected: "0.0416",
  198. },
  199. {
  200. name: "CNY currency",
  201. price: PriceListEC2PricePerUnit{
  202. USD: "0.0416",
  203. CNY: "0.2800",
  204. },
  205. currency: "CNY",
  206. expected: "0.2800",
  207. },
  208. {
  209. name: "CNY lowercase",
  210. price: PriceListEC2PricePerUnit{
  211. USD: "0.0416",
  212. CNY: "0.2800",
  213. },
  214. currency: "cny",
  215. expected: "0.2800",
  216. },
  217. {
  218. name: "Unknown currency defaults to USD",
  219. price: PriceListEC2PricePerUnit{
  220. USD: "0.0416",
  221. CNY: "0.2800",
  222. },
  223. currency: "EUR",
  224. expected: "0.0416",
  225. },
  226. {
  227. name: "CNY empty falls back to USD",
  228. price: PriceListEC2PricePerUnit{
  229. USD: "0.0416",
  230. CNY: "",
  231. },
  232. currency: "CNY",
  233. expected: "0.0416",
  234. },
  235. {
  236. name: "Empty currency defaults to USD",
  237. price: PriceListEC2PricePerUnit{
  238. USD: "0.0416",
  239. CNY: "0.2800",
  240. },
  241. currency: "",
  242. expected: "0.0416",
  243. },
  244. }
  245. for _, tt := range tests {
  246. t.Run(tt.name, func(t *testing.T) {
  247. result := tt.price.ForCurrency(tt.currency)
  248. if result != tt.expected {
  249. t.Errorf("ForCurrency(%q) = %v, want %v", tt.currency, result, tt.expected)
  250. }
  251. })
  252. }
  253. }
  254. func TestPriceListEC2Term_String(t *testing.T) {
  255. term := &PriceListEC2Term{
  256. Sku: "TEST123",
  257. OfferTermCode: "JRTCKXETXF",
  258. PriceDimensions: map[string]*PriceListEC2PriceDimension{
  259. "TEST123.JRTCKXETXF.6YS6EN2CT7": {
  260. Unit: "Hrs",
  261. PricePerUnit: PriceListEC2PricePerUnit{
  262. USD: "0.0416",
  263. },
  264. },
  265. },
  266. }
  267. result := term.String()
  268. if result == "" {
  269. t.Error("String() returned empty string")
  270. }
  271. // Should contain the SKU
  272. if !containsSubstring(result, "TEST123") {
  273. t.Errorf("String() = %v, should contain SKU 'TEST123'", result)
  274. }
  275. }
  276. func TestPriceListEC2PriceDimension_String(t *testing.T) {
  277. pd := &PriceListEC2PriceDimension{
  278. Unit: "Hrs",
  279. PricePerUnit: PriceListEC2PricePerUnit{
  280. USD: "0.0416",
  281. },
  282. }
  283. result := pd.String()
  284. if result == "" {
  285. t.Error("String() returned empty string")
  286. }
  287. // Should contain unit
  288. if !containsSubstring(result, "Hrs") {
  289. t.Errorf("String() = %v, should contain unit 'Hrs'", result)
  290. }
  291. }
  292. // Helper function to check if string contains substring
  293. func containsSubstring(s, substr string) bool {
  294. if len(substr) == 0 {
  295. return true
  296. }
  297. if len(s) < len(substr) {
  298. return false
  299. }
  300. for i := 0; i <= len(s)-len(substr); i++ {
  301. if s[i:i+len(substr)] == substr {
  302. return true
  303. }
  304. }
  305. return false
  306. }
  307. // Made with Bob