dryrunclient_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "testing"
  17. "k8s.io/api/core/v1"
  18. rbac "k8s.io/api/rbac/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. core "k8s.io/client-go/testing"
  22. )
  23. func TestLogDryRunAction(t *testing.T) {
  24. var tests = []struct {
  25. name string
  26. action core.Action
  27. expectedBytes []byte
  28. buf *bytes.Buffer
  29. }{
  30. {
  31. name: "action GET on services",
  32. action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"),
  33. expectedBytes: []byte(`[dryrun] Would perform action GET on resource "services" in API group "core/v1"
  34. [dryrun] Resource name: "kubernetes"
  35. `),
  36. },
  37. {
  38. name: "action GET on clusterrolebindings",
  39. action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"),
  40. expectedBytes: []byte(`[dryrun] Would perform action GET on resource "clusterrolebindings" in API group "rbac.authorization.k8s.io/v1"
  41. [dryrun] Resource name: "system:node"
  42. `),
  43. },
  44. {
  45. name: "action LIST on services",
  46. action: core.NewListAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, schema.GroupVersionKind{Version: "v1", Kind: "Service"}, "default", metav1.ListOptions{}),
  47. expectedBytes: []byte(`[dryrun] Would perform action LIST on resource "services" in API group "core/v1"
  48. `),
  49. },
  50. {
  51. name: "action CREATE on services",
  52. action: core.NewCreateAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", &v1.Service{
  53. ObjectMeta: metav1.ObjectMeta{
  54. Name: "foo",
  55. },
  56. Spec: v1.ServiceSpec{
  57. ClusterIP: "1.1.1.1",
  58. },
  59. }),
  60. expectedBytes: []byte(`[dryrun] Would perform action CREATE on resource "services" in API group "core/v1"
  61. apiVersion: v1
  62. kind: Service
  63. metadata:
  64. creationTimestamp: null
  65. name: foo
  66. spec:
  67. clusterIP: 1.1.1.1
  68. status:
  69. loadBalancer: {}
  70. `),
  71. },
  72. {
  73. name: "action PATCH on nodes",
  74. action: core.NewPatchAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "default", "my-node", "application/strategic-merge-patch+json", []byte(`{"spec":{"taints":[{"key": "foo", "value": "bar"}]}}`)),
  75. expectedBytes: []byte(`[dryrun] Would perform action PATCH on resource "nodes" in API group "core/v1"
  76. [dryrun] Resource name: "my-node"
  77. [dryrun] Attached patch:
  78. {"spec":{"taints":[{"key": "foo", "value": "bar"}]}}
  79. `),
  80. },
  81. {
  82. name: "action DELETE on pods",
  83. action: core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "pods"}, "default", "my-pod"),
  84. expectedBytes: []byte(`[dryrun] Would perform action DELETE on resource "pods" in API group "core/v1"
  85. [dryrun] Resource name: "my-pod"
  86. `),
  87. },
  88. }
  89. for _, rt := range tests {
  90. t.Run(rt.name, func(t *testing.T) {
  91. rt.buf = bytes.NewBufferString("")
  92. logDryRunAction(rt.action, rt.buf, DefaultMarshalFunc)
  93. actualBytes := rt.buf.Bytes()
  94. if !bytes.Equal(actualBytes, rt.expectedBytes) {
  95. t.Errorf(
  96. "failed LogDryRunAction:\n\texpected bytes: %q\n\t actual: %q",
  97. rt.expectedBytes,
  98. actualBytes,
  99. )
  100. }
  101. })
  102. }
  103. }