gcppricingsource_test.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. package gcp
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "math"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/opencost/opencost/core/pkg/pricing"
  10. )
  11. const floatTolerance = 0.0000001
  12. func TestNewGCPPricingSource(t *testing.T) {
  13. config := GCPPricingSourceConfig{
  14. APIKey: "test-api-key",
  15. CurrencyCode: "USD",
  16. }
  17. source := NewGCPPricingSource(config)
  18. if source == nil {
  19. t.Fatal("NewGCPPricingSource() returned nil")
  20. }
  21. if source.config.APIKey != "test-api-key" {
  22. t.Errorf("APIKey = %v, want test-api-key", source.config.APIKey)
  23. }
  24. if source.config.CurrencyCode != "USD" {
  25. t.Errorf("CurrencyCode = %v, want USD", source.config.CurrencyCode)
  26. }
  27. }
  28. func TestBuildURL(t *testing.T) {
  29. tests := []struct {
  30. name string
  31. apiKey string
  32. currencyCode string
  33. pageToken string
  34. wantContains []string
  35. wantNotContain string
  36. }{
  37. {
  38. name: "Basic URL without page token",
  39. apiKey: "test-key",
  40. currencyCode: "USD",
  41. pageToken: "",
  42. wantContains: []string{
  43. "cloudbilling.googleapis.com",
  44. "key=test-key",
  45. "currencyCode=USD",
  46. },
  47. wantNotContain: "pageToken",
  48. },
  49. {
  50. name: "URL with page token",
  51. apiKey: "test-key",
  52. currencyCode: "EUR",
  53. pageToken: "next-page-123",
  54. wantContains: []string{
  55. "cloudbilling.googleapis.com",
  56. "key=test-key",
  57. "currencyCode=EUR",
  58. "pageToken=next-page-123",
  59. },
  60. },
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. source := &GCPPricingSource{
  65. config: GCPPricingSourceConfig{
  66. APIKey: tt.apiKey,
  67. CurrencyCode: tt.currencyCode,
  68. },
  69. }
  70. url := source.buildURL(tt.pageToken)
  71. for _, want := range tt.wantContains {
  72. if !contains(url, want) {
  73. t.Errorf("buildURL() = %v, want to contain %v", url, want)
  74. }
  75. }
  76. if tt.wantNotContain != "" && contains(url, tt.wantNotContain) {
  77. t.Errorf("buildURL() = %v, should not contain %v", url, tt.wantNotContain)
  78. }
  79. })
  80. }
  81. }
  82. func TestExtractHourlyPrice(t *testing.T) {
  83. tests := []struct {
  84. name string
  85. sku *GCPPricing
  86. wantPrice float64
  87. wantErr bool
  88. }{
  89. {
  90. name: "Valid pricing with single tier",
  91. sku: &GCPPricing{
  92. PricingInfo: []*PricingInfo{
  93. {
  94. PricingExpression: &PricingExpression{
  95. TieredRates: []*TieredRates{
  96. {
  97. UnitPrice: &UnitPriceInfo{
  98. Units: "0",
  99. Nanos: 41600000,
  100. },
  101. },
  102. },
  103. },
  104. },
  105. },
  106. },
  107. wantPrice: 0.0416,
  108. wantErr: false,
  109. },
  110. {
  111. name: "Valid pricing with multiple tiers",
  112. sku: &GCPPricing{
  113. PricingInfo: []*PricingInfo{
  114. {
  115. PricingExpression: &PricingExpression{
  116. TieredRates: []*TieredRates{
  117. {
  118. UnitPrice: &UnitPriceInfo{
  119. Units: "0",
  120. Nanos: 10000000,
  121. },
  122. },
  123. {
  124. UnitPrice: &UnitPriceInfo{
  125. Units: "0",
  126. Nanos: 50000000,
  127. },
  128. },
  129. },
  130. },
  131. },
  132. },
  133. },
  134. wantPrice: 0.05, // Should use last tier
  135. wantErr: false,
  136. },
  137. {
  138. name: "Pricing with whole units",
  139. sku: &GCPPricing{
  140. PricingInfo: []*PricingInfo{
  141. {
  142. PricingExpression: &PricingExpression{
  143. TieredRates: []*TieredRates{
  144. {
  145. UnitPrice: &UnitPriceInfo{
  146. Units: "1",
  147. Nanos: 500000000,
  148. },
  149. },
  150. },
  151. },
  152. },
  153. },
  154. },
  155. wantPrice: 1.5,
  156. wantErr: false,
  157. },
  158. {
  159. name: "No pricing info",
  160. sku: &GCPPricing{
  161. PricingInfo: []*PricingInfo{},
  162. },
  163. wantPrice: 0,
  164. wantErr: true,
  165. },
  166. {
  167. name: "No tiered rates",
  168. sku: &GCPPricing{
  169. PricingInfo: []*PricingInfo{
  170. {
  171. PricingExpression: &PricingExpression{
  172. TieredRates: []*TieredRates{},
  173. },
  174. },
  175. },
  176. },
  177. wantPrice: 0,
  178. wantErr: true,
  179. },
  180. }
  181. for _, tt := range tests {
  182. t.Run(tt.name, func(t *testing.T) {
  183. source := &GCPPricingSource{}
  184. price, err := source.extractHourlyPrice(tt.sku)
  185. if (err != nil) != tt.wantErr {
  186. t.Errorf("extractHourlyPrice() error = %v, wantErr %v", err, tt.wantErr)
  187. return
  188. }
  189. if !tt.wantErr && math.Abs(price-tt.wantPrice) > floatTolerance {
  190. t.Errorf("extractHourlyPrice() = %v, want %v", price, tt.wantPrice)
  191. }
  192. })
  193. }
  194. }
  195. func TestExpandInstanceTypes(t *testing.T) {
  196. tests := []struct {
  197. name string
  198. instanceType string
  199. resourceGroup string
  200. want []string
  201. }{
  202. {
  203. name: "E2 CPU expands",
  204. instanceType: "e2",
  205. resourceGroup: "CPU",
  206. want: []string{"e2-micro", "e2-small", "e2-medium", "e2-standard", "e2-custom"},
  207. },
  208. {
  209. name: "E2 RAM expands",
  210. instanceType: "e2",
  211. resourceGroup: "RAM",
  212. want: []string{"e2-micro", "e2-small", "e2-medium", "e2-standard", "e2-custom"},
  213. },
  214. {
  215. name: "A2 CPU expands",
  216. instanceType: "a2",
  217. resourceGroup: "CPU",
  218. want: []string{"a2-highgpu", "a2-megagpu", "a2-ultragpu"},
  219. },
  220. {
  221. name: "A2 RAM expands",
  222. instanceType: "a2",
  223. resourceGroup: "RAM",
  224. want: []string{"a2-highgpu", "a2-megagpu", "a2-ultragpu"},
  225. },
  226. {
  227. name: "N2 does not expand",
  228. instanceType: "n2-standard",
  229. resourceGroup: "CPU",
  230. want: []string{"n2-standard"},
  231. },
  232. {
  233. name: "Custom does not expand",
  234. instanceType: "custom",
  235. resourceGroup: "CPU",
  236. want: []string{"custom"},
  237. },
  238. }
  239. for _, tt := range tests {
  240. t.Run(tt.name, func(t *testing.T) {
  241. source := &GCPPricingSource{}
  242. got := source.expandInstanceTypes(tt.instanceType, tt.resourceGroup)
  243. if len(got) != len(tt.want) {
  244. t.Errorf("expandInstanceTypes() returned %d types, want %d", len(got), len(tt.want))
  245. return
  246. }
  247. for i, wantType := range tt.want {
  248. if got[i] != wantType {
  249. t.Errorf("expandInstanceTypes()[%d] = %v, want %v", i, got[i], wantType)
  250. }
  251. }
  252. })
  253. }
  254. }
  255. func TestParsePage(t *testing.T) {
  256. tests := []struct {
  257. name string
  258. response GCPPricingResponse
  259. wantNextPageToken string
  260. wantErr bool
  261. checkCPUCosts bool
  262. checkRAMCosts bool
  263. checkVolumeCosts bool
  264. }{
  265. {
  266. name: "Valid compute SKU",
  267. response: GCPPricingResponse{
  268. Skus: []*GCPPricing{
  269. {
  270. Description: "N2 Instance Core running in Americas",
  271. Category: &GCPResourceInfo{
  272. ResourceGroup: "CPU",
  273. UsageType: "OnDemand",
  274. },
  275. ServiceRegions: []string{"us-central1"},
  276. PricingInfo: []*PricingInfo{
  277. {
  278. PricingExpression: &PricingExpression{
  279. TieredRates: []*TieredRates{
  280. {
  281. UnitPrice: &UnitPriceInfo{
  282. Units: "0",
  283. Nanos: 31611000,
  284. },
  285. },
  286. },
  287. },
  288. },
  289. },
  290. },
  291. },
  292. NextPageToken: "next-token-123",
  293. },
  294. wantNextPageToken: "next-token-123",
  295. wantErr: false,
  296. checkCPUCosts: true,
  297. },
  298. {
  299. name: "Valid storage SKU",
  300. response: GCPPricingResponse{
  301. Skus: []*GCPPricing{
  302. {
  303. Description: "SSD backed PD Capacity",
  304. Category: &GCPResourceInfo{
  305. ResourceGroup: "SSD",
  306. UsageType: "OnDemand",
  307. },
  308. ServiceRegions: []string{"us-central1"},
  309. PricingInfo: []*PricingInfo{
  310. {
  311. PricingExpression: &PricingExpression{
  312. TieredRates: []*TieredRates{
  313. {
  314. UnitPrice: &UnitPriceInfo{
  315. Units: "0",
  316. Nanos: 170000000,
  317. },
  318. },
  319. },
  320. },
  321. },
  322. },
  323. },
  324. },
  325. NextPageToken: "",
  326. },
  327. wantNextPageToken: "",
  328. wantErr: false,
  329. checkVolumeCosts: true,
  330. },
  331. {
  332. name: "SKU with no category",
  333. response: GCPPricingResponse{
  334. Skus: []*GCPPricing{
  335. {
  336. Description: "Some SKU",
  337. Category: nil,
  338. PricingInfo: []*PricingInfo{},
  339. },
  340. },
  341. NextPageToken: "",
  342. },
  343. wantNextPageToken: "",
  344. wantErr: false,
  345. },
  346. }
  347. for _, tt := range tests {
  348. t.Run(tt.name, func(t *testing.T) {
  349. source := &GCPPricingSource{
  350. config: GCPPricingSourceConfig{
  351. CurrencyCode: "USD",
  352. },
  353. }
  354. // Marshal response to JSON
  355. data, err := json.Marshal(tt.response)
  356. if err != nil {
  357. t.Fatalf("Failed to marshal test response: %v", err)
  358. }
  359. nodeCPUCosts := make(map[nodeKey]float64)
  360. nodeRAMCosts := make(map[nodeKey]float64)
  361. volumeCosts := make(map[volumeKey]float64)
  362. nextToken, err := source.parsePage(bytes.NewReader(data), nodeCPUCosts, nodeRAMCosts, volumeCosts)
  363. if (err != nil) != tt.wantErr {
  364. t.Errorf("parsePage() error = %v, wantErr %v", err, tt.wantErr)
  365. return
  366. }
  367. if nextToken != tt.wantNextPageToken {
  368. t.Errorf("parsePage() nextToken = %v, want %v", nextToken, tt.wantNextPageToken)
  369. }
  370. if tt.checkCPUCosts && len(nodeCPUCosts) == 0 {
  371. t.Error("parsePage() should have populated CPU costs")
  372. }
  373. if tt.checkRAMCosts && len(nodeRAMCosts) == 0 {
  374. t.Error("parsePage() should have populated RAM costs")
  375. }
  376. if tt.checkVolumeCosts && len(volumeCosts) == 0 {
  377. t.Error("parsePage() should have populated volume costs")
  378. }
  379. })
  380. }
  381. }
  382. func TestParseVolumeSKU(t *testing.T) {
  383. tests := []struct {
  384. name string
  385. sku *GCPPricing
  386. wantCosts int // Expected number of volume cost entries
  387. }{
  388. {
  389. name: "Valid PD-SSD",
  390. sku: &GCPPricing{
  391. Description: "SSD backed PD Capacity",
  392. Category: &GCPResourceInfo{
  393. ResourceGroup: "SSD",
  394. },
  395. ServiceRegions: []string{"us-central1", "us-east1"},
  396. PricingInfo: []*PricingInfo{
  397. {
  398. PricingExpression: &PricingExpression{
  399. TieredRates: []*TieredRates{
  400. {
  401. UnitPrice: &UnitPriceInfo{
  402. Units: "0",
  403. Nanos: 170000000,
  404. },
  405. },
  406. },
  407. },
  408. },
  409. },
  410. },
  411. wantCosts: 2, // Two regions
  412. },
  413. {
  414. name: "Regional disk",
  415. sku: &GCPPricing{
  416. Description: "Regional SSD backed PD Capacity",
  417. Category: &GCPResourceInfo{
  418. ResourceGroup: "SSD",
  419. },
  420. ServiceRegions: []string{"us-central1"},
  421. PricingInfo: []*PricingInfo{
  422. {
  423. PricingExpression: &PricingExpression{
  424. TieredRates: []*TieredRates{
  425. {
  426. UnitPrice: &UnitPriceInfo{
  427. Units: "0",
  428. Nanos: 204000000,
  429. },
  430. },
  431. },
  432. },
  433. },
  434. },
  435. },
  436. wantCosts: 1,
  437. },
  438. {
  439. name: "Unknown volume type",
  440. sku: &GCPPricing{
  441. Description: "Unknown Disk Type",
  442. Category: &GCPResourceInfo{
  443. ResourceGroup: "UnknownType",
  444. },
  445. ServiceRegions: []string{"us-central1"},
  446. PricingInfo: []*PricingInfo{
  447. {
  448. PricingExpression: &PricingExpression{
  449. TieredRates: []*TieredRates{
  450. {
  451. UnitPrice: &UnitPriceInfo{
  452. Units: "0",
  453. Nanos: 100000000,
  454. },
  455. },
  456. },
  457. },
  458. },
  459. },
  460. },
  461. wantCosts: 0, // Should not add unknown types
  462. },
  463. }
  464. for _, tt := range tests {
  465. t.Run(tt.name, func(t *testing.T) {
  466. source := &GCPPricingSource{}
  467. volumeCosts := make(map[volumeKey]float64)
  468. source.parseVolumeSKU(tt.sku, volumeCosts)
  469. if len(volumeCosts) != tt.wantCosts {
  470. t.Errorf("parseVolumeSKU() added %d costs, want %d", len(volumeCosts), tt.wantCosts)
  471. }
  472. })
  473. }
  474. }
  475. func TestParseComputeSKU(t *testing.T) {
  476. tests := []struct {
  477. name string
  478. sku *GCPPricing
  479. usageType string
  480. wantCPUCosts int
  481. wantRAMCosts int
  482. }{
  483. {
  484. name: "CPU SKU",
  485. sku: &GCPPricing{
  486. Description: "N2 Instance Core running in Americas",
  487. Category: &GCPResourceInfo{
  488. ResourceGroup: "CPU",
  489. },
  490. ServiceRegions: []string{"us-central1"},
  491. PricingInfo: []*PricingInfo{
  492. {
  493. PricingExpression: &PricingExpression{
  494. TieredRates: []*TieredRates{
  495. {
  496. UnitPrice: &UnitPriceInfo{
  497. Units: "0",
  498. Nanos: 31611000,
  499. },
  500. },
  501. },
  502. },
  503. },
  504. },
  505. },
  506. usageType: "ondemand",
  507. wantCPUCosts: 1,
  508. wantRAMCosts: 0,
  509. },
  510. {
  511. name: "RAM SKU",
  512. sku: &GCPPricing{
  513. Description: "N2 Instance RAM running in Americas",
  514. Category: &GCPResourceInfo{
  515. ResourceGroup: "RAM",
  516. },
  517. ServiceRegions: []string{"us-central1"},
  518. PricingInfo: []*PricingInfo{
  519. {
  520. PricingExpression: &PricingExpression{
  521. TieredRates: []*TieredRates{
  522. {
  523. UnitPrice: &UnitPriceInfo{
  524. Units: "0",
  525. Nanos: 4237000,
  526. },
  527. },
  528. },
  529. },
  530. },
  531. },
  532. },
  533. usageType: "ondemand",
  534. wantCPUCosts: 0,
  535. wantRAMCosts: 1,
  536. },
  537. {
  538. name: "E2 CPU expands to multiple types",
  539. sku: &GCPPricing{
  540. Description: "E2 Instance Core running in Americas",
  541. Category: &GCPResourceInfo{
  542. ResourceGroup: "CPU",
  543. },
  544. ServiceRegions: []string{"us-central1"},
  545. PricingInfo: []*PricingInfo{
  546. {
  547. PricingExpression: &PricingExpression{
  548. TieredRates: []*TieredRates{
  549. {
  550. UnitPrice: &UnitPriceInfo{
  551. Units: "0",
  552. Nanos: 21811000,
  553. },
  554. },
  555. },
  556. },
  557. },
  558. },
  559. },
  560. usageType: "ondemand",
  561. wantCPUCosts: 5, // e2-micro, e2-small, e2-medium, e2-standard, e2-custom
  562. wantRAMCosts: 0,
  563. },
  564. }
  565. for _, tt := range tests {
  566. t.Run(tt.name, func(t *testing.T) {
  567. source := &GCPPricingSource{}
  568. nodeCPUCosts := make(map[nodeKey]float64)
  569. nodeRAMCosts := make(map[nodeKey]float64)
  570. source.parseComputeSKU(tt.sku, tt.usageType, nodeCPUCosts, nodeRAMCosts)
  571. if len(nodeCPUCosts) != tt.wantCPUCosts {
  572. t.Errorf("parseComputeSKU() added %d CPU costs, want %d", len(nodeCPUCosts), tt.wantCPUCosts)
  573. }
  574. if len(nodeRAMCosts) != tt.wantRAMCosts {
  575. t.Errorf("parseComputeSKU() added %d RAM costs, want %d", len(nodeRAMCosts), tt.wantRAMCosts)
  576. }
  577. })
  578. }
  579. }
  580. func TestBuildNodePricing(t *testing.T) {
  581. tests := []struct {
  582. name string
  583. cpuCosts map[nodeKey]float64
  584. ramCosts map[nodeKey]float64
  585. wantNodes int
  586. currencyCode string
  587. }{
  588. {
  589. name: "Complete node with CPU and RAM",
  590. cpuCosts: map[nodeKey]float64{
  591. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  592. },
  593. ramCosts: map[nodeKey]float64{
  594. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  595. },
  596. wantNodes: 1,
  597. currencyCode: "USD",
  598. },
  599. {
  600. name: "Missing RAM cost",
  601. cpuCosts: map[nodeKey]float64{
  602. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  603. },
  604. ramCosts: map[nodeKey]float64{},
  605. wantNodes: 0,
  606. currencyCode: "USD",
  607. },
  608. {
  609. name: "Missing CPU cost",
  610. cpuCosts: map[nodeKey]float64{},
  611. ramCosts: map[nodeKey]float64{
  612. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  613. },
  614. wantNodes: 0,
  615. currencyCode: "USD",
  616. },
  617. {
  618. name: "Preemptible instance - excluded",
  619. cpuCosts: map[nodeKey]float64{
  620. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.007583,
  621. },
  622. ramCosts: map[nodeKey]float64{
  623. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.001017,
  624. },
  625. wantNodes: 0,
  626. currencyCode: "USD",
  627. },
  628. {
  629. name: "Multiple regions",
  630. cpuCosts: map[nodeKey]float64{
  631. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  632. {Region: "us-east1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  633. },
  634. ramCosts: map[nodeKey]float64{
  635. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  636. {Region: "us-east1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  637. },
  638. wantNodes: 2,
  639. currencyCode: "USD",
  640. },
  641. }
  642. for _, tt := range tests {
  643. t.Run(tt.name, func(t *testing.T) {
  644. source := &GCPPricingSource{
  645. config: GCPPricingSourceConfig{
  646. CurrencyCode: tt.currencyCode,
  647. },
  648. }
  649. ps := &pricing.PricingSet{
  650. NodePricing: []*pricing.NodePricing{},
  651. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  652. }
  653. source.buildNodePricing(ps, tt.cpuCosts, tt.ramCosts)
  654. if len(ps.NodePricing) != tt.wantNodes {
  655. t.Errorf("buildNodePricing() created %d nodes, want %d", len(ps.NodePricing), tt.wantNodes)
  656. }
  657. })
  658. }
  659. }
  660. func TestBuildVolumePricing(t *testing.T) {
  661. tests := []struct {
  662. name string
  663. volumeCosts map[volumeKey]float64
  664. wantVolumes int
  665. currencyCode string
  666. }{
  667. {
  668. name: "Single volume type",
  669. volumeCosts: map[volumeKey]float64{
  670. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  671. },
  672. wantVolumes: 1,
  673. currencyCode: "USD",
  674. },
  675. {
  676. name: "Multiple volume types",
  677. volumeCosts: map[volumeKey]float64{
  678. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  679. {Region: "us-central1", VolumeType: pricing.VolumeTypePDStandard, Regional: false}: 0.000054795,
  680. },
  681. wantVolumes: 2,
  682. currencyCode: "USD",
  683. },
  684. {
  685. name: "Regional and zonal",
  686. volumeCosts: map[volumeKey]float64{
  687. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  688. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: true}: 0.000279452,
  689. },
  690. wantVolumes: 2,
  691. currencyCode: "USD",
  692. },
  693. }
  694. for _, tt := range tests {
  695. t.Run(tt.name, func(t *testing.T) {
  696. source := &GCPPricingSource{
  697. config: GCPPricingSourceConfig{
  698. CurrencyCode: tt.currencyCode,
  699. },
  700. }
  701. ps := &pricing.PricingSet{
  702. NodePricing: []*pricing.NodePricing{},
  703. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  704. }
  705. source.buildVolumePricing(ps, tt.volumeCosts)
  706. if len(ps.PersistentVolumePricing) != tt.wantVolumes {
  707. t.Errorf("buildVolumePricing() created %d volumes, want %d", len(ps.PersistentVolumePricing), tt.wantVolumes)
  708. }
  709. })
  710. }
  711. }
  712. func TestGetPricing_Integration(t *testing.T) {
  713. // Serve one page of pricing: one N2 CPU SKU, one N2 RAM SKU, one SSD volume SKU.
  714. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  715. response := GCPPricingResponse{
  716. Skus: []*GCPPricing{
  717. {
  718. Description: "N2 Instance Core running in Americas",
  719. Category: &GCPResourceInfo{
  720. ResourceGroup: "CPU",
  721. UsageType: "OnDemand",
  722. },
  723. ServiceRegions: []string{"us-central1"},
  724. PricingInfo: []*PricingInfo{
  725. {
  726. PricingExpression: &PricingExpression{
  727. TieredRates: []*TieredRates{
  728. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 31611000}},
  729. },
  730. },
  731. },
  732. },
  733. },
  734. {
  735. Description: "N2 Instance RAM running in Americas",
  736. Category: &GCPResourceInfo{
  737. ResourceGroup: "RAM",
  738. UsageType: "OnDemand",
  739. },
  740. ServiceRegions: []string{"us-central1"},
  741. PricingInfo: []*PricingInfo{
  742. {
  743. PricingExpression: &PricingExpression{
  744. TieredRates: []*TieredRates{
  745. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 4237000}},
  746. },
  747. },
  748. },
  749. },
  750. },
  751. {
  752. Description: "SSD backed PD Capacity in Americas",
  753. Category: &GCPResourceInfo{
  754. ResourceGroup: "SSD",
  755. UsageType: "OnDemand",
  756. },
  757. ServiceRegions: []string{"us-central1"},
  758. PricingInfo: []*PricingInfo{
  759. {
  760. PricingExpression: &PricingExpression{
  761. TieredRates: []*TieredRates{
  762. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 170000000}},
  763. },
  764. },
  765. },
  766. },
  767. },
  768. },
  769. NextPageToken: "",
  770. }
  771. w.Header().Set("Content-Type", "application/json")
  772. _ = json.NewEncoder(w).Encode(response)
  773. }))
  774. defer server.Close()
  775. // Point the package-level base URL at the test server.
  776. originalURL := BillingAPIBaseURL
  777. BillingAPIBaseURL = server.URL
  778. defer func() { BillingAPIBaseURL = originalURL }()
  779. // Use the test server's own client so TLS/redirect rules match.
  780. originalClient := gcpHTTPClient
  781. gcpHTTPClient = server.Client()
  782. defer func() { gcpHTTPClient = originalClient }()
  783. source := NewGCPPricingSource(GCPPricingSourceConfig{
  784. APIKey: "test-key",
  785. CurrencyCode: "USD",
  786. })
  787. ps, err := source.GetPricing()
  788. if err != nil {
  789. t.Fatalf("GetPricing() unexpected error: %v", err)
  790. }
  791. if len(ps.NodePricing) != 1 {
  792. t.Errorf("NodePricing len = %d, want 1", len(ps.NodePricing))
  793. }
  794. if len(ps.PersistentVolumePricing) != 1 {
  795. t.Errorf("PersistentVolumePricing len = %d, want 1", len(ps.PersistentVolumePricing))
  796. }
  797. node := ps.NodePricing[0]
  798. if node.Properties.Region != "us-central1" {
  799. t.Errorf("NodePricing region = %q, want us-central1", node.Properties.Region)
  800. }
  801. if node.Properties.InstanceType != "n2-standard" {
  802. t.Errorf("NodePricing instance type = %q, want n2-standard", node.Properties.InstanceType)
  803. }
  804. }
  805. func TestMapGCPVolumeType(t *testing.T) {
  806. tests := []struct {
  807. name string
  808. resourceGroup string
  809. description string
  810. wantType pricing.VolumeType
  811. wantRegional bool
  812. }{
  813. {
  814. name: "PD-SSD zonal",
  815. resourceGroup: "SSD",
  816. description: "SSD backed PD Capacity",
  817. wantType: pricing.VolumeTypePDSSD,
  818. wantRegional: false,
  819. },
  820. {
  821. name: "PD-SSD regional",
  822. resourceGroup: "SSD",
  823. description: "Regional SSD backed PD Capacity",
  824. wantType: pricing.VolumeTypePDSSD,
  825. wantRegional: true,
  826. },
  827. {
  828. name: "PD-Standard",
  829. resourceGroup: "PDStandard",
  830. description: "Storage PD Capacity",
  831. wantType: pricing.VolumeTypePDStandard,
  832. wantRegional: false,
  833. },
  834. {
  835. name: "PD-Balanced",
  836. resourceGroup: "PDBalanced",
  837. description: "Balanced PD Capacity",
  838. wantType: pricing.VolumeTypePDBalanced,
  839. wantRegional: false,
  840. },
  841. {
  842. name: "PD-Extreme",
  843. resourceGroup: "PDExtreme",
  844. description: "Extreme PD Capacity",
  845. wantType: pricing.VolumeTypePDExtreme,
  846. wantRegional: false,
  847. },
  848. {
  849. name: "Hyperdisk Balanced",
  850. resourceGroup: "HyperdiskBalanced",
  851. description: "Hyperdisk Balanced",
  852. wantType: pricing.VolumeTypeHyperdiskBalanced,
  853. wantRegional: false,
  854. },
  855. {
  856. name: "Hyperdisk Extreme",
  857. resourceGroup: "HyperdiskExtreme",
  858. description: "Hyperdisk Extreme",
  859. wantType: pricing.VolumeTypeHyperdiskExtreme,
  860. wantRegional: false,
  861. },
  862. {
  863. name: "Hyperdisk Throughput",
  864. resourceGroup: "HyperdiskThroughput",
  865. description: "Hyperdisk Throughput",
  866. wantType: pricing.VolumeTypeHyperdiskThroughput,
  867. wantRegional: false,
  868. },
  869. {
  870. name: "Unknown type",
  871. resourceGroup: "UnknownDisk",
  872. description: "Some unknown disk",
  873. wantType: pricing.VolumeTypeNil,
  874. wantRegional: false,
  875. },
  876. }
  877. for _, tt := range tests {
  878. t.Run(tt.name, func(t *testing.T) {
  879. gotType, gotRegional := mapGCPVolumeType(tt.resourceGroup, tt.description)
  880. if gotType != tt.wantType {
  881. t.Errorf("mapGCPVolumeType() type = %v, want %v", gotType, tt.wantType)
  882. }
  883. if gotRegional != tt.wantRegional {
  884. t.Errorf("mapGCPVolumeType() regional = %v, want %v", gotRegional, tt.wantRegional)
  885. }
  886. })
  887. }
  888. }
  889. func TestNormalizeInstanceType(t *testing.T) {
  890. tests := []struct {
  891. name string
  892. resourceGroup string
  893. description string
  894. want string
  895. }{
  896. {
  897. name: "Custom instance",
  898. resourceGroup: "CPU",
  899. description: "Custom Instance Core running in Americas",
  900. want: "custom",
  901. },
  902. {
  903. name: "N2 standard",
  904. resourceGroup: "RAM",
  905. description: "N2 Instance Ram running in Americas",
  906. want: "n2-standard",
  907. },
  908. {
  909. name: "N2D AMD",
  910. resourceGroup: "CPU",
  911. description: "N2D AMD Instance Core running in Americas",
  912. want: "n2d-standard",
  913. },
  914. {
  915. name: "N4 instance",
  916. resourceGroup: "CPU",
  917. description: "N4 Instance Core running in Americas",
  918. want: "n4-standard",
  919. },
  920. {
  921. name: "A2 instance",
  922. resourceGroup: "RAM",
  923. description: "A2 Instance Ram running in Americas",
  924. want: "a2",
  925. },
  926. {
  927. name: "C2 compute optimized",
  928. resourceGroup: "CPU",
  929. description: "Compute Optimized Core running in Americas",
  930. want: "c2-standard",
  931. },
  932. {
  933. name: "E2 instance",
  934. resourceGroup: "CPU",
  935. description: "E2 Instance Core running in Americas",
  936. want: "e2",
  937. },
  938. {
  939. name: "T2D AMD",
  940. resourceGroup: "RAM",
  941. description: "T2D AMD Instance Ram running in Americas",
  942. want: "t2d-standard",
  943. },
  944. {
  945. name: "T2A ARM",
  946. resourceGroup: "CPU",
  947. description: "T2A ARM Instance Core running in Americas",
  948. want: "t2a-standard",
  949. },
  950. {
  951. name: "Unknown type defaults to resource group",
  952. resourceGroup: "SomeType",
  953. description: "Some Instance Type",
  954. want: "sometype",
  955. },
  956. }
  957. for _, tt := range tests {
  958. t.Run(tt.name, func(t *testing.T) {
  959. got := normalizeInstanceType(tt.resourceGroup, tt.description)
  960. if got != tt.want {
  961. t.Errorf("normalizeInstanceType() = %v, want %v", got, tt.want)
  962. }
  963. })
  964. }
  965. }
  966. func TestIsComputeResource(t *testing.T) {
  967. tests := []struct {
  968. name string
  969. resourceGroup string
  970. want bool
  971. }{
  972. {
  973. name: "CPU is compute",
  974. resourceGroup: "CPU",
  975. want: true,
  976. },
  977. {
  978. name: "RAM is compute",
  979. resourceGroup: "RAM",
  980. want: true,
  981. },
  982. {
  983. name: "cpu lowercase is compute",
  984. resourceGroup: "cpu",
  985. want: true,
  986. },
  987. {
  988. name: "ram lowercase is compute",
  989. resourceGroup: "ram",
  990. want: true,
  991. },
  992. {
  993. name: "SSD is not compute",
  994. resourceGroup: "SSD",
  995. want: false,
  996. },
  997. {
  998. name: "GPU is not compute",
  999. resourceGroup: "GPU",
  1000. want: false,
  1001. },
  1002. }
  1003. for _, tt := range tests {
  1004. t.Run(tt.name, func(t *testing.T) {
  1005. got := isComputeResource(tt.resourceGroup)
  1006. if got != tt.want {
  1007. t.Errorf("isComputeResource() = %v, want %v", got, tt.want)
  1008. }
  1009. })
  1010. }
  1011. }
  1012. func TestIsStorageResource(t *testing.T) {
  1013. tests := []struct {
  1014. name string
  1015. resourceGroup string
  1016. want bool
  1017. }{
  1018. {
  1019. name: "SSD is storage",
  1020. resourceGroup: "SSD",
  1021. want: true,
  1022. },
  1023. {
  1024. name: "PDStandard is storage",
  1025. resourceGroup: "PDStandard",
  1026. want: true,
  1027. },
  1028. {
  1029. name: "PDBalanced is storage",
  1030. resourceGroup: "PDBalanced",
  1031. want: true,
  1032. },
  1033. {
  1034. name: "PDExtreme is storage",
  1035. resourceGroup: "PDExtreme",
  1036. want: true,
  1037. },
  1038. {
  1039. name: "HyperdiskBalanced is storage",
  1040. resourceGroup: "HyperdiskBalanced",
  1041. want: true,
  1042. },
  1043. {
  1044. name: "HyperdiskExtreme is storage",
  1045. resourceGroup: "HyperdiskExtreme",
  1046. want: true,
  1047. },
  1048. {
  1049. name: "HyperdiskThroughput is storage",
  1050. resourceGroup: "HyperdiskThroughput",
  1051. want: true,
  1052. },
  1053. {
  1054. name: "CPU is not storage",
  1055. resourceGroup: "CPU",
  1056. want: false,
  1057. },
  1058. {
  1059. name: "RAM is not storage",
  1060. resourceGroup: "RAM",
  1061. want: false,
  1062. },
  1063. }
  1064. for _, tt := range tests {
  1065. t.Run(tt.name, func(t *testing.T) {
  1066. got := isStorageResource(tt.resourceGroup)
  1067. if got != tt.want {
  1068. t.Errorf("isStorageResource() = %v, want %v", got, tt.want)
  1069. }
  1070. })
  1071. }
  1072. }
  1073. // Helper function to check if a string contains a substring
  1074. func contains(s, substr string) bool {
  1075. return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
  1076. (len(s) > 0 && (s[0:len(substr)] == substr || contains(s[1:], substr))))
  1077. }
  1078. // Made with Bob