top_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "io"
  18. "io/ioutil"
  19. "time"
  20. metrics_api "k8s.io/heapster/metrics/apis/metrics/v1alpha1"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/api/resource"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. v1 "k8s.io/kubernetes/pkg/api/v1"
  25. "testing"
  26. )
  27. const (
  28. baseHeapsterServiceAddress = "/api/v1/namespaces/kube-system/services/http:heapster:"
  29. baseMetricsAddress = baseHeapsterServiceAddress + "/proxy/apis/metrics"
  30. metricsApiVersion = "v1alpha1"
  31. )
  32. func TestTopSubcommandsExist(t *testing.T) {
  33. initTestErrorHandler(t)
  34. f, _, _, _ := NewAPIFactory()
  35. buf := bytes.NewBuffer([]byte{})
  36. cmd := NewCmdTop(f, buf)
  37. if !cmd.HasSubCommands() {
  38. t.Error("top command should have subcommands")
  39. }
  40. }
  41. func marshallBody(metrics interface{}) (io.ReadCloser, error) {
  42. result, err := json.Marshal(metrics)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return ioutil.NopCloser(bytes.NewReader(result)), nil
  47. }
  48. func testNodeMetricsData() (*metrics_api.NodeMetricsList, *api.NodeList) {
  49. metrics := &metrics_api.NodeMetricsList{
  50. ListMeta: unversioned.ListMeta{
  51. ResourceVersion: "1",
  52. },
  53. Items: []metrics_api.NodeMetrics{
  54. {
  55. ObjectMeta: v1.ObjectMeta{Name: "node1", ResourceVersion: "10"},
  56. Window: unversioned.Duration{Duration: time.Minute},
  57. Usage: v1.ResourceList{
  58. v1.ResourceCPU: *resource.NewMilliQuantity(1, resource.DecimalSI),
  59. v1.ResourceMemory: *resource.NewQuantity(2*(1024*1024), resource.DecimalSI),
  60. v1.ResourceStorage: *resource.NewQuantity(3*(1024*1024), resource.DecimalSI),
  61. },
  62. },
  63. {
  64. ObjectMeta: v1.ObjectMeta{Name: "node2", ResourceVersion: "11"},
  65. Window: unversioned.Duration{Duration: time.Minute},
  66. Usage: v1.ResourceList{
  67. v1.ResourceCPU: *resource.NewMilliQuantity(5, resource.DecimalSI),
  68. v1.ResourceMemory: *resource.NewQuantity(6*(1024*1024), resource.DecimalSI),
  69. v1.ResourceStorage: *resource.NewQuantity(7*(1024*1024), resource.DecimalSI),
  70. },
  71. },
  72. },
  73. }
  74. nodes := &api.NodeList{
  75. ListMeta: unversioned.ListMeta{
  76. ResourceVersion: "15",
  77. },
  78. Items: []api.Node{
  79. {
  80. ObjectMeta: api.ObjectMeta{Name: "node1", ResourceVersion: "10"},
  81. Status: api.NodeStatus{
  82. Allocatable: api.ResourceList{
  83. api.ResourceCPU: *resource.NewMilliQuantity(10, resource.DecimalSI),
  84. api.ResourceMemory: *resource.NewQuantity(20*(1024*1024), resource.DecimalSI),
  85. api.ResourceStorage: *resource.NewQuantity(30*(1024*1024), resource.DecimalSI),
  86. },
  87. },
  88. },
  89. {
  90. ObjectMeta: api.ObjectMeta{Name: "node2", ResourceVersion: "11"},
  91. Status: api.NodeStatus{
  92. Allocatable: api.ResourceList{
  93. api.ResourceCPU: *resource.NewMilliQuantity(50, resource.DecimalSI),
  94. api.ResourceMemory: *resource.NewQuantity(60*(1024*1024), resource.DecimalSI),
  95. api.ResourceStorage: *resource.NewQuantity(70*(1024*1024), resource.DecimalSI),
  96. },
  97. },
  98. },
  99. },
  100. }
  101. return metrics, nodes
  102. }
  103. func testPodMetricsData() *metrics_api.PodMetricsList {
  104. return &metrics_api.PodMetricsList{
  105. ListMeta: unversioned.ListMeta{
  106. ResourceVersion: "2",
  107. },
  108. Items: []metrics_api.PodMetrics{
  109. {
  110. ObjectMeta: v1.ObjectMeta{Name: "pod1", Namespace: "test", ResourceVersion: "10"},
  111. Window: unversioned.Duration{Duration: time.Minute},
  112. Containers: []metrics_api.ContainerMetrics{
  113. {
  114. Name: "container1-1",
  115. Usage: v1.ResourceList{
  116. v1.ResourceCPU: *resource.NewMilliQuantity(1, resource.DecimalSI),
  117. v1.ResourceMemory: *resource.NewQuantity(2*(1024*1024), resource.DecimalSI),
  118. v1.ResourceStorage: *resource.NewQuantity(3*(1024*1024), resource.DecimalSI),
  119. },
  120. },
  121. {
  122. Name: "container1-2",
  123. Usage: v1.ResourceList{
  124. v1.ResourceCPU: *resource.NewMilliQuantity(4, resource.DecimalSI),
  125. v1.ResourceMemory: *resource.NewQuantity(5*(1024*1024), resource.DecimalSI),
  126. v1.ResourceStorage: *resource.NewQuantity(6*(1024*1024), resource.DecimalSI),
  127. },
  128. },
  129. },
  130. },
  131. {
  132. ObjectMeta: v1.ObjectMeta{Name: "pod2", Namespace: "test", ResourceVersion: "11"},
  133. Window: unversioned.Duration{Duration: time.Minute},
  134. Containers: []metrics_api.ContainerMetrics{
  135. {
  136. Name: "container2-1",
  137. Usage: v1.ResourceList{
  138. v1.ResourceCPU: *resource.NewMilliQuantity(7, resource.DecimalSI),
  139. v1.ResourceMemory: *resource.NewQuantity(8*(1024*1024), resource.DecimalSI),
  140. v1.ResourceStorage: *resource.NewQuantity(9*(1024*1024), resource.DecimalSI),
  141. },
  142. },
  143. {
  144. Name: "container2-2",
  145. Usage: v1.ResourceList{
  146. v1.ResourceCPU: *resource.NewMilliQuantity(10, resource.DecimalSI),
  147. v1.ResourceMemory: *resource.NewQuantity(11*(1024*1024), resource.DecimalSI),
  148. v1.ResourceStorage: *resource.NewQuantity(12*(1024*1024), resource.DecimalSI),
  149. },
  150. },
  151. {
  152. Name: "container2-3",
  153. Usage: v1.ResourceList{
  154. v1.ResourceCPU: *resource.NewMilliQuantity(13, resource.DecimalSI),
  155. v1.ResourceMemory: *resource.NewQuantity(14*(1024*1024), resource.DecimalSI),
  156. v1.ResourceStorage: *resource.NewQuantity(15*(1024*1024), resource.DecimalSI),
  157. },
  158. },
  159. },
  160. },
  161. {
  162. ObjectMeta: v1.ObjectMeta{Name: "pod3", Namespace: "test", ResourceVersion: "12"},
  163. Window: unversioned.Duration{Duration: time.Minute},
  164. Containers: []metrics_api.ContainerMetrics{
  165. {
  166. Name: "container3-1",
  167. Usage: v1.ResourceList{
  168. v1.ResourceCPU: *resource.NewMilliQuantity(7, resource.DecimalSI),
  169. v1.ResourceMemory: *resource.NewQuantity(8*(1024*1024), resource.DecimalSI),
  170. v1.ResourceStorage: *resource.NewQuantity(9*(1024*1024), resource.DecimalSI),
  171. },
  172. },
  173. },
  174. },
  175. },
  176. }
  177. }