gcppricingsource_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. name: "Commitment SKU is skipped",
  348. response: GCPPricingResponse{
  349. Skus: []*GCPPricing{
  350. {
  351. Description: "Commitment v1: T2D AMD CPU running in Americas for 1 Year",
  352. Category: &GCPResourceInfo{
  353. ResourceGroup: "CPU",
  354. UsageType: "OnDemand",
  355. },
  356. ServiceRegions: []string{"us-central1"},
  357. PricingInfo: []*PricingInfo{
  358. {
  359. PricingExpression: &PricingExpression{
  360. TieredRates: []*TieredRates{
  361. {
  362. UnitPrice: &UnitPriceInfo{
  363. Units: "0",
  364. Nanos: 19801600,
  365. },
  366. },
  367. },
  368. },
  369. },
  370. },
  371. },
  372. },
  373. NextPageToken: "",
  374. },
  375. wantNextPageToken: "",
  376. wantErr: false,
  377. },
  378. }
  379. for _, tt := range tests {
  380. t.Run(tt.name, func(t *testing.T) {
  381. source := &GCPPricingSource{
  382. config: GCPPricingSourceConfig{
  383. CurrencyCode: "USD",
  384. },
  385. }
  386. // Marshal response to JSON
  387. data, err := json.Marshal(tt.response)
  388. if err != nil {
  389. t.Fatalf("Failed to marshal test response: %v", err)
  390. }
  391. nodeCPUCosts := make(map[nodeKey]float64)
  392. nodeRAMCosts := make(map[nodeKey]float64)
  393. nodeGPUCosts := make(map[gpuKey]float64)
  394. volumeCosts := make(map[volumeKey]float64)
  395. nextToken, err := source.parsePage(bytes.NewReader(data), nodeCPUCosts, nodeRAMCosts, nodeGPUCosts, volumeCosts)
  396. if (err != nil) != tt.wantErr {
  397. t.Errorf("parsePage() error = %v, wantErr %v", err, tt.wantErr)
  398. return
  399. }
  400. if nextToken != tt.wantNextPageToken {
  401. t.Errorf("parsePage() nextToken = %v, want %v", nextToken, tt.wantNextPageToken)
  402. }
  403. if tt.checkCPUCosts && len(nodeCPUCosts) == 0 {
  404. t.Error("parsePage() should have populated CPU costs")
  405. }
  406. if tt.checkRAMCosts && len(nodeRAMCosts) == 0 {
  407. t.Error("parsePage() should have populated RAM costs")
  408. }
  409. if tt.checkVolumeCosts && len(volumeCosts) == 0 {
  410. t.Error("parsePage() should have populated volume costs")
  411. }
  412. })
  413. }
  414. }
  415. func TestParseVolumeSKU(t *testing.T) {
  416. tests := []struct {
  417. name string
  418. sku *GCPPricing
  419. wantCosts int // Expected number of volume cost entries
  420. }{
  421. {
  422. name: "Valid PD-SSD",
  423. sku: &GCPPricing{
  424. Description: "SSD backed PD Capacity",
  425. Category: &GCPResourceInfo{
  426. ResourceGroup: "SSD",
  427. },
  428. ServiceRegions: []string{"us-central1", "us-east1"},
  429. PricingInfo: []*PricingInfo{
  430. {
  431. PricingExpression: &PricingExpression{
  432. TieredRates: []*TieredRates{
  433. {
  434. UnitPrice: &UnitPriceInfo{
  435. Units: "0",
  436. Nanos: 170000000,
  437. },
  438. },
  439. },
  440. },
  441. },
  442. },
  443. },
  444. wantCosts: 2, // Two regions
  445. },
  446. {
  447. name: "Regional disk",
  448. sku: &GCPPricing{
  449. Description: "Regional SSD backed PD Capacity",
  450. Category: &GCPResourceInfo{
  451. ResourceGroup: "SSD",
  452. },
  453. ServiceRegions: []string{"us-central1"},
  454. PricingInfo: []*PricingInfo{
  455. {
  456. PricingExpression: &PricingExpression{
  457. TieredRates: []*TieredRates{
  458. {
  459. UnitPrice: &UnitPriceInfo{
  460. Units: "0",
  461. Nanos: 204000000,
  462. },
  463. },
  464. },
  465. },
  466. },
  467. },
  468. },
  469. wantCosts: 1,
  470. },
  471. {
  472. name: "Unknown volume type",
  473. sku: &GCPPricing{
  474. Description: "Unknown Disk Type",
  475. Category: &GCPResourceInfo{
  476. ResourceGroup: "UnknownType",
  477. },
  478. ServiceRegions: []string{"us-central1"},
  479. PricingInfo: []*PricingInfo{
  480. {
  481. PricingExpression: &PricingExpression{
  482. TieredRates: []*TieredRates{
  483. {
  484. UnitPrice: &UnitPriceInfo{
  485. Units: "0",
  486. Nanos: 100000000,
  487. },
  488. },
  489. },
  490. },
  491. },
  492. },
  493. },
  494. wantCosts: 0, // Should not add unknown types
  495. },
  496. }
  497. for _, tt := range tests {
  498. t.Run(tt.name, func(t *testing.T) {
  499. source := &GCPPricingSource{}
  500. volumeCosts := make(map[volumeKey]float64)
  501. source.parseVolumeSKU(tt.sku, volumeCosts)
  502. if len(volumeCosts) != tt.wantCosts {
  503. t.Errorf("parseVolumeSKU() added %d costs, want %d", len(volumeCosts), tt.wantCosts)
  504. }
  505. })
  506. }
  507. }
  508. func TestParseComputeSKU(t *testing.T) {
  509. tests := []struct {
  510. name string
  511. sku *GCPPricing
  512. usageType string
  513. wantCPUCosts int
  514. wantRAMCosts int
  515. }{
  516. {
  517. name: "CPU SKU",
  518. sku: &GCPPricing{
  519. Description: "N2 Instance Core running in Americas",
  520. Category: &GCPResourceInfo{
  521. ResourceGroup: "CPU",
  522. },
  523. ServiceRegions: []string{"us-central1"},
  524. PricingInfo: []*PricingInfo{
  525. {
  526. PricingExpression: &PricingExpression{
  527. TieredRates: []*TieredRates{
  528. {
  529. UnitPrice: &UnitPriceInfo{
  530. Units: "0",
  531. Nanos: 31611000,
  532. },
  533. },
  534. },
  535. },
  536. },
  537. },
  538. },
  539. usageType: "ondemand",
  540. wantCPUCosts: 1,
  541. wantRAMCosts: 0,
  542. },
  543. {
  544. name: "RAM SKU",
  545. sku: &GCPPricing{
  546. Description: "N2 Instance RAM running in Americas",
  547. Category: &GCPResourceInfo{
  548. ResourceGroup: "RAM",
  549. },
  550. ServiceRegions: []string{"us-central1"},
  551. PricingInfo: []*PricingInfo{
  552. {
  553. PricingExpression: &PricingExpression{
  554. TieredRates: []*TieredRates{
  555. {
  556. UnitPrice: &UnitPriceInfo{
  557. Units: "0",
  558. Nanos: 4237000,
  559. },
  560. },
  561. },
  562. },
  563. },
  564. },
  565. },
  566. usageType: "ondemand",
  567. wantCPUCosts: 0,
  568. wantRAMCosts: 1,
  569. },
  570. {
  571. name: "E2 CPU expands to multiple types",
  572. sku: &GCPPricing{
  573. Description: "E2 Instance Core running in Americas",
  574. Category: &GCPResourceInfo{
  575. ResourceGroup: "CPU",
  576. },
  577. ServiceRegions: []string{"us-central1"},
  578. PricingInfo: []*PricingInfo{
  579. {
  580. PricingExpression: &PricingExpression{
  581. TieredRates: []*TieredRates{
  582. {
  583. UnitPrice: &UnitPriceInfo{
  584. Units: "0",
  585. Nanos: 21811000,
  586. },
  587. },
  588. },
  589. },
  590. },
  591. },
  592. },
  593. usageType: "ondemand",
  594. wantCPUCosts: 5, // e2-micro, e2-small, e2-medium, e2-standard, e2-custom
  595. wantRAMCosts: 0,
  596. },
  597. }
  598. for _, tt := range tests {
  599. t.Run(tt.name, func(t *testing.T) {
  600. source := &GCPPricingSource{}
  601. nodeCPUCosts := make(map[nodeKey]float64)
  602. nodeRAMCosts := make(map[nodeKey]float64)
  603. source.parseComputeSKU(tt.sku, tt.usageType, nodeCPUCosts, nodeRAMCosts)
  604. if len(nodeCPUCosts) != tt.wantCPUCosts {
  605. t.Errorf("parseComputeSKU() added %d CPU costs, want %d", len(nodeCPUCosts), tt.wantCPUCosts)
  606. }
  607. if len(nodeRAMCosts) != tt.wantRAMCosts {
  608. t.Errorf("parseComputeSKU() added %d RAM costs, want %d", len(nodeRAMCosts), tt.wantRAMCosts)
  609. }
  610. })
  611. }
  612. }
  613. func TestBuildNodePricing(t *testing.T) {
  614. tests := []struct {
  615. name string
  616. cpuCosts map[nodeKey]float64
  617. ramCosts map[nodeKey]float64
  618. wantNodes int
  619. currencyCode string
  620. }{
  621. {
  622. name: "Complete node with CPU and RAM",
  623. cpuCosts: map[nodeKey]float64{
  624. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  625. },
  626. ramCosts: map[nodeKey]float64{
  627. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  628. },
  629. wantNodes: 1,
  630. currencyCode: "USD",
  631. },
  632. {
  633. name: "Missing RAM cost",
  634. cpuCosts: map[nodeKey]float64{
  635. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  636. },
  637. ramCosts: map[nodeKey]float64{},
  638. wantNodes: 0,
  639. currencyCode: "USD",
  640. },
  641. {
  642. name: "Missing CPU cost",
  643. cpuCosts: map[nodeKey]float64{},
  644. ramCosts: map[nodeKey]float64{
  645. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  646. },
  647. wantNodes: 0,
  648. currencyCode: "USD",
  649. },
  650. {
  651. name: "Preemptible instance - included as spot",
  652. cpuCosts: map[nodeKey]float64{
  653. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.007583,
  654. },
  655. ramCosts: map[nodeKey]float64{
  656. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.001017,
  657. },
  658. wantNodes: 1,
  659. currencyCode: "USD",
  660. },
  661. {
  662. name: "Multiple regions",
  663. cpuCosts: map[nodeKey]float64{
  664. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  665. {Region: "us-east1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  666. },
  667. ramCosts: map[nodeKey]float64{
  668. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  669. {Region: "us-east1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  670. },
  671. wantNodes: 2,
  672. currencyCode: "USD",
  673. },
  674. }
  675. for _, tt := range tests {
  676. t.Run(tt.name, func(t *testing.T) {
  677. source := &GCPPricingSource{
  678. config: GCPPricingSourceConfig{
  679. CurrencyCode: tt.currencyCode,
  680. },
  681. }
  682. ps := &pricing.PricingSet{
  683. NodePricing: []*pricing.NodePricing{},
  684. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  685. }
  686. source.buildNodePricing(ps, tt.cpuCosts, tt.ramCosts, map[gpuKey]float64{})
  687. if len(ps.NodePricing) != tt.wantNodes {
  688. t.Errorf("buildNodePricing() created %d nodes, want %d", len(ps.NodePricing), tt.wantNodes)
  689. }
  690. })
  691. }
  692. }
  693. func TestBuildNodePricing_SpotProvisioning(t *testing.T) {
  694. source := &GCPPricingSource{
  695. config: GCPPricingSourceConfig{
  696. CurrencyCode: "USD",
  697. },
  698. }
  699. ps := &pricing.PricingSet{
  700. NodePricing: []*pricing.NodePricing{},
  701. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  702. }
  703. cpuCosts := map[nodeKey]float64{
  704. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  705. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.007583,
  706. }
  707. ramCosts := map[nodeKey]float64{
  708. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  709. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.001017,
  710. }
  711. source.buildNodePricing(ps, cpuCosts, ramCosts, map[gpuKey]float64{})
  712. if len(ps.NodePricing) != 2 {
  713. t.Fatalf("buildNodePricing() created %d nodes, want 2", len(ps.NodePricing))
  714. }
  715. var sawOnDemand, sawSpot bool
  716. for _, np := range ps.NodePricing {
  717. switch np.Properties.Provisioning {
  718. case pricing.ProvisioningOnDemand:
  719. sawOnDemand = true
  720. if np.Prices[pricing.ResourceCPU].Price != 0.031611 {
  721. t.Errorf("on-demand CPU price = %v, want 0.031611", np.Prices[pricing.ResourceCPU].Price)
  722. }
  723. case pricing.ProvisioningSpot:
  724. sawSpot = true
  725. if np.Prices[pricing.ResourceCPU].Price != 0.007583 {
  726. t.Errorf("spot CPU price = %v, want 0.007583", np.Prices[pricing.ResourceCPU].Price)
  727. }
  728. default:
  729. t.Errorf("unexpected provisioning type: %v", np.Properties.Provisioning)
  730. }
  731. }
  732. if !sawOnDemand {
  733. t.Error("expected an on-demand node pricing entry")
  734. }
  735. if !sawSpot {
  736. t.Error("expected a spot node pricing entry")
  737. }
  738. }
  739. func TestBuildVolumePricing(t *testing.T) {
  740. tests := []struct {
  741. name string
  742. volumeCosts map[volumeKey]float64
  743. wantVolumes int
  744. currencyCode string
  745. }{
  746. {
  747. name: "Single volume type",
  748. volumeCosts: map[volumeKey]float64{
  749. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  750. },
  751. wantVolumes: 1,
  752. currencyCode: "USD",
  753. },
  754. {
  755. name: "Multiple volume types",
  756. volumeCosts: map[volumeKey]float64{
  757. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  758. {Region: "us-central1", VolumeType: pricing.VolumeTypePDStandard, Regional: false}: 0.000054795,
  759. },
  760. wantVolumes: 2,
  761. currencyCode: "USD",
  762. },
  763. {
  764. name: "Regional and zonal",
  765. volumeCosts: map[volumeKey]float64{
  766. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  767. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: true}: 0.000279452,
  768. },
  769. wantVolumes: 2,
  770. currencyCode: "USD",
  771. },
  772. }
  773. for _, tt := range tests {
  774. t.Run(tt.name, func(t *testing.T) {
  775. source := &GCPPricingSource{
  776. config: GCPPricingSourceConfig{
  777. CurrencyCode: tt.currencyCode,
  778. },
  779. }
  780. ps := &pricing.PricingSet{
  781. NodePricing: []*pricing.NodePricing{},
  782. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  783. }
  784. source.buildVolumePricing(ps, tt.volumeCosts)
  785. if len(ps.PersistentVolumePricing) != tt.wantVolumes {
  786. t.Errorf("buildVolumePricing() created %d volumes, want %d", len(ps.PersistentVolumePricing), tt.wantVolumes)
  787. }
  788. })
  789. }
  790. }
  791. func TestGetPricing_Integration(t *testing.T) {
  792. // Serve one page of pricing: one N2 CPU SKU, one N2 RAM SKU, one SSD volume SKU.
  793. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  794. response := GCPPricingResponse{
  795. Skus: []*GCPPricing{
  796. {
  797. Description: "N2 Instance Core running in Americas",
  798. Category: &GCPResourceInfo{
  799. ResourceGroup: "CPU",
  800. UsageType: "OnDemand",
  801. },
  802. ServiceRegions: []string{"us-central1"},
  803. PricingInfo: []*PricingInfo{
  804. {
  805. PricingExpression: &PricingExpression{
  806. TieredRates: []*TieredRates{
  807. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 31611000}},
  808. },
  809. },
  810. },
  811. },
  812. },
  813. {
  814. Description: "N2 Instance RAM running in Americas",
  815. Category: &GCPResourceInfo{
  816. ResourceGroup: "RAM",
  817. UsageType: "OnDemand",
  818. },
  819. ServiceRegions: []string{"us-central1"},
  820. PricingInfo: []*PricingInfo{
  821. {
  822. PricingExpression: &PricingExpression{
  823. TieredRates: []*TieredRates{
  824. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 4237000}},
  825. },
  826. },
  827. },
  828. },
  829. },
  830. {
  831. Description: "SSD backed PD Capacity in Americas",
  832. Category: &GCPResourceInfo{
  833. ResourceGroup: "SSD",
  834. UsageType: "OnDemand",
  835. },
  836. ServiceRegions: []string{"us-central1"},
  837. PricingInfo: []*PricingInfo{
  838. {
  839. PricingExpression: &PricingExpression{
  840. TieredRates: []*TieredRates{
  841. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 170000000}},
  842. },
  843. },
  844. },
  845. },
  846. },
  847. },
  848. NextPageToken: "",
  849. }
  850. w.Header().Set("Content-Type", "application/json")
  851. _ = json.NewEncoder(w).Encode(response)
  852. }))
  853. defer server.Close()
  854. // Point the package-level base URL at the test server.
  855. originalURL := BillingAPIBaseURL
  856. BillingAPIBaseURL = server.URL
  857. defer func() { BillingAPIBaseURL = originalURL }()
  858. // Use the test server's own client so TLS/redirect rules match.
  859. originalClient := gcpHTTPClient
  860. gcpHTTPClient = server.Client()
  861. defer func() { gcpHTTPClient = originalClient }()
  862. source := NewGCPPricingSource(GCPPricingSourceConfig{
  863. APIKey: "test-key",
  864. CurrencyCode: "USD",
  865. })
  866. ps, err := source.GetPricing()
  867. if err != nil {
  868. t.Fatalf("GetPricing() unexpected error: %v", err)
  869. }
  870. if len(ps.NodePricing) != 1 {
  871. t.Errorf("NodePricing len = %d, want 1", len(ps.NodePricing))
  872. }
  873. if len(ps.PersistentVolumePricing) != 1 {
  874. t.Errorf("PersistentVolumePricing len = %d, want 1", len(ps.PersistentVolumePricing))
  875. }
  876. node := ps.NodePricing[0]
  877. if node.Properties.Region != "us-central1" {
  878. t.Errorf("NodePricing region = %q, want us-central1", node.Properties.Region)
  879. }
  880. if node.Properties.InstanceType != "n2-standard" {
  881. t.Errorf("NodePricing instance type = %q, want n2-standard", node.Properties.InstanceType)
  882. }
  883. }
  884. func TestMapGCPVolumeType(t *testing.T) {
  885. tests := []struct {
  886. name string
  887. resourceGroup string
  888. description string
  889. wantType pricing.VolumeType
  890. wantRegional bool
  891. }{
  892. {
  893. name: "PD-SSD zonal",
  894. resourceGroup: "SSD",
  895. description: "SSD backed PD Capacity",
  896. wantType: pricing.VolumeTypePDSSD,
  897. wantRegional: false,
  898. },
  899. {
  900. name: "PD-SSD regional",
  901. resourceGroup: "SSD",
  902. description: "Regional SSD backed PD Capacity",
  903. wantType: pricing.VolumeTypePDSSD,
  904. wantRegional: true,
  905. },
  906. {
  907. name: "PD-Standard",
  908. resourceGroup: "PDStandard",
  909. description: "Storage PD Capacity",
  910. wantType: pricing.VolumeTypePDStandard,
  911. wantRegional: false,
  912. },
  913. {
  914. name: "PD-Balanced",
  915. resourceGroup: "PDBalanced",
  916. description: "Balanced PD Capacity",
  917. wantType: pricing.VolumeTypePDBalanced,
  918. wantRegional: false,
  919. },
  920. {
  921. name: "PD-Extreme",
  922. resourceGroup: "PDExtreme",
  923. description: "Extreme PD Capacity",
  924. wantType: pricing.VolumeTypePDExtreme,
  925. wantRegional: false,
  926. },
  927. {
  928. name: "Hyperdisk Balanced",
  929. resourceGroup: "HyperdiskBalanced",
  930. description: "Hyperdisk Balanced",
  931. wantType: pricing.VolumeTypeHyperdiskBalanced,
  932. wantRegional: false,
  933. },
  934. {
  935. name: "Hyperdisk Extreme",
  936. resourceGroup: "HyperdiskExtreme",
  937. description: "Hyperdisk Extreme",
  938. wantType: pricing.VolumeTypeHyperdiskExtreme,
  939. wantRegional: false,
  940. },
  941. {
  942. name: "Hyperdisk Throughput",
  943. resourceGroup: "HyperdiskThroughput",
  944. description: "Hyperdisk Throughput",
  945. wantType: pricing.VolumeTypeHyperdiskThroughput,
  946. wantRegional: false,
  947. },
  948. {
  949. name: "Unknown type",
  950. resourceGroup: "UnknownDisk",
  951. description: "Some unknown disk",
  952. wantType: pricing.VolumeTypeNil,
  953. wantRegional: false,
  954. },
  955. }
  956. for _, tt := range tests {
  957. t.Run(tt.name, func(t *testing.T) {
  958. gotType, gotRegional := mapGCPVolumeType(tt.resourceGroup, tt.description)
  959. if gotType != tt.wantType {
  960. t.Errorf("mapGCPVolumeType() type = %v, want %v", gotType, tt.wantType)
  961. }
  962. if gotRegional != tt.wantRegional {
  963. t.Errorf("mapGCPVolumeType() regional = %v, want %v", gotRegional, tt.wantRegional)
  964. }
  965. })
  966. }
  967. }
  968. func TestNormalizeInstanceType(t *testing.T) {
  969. tests := []struct {
  970. name string
  971. resourceGroup string
  972. description string
  973. want string
  974. }{
  975. {
  976. name: "Custom instance",
  977. resourceGroup: "CPU",
  978. description: "Custom Instance Core running in Americas",
  979. want: "custom",
  980. },
  981. {
  982. name: "N2 standard",
  983. resourceGroup: "RAM",
  984. description: "N2 Instance Ram running in Americas",
  985. want: "n2-standard",
  986. },
  987. {
  988. name: "N2D AMD",
  989. resourceGroup: "CPU",
  990. description: "N2D AMD Instance Core running in Americas",
  991. want: "n2d-standard",
  992. },
  993. {
  994. name: "N4 instance",
  995. resourceGroup: "CPU",
  996. description: "N4 Instance Core running in Americas",
  997. want: "n4-standard",
  998. },
  999. {
  1000. name: "A2 instance",
  1001. resourceGroup: "RAM",
  1002. description: "A2 Instance Ram running in Americas",
  1003. want: "a2",
  1004. },
  1005. {
  1006. name: "C2 compute optimized",
  1007. resourceGroup: "CPU",
  1008. description: "Compute Optimized Core running in Americas",
  1009. want: "c2-standard",
  1010. },
  1011. {
  1012. name: "E2 instance",
  1013. resourceGroup: "CPU",
  1014. description: "E2 Instance Core running in Americas",
  1015. want: "e2",
  1016. },
  1017. {
  1018. name: "T2D AMD",
  1019. resourceGroup: "RAM",
  1020. description: "T2D AMD Instance Ram running in Americas",
  1021. want: "t2d-standard",
  1022. },
  1023. {
  1024. name: "T2A ARM",
  1025. resourceGroup: "CPU",
  1026. description: "T2A ARM Instance Core running in Americas",
  1027. want: "t2a-standard",
  1028. },
  1029. {
  1030. name: "Unknown type defaults to resource group",
  1031. resourceGroup: "SomeType",
  1032. description: "Some Instance Type",
  1033. want: "sometype",
  1034. },
  1035. }
  1036. for _, tt := range tests {
  1037. t.Run(tt.name, func(t *testing.T) {
  1038. got := normalizeInstanceType(tt.resourceGroup, tt.description)
  1039. if got != tt.want {
  1040. t.Errorf("normalizeInstanceType() = %v, want %v", got, tt.want)
  1041. }
  1042. })
  1043. }
  1044. }
  1045. func TestIsComputeResource(t *testing.T) {
  1046. tests := []struct {
  1047. name string
  1048. resourceGroup string
  1049. want bool
  1050. }{
  1051. {
  1052. name: "CPU is compute",
  1053. resourceGroup: "CPU",
  1054. want: true,
  1055. },
  1056. {
  1057. name: "RAM is compute",
  1058. resourceGroup: "RAM",
  1059. want: true,
  1060. },
  1061. {
  1062. name: "cpu lowercase is compute",
  1063. resourceGroup: "cpu",
  1064. want: true,
  1065. },
  1066. {
  1067. name: "ram lowercase is compute",
  1068. resourceGroup: "ram",
  1069. want: true,
  1070. },
  1071. {
  1072. name: "SSD is not compute",
  1073. resourceGroup: "SSD",
  1074. want: false,
  1075. },
  1076. {
  1077. name: "GPU is not compute",
  1078. resourceGroup: "GPU",
  1079. want: false,
  1080. },
  1081. }
  1082. for _, tt := range tests {
  1083. t.Run(tt.name, func(t *testing.T) {
  1084. got := isComputeResource(tt.resourceGroup)
  1085. if got != tt.want {
  1086. t.Errorf("isComputeResource() = %v, want %v", got, tt.want)
  1087. }
  1088. })
  1089. }
  1090. }
  1091. func TestIsStorageResource(t *testing.T) {
  1092. tests := []struct {
  1093. name string
  1094. resourceGroup string
  1095. want bool
  1096. }{
  1097. {
  1098. name: "SSD is storage",
  1099. resourceGroup: "SSD",
  1100. want: true,
  1101. },
  1102. {
  1103. name: "PDStandard is storage",
  1104. resourceGroup: "PDStandard",
  1105. want: true,
  1106. },
  1107. {
  1108. name: "PDBalanced is storage",
  1109. resourceGroup: "PDBalanced",
  1110. want: true,
  1111. },
  1112. {
  1113. name: "PDExtreme is storage",
  1114. resourceGroup: "PDExtreme",
  1115. want: true,
  1116. },
  1117. {
  1118. name: "HyperdiskBalanced is storage",
  1119. resourceGroup: "HyperdiskBalanced",
  1120. want: true,
  1121. },
  1122. {
  1123. name: "HyperdiskExtreme is storage",
  1124. resourceGroup: "HyperdiskExtreme",
  1125. want: true,
  1126. },
  1127. {
  1128. name: "HyperdiskThroughput is storage",
  1129. resourceGroup: "HyperdiskThroughput",
  1130. want: true,
  1131. },
  1132. {
  1133. name: "CPU is not storage",
  1134. resourceGroup: "CPU",
  1135. want: false,
  1136. },
  1137. {
  1138. name: "RAM is not storage",
  1139. resourceGroup: "RAM",
  1140. want: false,
  1141. },
  1142. }
  1143. for _, tt := range tests {
  1144. t.Run(tt.name, func(t *testing.T) {
  1145. got := isStorageResource(tt.resourceGroup)
  1146. if got != tt.want {
  1147. t.Errorf("isStorageResource() = %v, want %v", got, tt.want)
  1148. }
  1149. })
  1150. }
  1151. }
  1152. func TestNormalizeGPUProduct(t *testing.T) {
  1153. tests := []struct {
  1154. desc string
  1155. want string
  1156. }{
  1157. {"Nvidia Tesla T4 GPU running in Americas", "Tesla-T4"},
  1158. {"Tesla T4 GPU", "Tesla-T4"},
  1159. {"Nvidia Tesla V100 GPU running in Americas", "Tesla-V100"},
  1160. {"Nvidia Tesla P100 GPU running in Melbourne", "Tesla-P100"},
  1161. {"Nvidia Tesla P4 GPU", "Tesla-P4"},
  1162. {"Nvidia Tesla K80 GPU", "Tesla-K80"},
  1163. {"Nvidia Tesla A100 80GB GPU (SXM4) in region us-central1", "NVIDIA-A100-80GB-PCIe"},
  1164. {"Nvidia Tesla A100 GPU attached", "Tesla-A100"},
  1165. {"Nvidia Tesla A100 40GB GPU", "Tesla-A100"},
  1166. {"Nvidia L4 GPU running in Americas", "NVIDIA-L4"},
  1167. {"Unknown GPU Device", ""},
  1168. {"N2 Instance Core running in Americas", ""},
  1169. }
  1170. for _, tt := range tests {
  1171. t.Run(tt.desc, func(t *testing.T) {
  1172. got := normalizeGPUProduct(tt.desc)
  1173. if got != tt.want {
  1174. t.Errorf("normalizeGPUProduct(%q) = %q, want %q", tt.desc, got, tt.want)
  1175. }
  1176. })
  1177. }
  1178. }
  1179. // Helper function to check if a string contains a substring
  1180. func contains(s, substr string) bool {
  1181. return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
  1182. (len(s) > 0 && (s[0:len(substr)] == substr || contains(s[1:], substr))))
  1183. }