winstats_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package winstats
  15. import (
  16. "testing"
  17. "time"
  18. cadvisorapi "github.com/google/cadvisor/info/v1"
  19. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. var timeStamp = time.Now()
  23. type fakeWinNodeStatsClient struct{}
  24. func (f fakeWinNodeStatsClient) startMonitoring() error {
  25. return nil
  26. }
  27. func (f fakeWinNodeStatsClient) getNodeMetrics() (nodeMetrics, error) {
  28. return nodeMetrics{
  29. cpuUsageCoreNanoSeconds: 123,
  30. cpuUsageNanoCores: 23,
  31. memoryPrivWorkingSetBytes: 1234,
  32. memoryCommittedBytes: 12345,
  33. timeStamp: timeStamp,
  34. }, nil
  35. }
  36. func (f fakeWinNodeStatsClient) getNodeInfo() nodeInfo {
  37. return nodeInfo{
  38. kernelVersion: "v42",
  39. memoryPhysicalCapacityBytes: 1.6e+10,
  40. }
  41. }
  42. func (f fakeWinNodeStatsClient) getMachineInfo() (*cadvisorapi.MachineInfo, error) {
  43. return &cadvisorapi.MachineInfo{
  44. NumCores: 4,
  45. MemoryCapacity: 1.6e+10,
  46. MachineID: "somehostname",
  47. SystemUUID: "E6C8AC43-582B-3575-4E1F-6DA170888906",
  48. }, nil
  49. }
  50. func (f fakeWinNodeStatsClient) getVersionInfo() (*cadvisorapi.VersionInfo, error) {
  51. return &cadvisorapi.VersionInfo{
  52. KernelVersion: "v42",
  53. }, nil
  54. }
  55. func TestWinContainerInfos(t *testing.T) {
  56. c := getClient(t)
  57. actualRootInfos, err := c.WinContainerInfos()
  58. assert.NoError(t, err)
  59. var stats []*cadvisorapiv2.ContainerStats
  60. stats = append(stats, &cadvisorapiv2.ContainerStats{
  61. Timestamp: timeStamp,
  62. Cpu: &cadvisorapi.CpuStats{
  63. Usage: cadvisorapi.CpuUsage{
  64. Total: 123,
  65. },
  66. },
  67. CpuInst: &cadvisorapiv2.CpuInstStats{
  68. Usage: cadvisorapiv2.CpuInstUsage{
  69. Total: 23,
  70. },
  71. },
  72. Memory: &cadvisorapi.MemoryStats{
  73. WorkingSet: 1234,
  74. Usage: 12345,
  75. },
  76. })
  77. infos := make(map[string]cadvisorapiv2.ContainerInfo)
  78. infos["/"] = cadvisorapiv2.ContainerInfo{
  79. Spec: cadvisorapiv2.ContainerSpec{
  80. HasCpu: true,
  81. HasMemory: true,
  82. HasNetwork: true,
  83. Memory: cadvisorapiv2.MemorySpec{
  84. Limit: 1.6e+10,
  85. },
  86. },
  87. Stats: stats,
  88. }
  89. assert.Equal(t, len(actualRootInfos), len(infos))
  90. assert.Equal(t, actualRootInfos["/"].Spec, infos["/"].Spec)
  91. assert.Equal(t, len(actualRootInfos["/"].Stats), len(infos["/"].Stats))
  92. assert.Equal(t, actualRootInfos["/"].Stats[0].Cpu, infos["/"].Stats[0].Cpu)
  93. assert.Equal(t, actualRootInfos["/"].Stats[0].CpuInst, infos["/"].Stats[0].CpuInst)
  94. assert.Equal(t, actualRootInfos["/"].Stats[0].Memory, infos["/"].Stats[0].Memory)
  95. }
  96. func TestWinMachineInfo(t *testing.T) {
  97. c := getClient(t)
  98. machineInfo, err := c.WinMachineInfo()
  99. assert.NoError(t, err)
  100. assert.Equal(t, machineInfo, &cadvisorapi.MachineInfo{
  101. NumCores: 4,
  102. MemoryCapacity: 1.6e+10,
  103. MachineID: "somehostname",
  104. SystemUUID: "E6C8AC43-582B-3575-4E1F-6DA170888906"})
  105. }
  106. func TestWinVersionInfo(t *testing.T) {
  107. c := getClient(t)
  108. versionInfo, err := c.WinVersionInfo()
  109. assert.NoError(t, err)
  110. assert.Equal(t, versionInfo, &cadvisorapi.VersionInfo{
  111. KernelVersion: "v42"})
  112. }
  113. func TestConvertCPUValue(t *testing.T) {
  114. testCases := []struct {
  115. cpuValue uint64
  116. expected uint64
  117. }{
  118. {cpuValue: uint64(50), expected: uint64(2000000000)},
  119. {cpuValue: uint64(0), expected: uint64(0)},
  120. {cpuValue: uint64(100), expected: uint64(4000000000)},
  121. }
  122. var cpuCores = 4
  123. for _, tc := range testCases {
  124. p := perfCounterNodeStatsClient{}
  125. newValue := p.convertCPUValue(cpuCores, tc.cpuValue)
  126. assert.Equal(t, newValue, tc.expected)
  127. }
  128. }
  129. func TestGetCPUUsageNanoCores(t *testing.T) {
  130. testCases := []struct {
  131. latestValue uint64
  132. previousValue uint64
  133. expected uint64
  134. }{
  135. {latestValue: uint64(0), previousValue: uint64(0), expected: uint64(0)},
  136. {latestValue: uint64(2000000000), previousValue: uint64(0), expected: uint64(200000000)},
  137. {latestValue: uint64(5000000000), previousValue: uint64(2000000000), expected: uint64(300000000)},
  138. }
  139. for _, tc := range testCases {
  140. p := perfCounterNodeStatsClient{}
  141. p.cpuUsageCoreNanoSecondsCache = cpuUsageCoreNanoSecondsCache{
  142. latestValue: tc.latestValue,
  143. previousValue: tc.previousValue,
  144. }
  145. cpuUsageNanoCores := p.getCPUUsageNanoCores()
  146. assert.Equal(t, cpuUsageNanoCores, tc.expected)
  147. }
  148. }
  149. func getClient(t *testing.T) Client {
  150. f := fakeWinNodeStatsClient{}
  151. c, err := newClient(f)
  152. assert.NoError(t, err)
  153. assert.NotNil(t, c)
  154. return c
  155. }