util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2018 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 testing
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "io"
  18. "io/ioutil"
  19. "net/http"
  20. "testing"
  21. corev1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. runtime "k8s.io/apimachinery/pkg/runtime"
  24. restclient "k8s.io/client-go/rest"
  25. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  26. "k8s.io/kubernetes/pkg/kubectl/scheme"
  27. )
  28. var (
  29. grace = int64(30)
  30. enableServiceLinks = corev1.DefaultEnableServiceLinks
  31. )
  32. func DefaultHeader() http.Header {
  33. header := http.Header{}
  34. header.Set("Content-Type", runtime.ContentTypeJSON)
  35. return header
  36. }
  37. func DefaultClientConfig() *restclient.Config {
  38. return &restclient.Config{
  39. APIPath: "/api",
  40. ContentConfig: restclient.ContentConfig{
  41. NegotiatedSerializer: scheme.Codecs,
  42. ContentType: runtime.ContentTypeJSON,
  43. GroupVersion: &corev1.SchemeGroupVersion,
  44. },
  45. }
  46. }
  47. func ObjBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser {
  48. return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj))))
  49. }
  50. func BytesBody(bodyBytes []byte) io.ReadCloser {
  51. return ioutil.NopCloser(bytes.NewReader(bodyBytes))
  52. }
  53. func StringBody(body string) io.ReadCloser {
  54. return ioutil.NopCloser(bytes.NewReader([]byte(body)))
  55. }
  56. func TestData() (*corev1.PodList, *corev1.ServiceList, *corev1.ReplicationControllerList) {
  57. pods := &corev1.PodList{
  58. ListMeta: metav1.ListMeta{
  59. ResourceVersion: "15",
  60. },
  61. Items: []corev1.Pod{
  62. {
  63. ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"},
  64. Spec: corev1.PodSpec{
  65. RestartPolicy: corev1.RestartPolicyAlways,
  66. DNSPolicy: corev1.DNSClusterFirst,
  67. TerminationGracePeriodSeconds: &grace,
  68. SecurityContext: &corev1.PodSecurityContext{},
  69. EnableServiceLinks: &enableServiceLinks,
  70. },
  71. },
  72. {
  73. ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "test", ResourceVersion: "11"},
  74. Spec: corev1.PodSpec{
  75. RestartPolicy: corev1.RestartPolicyAlways,
  76. DNSPolicy: corev1.DNSClusterFirst,
  77. TerminationGracePeriodSeconds: &grace,
  78. SecurityContext: &corev1.PodSecurityContext{},
  79. EnableServiceLinks: &enableServiceLinks,
  80. },
  81. },
  82. },
  83. }
  84. svc := &corev1.ServiceList{
  85. ListMeta: metav1.ListMeta{
  86. ResourceVersion: "16",
  87. },
  88. Items: []corev1.Service{
  89. {
  90. ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
  91. Spec: corev1.ServiceSpec{
  92. SessionAffinity: "None",
  93. Type: corev1.ServiceTypeClusterIP,
  94. },
  95. },
  96. },
  97. }
  98. one := int32(1)
  99. rc := &corev1.ReplicationControllerList{
  100. ListMeta: metav1.ListMeta{
  101. ResourceVersion: "17",
  102. },
  103. Items: []corev1.ReplicationController{
  104. {
  105. ObjectMeta: metav1.ObjectMeta{Name: "rc1", Namespace: "test", ResourceVersion: "18"},
  106. Spec: corev1.ReplicationControllerSpec{
  107. Replicas: &one,
  108. },
  109. },
  110. },
  111. }
  112. return pods, svc, rc
  113. }
  114. func GenResponseWithJsonEncodedBody(bodyStruct interface{}) (*http.Response, error) {
  115. jsonBytes, err := json.Marshal(bodyStruct)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return &http.Response{StatusCode: 200, Header: DefaultHeader(), Body: BytesBody(jsonBytes)}, nil
  120. }
  121. func InitTestErrorHandler(t *testing.T) {
  122. cmdutil.BehaviorOnFatal(func(str string, code int) {
  123. t.Errorf("Error running command (exit code %d): %s", code, str)
  124. })
  125. }