accumulator_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awsecscontainermetrics
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. "go.uber.org/zap"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil"
  9. )
  10. var (
  11. v = uint64(1)
  12. f = 1.0
  13. floatZero = float64(0)
  14. logger = zap.NewNop()
  15. memStats = map[string]uint64{"cache": v}
  16. mem = MemoryStats{
  17. Usage: &v,
  18. MaxUsage: &v,
  19. Limit: &v,
  20. MemoryReserved: &v,
  21. MemoryUtilized: &v,
  22. Stats: memStats,
  23. }
  24. disk = DiskStats{
  25. IoServiceBytesRecursives: []IoServiceBytesRecursive{
  26. {Op: "Read", Value: &v},
  27. {Op: "Write", Value: &v},
  28. {Op: "Total", Value: &v},
  29. },
  30. }
  31. networkStat = NetworkStats{
  32. RxBytes: &v,
  33. RxPackets: &v,
  34. RxErrors: &v,
  35. RxDropped: &v,
  36. TxBytes: &v,
  37. TxPackets: &v,
  38. TxErrors: &v,
  39. TxDropped: &v,
  40. }
  41. net = map[string]NetworkStats{"eth0": networkStat}
  42. netRate = NetworkRateStats{
  43. RxBytesPerSecond: &f,
  44. TxBytesPerSecond: &f,
  45. }
  46. percpu = []*uint64{&v, &v}
  47. cpuUsage = CPUUsage{
  48. TotalUsage: &v,
  49. UsageInKernelmode: &v,
  50. UsageInUserMode: &v,
  51. PerCPUUsage: percpu,
  52. }
  53. cpuStats = CPUStats{
  54. CPUUsage: &cpuUsage,
  55. OnlineCpus: &v,
  56. SystemCPUUsage: &v,
  57. CPUUtilized: &v,
  58. CPUReserved: &v,
  59. }
  60. containerStats = ContainerStats{
  61. Name: "test",
  62. ID: "001",
  63. Memory: &mem,
  64. Disk: &disk,
  65. Network: net,
  66. NetworkRate: &netRate,
  67. CPU: &cpuStats,
  68. }
  69. tm = ecsutil.TaskMetadata{
  70. Cluster: "cluster-1",
  71. TaskARN: "arn:aws:some-value/001",
  72. Family: "task-def-family-1",
  73. Revision: "task-def-version",
  74. Containers: []ecsutil.ContainerMetadata{
  75. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  76. },
  77. Limits: ecsutil.Limits{CPU: &f, Memory: &v},
  78. }
  79. cstats = map[string]*ContainerStats{"001": &containerStats}
  80. acc = metricDataAccumulator{
  81. mds: nil,
  82. }
  83. )
  84. func TestGetMetricsDataAllValid(t *testing.T) {
  85. acc.getMetricsData(cstats, tm, logger)
  86. require.Less(t, 0, len(acc.mds))
  87. }
  88. func TestGetMetricsDataMissingContainerStats(t *testing.T) {
  89. tm.Containers = []ecsutil.ContainerMetadata{
  90. {ContainerName: "container-1", DockerID: "001-Missing", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  91. }
  92. acc.getMetricsData(cstats, tm, logger)
  93. require.Less(t, 0, len(acc.mds))
  94. }
  95. func TestGetMetricsDataForStoppedContainer(t *testing.T) {
  96. cstats = map[string]*ContainerStats{"001": nil}
  97. tm.Containers = []ecsutil.ContainerMetadata{
  98. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", CreatedAt: "2020-07-30T22:12:29.842610987Z", StartedAt: "2020-07-30T22:12:31.842610987Z", FinishedAt: "2020-07-31T22:10:29.842610987Z", KnownStatus: "STOPPED", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  99. }
  100. acc.getMetricsData(cstats, tm, logger)
  101. require.Less(t, 0, len(acc.mds))
  102. }
  103. func TestWrongFormatTimeDataForStoppedContainer(t *testing.T) {
  104. cstats = map[string]*ContainerStats{"001": nil}
  105. tm.Containers = []ecsutil.ContainerMetadata{
  106. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", CreatedAt: "2020-07-30T22:12:29.842610987Z", StartedAt: "2020-07-30T22:12:31.842610987Z", FinishedAt: "2020-07-31 22:10:29", KnownStatus: "STOPPED", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  107. }
  108. acc.getMetricsData(cstats, tm, logger)
  109. require.Less(t, 0, len(acc.mds))
  110. }
  111. func TestGetMetricsDataMissingContainerLimit(t *testing.T) {
  112. tm.Containers = []ecsutil.ContainerMetadata{
  113. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1"},
  114. }
  115. acc.getMetricsData(cstats, tm, logger)
  116. require.Less(t, 0, len(acc.mds))
  117. }
  118. func TestGetMetricsDataContainerLimitCpuNil(t *testing.T) {
  119. tm.Containers = []ecsutil.ContainerMetadata{
  120. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: nil, Memory: &v}},
  121. }
  122. acc.getMetricsData(cstats, tm, logger)
  123. require.Less(t, 0, len(acc.mds))
  124. }
  125. func TestGetMetricsDataContainerLimitMemoryNil(t *testing.T) {
  126. tm.Containers = []ecsutil.ContainerMetadata{
  127. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: nil}},
  128. }
  129. acc.getMetricsData(cstats, tm, logger)
  130. require.Less(t, 0, len(acc.mds))
  131. }
  132. func TestGetMetricsDataMissingTaskLimit(t *testing.T) {
  133. tm = ecsutil.TaskMetadata{
  134. Cluster: "cluster-1",
  135. TaskARN: "arn:aws:some-value/001",
  136. Family: "task-def-family-1",
  137. Revision: "task-def-version",
  138. Containers: []ecsutil.ContainerMetadata{
  139. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  140. },
  141. }
  142. acc.getMetricsData(cstats, tm, logger)
  143. require.Less(t, 0, len(acc.mds))
  144. }
  145. func TestGetMetricsDataTaskLimitCpuNil(t *testing.T) {
  146. tm = ecsutil.TaskMetadata{
  147. Cluster: "cluster-1",
  148. TaskARN: "arn:aws:some-value/001",
  149. Family: "task-def-family-1",
  150. Revision: "task-def-version",
  151. Containers: []ecsutil.ContainerMetadata{
  152. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  153. },
  154. Limits: ecsutil.Limits{CPU: nil, Memory: &v},
  155. }
  156. acc.getMetricsData(cstats, tm, logger)
  157. require.Less(t, 0, len(acc.mds))
  158. }
  159. func TestGetMetricsDataTaskLimitMemoryNil(t *testing.T) {
  160. tm = ecsutil.TaskMetadata{
  161. Cluster: "cluster-1",
  162. TaskARN: "arn:aws:some-value/001",
  163. Family: "task-def-family-1",
  164. Revision: "task-def-version",
  165. Containers: []ecsutil.ContainerMetadata{
  166. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &f, Memory: &v}},
  167. },
  168. Limits: ecsutil.Limits{CPU: &f, Memory: nil},
  169. }
  170. acc.getMetricsData(cstats, tm, logger)
  171. require.Less(t, 0, len(acc.mds))
  172. }
  173. func TestGetMetricsDataCpuReservedZero(t *testing.T) {
  174. tm = ecsutil.TaskMetadata{
  175. Cluster: "cluster-1",
  176. TaskARN: "arn:aws:some-value/001",
  177. Family: "task-def-family-1",
  178. Revision: "task-def-version",
  179. Containers: []ecsutil.ContainerMetadata{
  180. {ContainerName: "container-1", DockerID: "001", DockerName: "docker-container-1", Limits: ecsutil.Limits{CPU: &floatZero, Memory: nil}},
  181. },
  182. Limits: ecsutil.Limits{CPU: &floatZero, Memory: &v},
  183. }
  184. acc.getMetricsData(cstats, tm, logger)
  185. require.Less(t, 0, len(acc.mds))
  186. }
  187. func TestIsEmptyStats(t *testing.T) {
  188. require.EqualValues(t, false, isEmptyStats(&containerStats))
  189. require.EqualValues(t, true, isEmptyStats(cstats["002"]))
  190. cstats = map[string]*ContainerStats{"001": nil}
  191. require.EqualValues(t, true, isEmptyStats(cstats["001"]))
  192. cstats = map[string]*ContainerStats{"001": {}}
  193. require.EqualValues(t, true, isEmptyStats(cstats["001"]))
  194. }
  195. func TestCalculateDuration(t *testing.T) {
  196. startTime := "2020-10-02T00:15:07.620912337Z"
  197. endTime := "2020-10-03T15:14:06.620913372Z"
  198. result, err := calculateDuration(startTime, endTime)
  199. require.EqualValues(t, 140339.000001035, result)
  200. require.NoError(t, err)
  201. startTime = "2010-10-02T00:15:07.620912337Z"
  202. endTime = "2020-10-03T15:14:06.620913372Z"
  203. result, err = calculateDuration(startTime, endTime)
  204. require.EqualValues(t, 3.15759539000001e+08, result)
  205. require.NoError(t, err)
  206. startTime = "2010-10-02 00:15:07"
  207. endTime = "2020-10-03T15:14:06.620913372Z"
  208. result, err = calculateDuration(startTime, endTime)
  209. require.NotNil(t, err)
  210. require.EqualValues(t, 0, result)
  211. startTime = "2010-10-02T00:15:07.620912337Z"
  212. endTime = "2020-10-03 15:14:06 +800"
  213. result, err = calculateDuration(startTime, endTime)
  214. require.NotNil(t, err)
  215. require.EqualValues(t, 0, result)
  216. }