costmodel_test.go 10 KB

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