costmodel_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package costmodel
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/util"
  5. "github.com/stretchr/testify/assert"
  6. v1 "k8s.io/api/core/v1"
  7. "k8s.io/apimachinery/pkg/api/resource"
  8. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. )
  10. func TestGetGPUCount(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. node *v1.Node
  14. expectedGPU float64
  15. expectedVGPU float64
  16. expectedError bool
  17. }{
  18. {
  19. name: "Standard NVIDIA GPU",
  20. node: &v1.Node{
  21. Status: v1.NodeStatus{
  22. Capacity: v1.ResourceList{
  23. "nvidia.com/gpu": resource.MustParse("2"),
  24. },
  25. },
  26. },
  27. expectedGPU: 2.0,
  28. expectedVGPU: 2.0,
  29. },
  30. {
  31. name: "NVIDIA GPU with GFD - renameByDefault=true",
  32. node: &v1.Node{
  33. ObjectMeta: metav1.ObjectMeta{
  34. Labels: map[string]string{
  35. "nvidia.com/gpu.replicas": "4",
  36. "nvidia.com/gpu.count": "1",
  37. },
  38. },
  39. Status: v1.NodeStatus{
  40. Capacity: v1.ResourceList{
  41. "nvidia.com/gpu.shared": resource.MustParse("4"),
  42. },
  43. },
  44. },
  45. expectedGPU: 1.0,
  46. expectedVGPU: 4.0,
  47. },
  48. {
  49. name: "NVIDIA GPU with GFD - renameByDefault=false",
  50. node: &v1.Node{
  51. ObjectMeta: metav1.ObjectMeta{
  52. Labels: map[string]string{
  53. "nvidia.com/gpu.replicas": "4",
  54. "nvidia.com/gpu.count": "1",
  55. },
  56. },
  57. Status: v1.NodeStatus{
  58. Capacity: v1.ResourceList{
  59. "nvidia.com/gpu": resource.MustParse("4"),
  60. },
  61. },
  62. },
  63. expectedGPU: 1.0,
  64. expectedVGPU: 4.0,
  65. },
  66. {
  67. name: "No GPU",
  68. node: &v1.Node{
  69. Status: v1.NodeStatus{
  70. Capacity: v1.ResourceList{},
  71. },
  72. },
  73. expectedGPU: -1.0,
  74. expectedVGPU: -1.0,
  75. },
  76. }
  77. for _, tt := range tests {
  78. t.Run(tt.name, func(t *testing.T) {
  79. gpu, vgpu, err := getGPUCount(nil, tt.node)
  80. if tt.expectedError {
  81. assert.Error(t, err)
  82. } else {
  83. assert.NoError(t, err)
  84. assert.Equal(t, tt.expectedGPU, gpu)
  85. assert.Equal(t, tt.expectedVGPU, vgpu)
  86. }
  87. })
  88. }
  89. }
  90. func Test_CostData_GetController_CronJob(t *testing.T) {
  91. cases := []struct {
  92. name string
  93. cd CostData
  94. expectedName string
  95. expectedKind string
  96. expectedHasController bool
  97. }{
  98. {
  99. name: "batch/v1beta1 CronJob Job name",
  100. cd: CostData{
  101. // batch/v1beta1 CronJobs create Jobs with a 10 character
  102. // timestamp appended to the end of the name.
  103. //
  104. // It looks like this:
  105. // CronJob: cronjob-1
  106. // Job: cronjob-1-1651057200
  107. // Pod: cronjob-1-1651057200-mf5c9
  108. Jobs: []string{"cronjob-1-1651057200"},
  109. },
  110. expectedName: "cronjob-1",
  111. expectedKind: "job",
  112. expectedHasController: true,
  113. },
  114. {
  115. name: "batch/v1 CronJob Job name",
  116. cd: CostData{
  117. // batch/v1CronJobs create Jobs with an 8 character timestamp
  118. // appended to the end of the name.
  119. //
  120. // It looks like this:
  121. // CronJob: cj-v1
  122. // Job: cj-v1-27517770
  123. // Pod: cj-v1-27517770-xkrgn
  124. Jobs: []string{"cj-v1-27517770"},
  125. },
  126. expectedName: "cj-v1",
  127. expectedKind: "job",
  128. expectedHasController: true,
  129. },
  130. }
  131. for _, c := range cases {
  132. t.Run(c.name, func(t *testing.T) {
  133. name, kind, hasController := c.cd.GetController()
  134. if name != c.expectedName {
  135. t.Errorf("Name mismatch. Expected: %s. Got: %s", c.expectedName, name)
  136. }
  137. if kind != c.expectedKind {
  138. t.Errorf("Kind mismatch. Expected: %s. Got: %s", c.expectedKind, kind)
  139. }
  140. if hasController != c.expectedHasController {
  141. t.Errorf("HasController mismatch. Expected: %t. Got: %t", c.expectedHasController, hasController)
  142. }
  143. })
  144. }
  145. }
  146. func Test_getContainerAllocation(t *testing.T) {
  147. cases := []struct {
  148. name string
  149. cd CostData
  150. expectedCPUAllocation []*util.Vector
  151. expectedRAMAllocation []*util.Vector
  152. }{
  153. {
  154. name: "Requests greater than usage",
  155. cd: CostData{
  156. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  157. CPUUsed: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  158. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  159. RAMUsed: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  160. },
  161. expectedCPUAllocation: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  162. expectedRAMAllocation: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  163. },
  164. {
  165. name: "Requests less than usage",
  166. cd: CostData{
  167. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  168. CPUUsed: []*util.Vector{{Value: 2.2, Timestamp: 1686929350}},
  169. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  170. RAMUsed: []*util.Vector{{Value: 75000000, Timestamp: 1686929350}},
  171. },
  172. expectedCPUAllocation: []*util.Vector{{Value: 2.2, Timestamp: 1686929350}},
  173. expectedRAMAllocation: []*util.Vector{{Value: 75000000, Timestamp: 1686929350}},
  174. },
  175. {
  176. // Expected behavior for getContainerAllocation is to always use the
  177. // highest Timestamp value. The significance of 10 seconds comes
  178. // from the current default in ApplyVectorOp() in
  179. // pkg/util/vector.go.
  180. name: "Mismatched timestamps less than 10 seconds apart",
  181. cd: CostData{
  182. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929354}},
  183. CPUUsed: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  184. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929354}},
  185. RAMUsed: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  186. },
  187. expectedCPUAllocation: []*util.Vector{{Value: 1.0, Timestamp: 1686929354}},
  188. expectedRAMAllocation: []*util.Vector{{Value: 10000000, Timestamp: 1686929354}},
  189. },
  190. {
  191. // Expected behavior for getContainerAllocation is to always use the
  192. // hightest Timestamp value. The significance of 10 seconds comes
  193. // from the current default in ApplyVectorOp() in
  194. // pkg/util/vector.go.
  195. name: "Mismatched timestamps greater than 10 seconds apart",
  196. cd: CostData{
  197. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929399}},
  198. CPUUsed: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  199. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929399}},
  200. RAMUsed: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  201. },
  202. expectedCPUAllocation: []*util.Vector{{Value: 1.0, Timestamp: 1686929399}},
  203. expectedRAMAllocation: []*util.Vector{{Value: 10000000, Timestamp: 1686929399}},
  204. },
  205. {
  206. name: "Requests has no values",
  207. cd: CostData{
  208. CPUReq: []*util.Vector{{Value: 0, Timestamp: 0}},
  209. CPUUsed: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  210. RAMReq: []*util.Vector{{Value: 0, Timestamp: 0}},
  211. RAMUsed: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  212. },
  213. expectedCPUAllocation: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  214. expectedRAMAllocation: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  215. },
  216. {
  217. name: "Usage has no values",
  218. cd: CostData{
  219. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  220. CPUUsed: []*util.Vector{{Value: 0, Timestamp: 0}},
  221. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  222. RAMUsed: []*util.Vector{{Value: 0, Timestamp: 0}},
  223. },
  224. expectedCPUAllocation: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  225. expectedRAMAllocation: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  226. },
  227. {
  228. // WRN Log should be thrown
  229. name: "Both have no values",
  230. cd: CostData{
  231. CPUReq: []*util.Vector{{Value: 0, Timestamp: 0}},
  232. CPUUsed: []*util.Vector{{Value: 0, Timestamp: 0}},
  233. RAMReq: []*util.Vector{{Value: 0, Timestamp: 0}},
  234. RAMUsed: []*util.Vector{{Value: 0, Timestamp: 0}},
  235. },
  236. expectedCPUAllocation: []*util.Vector{{Value: 0, Timestamp: 0}},
  237. expectedRAMAllocation: []*util.Vector{{Value: 0, Timestamp: 0}},
  238. },
  239. {
  240. name: "Requests is Nil",
  241. cd: CostData{
  242. CPUReq: []*util.Vector{nil},
  243. CPUUsed: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  244. RAMReq: []*util.Vector{nil},
  245. RAMUsed: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  246. },
  247. expectedCPUAllocation: []*util.Vector{{Value: .01, Timestamp: 1686929350}},
  248. expectedRAMAllocation: []*util.Vector{{Value: 5500000, Timestamp: 1686929350}},
  249. },
  250. {
  251. name: "Usage is nil",
  252. cd: CostData{
  253. CPUReq: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  254. CPUUsed: []*util.Vector{nil},
  255. RAMReq: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  256. RAMUsed: []*util.Vector{nil},
  257. },
  258. expectedCPUAllocation: []*util.Vector{{Value: 1.0, Timestamp: 1686929350}},
  259. expectedRAMAllocation: []*util.Vector{{Value: 10000000, Timestamp: 1686929350}},
  260. },
  261. }
  262. for _, c := range cases {
  263. t.Run(c.name, func(t *testing.T) {
  264. cpuAllocation := getContainerAllocation(c.cd.CPUReq[0], c.cd.CPUUsed[0], "CPU")
  265. ramAllocation := getContainerAllocation(c.cd.RAMReq[0], c.cd.RAMUsed[0], "RAM")
  266. if cpuAllocation[0].Value != c.expectedCPUAllocation[0].Value {
  267. t.Errorf("CPU Allocation mismatch. Expected Value: %f. Got: %f", cpuAllocation[0].Value, c.expectedCPUAllocation[0].Value)
  268. }
  269. if cpuAllocation[0].Timestamp != c.expectedCPUAllocation[0].Timestamp {
  270. t.Errorf("CPU Allocation mismatch. Expected Timestamp: %f. Got: %f", cpuAllocation[0].Timestamp, c.expectedCPUAllocation[0].Timestamp)
  271. }
  272. if ramAllocation[0].Value != c.expectedRAMAllocation[0].Value {
  273. t.Errorf("RAM Allocation mismatch. Expected Value: %f. Got: %f", ramAllocation[0].Value, c.expectedRAMAllocation[0].Value)
  274. }
  275. if ramAllocation[0].Timestamp != c.expectedRAMAllocation[0].Timestamp {
  276. t.Errorf("RAM Allocation mismatch. Expected Timestamp: %f. Got: %f", ramAllocation[0].Timestamp, c.expectedRAMAllocation[0].Timestamp)
  277. }
  278. })
  279. }
  280. }