bigqueryconfiguration_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package gcp
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. "github.com/opencost/opencost/core/pkg/util/json"
  7. "github.com/opencost/opencost/pkg/cloud"
  8. )
  9. func TestBigQueryConfiguration_Validate(t *testing.T) {
  10. testCases := map[string]struct {
  11. config BigQueryConfiguration
  12. expected error
  13. }{
  14. "valid config GCP Key": {
  15. config: BigQueryConfiguration{
  16. ProjectID: "projectID",
  17. Dataset: "dataset",
  18. Table: "table",
  19. Authorizer: &ServiceAccountKey{
  20. Key: map[string]string{
  21. "Key": "Key",
  22. "key1": "key2",
  23. },
  24. },
  25. },
  26. expected: nil,
  27. },
  28. "valid config WorkloadIdentity": {
  29. config: BigQueryConfiguration{
  30. ProjectID: "projectID",
  31. Dataset: "dataset",
  32. Table: "table",
  33. Authorizer: &WorkloadIdentity{},
  34. },
  35. expected: nil,
  36. },
  37. "access Key invalid": {
  38. config: BigQueryConfiguration{
  39. ProjectID: "projectID",
  40. Dataset: "dataset",
  41. Table: "table",
  42. Authorizer: &ServiceAccountKey{
  43. Key: nil,
  44. },
  45. },
  46. expected: fmt.Errorf("BigQueryConfig: issue with GCP Authorizer: ServiceAccountKey: missing Key"),
  47. },
  48. "missing configurer": {
  49. config: BigQueryConfiguration{
  50. ProjectID: "projectID",
  51. Dataset: "dataset",
  52. Table: "table",
  53. Authorizer: nil,
  54. },
  55. expected: fmt.Errorf("BigQueryConfig: missing configurer"),
  56. },
  57. "missing projectID": {
  58. config: BigQueryConfiguration{
  59. ProjectID: "",
  60. Dataset: "dataset",
  61. Table: "table",
  62. Authorizer: &ServiceAccountKey{
  63. Key: map[string]string{
  64. "Key": "Key",
  65. "key1": "key2",
  66. },
  67. },
  68. },
  69. expected: fmt.Errorf("BigQueryConfig: missing ProjectID"),
  70. },
  71. "missing dataset": {
  72. config: BigQueryConfiguration{
  73. ProjectID: "projectID",
  74. Dataset: "",
  75. Table: "table",
  76. Authorizer: &ServiceAccountKey{
  77. Key: map[string]string{
  78. "Key": "Key",
  79. "key1": "key2",
  80. },
  81. },
  82. },
  83. expected: fmt.Errorf("BigQueryConfig: missing Dataset"),
  84. },
  85. "missing table": {
  86. config: BigQueryConfiguration{
  87. ProjectID: "projectID",
  88. Dataset: "dataset",
  89. Table: "",
  90. Authorizer: &ServiceAccountKey{
  91. Key: map[string]string{
  92. "Key": "Key",
  93. "key1": "key2",
  94. },
  95. },
  96. },
  97. expected: fmt.Errorf("BigQueryConfig: missing Table"),
  98. },
  99. }
  100. for name, testCase := range testCases {
  101. t.Run(name, func(t *testing.T) {
  102. actual := testCase.config.Validate()
  103. actualString := "nil"
  104. if actual != nil {
  105. actualString = actual.Error()
  106. }
  107. expectedString := "nil"
  108. if testCase.expected != nil {
  109. expectedString = testCase.expected.Error()
  110. }
  111. if actualString != expectedString {
  112. t.Errorf("errors do not match: Actual: '%s', Expected: '%s", actualString, expectedString)
  113. }
  114. })
  115. }
  116. }
  117. func TestBigQueryConfiguration_Equals(t *testing.T) {
  118. testCases := map[string]struct {
  119. left BigQueryConfiguration
  120. right cloud.Config
  121. expected bool
  122. }{
  123. "matching config": {
  124. left: BigQueryConfiguration{
  125. ProjectID: "projectID",
  126. Dataset: "dataset",
  127. Table: "table",
  128. Authorizer: &ServiceAccountKey{
  129. Key: map[string]string{
  130. "Key": "Key",
  131. "key1": "key2",
  132. },
  133. },
  134. },
  135. right: &BigQueryConfiguration{
  136. ProjectID: "projectID",
  137. Dataset: "dataset",
  138. Table: "table",
  139. Authorizer: &ServiceAccountKey{
  140. Key: map[string]string{
  141. "Key": "Key",
  142. "key1": "key2",
  143. },
  144. },
  145. },
  146. expected: true,
  147. },
  148. "different configurer": {
  149. left: BigQueryConfiguration{
  150. ProjectID: "projectID",
  151. Dataset: "dataset",
  152. Table: "table",
  153. Authorizer: &ServiceAccountKey{
  154. Key: map[string]string{
  155. "Key": "Key",
  156. "key1": "key2",
  157. },
  158. },
  159. },
  160. right: &BigQueryConfiguration{
  161. ProjectID: "projectID",
  162. Dataset: "dataset",
  163. Table: "table",
  164. Authorizer: &WorkloadIdentity{},
  165. },
  166. expected: false,
  167. },
  168. "missing both configurer": {
  169. left: BigQueryConfiguration{
  170. ProjectID: "projectID",
  171. Dataset: "dataset",
  172. Table: "table",
  173. Authorizer: nil,
  174. },
  175. right: &BigQueryConfiguration{
  176. ProjectID: "projectID",
  177. Dataset: "dataset",
  178. Table: "table",
  179. Authorizer: nil,
  180. },
  181. expected: true,
  182. },
  183. "missing left configurer": {
  184. left: BigQueryConfiguration{
  185. ProjectID: "projectID",
  186. Dataset: "dataset",
  187. Table: "table",
  188. Authorizer: nil,
  189. },
  190. right: &BigQueryConfiguration{
  191. ProjectID: "projectID",
  192. Dataset: "dataset",
  193. Table: "table",
  194. Authorizer: &WorkloadIdentity{},
  195. },
  196. expected: false,
  197. },
  198. "missing right configurer": {
  199. left: BigQueryConfiguration{
  200. ProjectID: "projectID",
  201. Dataset: "dataset",
  202. Table: "table",
  203. Authorizer: &ServiceAccountKey{
  204. Key: map[string]string{
  205. "Key": "Key",
  206. "key1": "key2",
  207. },
  208. },
  209. },
  210. right: &BigQueryConfiguration{
  211. ProjectID: "projectID",
  212. Dataset: "dataset",
  213. Table: "table",
  214. Authorizer: nil,
  215. },
  216. expected: false,
  217. },
  218. "different projectID": {
  219. left: BigQueryConfiguration{
  220. ProjectID: "projectID",
  221. Dataset: "dataset",
  222. Table: "table",
  223. Authorizer: &ServiceAccountKey{
  224. Key: map[string]string{
  225. "Key": "Key",
  226. "key1": "key2",
  227. },
  228. },
  229. },
  230. right: &BigQueryConfiguration{
  231. ProjectID: "projectID2",
  232. Dataset: "dataset",
  233. Table: "table",
  234. Authorizer: &ServiceAccountKey{
  235. Key: map[string]string{
  236. "Key": "Key",
  237. "key1": "key2",
  238. },
  239. },
  240. },
  241. expected: false,
  242. },
  243. "different dataset": {
  244. left: BigQueryConfiguration{
  245. ProjectID: "projectID",
  246. Dataset: "dataset",
  247. Table: "table",
  248. Authorizer: &ServiceAccountKey{
  249. Key: map[string]string{
  250. "Key": "Key",
  251. "key1": "key2",
  252. },
  253. },
  254. },
  255. right: &BigQueryConfiguration{
  256. ProjectID: "projectID",
  257. Dataset: "dataset2",
  258. Table: "table",
  259. Authorizer: &ServiceAccountKey{
  260. Key: map[string]string{
  261. "Key": "Key",
  262. "key1": "key2",
  263. },
  264. },
  265. },
  266. expected: false,
  267. },
  268. "different table": {
  269. left: BigQueryConfiguration{
  270. ProjectID: "projectID",
  271. Dataset: "dataset",
  272. Table: "table",
  273. Authorizer: &ServiceAccountKey{
  274. Key: map[string]string{
  275. "Key": "Key",
  276. "key1": "key2",
  277. },
  278. },
  279. },
  280. right: &BigQueryConfiguration{
  281. ProjectID: "projectID",
  282. Dataset: "dataset",
  283. Table: "table2",
  284. Authorizer: &ServiceAccountKey{
  285. Key: map[string]string{
  286. "Key": "Key",
  287. "key1": "key2",
  288. },
  289. },
  290. },
  291. expected: false,
  292. },
  293. "different config": {
  294. left: BigQueryConfiguration{
  295. ProjectID: "projectID",
  296. Dataset: "dataset",
  297. Table: "table",
  298. Authorizer: &ServiceAccountKey{
  299. Key: map[string]string{
  300. "Key": "Key",
  301. "key1": "key2",
  302. },
  303. },
  304. },
  305. right: &ServiceAccountKey{
  306. Key: map[string]string{
  307. "Key": "Key",
  308. "key1": "key2",
  309. },
  310. },
  311. expected: false,
  312. },
  313. }
  314. for name, testCase := range testCases {
  315. t.Run(name, func(t *testing.T) {
  316. actual := testCase.left.Equals(testCase.right)
  317. if actual != testCase.expected {
  318. t.Errorf("incorrect result: Actual: '%t', Expected: '%t", actual, testCase.expected)
  319. }
  320. })
  321. }
  322. }
  323. func TestBigQueryConfiguration_JSON(t *testing.T) {
  324. testCases := map[string]struct {
  325. config BigQueryConfiguration
  326. }{
  327. "Empty Config": {
  328. config: BigQueryConfiguration{},
  329. },
  330. "Nil Authorizer": {
  331. config: BigQueryConfiguration{
  332. ProjectID: "projectID",
  333. Dataset: "dataset",
  334. Table: "table",
  335. Authorizer: nil,
  336. },
  337. },
  338. "ServiceAccountKeyConfigurer": {
  339. config: BigQueryConfiguration{
  340. ProjectID: "projectID",
  341. Dataset: "dataset",
  342. Table: "table",
  343. Authorizer: &ServiceAccountKey{
  344. Key: map[string]string{
  345. "Key": "Key",
  346. "key1": "key2",
  347. },
  348. },
  349. },
  350. },
  351. "WorkLoadIdentityConfigurer": {
  352. config: BigQueryConfiguration{
  353. ProjectID: "projectID",
  354. Dataset: "dataset",
  355. Table: "table",
  356. Authorizer: &WorkloadIdentity{},
  357. },
  358. },
  359. }
  360. for name, testCase := range testCases {
  361. t.Run(name, func(t *testing.T) {
  362. // test JSON Marshalling
  363. configJSON, err := json.Marshal(testCase.config)
  364. if err != nil {
  365. t.Errorf("failed to marshal configuration: %s", err.Error())
  366. }
  367. log.Info(string(configJSON))
  368. unmarshalledConfig := &BigQueryConfiguration{}
  369. err = json.Unmarshal(configJSON, unmarshalledConfig)
  370. if err != nil {
  371. t.Errorf("failed to unmarshal configuration: %s", err.Error())
  372. }
  373. if !testCase.config.Equals(unmarshalledConfig) {
  374. t.Error("config does not equal unmarshalled config")
  375. }
  376. })
  377. }
  378. }