init_dryrun_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2017 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 apiclient
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "testing"
  18. rbac "k8s.io/api/rbac/v1"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. core "k8s.io/client-go/testing"
  21. )
  22. func TestHandleGetAction(t *testing.T) {
  23. controlPlaneName := "control-plane-foo"
  24. serviceSubnet := "10.96.0.1/12"
  25. idr := NewInitDryRunGetter(controlPlaneName, serviceSubnet)
  26. var tests = []struct {
  27. name string
  28. action core.GetActionImpl
  29. expectedHandled bool
  30. expectedObjectJSON []byte
  31. expectedErr bool
  32. }{
  33. {
  34. name: "get default services",
  35. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"),
  36. expectedHandled: true,
  37. expectedObjectJSON: []byte(`{"metadata":{"name":"kubernetes","namespace":"default","creationTimestamp":null,"labels":{"component":"apiserver","provider":"kubernetes"}},"spec":{"ports":[{"name":"https","port":443,"targetPort":6443}],"clusterIP":"10.96.0.1"},"status":{"loadBalancer":{}}}`),
  38. expectedErr: false,
  39. },
  40. {
  41. name: "get nodes",
  42. action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, controlPlaneName),
  43. expectedHandled: true,
  44. expectedObjectJSON: []byte(`{"metadata":{"name":"control-plane-foo","creationTimestamp":null,"labels":{"kubernetes.io/hostname":"control-plane-foo"}},"spec":{},"status":{"daemonEndpoints":{"kubeletEndpoint":{"Port":0}},"nodeInfo":{"machineID":"","systemUUID":"","bootID":"","kernelVersion":"","osImage":"","containerRuntimeVersion":"","kubeletVersion":"","kubeProxyVersion":"","operatingSystem":"","architecture":""}}}`),
  45. expectedErr: false,
  46. },
  47. {
  48. name: "get clusterrolebinings",
  49. action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"),
  50. expectedHandled: true,
  51. expectedObjectJSON: []byte(``),
  52. expectedErr: true, // we expect a NotFound error here
  53. },
  54. {
  55. name: "get kube-system secret bootstrap-token-abcdef",
  56. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, "kube-system", "bootstrap-token-abcdef"),
  57. expectedHandled: true,
  58. expectedObjectJSON: []byte(``),
  59. expectedErr: true, // we expect a NotFound error here
  60. },
  61. { // an ask for a kubernetes service in the _kube-system_ ns should not be answered
  62. name: "get kube-system services",
  63. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "kube-system", "kubernetes"),
  64. expectedHandled: false,
  65. expectedObjectJSON: []byte(``),
  66. expectedErr: false,
  67. },
  68. { // an ask for an other service than kubernetes should not be answered
  69. name: "get default my-other-service",
  70. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "my-other-service"),
  71. expectedHandled: false,
  72. expectedObjectJSON: []byte(``),
  73. expectedErr: false,
  74. },
  75. { // an ask for an other node than the control-plane should not be answered
  76. name: "get other-node",
  77. action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "other-node"),
  78. expectedHandled: false,
  79. expectedObjectJSON: []byte(``),
  80. expectedErr: false,
  81. },
  82. { // an ask for a secret in any other ns than kube-system should not be answered
  83. name: "get default secret bootstrap-token-abcdef",
  84. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, "default", "bootstrap-token-abcdef"),
  85. expectedHandled: false,
  86. expectedObjectJSON: []byte(``),
  87. expectedErr: false,
  88. },
  89. }
  90. for _, rt := range tests {
  91. t.Run(rt.name, func(t *testing.T) {
  92. handled, obj, actualErr := idr.HandleGetAction(rt.action)
  93. objBytes := []byte(``)
  94. if obj != nil {
  95. var err error
  96. objBytes, err = json.Marshal(obj)
  97. if err != nil {
  98. t.Fatalf("couldn't marshal returned object")
  99. }
  100. }
  101. if handled != rt.expectedHandled {
  102. t.Errorf(
  103. "failed HandleGetAction:\n\texpected handled: %t\n\t actual: %t %v",
  104. rt.expectedHandled,
  105. handled,
  106. rt.action,
  107. )
  108. }
  109. if !bytes.Equal(objBytes, rt.expectedObjectJSON) {
  110. t.Errorf(
  111. "failed HandleGetAction:\n\texpected object: %q\n\t actual: %q",
  112. rt.expectedObjectJSON,
  113. objBytes,
  114. )
  115. }
  116. if (actualErr != nil) != rt.expectedErr {
  117. t.Errorf(
  118. "failed HandleGetAction:\n\texpected error: %t\n\t actual: %t %v",
  119. rt.expectedErr,
  120. (actualErr != nil),
  121. rt.action,
  122. )
  123. }
  124. })
  125. }
  126. }