2
0

s3connection_test.go 8.2 KB

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