costmodel_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. package costmodel
  2. import (
  3. "math"
  4. "math/rand"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/opencost/opencost/core/pkg/clustercache"
  9. coreenv "github.com/opencost/opencost/core/pkg/env"
  10. "github.com/opencost/opencost/core/pkg/storage"
  11. "github.com/opencost/opencost/core/pkg/util"
  12. "github.com/opencost/opencost/pkg/cloud/models"
  13. "github.com/opencost/opencost/pkg/cloud/provider"
  14. "github.com/opencost/opencost/pkg/config"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. )
  20. func TestIsValidNodeName(t *testing.T) {
  21. tests := []string{
  22. "ip-10-1-2-3.ec2.internal",
  23. "node-1",
  24. "another.test.node",
  25. "10-55.23-10",
  26. "s21",
  27. "s1",
  28. "s",
  29. }
  30. for _, test := range tests {
  31. if !isValidNodeName(test) {
  32. t.Errorf("Expected %s to be a valid node name", test)
  33. }
  34. }
  35. chars := "abcdefghijklmnopqrstuvwxyz"
  36. longName := ""
  37. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  38. for i := 0; i < 255; i++ {
  39. longName += string(chars[r.Intn(len(chars))])
  40. }
  41. fails := []string{
  42. longName,
  43. "192.168.1.1:80",
  44. "10.0.0.1:443",
  45. "127.0.0.1:8080",
  46. "172.16.254.1:22",
  47. "0.0.0.0:5000",
  48. "::1:80",
  49. "2001:db8::1:443",
  50. "2001:0db8:85a3:0000:0000:8a2e:0370:7334:8080",
  51. "fe80::1:22",
  52. "10.1.2.3:10240",
  53. ":::80",
  54. "node$-15",
  55. "not:valid",
  56. ".hello-world",
  57. "hello-world.",
  58. "i--",
  59. }
  60. for _, fail := range fails {
  61. if isValidNodeName(fail) {
  62. t.Errorf("Expected %s to be an invalid node name", fail)
  63. }
  64. }
  65. }
  66. func TestGetGPUCount(t *testing.T) {
  67. tests := []struct {
  68. name string
  69. node *clustercache.Node
  70. expectedGPU float64
  71. expectedVGPU float64
  72. expectedError bool
  73. }{
  74. {
  75. name: "Standard NVIDIA GPU",
  76. node: &clustercache.Node{
  77. Status: v1.NodeStatus{
  78. Capacity: v1.ResourceList{
  79. "nvidia.com/gpu": resource.MustParse("2"),
  80. },
  81. },
  82. },
  83. expectedGPU: 2.0,
  84. expectedVGPU: 2.0,
  85. },
  86. {
  87. name: "NVIDIA GPU with GFD - renameByDefault=true",
  88. node: &clustercache.Node{
  89. Labels: map[string]string{
  90. "nvidia.com/gpu.replicas": "4",
  91. "nvidia.com/gpu.count": "1",
  92. },
  93. Status: v1.NodeStatus{
  94. Capacity: v1.ResourceList{
  95. "nvidia.com/gpu.shared": resource.MustParse("4"),
  96. },
  97. },
  98. },
  99. expectedGPU: 1.0,
  100. expectedVGPU: 4.0,
  101. },
  102. {
  103. name: "NVIDIA GPU with GFD - renameByDefault=false",
  104. node: &clustercache.Node{
  105. Labels: map[string]string{
  106. "nvidia.com/gpu.replicas": "4",
  107. "nvidia.com/gpu.count": "1",
  108. },
  109. Status: v1.NodeStatus{
  110. Capacity: v1.ResourceList{
  111. "nvidia.com/gpu": resource.MustParse("4"),
  112. },
  113. },
  114. },
  115. expectedGPU: 1.0,
  116. expectedVGPU: 4.0,
  117. },
  118. {
  119. name: "No GPU",
  120. node: &clustercache.Node{
  121. Status: v1.NodeStatus{
  122. Capacity: v1.ResourceList{},
  123. },
  124. },
  125. expectedGPU: -1.0,
  126. expectedVGPU: -1.0,
  127. },
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.name, func(t *testing.T) {
  131. gpu, vgpu, err := getGPUCount(nil, tt.node)
  132. if tt.expectedError {
  133. assert.Error(t, err)
  134. } else {
  135. assert.NoError(t, err)
  136. assert.Equal(t, tt.expectedGPU, gpu)
  137. assert.Equal(t, tt.expectedVGPU, vgpu)
  138. }
  139. })
  140. }
  141. }
  142. func Test_CostData_GetController_CronJob(t *testing.T) {
  143. cases := []struct {
  144. name string
  145. cd CostData
  146. expectedName string
  147. expectedKind string
  148. expectedHasController bool
  149. }{
  150. {
  151. name: "batch/v1beta1 CronJob Job name",
  152. cd: CostData{
  153. // batch/v1beta1 CronJobs create Jobs with a 10 character
  154. // timestamp appended to the end of the name.
  155. //
  156. // It looks like this:
  157. // CronJob: cronjob-1
  158. // Job: cronjob-1-1651057200
  159. // Pod: cronjob-1-1651057200-mf5c9
  160. Jobs: []string{"cronjob-1-1651057200"},
  161. },
  162. expectedName: "cronjob-1",
  163. expectedKind: "job",
  164. expectedHasController: true,
  165. },
  166. {
  167. name: "batch/v1 CronJob Job name",
  168. cd: CostData{
  169. // batch/v1CronJobs create Jobs with an 8 character timestamp
  170. // appended to the end of the name.
  171. //
  172. // It looks like this:
  173. // CronJob: cj-v1
  174. // Job: cj-v1-27517770
  175. // Pod: cj-v1-27517770-xkrgn
  176. Jobs: []string{"cj-v1-27517770"},
  177. },
  178. expectedName: "cj-v1",
  179. expectedKind: "job",
  180. expectedHasController: true,
  181. },
  182. }
  183. for _, c := range cases {
  184. t.Run(c.name, func(t *testing.T) {
  185. name, kind, hasController := c.cd.GetController()
  186. if name != c.expectedName {
  187. t.Errorf("Name mismatch. Expected: %s. Got: %s", c.expectedName, name)
  188. }
  189. if kind != c.expectedKind {
  190. t.Errorf("Kind mismatch. Expected: %s. Got: %s", c.expectedKind, kind)
  191. }
  192. if hasController != c.expectedHasController {
  193. t.Errorf("HasController mismatch. Expected: %t. Got: %t", c.expectedHasController, hasController)
  194. }
  195. })
  196. }
  197. }
  198. func TestGetContainerAllocation(t *testing.T) {
  199. cases := []struct {
  200. name string
  201. req *util.Vector
  202. used *util.Vector
  203. allocationType string
  204. expected []*util.Vector
  205. }{
  206. {
  207. name: "request > usage",
  208. req: &util.Vector{
  209. Value: 100,
  210. Timestamp: 1672531200,
  211. },
  212. used: &util.Vector{
  213. Value: 50,
  214. Timestamp: 1672531200,
  215. },
  216. allocationType: "RAM",
  217. expected: []*util.Vector{
  218. {
  219. Value: 100,
  220. Timestamp: 1672531200,
  221. },
  222. },
  223. },
  224. {
  225. name: "usage > request",
  226. req: &util.Vector{
  227. Value: 50,
  228. Timestamp: 1672531200,
  229. },
  230. used: &util.Vector{
  231. Value: 100,
  232. Timestamp: 1672531200,
  233. },
  234. allocationType: "RAM",
  235. expected: []*util.Vector{
  236. {
  237. Value: 100,
  238. Timestamp: 1672531200,
  239. },
  240. },
  241. },
  242. {
  243. name: "only request is non-nil",
  244. req: &util.Vector{
  245. Value: 100,
  246. Timestamp: 1672531200,
  247. },
  248. used: nil,
  249. allocationType: "CPU",
  250. expected: []*util.Vector{
  251. {
  252. Value: 100,
  253. Timestamp: 1672531200,
  254. },
  255. },
  256. },
  257. {
  258. name: "only used is non-nil",
  259. req: nil,
  260. used: &util.Vector{
  261. Value: 100,
  262. Timestamp: 1672531200,
  263. },
  264. allocationType: "CPU",
  265. expected: []*util.Vector{
  266. {
  267. Value: 100,
  268. Timestamp: 1672531200,
  269. },
  270. },
  271. },
  272. {
  273. name: "both req and used are nil",
  274. req: nil,
  275. used: nil,
  276. allocationType: "GPU",
  277. expected: []*util.Vector{
  278. {
  279. Value: 0,
  280. Timestamp: float64(time.Now().UTC().Unix()),
  281. },
  282. },
  283. },
  284. {
  285. name: "NaN in request value",
  286. req: &util.Vector{
  287. Value: math.NaN(),
  288. Timestamp: 1672531200,
  289. },
  290. used: &util.Vector{
  291. Value: 50,
  292. Timestamp: 1672531200,
  293. },
  294. allocationType: "RAM",
  295. expected: []*util.Vector{
  296. {
  297. Value: 50,
  298. Timestamp: 1672531200,
  299. },
  300. },
  301. },
  302. {
  303. name: "NaN in used value",
  304. req: &util.Vector{
  305. Value: 100,
  306. Timestamp: 1672531200,
  307. },
  308. used: &util.Vector{
  309. Value: math.NaN(),
  310. Timestamp: 1672531200,
  311. },
  312. allocationType: "CPU",
  313. expected: []*util.Vector{
  314. {
  315. Value: 100,
  316. Timestamp: 1672531200,
  317. },
  318. },
  319. },
  320. }
  321. for _, tc := range cases {
  322. t.Run(tc.name, func(t *testing.T) {
  323. // For the nil case, the timestamp is dynamic, so we need to handle it separately
  324. if tc.name == "both req and used are nil" {
  325. result := getContainerAllocation(tc.req, tc.used, tc.allocationType)
  326. if result[0].Value != 0 {
  327. t.Errorf("Expected value to be 0, but got %f", result[0].Value)
  328. }
  329. if time.Now().UTC().Unix()-int64(result[0].Timestamp) > 5 {
  330. t.Errorf("Expected timestamp to be recent, but it was not")
  331. }
  332. return
  333. }
  334. result := getContainerAllocation(tc.req, tc.used, tc.allocationType)
  335. if diff := cmp.Diff(tc.expected, result); diff != "" {
  336. t.Errorf("getContainerAllocation() mismatch (-want +got):\n%s", diff)
  337. }
  338. })
  339. }
  340. }
  341. func TestStorageCostAnnotations(t *testing.T) {
  342. t.Parallel()
  343. confMan := config.NewConfigFileManager(storage.NewFileStorage("../../"))
  344. customProvider := &provider.CSVProvider{
  345. CSVLocation: "../../configs/pricing_schema_pv.csv",
  346. CustomProvider: &provider.CustomProvider{
  347. Config: provider.NewProviderConfig(confMan, "../../configs/default.json"),
  348. },
  349. }
  350. err := customProvider.DownloadPricingData()
  351. assert.NoError(t, err)
  352. costModel := &CostModel{
  353. Provider: customProvider,
  354. }
  355. providerConfig, err := customProvider.GetConfig()
  356. assert.NoError(t, err)
  357. assert.NotNil(t, providerConfig)
  358. type testCase struct {
  359. name string
  360. pv *models.PV
  361. pvc *clustercache.PersistentVolume
  362. expectedCost string
  363. }
  364. testCases := []testCase{
  365. {
  366. name: "Cost from provider",
  367. pv: &models.PV{},
  368. pvc: &clustercache.PersistentVolume{
  369. Name: "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d",
  370. },
  371. expectedCost: "0.1337",
  372. },
  373. {
  374. name: "Cost from custom provider config",
  375. pv: &models.PV{},
  376. pvc: &clustercache.PersistentVolume{
  377. Name: "fake-name",
  378. },
  379. expectedCost: providerConfig.Storage,
  380. },
  381. {
  382. name: "Cost from annotations",
  383. pv: &models.PV{},
  384. pvc: &clustercache.PersistentVolume{
  385. Name: "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d",
  386. Annotations: map[string]string{
  387. annotationStorageCost: "123.123",
  388. },
  389. },
  390. expectedCost: "123.123",
  391. },
  392. {
  393. name: "Cost from storage class and with no annotations",
  394. pv: &models.PV{
  395. Parameters: map[string]string{
  396. annotationStorageCost: "123.124",
  397. },
  398. },
  399. pvc: &clustercache.PersistentVolume{
  400. Name: "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d",
  401. },
  402. expectedCost: "123.124",
  403. },
  404. {
  405. name: "Cost from storage class and with annotations",
  406. pv: &models.PV{
  407. Parameters: map[string]string{
  408. annotationStorageCost: "123.124",
  409. },
  410. },
  411. pvc: &clustercache.PersistentVolume{
  412. Name: "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d",
  413. Annotations: map[string]string{
  414. annotationStorageCost: "123.125",
  415. },
  416. },
  417. expectedCost: "123.125",
  418. },
  419. }
  420. for _, testCase := range testCases {
  421. t.Run(testCase.name, func(t *testing.T) {
  422. t.Parallel()
  423. err := costModel.GetPVCost(testCase.pv, testCase.pvc, "default-region")
  424. assert.NoError(t, err)
  425. assert.Equal(t, testCase.expectedCost, testCase.pv.Cost)
  426. })
  427. }
  428. }
  429. func TestNodeCostAnnotations(t *testing.T) {
  430. t.Parallel()
  431. confMan := config.NewConfigFileManager(storage.NewFileStorage("../../"))
  432. customProvider := &provider.CSVProvider{
  433. CSVLocation: "../../configs/pricing_schema_region.csv",
  434. CustomProvider: &provider.CustomProvider{
  435. Config: provider.NewProviderConfig(confMan, "../../configs/default.json"),
  436. },
  437. }
  438. err := customProvider.DownloadPricingData()
  439. assert.NoError(t, err)
  440. costModel := &CostModel{
  441. Provider: customProvider,
  442. Cache: NewFakeNodeCache([]*clustercache.Node{
  443. {
  444. Name: "test-node-001",
  445. Labels: map[string]string{
  446. "topology.kubernetes.io/region": "regionone",
  447. },
  448. },
  449. {
  450. Name: "test-node-002",
  451. Labels: map[string]string{
  452. "topology.kubernetes.io/region": "regionone",
  453. },
  454. Annotations: map[string]string{
  455. "opencost.io/node-cpu-hourly-cost": "111",
  456. "opencost.io/node-ram-hourly-cost": "222",
  457. },
  458. },
  459. }),
  460. }
  461. assert.NotNil(t, costModel)
  462. providerConfig, err := customProvider.GetConfig()
  463. assert.NoError(t, err)
  464. assert.NotNil(t, providerConfig)
  465. nodeCost, err := costModel.GetNodeCost()
  466. assert.NoError(t, err)
  467. assert.NotNil(t, nodeCost)
  468. assert.NotEmpty(t, nodeCost)
  469. type testCase struct {
  470. node string
  471. VCPUCost string
  472. RAMCost string
  473. }
  474. testCases := []testCase{
  475. {
  476. node: "test-node-001",
  477. VCPUCost: "+Inf",
  478. RAMCost: "+Inf",
  479. },
  480. {
  481. node: "test-node-002",
  482. VCPUCost: "111",
  483. RAMCost: "222",
  484. },
  485. }
  486. for _, tc := range testCases {
  487. t.Run(tc.node, func(t *testing.T) {
  488. t.Parallel()
  489. nodeCost, ok := nodeCost[tc.node]
  490. require.True(t, ok)
  491. assert.Equal(t, tc.VCPUCost, nodeCost.VCPUCost)
  492. assert.Equal(t, tc.RAMCost, nodeCost.RAMCost)
  493. })
  494. }
  495. }
  496. func TestCustomProviderGPUNodeUsesDefaultHourlyPricing(t *testing.T) {
  497. configPath := t.TempDir()
  498. t.Setenv(coreenv.ConfigPathEnvVar, configPath)
  499. confMan := config.NewConfigFileManager(storage.NewFileStorage("/"))
  500. customProvider := &provider.CustomProvider{
  501. Config: provider.NewProviderConfig(confMan, "default.json"),
  502. }
  503. err := customProvider.DownloadPricingData()
  504. require.NoError(t, err)
  505. cfg, err := customProvider.GetConfig()
  506. require.NoError(t, err)
  507. costModel := &CostModel{
  508. Provider: customProvider,
  509. Cache: NewFakeNodeCache([]*clustercache.Node{
  510. {
  511. Name: "on-prem-gpu-node",
  512. Labels: map[string]string{
  513. "kubernetes.io/arch": "amd64",
  514. },
  515. Status: v1.NodeStatus{
  516. Capacity: v1.ResourceList{
  517. v1.ResourceCPU: resource.MustParse("16"),
  518. v1.ResourceMemory: resource.MustParse("128Gi"),
  519. "nvidia.com/gpu": resource.MustParse("2"),
  520. },
  521. },
  522. },
  523. }),
  524. }
  525. nodeCost, err := costModel.GetNodeCost()
  526. require.NoError(t, err)
  527. node, ok := nodeCost["on-prem-gpu-node"]
  528. require.True(t, ok)
  529. assert.Equal(t, cfg.CPU, node.VCPUCost)
  530. assert.Equal(t, cfg.RAM, node.RAMCost)
  531. assert.Equal(t, cfg.GPU, node.GPUCost)
  532. assert.Equal(t, "2.000000", node.GPU)
  533. assert.Empty(t, node.ProviderID)
  534. }
  535. // FakeNodeCache implements ClusterCache interface for testing
  536. type FakeNodeCache struct {
  537. clustercache.ClusterCache
  538. nodes []*clustercache.Node
  539. }
  540. func (f FakeNodeCache) GetAllNodes() []*clustercache.Node {
  541. return f.nodes
  542. }
  543. func NewFakeNodeCache(nodes []*clustercache.Node) FakeNodeCache {
  544. return FakeNodeCache{
  545. nodes: nodes,
  546. }
  547. }