authorizer_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package gcp
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestSelectAuthorizerByType(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. authorizerType string
  13. expectError bool
  14. }{
  15. {
  16. name: "ServiceAccountKey type",
  17. authorizerType: ServiceAccountKeyAuthorizerType,
  18. expectError: false,
  19. },
  20. {
  21. name: "WorkloadIdentity type",
  22. authorizerType: WorkloadIdentityAuthorizerType,
  23. expectError: false,
  24. },
  25. {
  26. name: "Invalid type",
  27. authorizerType: "InvalidType",
  28. expectError: true,
  29. },
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. authorizer, err := SelectAuthorizerByType(tt.authorizerType)
  34. if tt.expectError {
  35. assert.Error(t, err)
  36. assert.Nil(t, authorizer)
  37. } else {
  38. assert.NoError(t, err)
  39. assert.NotNil(t, authorizer)
  40. }
  41. })
  42. }
  43. }
  44. func TestServiceAccountKey_MarshalJSON(t *testing.T) {
  45. key := &ServiceAccountKey{
  46. Key: map[string]string{
  47. "type": "service_account",
  48. "project_id": "test-project",
  49. },
  50. }
  51. data, err := json.Marshal(key)
  52. require.NoError(t, err)
  53. var result map[string]interface{}
  54. err = json.Unmarshal(data, &result)
  55. require.NoError(t, err)
  56. assert.Equal(t, ServiceAccountKeyAuthorizerType, result["authorizerType"])
  57. assert.NotNil(t, result["key"])
  58. }
  59. func TestServiceAccountKey_Validate(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. key map[string]string
  63. expectError bool
  64. }{
  65. {
  66. name: "Valid key",
  67. key: map[string]string{
  68. "type": "service_account",
  69. },
  70. expectError: false,
  71. },
  72. {
  73. name: "Nil key",
  74. key: nil,
  75. expectError: true,
  76. },
  77. {
  78. name: "Empty key",
  79. key: map[string]string{},
  80. expectError: true,
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. saKey := &ServiceAccountKey{Key: tt.key}
  86. err := saKey.Validate()
  87. if tt.expectError {
  88. assert.Error(t, err)
  89. } else {
  90. assert.NoError(t, err)
  91. }
  92. })
  93. }
  94. }
  95. func TestServiceAccountKey_Equals(t *testing.T) {
  96. key1 := &ServiceAccountKey{
  97. Key: map[string]string{"type": "service_account"},
  98. }
  99. key2 := &ServiceAccountKey{
  100. Key: map[string]string{"type": "service_account"},
  101. }
  102. key3 := &ServiceAccountKey{
  103. Key: map[string]string{"type": "different"},
  104. }
  105. workloadIdentity := &WorkloadIdentity{}
  106. tests := []struct {
  107. name string
  108. config1 cloud.Config
  109. config2 cloud.Config
  110. expected bool
  111. }{
  112. {
  113. name: "Same keys",
  114. config1: key1,
  115. config2: key2,
  116. expected: true,
  117. },
  118. {
  119. name: "Different keys",
  120. config1: key1,
  121. config2: key3,
  122. expected: false,
  123. },
  124. {
  125. name: "Different types",
  126. config1: key1,
  127. config2: workloadIdentity,
  128. expected: false,
  129. },
  130. {
  131. name: "Nil config",
  132. config1: key1,
  133. config2: nil,
  134. expected: false,
  135. },
  136. }
  137. for _, tt := range tests {
  138. t.Run(tt.name, func(t *testing.T) {
  139. result := tt.config1.Equals(tt.config2)
  140. assert.Equal(t, tt.expected, result)
  141. })
  142. }
  143. }
  144. func TestServiceAccountKey_Sanitize(t *testing.T) {
  145. key := &ServiceAccountKey{
  146. Key: map[string]string{
  147. "type": "service_account",
  148. "private_key": "secret-key",
  149. },
  150. }
  151. sanitized := key.Sanitize()
  152. require.NotNil(t, sanitized)
  153. saKey, ok := sanitized.(*ServiceAccountKey)
  154. require.True(t, ok)
  155. for _, value := range saKey.Key {
  156. assert.Equal(t, cloud.Redacted, value)
  157. }
  158. }
  159. func TestServiceAccountKey_CreateGCPClientOptions(t *testing.T) {
  160. tests := []struct {
  161. name string
  162. key map[string]string
  163. expectError bool
  164. }{
  165. {
  166. name: "Valid key",
  167. key: map[string]string{
  168. "type": "service_account",
  169. },
  170. expectError: false,
  171. },
  172. {
  173. name: "Invalid key",
  174. key: nil,
  175. expectError: true,
  176. },
  177. }
  178. for _, tt := range tests {
  179. t.Run(tt.name, func(t *testing.T) {
  180. saKey := &ServiceAccountKey{Key: tt.key}
  181. options, err := saKey.CreateGCPClientOptions()
  182. if tt.expectError {
  183. assert.Error(t, err)
  184. assert.Nil(t, options)
  185. } else {
  186. assert.NoError(t, err)
  187. assert.NotNil(t, options)
  188. assert.Len(t, options, 1)
  189. }
  190. })
  191. }
  192. }
  193. func TestWorkloadIdentity_MarshalJSON(t *testing.T) {
  194. wi := &WorkloadIdentity{}
  195. data, err := json.Marshal(wi)
  196. require.NoError(t, err)
  197. var result map[string]interface{}
  198. err = json.Unmarshal(data, &result)
  199. require.NoError(t, err)
  200. assert.Equal(t, WorkloadIdentityAuthorizerType, result["authorizerType"])
  201. }
  202. func TestWorkloadIdentity_Validate(t *testing.T) {
  203. wi := &WorkloadIdentity{}
  204. err := wi.Validate()
  205. assert.NoError(t, err)
  206. }
  207. func TestWorkloadIdentity_Equals(t *testing.T) {
  208. wi1 := &WorkloadIdentity{}
  209. wi2 := &WorkloadIdentity{}
  210. saKey := &ServiceAccountKey{Key: map[string]string{"type": "service_account"}}
  211. tests := []struct {
  212. name string
  213. config1 cloud.Config
  214. config2 cloud.Config
  215. expected bool
  216. }{
  217. {
  218. name: "Same workload identity",
  219. config1: wi1,
  220. config2: wi2,
  221. expected: true,
  222. },
  223. {
  224. name: "Different types",
  225. config1: wi1,
  226. config2: saKey,
  227. expected: false,
  228. },
  229. {
  230. name: "Nil config",
  231. config1: wi1,
  232. config2: nil,
  233. expected: false,
  234. },
  235. }
  236. for _, tt := range tests {
  237. t.Run(tt.name, func(t *testing.T) {
  238. result := tt.config1.Equals(tt.config2)
  239. assert.Equal(t, tt.expected, result)
  240. })
  241. }
  242. }
  243. func TestWorkloadIdentity_Sanitize(t *testing.T) {
  244. wi := &WorkloadIdentity{}
  245. sanitized := wi.Sanitize()
  246. _, ok := sanitized.(*WorkloadIdentity)
  247. assert.True(t, ok)
  248. }
  249. func TestWorkloadIdentity_CreateGCPClientOptions(t *testing.T) {
  250. wi := &WorkloadIdentity{}
  251. options, err := wi.CreateGCPClientOptions()
  252. assert.NoError(t, err)
  253. assert.NotNil(t, options)
  254. assert.Len(t, options, 0)
  255. }