helper_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "reflect"
  19. "testing"
  20. "k8s.io/kubernetes/pkg/api/testapi"
  21. "k8s.io/kubernetes/pkg/api/unversioned"
  22. "k8s.io/kubernetes/pkg/client/restclient"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. )
  25. func TestSetKubernetesDefaults(t *testing.T) {
  26. testCases := []struct {
  27. Config restclient.Config
  28. After restclient.Config
  29. Err bool
  30. }{
  31. {
  32. restclient.Config{},
  33. restclient.Config{
  34. APIPath: "/api",
  35. ContentConfig: restclient.ContentConfig{
  36. GroupVersion: testapi.Default.GroupVersion(),
  37. NegotiatedSerializer: testapi.Default.NegotiatedSerializer(),
  38. },
  39. },
  40. false,
  41. },
  42. // Add this test back when we fixed config and SetKubernetesDefaults
  43. // {
  44. // restclient.Config{
  45. // GroupVersion: &unversioned.GroupVersion{Group: "not.a.group", Version: "not_an_api"},
  46. // },
  47. // restclient.Config{},
  48. // true,
  49. // },
  50. }
  51. for _, testCase := range testCases {
  52. val := &testCase.Config
  53. err := SetKubernetesDefaults(val)
  54. val.UserAgent = ""
  55. switch {
  56. case err == nil && testCase.Err:
  57. t.Errorf("expected error but was nil")
  58. continue
  59. case err != nil && !testCase.Err:
  60. t.Errorf("unexpected error %v", err)
  61. continue
  62. case err != nil:
  63. continue
  64. }
  65. if !reflect.DeepEqual(*val, testCase.After) {
  66. t.Errorf("unexpected result object: %#v", val)
  67. }
  68. }
  69. }
  70. func TestHelperGetServerAPIVersions(t *testing.T) {
  71. expect := []string{"v1", "v2", "v3"}
  72. APIVersions := unversioned.APIVersions{Versions: expect}
  73. expect = append(expect, "group1/v1", "group1/v2", "group2/v1", "group2/v2")
  74. APIGroupList := unversioned.APIGroupList{
  75. Groups: []unversioned.APIGroup{
  76. {
  77. Versions: []unversioned.GroupVersionForDiscovery{
  78. {
  79. GroupVersion: "group1/v1",
  80. },
  81. {
  82. GroupVersion: "group1/v2",
  83. },
  84. },
  85. },
  86. {
  87. Versions: []unversioned.GroupVersionForDiscovery{
  88. {
  89. GroupVersion: "group2/v1",
  90. },
  91. {
  92. GroupVersion: "group2/v2",
  93. },
  94. },
  95. },
  96. },
  97. }
  98. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  99. var output []byte
  100. var err error
  101. switch req.URL.Path {
  102. case "/api":
  103. output, err = json.Marshal(APIVersions)
  104. case "/apis":
  105. output, err = json.Marshal(APIGroupList)
  106. }
  107. if err != nil {
  108. t.Errorf("unexpected encoding error: %v", err)
  109. return
  110. }
  111. w.Header().Set("Content-Type", "application/json")
  112. w.WriteHeader(http.StatusOK)
  113. w.Write(output)
  114. }))
  115. defer server.Close()
  116. got, err := restclient.ServerAPIVersions(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Group: "invalid version", Version: "one"}, NegotiatedSerializer: testapi.Default.NegotiatedSerializer()}})
  117. if err != nil {
  118. t.Fatalf("unexpected encoding error: %v", err)
  119. }
  120. if e, a := expect, got; !reflect.DeepEqual(e, a) {
  121. t.Errorf("expected %v, got %v", e, a)
  122. }
  123. }
  124. func TestSetsCodec(t *testing.T) {
  125. testCases := map[string]struct {
  126. Err bool
  127. Prefix string
  128. NegotiatedSerializer runtime.NegotiatedSerializer
  129. }{
  130. testapi.Default.GroupVersion().Version: {
  131. Err: false,
  132. Prefix: "/api/" + testapi.Default.GroupVersion().Version,
  133. NegotiatedSerializer: testapi.Default.NegotiatedSerializer(),
  134. },
  135. // Add this test back when we fixed config and SetKubernetesDefaults
  136. // "invalidVersion": {true, "", nil},
  137. }
  138. for version, expected := range testCases {
  139. conf := &restclient.Config{
  140. Host: "127.0.0.1",
  141. ContentConfig: restclient.ContentConfig{
  142. GroupVersion: &unversioned.GroupVersion{Version: version},
  143. },
  144. }
  145. var versionedPath string
  146. err := SetKubernetesDefaults(conf)
  147. if err == nil {
  148. _, versionedPath, err = restclient.DefaultServerURL(conf.Host, conf.APIPath, *conf.GroupVersion, false)
  149. }
  150. switch {
  151. case err == nil && expected.Err:
  152. t.Errorf("expected error but was nil")
  153. continue
  154. case err != nil && !expected.Err:
  155. t.Errorf("unexpected error %v", err)
  156. continue
  157. case err != nil:
  158. continue
  159. }
  160. if e, a := expected.Prefix, versionedPath; e != a {
  161. t.Errorf("expected %#v, got %#v", e, a)
  162. }
  163. if e, a := expected.NegotiatedSerializer, conf.NegotiatedSerializer; !reflect.DeepEqual(e, a) {
  164. t.Errorf("expected %#v, got %#v", e, a)
  165. }
  166. }
  167. }