metric_helper_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package dockerstatsreceiver
  4. import (
  5. "errors"
  6. "testing"
  7. ctypes "github.com/docker/docker/api/types/container"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func Test_calculateCPULimit1(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. args *ctypes.HostConfig
  14. want float64
  15. err error
  16. }{
  17. {
  18. "Test CPULimit",
  19. &ctypes.HostConfig{
  20. Resources: ctypes.Resources{
  21. NanoCPUs: 2500000000,
  22. },
  23. },
  24. 2.5,
  25. nil,
  26. },
  27. {
  28. "Test CPUSetCpu",
  29. &ctypes.HostConfig{
  30. Resources: ctypes.Resources{
  31. CpusetCpus: "0-2",
  32. },
  33. },
  34. 3,
  35. nil,
  36. },
  37. {
  38. "Test CPUQuota",
  39. &ctypes.HostConfig{
  40. Resources: ctypes.Resources{
  41. CPUQuota: 50000,
  42. },
  43. },
  44. 0.5,
  45. nil,
  46. },
  47. {
  48. "Test CPUQuota Custom Period",
  49. &ctypes.HostConfig{
  50. Resources: ctypes.Resources{
  51. CPUQuota: 300000,
  52. CPUPeriod: 200000,
  53. },
  54. },
  55. 1.5,
  56. nil,
  57. },
  58. {
  59. "Test Default",
  60. &ctypes.HostConfig{
  61. Resources: ctypes.Resources{
  62. NanoCPUs: 1800000000,
  63. CpusetCpus: "0-1",
  64. CPUQuota: 400000,
  65. },
  66. },
  67. 1.8,
  68. nil,
  69. },
  70. {
  71. "Test No Values",
  72. &ctypes.HostConfig{
  73. Resources: ctypes.Resources{},
  74. },
  75. 0,
  76. nil,
  77. },
  78. }
  79. for _, tt := range tests {
  80. t.Run(tt.name, func(t *testing.T) {
  81. want, err := calculateCPULimit(tt.args)
  82. assert.Equalf(t, tt.want, want, "calculateCPULimit(%v)", tt.args)
  83. assert.Equalf(t, tt.err, err, "calculateCPULimit(%v)", tt.args)
  84. })
  85. }
  86. }
  87. func Test_parseCPUSet(t *testing.T) {
  88. tests := []struct {
  89. input string
  90. expected float64
  91. err error
  92. }{
  93. {"0,2", 2, nil},
  94. {"0-2", 3, nil},
  95. {"0-2,4", 4, nil},
  96. {"0-2,4-5", 5, nil},
  97. {"a-b", 0, errors.New("invalid -cpuset-cpus value: strconv.Atoi: parsing \"a\": invalid syntax")},
  98. {"", 1, nil},
  99. }
  100. for _, test := range tests {
  101. result, err := parseCPUSet(test.input)
  102. if err != nil && test.err != nil {
  103. if err.Error() != test.err.Error() {
  104. t.Errorf("parseCPUSet(%s) returned error %v, expected %v", test.input, err, test.err)
  105. }
  106. } else if !errors.Is(err, test.err) {
  107. t.Errorf("parseCPUSet(%s) returned error %v, expected %v", test.input, err, test.err)
  108. }
  109. if result != test.expected {
  110. t.Errorf("parseCPUSet(%s) returned %f, expected %f", test.input, result, test.expected)
  111. }
  112. }
  113. }