create_secret_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 create
  14. import (
  15. "net/http"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. "k8s.io/client-go/rest/fake"
  21. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  22. "k8s.io/kubernetes/pkg/kubectl/scheme"
  23. )
  24. func TestCreateSecretGeneric(t *testing.T) {
  25. secretObject := &v1.Secret{
  26. Data: map[string][]byte{
  27. "password": []byte("includes,comma"),
  28. "username": []byte("test_user"),
  29. },
  30. }
  31. secretObject.Name = "my-secret"
  32. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  33. defer tf.Cleanup()
  34. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  35. ns := scheme.Codecs
  36. tf.Client = &fake.RESTClient{
  37. GroupVersion: schema.GroupVersion{Version: "v1"},
  38. NegotiatedSerializer: ns,
  39. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  40. switch p, m := req.URL.Path, req.Method; {
  41. case p == "/namespaces/test/secrets" && m == "POST":
  42. return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, secretObject)}, nil
  43. default:
  44. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  45. return nil, nil
  46. }
  47. }),
  48. }
  49. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  50. cmd := NewCmdCreateSecretGeneric(tf, ioStreams)
  51. cmd.Flags().Set("output", "name")
  52. cmd.Flags().Set("from-literal", "password=includes,comma")
  53. cmd.Flags().Set("from-literal", "username=test_user")
  54. cmd.Run(cmd, []string{secretObject.Name})
  55. expectedOutput := "secret/" + secretObject.Name + "\n"
  56. if buf.String() != expectedOutput {
  57. t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
  58. }
  59. }
  60. func TestCreateSecretDockerRegistry(t *testing.T) {
  61. secretObject := &v1.Secret{}
  62. secretObject.Name = "my-secret"
  63. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  64. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  65. ns := scheme.Codecs
  66. tf.Client = &fake.RESTClient{
  67. GroupVersion: schema.GroupVersion{Version: "v1"},
  68. NegotiatedSerializer: ns,
  69. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  70. switch p, m := req.URL.Path, req.Method; {
  71. case p == "/namespaces/test/secrets" && m == "POST":
  72. return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, secretObject)}, nil
  73. default:
  74. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  75. return nil, nil
  76. }
  77. }),
  78. }
  79. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  80. cmd := NewCmdCreateSecretDockerRegistry(tf, ioStreams)
  81. cmd.Flags().Set("docker-username", "test-user")
  82. cmd.Flags().Set("docker-password", "test-pass")
  83. cmd.Flags().Set("docker-email", "test-email")
  84. cmd.Flags().Set("output", "name")
  85. cmd.Run(cmd, []string{secretObject.Name})
  86. expectedOutput := "secret/" + secretObject.Name + "\n"
  87. if buf.String() != expectedOutput {
  88. t.Errorf("expected output: %s, but got: %s", buf.String(), expectedOutput)
  89. }
  90. }