azurepricingsource_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package azure
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/pricing"
  5. )
  6. func TestMapAzureDiskType(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. skuName string
  10. expected pricing.VolumeType
  11. }{
  12. {
  13. name: "Premium SSD V2",
  14. skuName: "Premium SSD v2 Managed Disk",
  15. expected: pricing.VolumeTypePremiumV2LRS,
  16. },
  17. {
  18. name: "PremiumV2 variant",
  19. skuName: "PremiumV2 LRS Disk",
  20. expected: pricing.VolumeTypePremiumV2LRS,
  21. },
  22. {
  23. name: "Premium SSD",
  24. skuName: "Premium SSD Managed Disk",
  25. expected: pricing.VolumeTypePremiumLRS,
  26. },
  27. {
  28. name: "Standard SSD",
  29. skuName: "Standard SSD Managed Disk",
  30. expected: pricing.VolumeTypeStandardSSDLRS,
  31. },
  32. {
  33. name: "StandardSSD variant",
  34. skuName: "StandardSSD LRS",
  35. expected: pricing.VolumeTypeStandardSSDLRS,
  36. },
  37. {
  38. name: "Standard HDD",
  39. skuName: "Standard HDD Managed Disk",
  40. expected: pricing.VolumeTypeStandardHDDLRS,
  41. },
  42. {
  43. name: "Ultra SSD",
  44. skuName: "Ultra SSD Managed Disk",
  45. expected: pricing.VolumeTypeUltraSSDLRS,
  46. },
  47. {
  48. name: "Unknown type",
  49. skuName: "Some Unknown Disk Type",
  50. expected: pricing.VolumeTypeNil,
  51. },
  52. {
  53. name: "Case insensitive Premium",
  54. skuName: "PREMIUM SSD MANAGED DISK",
  55. expected: pricing.VolumeTypePremiumLRS,
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. result := mapAzureDiskType(tt.skuName)
  61. if result != tt.expected {
  62. t.Errorf("mapAzureDiskType(%q) = %v, want %v", tt.skuName, result, tt.expected)
  63. }
  64. })
  65. }
  66. }
  67. func TestIncludeItem(t *testing.T) {
  68. source := &AzurePricingSource{
  69. config: AzurePricingSourceConfig{
  70. CurrencyCode: "USD",
  71. },
  72. }
  73. tests := []struct {
  74. name string
  75. item AzurePricingAttributes
  76. expected bool
  77. }{
  78. {
  79. name: "valid Linux VM",
  80. item: AzurePricingAttributes{
  81. ArmSkuName: "Standard_D2s_v3",
  82. ArmRegionName: "eastus",
  83. ProductName: "Virtual Machines Dsv3 Series",
  84. SkuName: "D2s v3",
  85. },
  86. expected: true,
  87. },
  88. {
  89. name: "Windows VM - excluded",
  90. item: AzurePricingAttributes{
  91. ArmSkuName: "Standard_D2s_v3",
  92. ArmRegionName: "eastus",
  93. ProductName: "Virtual Machines Dsv3 Series Windows",
  94. SkuName: "D2s v3",
  95. },
  96. expected: false,
  97. },
  98. {
  99. name: "Low priority - excluded",
  100. item: AzurePricingAttributes{
  101. ArmSkuName: "Standard_D2s_v3",
  102. ArmRegionName: "eastus",
  103. ProductName: "Virtual Machines Dsv3 Series",
  104. SkuName: "D2s v3 Low Priority",
  105. },
  106. expected: false,
  107. },
  108. {
  109. name: "Cloud Services - excluded",
  110. item: AzurePricingAttributes{
  111. ArmSkuName: "Standard_D2s_v3",
  112. ArmRegionName: "eastus",
  113. ProductName: "Cloud Services Dsv3 Series",
  114. SkuName: "D2s v3",
  115. },
  116. expected: false,
  117. },
  118. {
  119. name: "CloudServices variant - excluded",
  120. item: AzurePricingAttributes{
  121. ArmSkuName: "Standard_D2s_v3",
  122. ArmRegionName: "eastus",
  123. ProductName: "CloudServices Dsv3 Series",
  124. SkuName: "D2s v3",
  125. },
  126. expected: false,
  127. },
  128. {
  129. name: "Missing ArmSkuName - excluded",
  130. item: AzurePricingAttributes{
  131. ArmSkuName: "",
  132. ArmRegionName: "eastus",
  133. ProductName: "Virtual Machines Dsv3 Series",
  134. SkuName: "D2s v3",
  135. },
  136. expected: false,
  137. },
  138. {
  139. name: "Missing ArmRegionName - excluded",
  140. item: AzurePricingAttributes{
  141. ArmSkuName: "Standard_D2s_v3",
  142. ArmRegionName: "",
  143. ProductName: "Virtual Machines Dsv3 Series",
  144. SkuName: "D2s v3",
  145. },
  146. expected: false,
  147. },
  148. }
  149. for _, tt := range tests {
  150. t.Run(tt.name, func(t *testing.T) {
  151. result := source.includeItem(tt.item)
  152. if result != tt.expected {
  153. t.Errorf("includeItem() = %v, want %v", result, tt.expected)
  154. }
  155. })
  156. }
  157. }
  158. func TestIncludeDiskItem(t *testing.T) {
  159. source := &AzurePricingSource{
  160. config: AzurePricingSourceConfig{
  161. CurrencyCode: "USD",
  162. },
  163. }
  164. tests := []struct {
  165. name string
  166. item AzurePricingAttributes
  167. expected bool
  168. }{
  169. {
  170. name: "Managed disk - included",
  171. item: AzurePricingAttributes{
  172. ArmRegionName: "eastus",
  173. ProductName: "Premium SSD Managed Disk",
  174. SkuName: "P10 LRS",
  175. },
  176. expected: true,
  177. },
  178. {
  179. name: "Managed Disk uppercase - included",
  180. item: AzurePricingAttributes{
  181. ArmRegionName: "eastus",
  182. ProductName: "PREMIUM SSD MANAGED DISK",
  183. SkuName: "P10 LRS",
  184. },
  185. expected: true,
  186. },
  187. {
  188. name: "Unmanaged disk - excluded",
  189. item: AzurePricingAttributes{
  190. ArmRegionName: "eastus",
  191. ProductName: "Premium SSD Unmanaged Disk",
  192. SkuName: "P10",
  193. },
  194. expected: false,
  195. },
  196. {
  197. name: "Missing region - excluded",
  198. item: AzurePricingAttributes{
  199. ArmRegionName: "",
  200. ProductName: "Premium SSD Managed Disk",
  201. SkuName: "P10 LRS",
  202. },
  203. expected: false,
  204. },
  205. {
  206. name: "Storage account - excluded",
  207. item: AzurePricingAttributes{
  208. ArmRegionName: "eastus",
  209. ProductName: "Storage Account",
  210. SkuName: "Standard LRS",
  211. },
  212. expected: false,
  213. },
  214. }
  215. for _, tt := range tests {
  216. t.Run(tt.name, func(t *testing.T) {
  217. result := source.includeDiskItem(tt.item)
  218. if result != tt.expected {
  219. t.Errorf("includeDiskItem() = %v, want %v", result, tt.expected)
  220. }
  221. })
  222. }
  223. }
  224. func TestBuildVMURL(t *testing.T) {
  225. tests := []struct {
  226. name string
  227. currencyCode string
  228. wantContains []string
  229. }{
  230. {
  231. name: "USD currency",
  232. currencyCode: "USD",
  233. wantContains: []string{
  234. "prices.azure.com",
  235. "serviceName+eq+%27Virtual+Machines%27",
  236. "priceType+eq+%27Consumption%27",
  237. "currencyCode=USD",
  238. },
  239. },
  240. {
  241. name: "EUR currency",
  242. currencyCode: "EUR",
  243. wantContains: []string{
  244. "prices.azure.com",
  245. "currencyCode=EUR",
  246. },
  247. },
  248. {
  249. name: "Empty currency",
  250. currencyCode: "",
  251. wantContains: []string{
  252. "prices.azure.com",
  253. "serviceName+eq+%27Virtual+Machines%27",
  254. },
  255. },
  256. }
  257. for _, tt := range tests {
  258. t.Run(tt.name, func(t *testing.T) {
  259. source := &AzurePricingSource{
  260. config: AzurePricingSourceConfig{
  261. CurrencyCode: tt.currencyCode,
  262. },
  263. }
  264. url := source.buildVMURL()
  265. for _, want := range tt.wantContains {
  266. if !contains(url, want) {
  267. t.Errorf("buildVMURL() = %v, want to contain %v", url, want)
  268. }
  269. }
  270. })
  271. }
  272. }
  273. func TestBuildDiskURL(t *testing.T) {
  274. tests := []struct {
  275. name string
  276. currencyCode string
  277. wantContains []string
  278. }{
  279. {
  280. name: "USD currency",
  281. currencyCode: "USD",
  282. wantContains: []string{
  283. "prices.azure.com",
  284. "serviceName+eq+%27Storage%27",
  285. "priceType+eq+%27Consumption%27",
  286. "currencyCode=USD",
  287. },
  288. },
  289. {
  290. name: "EUR currency",
  291. currencyCode: "EUR",
  292. wantContains: []string{
  293. "prices.azure.com",
  294. "currencyCode=EUR",
  295. },
  296. },
  297. }
  298. for _, tt := range tests {
  299. t.Run(tt.name, func(t *testing.T) {
  300. source := &AzurePricingSource{
  301. config: AzurePricingSourceConfig{
  302. CurrencyCode: tt.currencyCode,
  303. },
  304. }
  305. url := source.buildDiskURL()
  306. for _, want := range tt.wantContains {
  307. if !contains(url, want) {
  308. t.Errorf("buildDiskURL() = %v, want to contain %v", url, want)
  309. }
  310. }
  311. })
  312. }
  313. }
  314. func TestNewAzurePricingSource(t *testing.T) {
  315. config := AzurePricingSourceConfig{
  316. CurrencyCode: "USD",
  317. }
  318. source := NewAzurePricingSource(config)
  319. if source == nil {
  320. t.Fatal("NewAzurePricingSource() returned nil")
  321. }
  322. if source.config.CurrencyCode != "USD" {
  323. t.Errorf("CurrencyCode = %v, want USD", source.config.CurrencyCode)
  324. }
  325. }
  326. // Helper function to check if a string contains a substring
  327. func contains(s, substr string) bool {
  328. return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
  329. (len(s) > 0 && (s[0:len(substr)] == substr || contains(s[1:], substr))))
  330. }
  331. // Made with Bob