rest_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2015 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 componentstatus
  14. import (
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. "net/http"
  20. "net/url"
  21. "time"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/apiserver"
  24. "k8s.io/kubernetes/pkg/probe"
  25. "k8s.io/kubernetes/pkg/util/diff"
  26. )
  27. type fakeHttpProber struct {
  28. result probe.Result
  29. body string
  30. err error
  31. }
  32. func (f *fakeHttpProber) Probe(*url.URL, http.Header, time.Duration) (probe.Result, string, error) {
  33. return f.result, f.body, f.err
  34. }
  35. type testResponse struct {
  36. result probe.Result
  37. data string
  38. err error
  39. }
  40. func NewTestREST(resp testResponse) *REST {
  41. return &REST{
  42. GetServersToValidate: func() map[string]apiserver.Server {
  43. return map[string]apiserver.Server{
  44. "test1": {Addr: "testserver1", Port: 8000, Path: "/healthz"},
  45. }
  46. },
  47. prober: &fakeHttpProber{
  48. result: resp.result,
  49. body: resp.data,
  50. err: resp.err,
  51. },
  52. }
  53. }
  54. func createTestStatus(name string, status api.ConditionStatus, msg string, err string) *api.ComponentStatus {
  55. retVal := &api.ComponentStatus{
  56. Conditions: []api.ComponentCondition{
  57. {Type: api.ComponentHealthy, Status: status, Message: msg, Error: err},
  58. },
  59. }
  60. retVal.Name = name
  61. return retVal
  62. }
  63. func TestList_NoError(t *testing.T) {
  64. r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
  65. got, err := r.List(api.NewContext(), nil)
  66. if err != nil {
  67. t.Fatalf("Unexpected error: %v", err)
  68. }
  69. expect := &api.ComponentStatusList{
  70. Items: []api.ComponentStatus{*(createTestStatus("test1", api.ConditionTrue, "ok", ""))},
  71. }
  72. if e, a := expect, got; !reflect.DeepEqual(e, a) {
  73. t.Errorf("Got unexpected object. Diff: %s", diff.ObjectDiff(e, a))
  74. }
  75. }
  76. func TestList_FailedCheck(t *testing.T) {
  77. r := NewTestREST(testResponse{result: probe.Failure, data: ""})
  78. got, err := r.List(api.NewContext(), nil)
  79. if err != nil {
  80. t.Fatalf("Unexpected error: %v", err)
  81. }
  82. expect := &api.ComponentStatusList{
  83. Items: []api.ComponentStatus{
  84. *(createTestStatus("test1", api.ConditionFalse, "", ""))},
  85. }
  86. if e, a := expect, got; !reflect.DeepEqual(e, a) {
  87. t.Errorf("Got unexpected object. Diff: %s", diff.ObjectDiff(e, a))
  88. }
  89. }
  90. func TestList_UnknownError(t *testing.T) {
  91. r := NewTestREST(testResponse{result: probe.Unknown, data: "", err: fmt.Errorf("fizzbuzz error")})
  92. got, err := r.List(api.NewContext(), nil)
  93. if err != nil {
  94. t.Fatalf("Unexpected error: %v", err)
  95. }
  96. expect := &api.ComponentStatusList{
  97. Items: []api.ComponentStatus{
  98. *(createTestStatus("test1", api.ConditionUnknown, "", "fizzbuzz error"))},
  99. }
  100. if e, a := expect, got; !reflect.DeepEqual(e, a) {
  101. t.Errorf("Got unexpected object. Diff: %s", diff.ObjectDiff(e, a))
  102. }
  103. }
  104. func TestGet_NoError(t *testing.T) {
  105. r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
  106. got, err := r.Get(api.NewContext(), "test1")
  107. if err != nil {
  108. t.Fatalf("Unexpected error: %v", err)
  109. }
  110. expect := createTestStatus("test1", api.ConditionTrue, "ok", "")
  111. if e, a := expect, got; !reflect.DeepEqual(e, a) {
  112. t.Errorf("Got unexpected object. Diff: %s", diff.ObjectDiff(e, a))
  113. }
  114. }
  115. func TestGet_BadName(t *testing.T) {
  116. r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
  117. _, err := r.Get(api.NewContext(), "invalidname")
  118. if err == nil {
  119. t.Fatalf("Expected error, but did not get one")
  120. }
  121. if !strings.Contains(err.Error(), "Component not found: invalidname") {
  122. t.Fatalf("Got unexpected error: %v", err)
  123. }
  124. }