gcppricingsource_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 - included as spot",
  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: 1,
  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 TestBuildNodePricing_SpotProvisioning(t *testing.T) {
  661. source := &GCPPricingSource{
  662. config: GCPPricingSourceConfig{
  663. CurrencyCode: "USD",
  664. },
  665. }
  666. ps := &pricing.PricingSet{
  667. NodePricing: []*pricing.NodePricing{},
  668. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  669. }
  670. cpuCosts := map[nodeKey]float64{
  671. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.031611,
  672. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.007583,
  673. }
  674. ramCosts := map[nodeKey]float64{
  675. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "ondemand"}: 0.004237,
  676. {Region: "us-central1", InstanceType: "n2-standard", UsageType: "preemptible"}: 0.001017,
  677. }
  678. source.buildNodePricing(ps, cpuCosts, ramCosts)
  679. if len(ps.NodePricing) != 2 {
  680. t.Fatalf("buildNodePricing() created %d nodes, want 2", len(ps.NodePricing))
  681. }
  682. var sawOnDemand, sawSpot bool
  683. for _, np := range ps.NodePricing {
  684. switch np.Properties.Provisioning {
  685. case pricing.ProvisioningOnDemand:
  686. sawOnDemand = true
  687. if np.Prices[pricing.ResourceCPU].Price != 0.031611 {
  688. t.Errorf("on-demand CPU price = %v, want 0.031611", np.Prices[pricing.ResourceCPU].Price)
  689. }
  690. case pricing.ProvisioningSpot:
  691. sawSpot = true
  692. if np.Prices[pricing.ResourceCPU].Price != 0.007583 {
  693. t.Errorf("spot CPU price = %v, want 0.007583", np.Prices[pricing.ResourceCPU].Price)
  694. }
  695. default:
  696. t.Errorf("unexpected provisioning type: %v", np.Properties.Provisioning)
  697. }
  698. }
  699. if !sawOnDemand {
  700. t.Error("expected an on-demand node pricing entry")
  701. }
  702. if !sawSpot {
  703. t.Error("expected a spot node pricing entry")
  704. }
  705. }
  706. func TestBuildVolumePricing(t *testing.T) {
  707. tests := []struct {
  708. name string
  709. volumeCosts map[volumeKey]float64
  710. wantVolumes int
  711. currencyCode string
  712. }{
  713. {
  714. name: "Single volume type",
  715. volumeCosts: map[volumeKey]float64{
  716. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  717. },
  718. wantVolumes: 1,
  719. currencyCode: "USD",
  720. },
  721. {
  722. name: "Multiple volume types",
  723. volumeCosts: map[volumeKey]float64{
  724. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  725. {Region: "us-central1", VolumeType: pricing.VolumeTypePDStandard, Regional: false}: 0.000054795,
  726. },
  727. wantVolumes: 2,
  728. currencyCode: "USD",
  729. },
  730. {
  731. name: "Regional and zonal",
  732. volumeCosts: map[volumeKey]float64{
  733. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: false}: 0.000232876,
  734. {Region: "us-central1", VolumeType: pricing.VolumeTypePDSSD, Regional: true}: 0.000279452,
  735. },
  736. wantVolumes: 2,
  737. currencyCode: "USD",
  738. },
  739. }
  740. for _, tt := range tests {
  741. t.Run(tt.name, func(t *testing.T) {
  742. source := &GCPPricingSource{
  743. config: GCPPricingSourceConfig{
  744. CurrencyCode: tt.currencyCode,
  745. },
  746. }
  747. ps := &pricing.PricingSet{
  748. NodePricing: []*pricing.NodePricing{},
  749. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  750. }
  751. source.buildVolumePricing(ps, tt.volumeCosts)
  752. if len(ps.PersistentVolumePricing) != tt.wantVolumes {
  753. t.Errorf("buildVolumePricing() created %d volumes, want %d", len(ps.PersistentVolumePricing), tt.wantVolumes)
  754. }
  755. })
  756. }
  757. }
  758. func TestGetPricing_Integration(t *testing.T) {
  759. // Serve one page of pricing: one N2 CPU SKU, one N2 RAM SKU, one SSD volume SKU.
  760. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  761. response := GCPPricingResponse{
  762. Skus: []*GCPPricing{
  763. {
  764. Description: "N2 Instance Core running in Americas",
  765. Category: &GCPResourceInfo{
  766. ResourceGroup: "CPU",
  767. UsageType: "OnDemand",
  768. },
  769. ServiceRegions: []string{"us-central1"},
  770. PricingInfo: []*PricingInfo{
  771. {
  772. PricingExpression: &PricingExpression{
  773. TieredRates: []*TieredRates{
  774. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 31611000}},
  775. },
  776. },
  777. },
  778. },
  779. },
  780. {
  781. Description: "N2 Instance RAM running in Americas",
  782. Category: &GCPResourceInfo{
  783. ResourceGroup: "RAM",
  784. UsageType: "OnDemand",
  785. },
  786. ServiceRegions: []string{"us-central1"},
  787. PricingInfo: []*PricingInfo{
  788. {
  789. PricingExpression: &PricingExpression{
  790. TieredRates: []*TieredRates{
  791. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 4237000}},
  792. },
  793. },
  794. },
  795. },
  796. },
  797. {
  798. Description: "SSD backed PD Capacity in Americas",
  799. Category: &GCPResourceInfo{
  800. ResourceGroup: "SSD",
  801. UsageType: "OnDemand",
  802. },
  803. ServiceRegions: []string{"us-central1"},
  804. PricingInfo: []*PricingInfo{
  805. {
  806. PricingExpression: &PricingExpression{
  807. TieredRates: []*TieredRates{
  808. {UnitPrice: &UnitPriceInfo{Units: "0", Nanos: 170000000}},
  809. },
  810. },
  811. },
  812. },
  813. },
  814. },
  815. NextPageToken: "",
  816. }
  817. w.Header().Set("Content-Type", "application/json")
  818. _ = json.NewEncoder(w).Encode(response)
  819. }))
  820. defer server.Close()
  821. // Point the package-level base URL at the test server.
  822. originalURL := BillingAPIBaseURL
  823. BillingAPIBaseURL = server.URL
  824. defer func() { BillingAPIBaseURL = originalURL }()
  825. // Use the test server's own client so TLS/redirect rules match.
  826. originalClient := gcpHTTPClient
  827. gcpHTTPClient = server.Client()
  828. defer func() { gcpHTTPClient = originalClient }()
  829. source := NewGCPPricingSource(GCPPricingSourceConfig{
  830. APIKey: "test-key",
  831. CurrencyCode: "USD",
  832. })
  833. ps, err := source.GetPricing()
  834. if err != nil {
  835. t.Fatalf("GetPricing() unexpected error: %v", err)
  836. }
  837. if len(ps.NodePricing) != 1 {
  838. t.Errorf("NodePricing len = %d, want 1", len(ps.NodePricing))
  839. }
  840. if len(ps.PersistentVolumePricing) != 1 {
  841. t.Errorf("PersistentVolumePricing len = %d, want 1", len(ps.PersistentVolumePricing))
  842. }
  843. node := ps.NodePricing[0]
  844. if node.Properties.Region != "us-central1" {
  845. t.Errorf("NodePricing region = %q, want us-central1", node.Properties.Region)
  846. }
  847. if node.Properties.InstanceType != "n2-standard" {
  848. t.Errorf("NodePricing instance type = %q, want n2-standard", node.Properties.InstanceType)
  849. }
  850. }
  851. func TestMapGCPVolumeType(t *testing.T) {
  852. tests := []struct {
  853. name string
  854. resourceGroup string
  855. description string
  856. wantType pricing.VolumeType
  857. wantRegional bool
  858. }{
  859. {
  860. name: "PD-SSD zonal",
  861. resourceGroup: "SSD",
  862. description: "SSD backed PD Capacity",
  863. wantType: pricing.VolumeTypePDSSD,
  864. wantRegional: false,
  865. },
  866. {
  867. name: "PD-SSD regional",
  868. resourceGroup: "SSD",
  869. description: "Regional SSD backed PD Capacity",
  870. wantType: pricing.VolumeTypePDSSD,
  871. wantRegional: true,
  872. },
  873. {
  874. name: "PD-Standard",
  875. resourceGroup: "PDStandard",
  876. description: "Storage PD Capacity",
  877. wantType: pricing.VolumeTypePDStandard,
  878. wantRegional: false,
  879. },
  880. {
  881. name: "PD-Balanced",
  882. resourceGroup: "PDBalanced",
  883. description: "Balanced PD Capacity",
  884. wantType: pricing.VolumeTypePDBalanced,
  885. wantRegional: false,
  886. },
  887. {
  888. name: "PD-Extreme",
  889. resourceGroup: "PDExtreme",
  890. description: "Extreme PD Capacity",
  891. wantType: pricing.VolumeTypePDExtreme,
  892. wantRegional: false,
  893. },
  894. {
  895. name: "Hyperdisk Balanced",
  896. resourceGroup: "HyperdiskBalanced",
  897. description: "Hyperdisk Balanced",
  898. wantType: pricing.VolumeTypeHyperdiskBalanced,
  899. wantRegional: false,
  900. },
  901. {
  902. name: "Hyperdisk Extreme",
  903. resourceGroup: "HyperdiskExtreme",
  904. description: "Hyperdisk Extreme",
  905. wantType: pricing.VolumeTypeHyperdiskExtreme,
  906. wantRegional: false,
  907. },
  908. {
  909. name: "Hyperdisk Throughput",
  910. resourceGroup: "HyperdiskThroughput",
  911. description: "Hyperdisk Throughput",
  912. wantType: pricing.VolumeTypeHyperdiskThroughput,
  913. wantRegional: false,
  914. },
  915. {
  916. name: "Unknown type",
  917. resourceGroup: "UnknownDisk",
  918. description: "Some unknown disk",
  919. wantType: pricing.VolumeTypeNil,
  920. wantRegional: false,
  921. },
  922. }
  923. for _, tt := range tests {
  924. t.Run(tt.name, func(t *testing.T) {
  925. gotType, gotRegional := mapGCPVolumeType(tt.resourceGroup, tt.description)
  926. if gotType != tt.wantType {
  927. t.Errorf("mapGCPVolumeType() type = %v, want %v", gotType, tt.wantType)
  928. }
  929. if gotRegional != tt.wantRegional {
  930. t.Errorf("mapGCPVolumeType() regional = %v, want %v", gotRegional, tt.wantRegional)
  931. }
  932. })
  933. }
  934. }
  935. func TestNormalizeInstanceType(t *testing.T) {
  936. tests := []struct {
  937. name string
  938. resourceGroup string
  939. description string
  940. want string
  941. }{
  942. {
  943. name: "Custom instance",
  944. resourceGroup: "CPU",
  945. description: "Custom Instance Core running in Americas",
  946. want: "custom",
  947. },
  948. {
  949. name: "N2 standard",
  950. resourceGroup: "RAM",
  951. description: "N2 Instance Ram running in Americas",
  952. want: "n2-standard",
  953. },
  954. {
  955. name: "N2D AMD",
  956. resourceGroup: "CPU",
  957. description: "N2D AMD Instance Core running in Americas",
  958. want: "n2d-standard",
  959. },
  960. {
  961. name: "N4 instance",
  962. resourceGroup: "CPU",
  963. description: "N4 Instance Core running in Americas",
  964. want: "n4-standard",
  965. },
  966. {
  967. name: "A2 instance",
  968. resourceGroup: "RAM",
  969. description: "A2 Instance Ram running in Americas",
  970. want: "a2",
  971. },
  972. {
  973. name: "C2 compute optimized",
  974. resourceGroup: "CPU",
  975. description: "Compute Optimized Core running in Americas",
  976. want: "c2-standard",
  977. },
  978. {
  979. name: "E2 instance",
  980. resourceGroup: "CPU",
  981. description: "E2 Instance Core running in Americas",
  982. want: "e2",
  983. },
  984. {
  985. name: "T2D AMD",
  986. resourceGroup: "RAM",
  987. description: "T2D AMD Instance Ram running in Americas",
  988. want: "t2d-standard",
  989. },
  990. {
  991. name: "T2A ARM",
  992. resourceGroup: "CPU",
  993. description: "T2A ARM Instance Core running in Americas",
  994. want: "t2a-standard",
  995. },
  996. {
  997. name: "Unknown type defaults to resource group",
  998. resourceGroup: "SomeType",
  999. description: "Some Instance Type",
  1000. want: "sometype",
  1001. },
  1002. }
  1003. for _, tt := range tests {
  1004. t.Run(tt.name, func(t *testing.T) {
  1005. got := normalizeInstanceType(tt.resourceGroup, tt.description)
  1006. if got != tt.want {
  1007. t.Errorf("normalizeInstanceType() = %v, want %v", got, tt.want)
  1008. }
  1009. })
  1010. }
  1011. }
  1012. func TestIsComputeResource(t *testing.T) {
  1013. tests := []struct {
  1014. name string
  1015. resourceGroup string
  1016. want bool
  1017. }{
  1018. {
  1019. name: "CPU is compute",
  1020. resourceGroup: "CPU",
  1021. want: true,
  1022. },
  1023. {
  1024. name: "RAM is compute",
  1025. resourceGroup: "RAM",
  1026. want: true,
  1027. },
  1028. {
  1029. name: "cpu lowercase is compute",
  1030. resourceGroup: "cpu",
  1031. want: true,
  1032. },
  1033. {
  1034. name: "ram lowercase is compute",
  1035. resourceGroup: "ram",
  1036. want: true,
  1037. },
  1038. {
  1039. name: "SSD is not compute",
  1040. resourceGroup: "SSD",
  1041. want: false,
  1042. },
  1043. {
  1044. name: "GPU is not compute",
  1045. resourceGroup: "GPU",
  1046. want: false,
  1047. },
  1048. }
  1049. for _, tt := range tests {
  1050. t.Run(tt.name, func(t *testing.T) {
  1051. got := isComputeResource(tt.resourceGroup)
  1052. if got != tt.want {
  1053. t.Errorf("isComputeResource() = %v, want %v", got, tt.want)
  1054. }
  1055. })
  1056. }
  1057. }
  1058. func TestIsStorageResource(t *testing.T) {
  1059. tests := []struct {
  1060. name string
  1061. resourceGroup string
  1062. want bool
  1063. }{
  1064. {
  1065. name: "SSD is storage",
  1066. resourceGroup: "SSD",
  1067. want: true,
  1068. },
  1069. {
  1070. name: "PDStandard is storage",
  1071. resourceGroup: "PDStandard",
  1072. want: true,
  1073. },
  1074. {
  1075. name: "PDBalanced is storage",
  1076. resourceGroup: "PDBalanced",
  1077. want: true,
  1078. },
  1079. {
  1080. name: "PDExtreme is storage",
  1081. resourceGroup: "PDExtreme",
  1082. want: true,
  1083. },
  1084. {
  1085. name: "HyperdiskBalanced is storage",
  1086. resourceGroup: "HyperdiskBalanced",
  1087. want: true,
  1088. },
  1089. {
  1090. name: "HyperdiskExtreme is storage",
  1091. resourceGroup: "HyperdiskExtreme",
  1092. want: true,
  1093. },
  1094. {
  1095. name: "HyperdiskThroughput is storage",
  1096. resourceGroup: "HyperdiskThroughput",
  1097. want: true,
  1098. },
  1099. {
  1100. name: "CPU is not storage",
  1101. resourceGroup: "CPU",
  1102. want: false,
  1103. },
  1104. {
  1105. name: "RAM is not storage",
  1106. resourceGroup: "RAM",
  1107. want: false,
  1108. },
  1109. }
  1110. for _, tt := range tests {
  1111. t.Run(tt.name, func(t *testing.T) {
  1112. got := isStorageResource(tt.resourceGroup)
  1113. if got != tt.want {
  1114. t.Errorf("isStorageResource() = %v, want %v", got, tt.want)
  1115. }
  1116. })
  1117. }
  1118. }
  1119. // Helper function to check if a string contains a substring
  1120. func contains(s, substr string) bool {
  1121. return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
  1122. (len(s) > 0 && (s[0:len(substr)] == substr || contains(s[1:], substr))))
  1123. }
  1124. // Made with Bob