certificate_controller_utils_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 certificates
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "k8s.io/api/certificates/v1beta1"
  18. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. )
  20. func TestIsCertificateRequestApproved(t *testing.T) {
  21. testCases := []struct {
  22. name string
  23. conditions []v1beta1.CertificateSigningRequestCondition
  24. expectedIsApproved bool
  25. }{
  26. {
  27. "Not any conditions exist",
  28. nil,
  29. false,
  30. }, {
  31. "Approved not exist and Denied exist",
  32. []v1beta1.CertificateSigningRequestCondition{
  33. {
  34. Type: v1beta1.CertificateDenied,
  35. },
  36. },
  37. false,
  38. }, {
  39. "Approved exist and Denied not exist",
  40. []v1beta1.CertificateSigningRequestCondition{
  41. {
  42. Type: v1beta1.CertificateApproved,
  43. },
  44. },
  45. true,
  46. }, {
  47. "Both of Approved and Denied exist",
  48. []v1beta1.CertificateSigningRequestCondition{
  49. {
  50. Type: v1beta1.CertificateApproved,
  51. },
  52. {
  53. Type: v1beta1.CertificateDenied,
  54. },
  55. },
  56. false,
  57. },
  58. }
  59. for _, tc := range testCases {
  60. csr := &v1beta1.CertificateSigningRequest{
  61. ObjectMeta: v1.ObjectMeta{
  62. Name: "fake-csr",
  63. },
  64. Status: v1beta1.CertificateSigningRequestStatus{
  65. Conditions: tc.conditions,
  66. },
  67. }
  68. assert.Equalf(t, tc.expectedIsApproved, IsCertificateRequestApproved(csr), "Failed to test: %s", tc.name)
  69. }
  70. }