containerinfo_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2014 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 unversioned
  14. import (
  15. "encoding/json"
  16. "net/http"
  17. "net/http/httptest"
  18. "net/url"
  19. "path"
  20. "reflect"
  21. "strconv"
  22. "strings"
  23. "testing"
  24. "time"
  25. cadvisorapi "github.com/google/cadvisor/info/v1"
  26. cadvisorapitest "github.com/google/cadvisor/info/v1/test"
  27. )
  28. func testHTTPContainerInfoGetter(
  29. req *cadvisorapi.ContainerInfoRequest,
  30. cinfo *cadvisorapi.ContainerInfo,
  31. podID string,
  32. containerID string,
  33. status int,
  34. t *testing.T,
  35. ) {
  36. expectedPath := "/stats"
  37. if len(podID) > 0 && len(containerID) > 0 {
  38. expectedPath = path.Join(expectedPath, podID, containerID)
  39. }
  40. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  41. if status != 0 {
  42. w.WriteHeader(status)
  43. }
  44. if strings.TrimRight(r.URL.Path, "/") != strings.TrimRight(expectedPath, "/") {
  45. t.Fatalf("Received request to an invalid path. Should be %v. got %v",
  46. expectedPath, r.URL.Path)
  47. }
  48. var receivedReq cadvisorapi.ContainerInfoRequest
  49. err := json.NewDecoder(r.Body).Decode(&receivedReq)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. // Note: This will not make a deep copy of req.
  54. // So changing req after Get*Info would be a race.
  55. expectedReq := req
  56. // Fill any empty fields with default value
  57. if !expectedReq.Equals(receivedReq) {
  58. t.Errorf("received wrong request")
  59. }
  60. err = json.NewEncoder(w).Encode(cinfo)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }))
  65. defer ts.Close()
  66. hostURL, err := url.Parse(ts.URL)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. parts := strings.Split(hostURL.Host, ":")
  71. port, err := strconv.Atoi(parts[1])
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. containerInfoGetter := &HTTPContainerInfoGetter{
  76. Client: http.DefaultClient,
  77. Port: port,
  78. }
  79. var receivedContainerInfo *cadvisorapi.ContainerInfo
  80. if len(podID) > 0 && len(containerID) > 0 {
  81. receivedContainerInfo, err = containerInfoGetter.GetContainerInfo(parts[0], podID, containerID, req)
  82. } else {
  83. receivedContainerInfo, err = containerInfoGetter.GetRootInfo(parts[0], req)
  84. }
  85. if status == 0 || status == http.StatusOK {
  86. if err != nil {
  87. t.Errorf("received unexpected error: %v", err)
  88. }
  89. if !receivedContainerInfo.Eq(cinfo) {
  90. t.Error("received unexpected container info")
  91. }
  92. } else {
  93. if err == nil {
  94. t.Error("did not receive expected error.")
  95. }
  96. }
  97. }
  98. func TestHTTPContainerInfoGetterGetContainerInfoSuccessfully(t *testing.T) {
  99. req := &cadvisorapi.ContainerInfoRequest{
  100. NumStats: 10,
  101. }
  102. cinfo := cadvisorapitest.GenerateRandomContainerInfo(
  103. "dockerIDWhichWillNotBeChecked", // docker ID
  104. 2, // Number of cores
  105. req,
  106. 1*time.Second,
  107. )
  108. testHTTPContainerInfoGetter(req, cinfo, "somePodID", "containerNameInK8S", 0, t)
  109. }
  110. func TestHTTPContainerInfoGetterGetRootInfoSuccessfully(t *testing.T) {
  111. req := &cadvisorapi.ContainerInfoRequest{
  112. NumStats: 10,
  113. }
  114. cinfo := cadvisorapitest.GenerateRandomContainerInfo(
  115. "dockerIDWhichWillNotBeChecked", // docker ID
  116. 2, // Number of cores
  117. req,
  118. 1*time.Second,
  119. )
  120. testHTTPContainerInfoGetter(req, cinfo, "", "", 0, t)
  121. }
  122. func TestHTTPContainerInfoGetterGetContainerInfoWithError(t *testing.T) {
  123. req := &cadvisorapi.ContainerInfoRequest{
  124. NumStats: 10,
  125. }
  126. cinfo := cadvisorapitest.GenerateRandomContainerInfo(
  127. "dockerIDWhichWillNotBeChecked", // docker ID
  128. 2, // Number of cores
  129. req,
  130. 1*time.Second,
  131. )
  132. testHTTPContainerInfoGetter(req, cinfo, "somePodID", "containerNameInK8S", http.StatusNotFound, t)
  133. }
  134. func TestHTTPContainerInfoGetterGetRootInfoWithError(t *testing.T) {
  135. req := &cadvisorapi.ContainerInfoRequest{
  136. NumStats: 10,
  137. }
  138. cinfo := cadvisorapitest.GenerateRandomContainerInfo(
  139. "dockerIDWhichWillNotBeChecked", // docker ID
  140. 2, // Number of cores
  141. req,
  142. 1*time.Second,
  143. )
  144. testHTTPContainerInfoGetter(req, cinfo, "", "", http.StatusNotFound, t)
  145. }
  146. func TestHTTPGetMachineInfo(t *testing.T) {
  147. mspec := &cadvisorapi.MachineInfo{
  148. NumCores: 4,
  149. MemoryCapacity: 2048,
  150. }
  151. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  152. err := json.NewEncoder(w).Encode(mspec)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. }))
  157. defer ts.Close()
  158. hostURL, err := url.Parse(ts.URL)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. parts := strings.Split(hostURL.Host, ":")
  163. port, err := strconv.Atoi(parts[1])
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. containerInfoGetter := &HTTPContainerInfoGetter{
  168. Client: http.DefaultClient,
  169. Port: port,
  170. }
  171. received, err := containerInfoGetter.GetMachineInfo(parts[0])
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if !reflect.DeepEqual(received, mspec) {
  176. t.Errorf("received wrong machine spec")
  177. }
  178. }