utils_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package utils
  13. import (
  14. "testing"
  15. vault "github.com/oracle/oci-go-sdk/v45/vault"
  16. v1 "k8s.io/api/core/v1"
  17. )
  18. func TestObjectHash(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. input interface{}
  22. want string
  23. }{
  24. {
  25. name: "A nil should be still working",
  26. input: nil,
  27. want: "60046f14c917c18a9a0f923e191ba0dc",
  28. },
  29. {
  30. name: "We accept a simple scalar value, i.e. string",
  31. input: "hello there",
  32. want: "161bc25962da8fed6d2f59922fb642aa",
  33. },
  34. {
  35. name: "A complex object like a secret is not an issue",
  36. input: v1.Secret{Data: map[string][]byte{
  37. "xx": []byte("yyy"),
  38. }},
  39. want: "a9fe13fd43b20829b45f0a93372413dd",
  40. },
  41. {
  42. name: "map also works",
  43. input: map[string][]byte{
  44. "foo": []byte("value1"),
  45. "bar": []byte("value2"),
  46. },
  47. want: "caa0155759a6a9b3b6ada5a6883ee2bb",
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. if got := ObjectHash(tt.input); got != tt.want {
  53. t.Errorf("ObjectHash() = %v, want %v", got, tt.want)
  54. }
  55. })
  56. }
  57. }
  58. func TestIsNil(t *testing.T) {
  59. tbl := []struct {
  60. name string
  61. val interface{}
  62. exp bool
  63. }{
  64. {
  65. name: "simple nil val",
  66. val: nil,
  67. exp: true,
  68. },
  69. {
  70. name: "nil slice",
  71. val: (*[]struct{})(nil),
  72. exp: true,
  73. },
  74. {
  75. name: "struct pointer",
  76. val: &testing.T{},
  77. exp: false,
  78. },
  79. {
  80. name: "struct",
  81. val: testing.T{},
  82. exp: false,
  83. },
  84. {
  85. name: "slice of struct",
  86. val: []struct{}{{}},
  87. exp: false,
  88. },
  89. {
  90. name: "slice of ptr",
  91. val: []*testing.T{nil},
  92. exp: false,
  93. },
  94. {
  95. name: "slice",
  96. val: []struct{}(nil),
  97. exp: false,
  98. },
  99. {
  100. name: "int default value",
  101. val: 0,
  102. exp: false,
  103. },
  104. {
  105. name: "empty str",
  106. val: "",
  107. exp: false,
  108. },
  109. {
  110. name: "oracle vault",
  111. val: vault.VaultsClient{},
  112. exp: false,
  113. },
  114. {
  115. name: "func",
  116. val: func() {
  117. // noop for testing and to make linter happy
  118. },
  119. exp: false,
  120. },
  121. {
  122. name: "channel",
  123. val: make(chan struct{}),
  124. exp: false,
  125. },
  126. {
  127. name: "map",
  128. val: map[string]string{},
  129. exp: false,
  130. },
  131. }
  132. for _, row := range tbl {
  133. t.Run(row.name, func(t *testing.T) {
  134. res := IsNil(row.val)
  135. if res != row.exp {
  136. t.Errorf("IsNil(%#v)=%t, expected %t", row.val, res, row.exp)
  137. }
  138. })
  139. }
  140. }