usageapiconfiguration_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package oracle
  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 TestUsageApiConfiguration_Validate(t *testing.T) {
  10. testCases := map[string]struct {
  11. config UsageApiConfiguration
  12. expected error
  13. }{
  14. "valid config OCI Key": {
  15. config: UsageApiConfiguration{
  16. TenancyID: "tenancyID",
  17. Region: "region",
  18. Authorizer: &RawConfigProvider{
  19. TenancyID: "tenancyID",
  20. UserID: "userID",
  21. Region: "region",
  22. Fingerprint: "fingerprint",
  23. PrivateKey: "key",
  24. },
  25. },
  26. expected: nil,
  27. },
  28. "invalid authorizer": {
  29. config: UsageApiConfiguration{
  30. TenancyID: "tenancyID",
  31. Region: "region",
  32. Authorizer: &RawConfigProvider{
  33. TenancyID: "tenancyID",
  34. UserID: "userID",
  35. Region: "region",
  36. Fingerprint: "",
  37. PrivateKey: "",
  38. },
  39. },
  40. expected: fmt.Errorf("UsageApiConfiguration: RawConfigProvider: missing key fingerprint"),
  41. },
  42. "missing authorizer": {
  43. config: UsageApiConfiguration{
  44. TenancyID: "tenancyID",
  45. Region: "region",
  46. Authorizer: nil,
  47. },
  48. expected: fmt.Errorf("UsageApiConfiguration: missing Authorizer"),
  49. },
  50. "missing tenancyID": {
  51. config: UsageApiConfiguration{
  52. TenancyID: "",
  53. Region: "region",
  54. Authorizer: &RawConfigProvider{
  55. TenancyID: "tenancyID",
  56. UserID: "userID",
  57. Region: "region",
  58. Fingerprint: "fingerprint",
  59. PrivateKey: "key",
  60. },
  61. },
  62. expected: fmt.Errorf("UsageApiConfiguration: missing tenancyID"),
  63. },
  64. "missing region": {
  65. config: UsageApiConfiguration{
  66. TenancyID: "tenancyID",
  67. Region: "",
  68. Authorizer: &RawConfigProvider{
  69. TenancyID: "tenancyID",
  70. UserID: "userID",
  71. Region: "region",
  72. Fingerprint: "fingerprint",
  73. PrivateKey: "key",
  74. },
  75. },
  76. expected: fmt.Errorf("UsageApiConfiguration: missing region"),
  77. },
  78. }
  79. for name, testCase := range testCases {
  80. t.Run(name, func(t *testing.T) {
  81. actual := testCase.config.Validate()
  82. actualString := "nil"
  83. if actual != nil {
  84. actualString = actual.Error()
  85. }
  86. expectedString := "nil"
  87. if testCase.expected != nil {
  88. expectedString = testCase.expected.Error()
  89. }
  90. if actualString != expectedString {
  91. t.Errorf("errors do not match: Actual: '%s', Expected: '%s", actualString, expectedString)
  92. }
  93. })
  94. }
  95. }
  96. func TestUsageApiConfiguration_Equals(t *testing.T) {
  97. testCases := map[string]struct {
  98. left UsageApiConfiguration
  99. right cloud.Config
  100. expected bool
  101. }{
  102. "matching config": {
  103. left: UsageApiConfiguration{
  104. TenancyID: "tenancyID",
  105. Region: "region",
  106. Authorizer: &RawConfigProvider{
  107. TenancyID: "tenancyID",
  108. UserID: "userID",
  109. Region: "region",
  110. Fingerprint: "fingerprint",
  111. PrivateKey: "key",
  112. },
  113. },
  114. right: &UsageApiConfiguration{
  115. TenancyID: "tenancyID",
  116. Region: "region",
  117. Authorizer: &RawConfigProvider{
  118. TenancyID: "tenancyID",
  119. UserID: "userID",
  120. Region: "region",
  121. Fingerprint: "fingerprint",
  122. PrivateKey: "key",
  123. },
  124. },
  125. expected: true,
  126. },
  127. "different configurer": {
  128. left: UsageApiConfiguration{
  129. TenancyID: "tenancyID",
  130. Region: "region",
  131. Authorizer: &RawConfigProvider{
  132. TenancyID: "tenancyID",
  133. UserID: "userID",
  134. Region: "region",
  135. Fingerprint: "fingerprint2",
  136. PrivateKey: "key",
  137. },
  138. },
  139. right: &UsageApiConfiguration{
  140. TenancyID: "tenancyID",
  141. Region: "region",
  142. Authorizer: &RawConfigProvider{
  143. TenancyID: "tenancyID",
  144. UserID: "userID",
  145. Region: "region",
  146. Fingerprint: "fingerprint",
  147. PrivateKey: "key",
  148. },
  149. },
  150. expected: false,
  151. },
  152. "missing both configurer": {
  153. left: UsageApiConfiguration{
  154. TenancyID: "tenancyID",
  155. Region: "region",
  156. Authorizer: nil,
  157. },
  158. right: &UsageApiConfiguration{
  159. TenancyID: "tenancyID",
  160. Region: "region",
  161. Authorizer: nil,
  162. },
  163. expected: true,
  164. },
  165. "missing left configurer": {
  166. left: UsageApiConfiguration{
  167. TenancyID: "tenancyID",
  168. Region: "region",
  169. Authorizer: nil,
  170. },
  171. right: &UsageApiConfiguration{
  172. TenancyID: "tenancyID",
  173. Region: "region",
  174. Authorizer: &RawConfigProvider{
  175. TenancyID: "tenancyID",
  176. UserID: "userID",
  177. Region: "region",
  178. Fingerprint: "fingerprint",
  179. PrivateKey: "key",
  180. },
  181. },
  182. expected: false,
  183. },
  184. "missing right configurer": {
  185. left: UsageApiConfiguration{
  186. TenancyID: "tenancyID",
  187. Region: "region",
  188. Authorizer: &RawConfigProvider{
  189. TenancyID: "tenancyID",
  190. UserID: "userID",
  191. Region: "region",
  192. Fingerprint: "fingerprint",
  193. PrivateKey: "key",
  194. },
  195. },
  196. right: &UsageApiConfiguration{
  197. TenancyID: "tenancyID",
  198. Region: "region",
  199. Authorizer: nil,
  200. },
  201. expected: false,
  202. },
  203. "different tenancyID": {
  204. left: UsageApiConfiguration{
  205. TenancyID: "tenancyID",
  206. Region: "region",
  207. Authorizer: &RawConfigProvider{
  208. TenancyID: "tenancyID",
  209. UserID: "userID",
  210. Region: "region",
  211. Fingerprint: "fingerprint",
  212. PrivateKey: "key",
  213. },
  214. },
  215. right: &UsageApiConfiguration{
  216. TenancyID: "tenancyID2",
  217. Region: "region",
  218. Authorizer: &RawConfigProvider{
  219. TenancyID: "tenancyID2",
  220. UserID: "userID",
  221. Region: "region",
  222. Fingerprint: "fingerprint",
  223. PrivateKey: "key",
  224. },
  225. },
  226. expected: false,
  227. },
  228. "different region": {
  229. left: UsageApiConfiguration{
  230. TenancyID: "tenancyID",
  231. Region: "region",
  232. Authorizer: &RawConfigProvider{
  233. TenancyID: "tenancyID",
  234. UserID: "userID",
  235. Region: "region",
  236. Fingerprint: "fingerprint",
  237. PrivateKey: "key",
  238. },
  239. },
  240. right: &UsageApiConfiguration{
  241. TenancyID: "tenancyID",
  242. Region: "region2",
  243. Authorizer: &RawConfigProvider{
  244. TenancyID: "tenancyID",
  245. UserID: "userID",
  246. Region: "region2",
  247. Fingerprint: "fingerprint",
  248. PrivateKey: "key",
  249. },
  250. },
  251. expected: false,
  252. },
  253. }
  254. for name, testCase := range testCases {
  255. t.Run(name, func(t *testing.T) {
  256. actual := testCase.left.Equals(testCase.right)
  257. if actual != testCase.expected {
  258. t.Errorf("incorrect result: Actual: '%t', Expected: '%t", actual, testCase.expected)
  259. }
  260. })
  261. }
  262. }
  263. func TestUsageApiConfiguration_JSON(t *testing.T) {
  264. testCases := map[string]struct {
  265. config UsageApiConfiguration
  266. }{
  267. "Empty Config": {
  268. config: UsageApiConfiguration{},
  269. },
  270. "Nil Authorizer": {
  271. config: UsageApiConfiguration{
  272. TenancyID: "tenancyID",
  273. Region: "region",
  274. Authorizer: nil,
  275. },
  276. },
  277. "RawConfigProviderAuthorizer": {
  278. config: UsageApiConfiguration{
  279. TenancyID: "tenancyID",
  280. Region: "region",
  281. Authorizer: &RawConfigProvider{
  282. TenancyID: "tenancyID",
  283. UserID: "userID",
  284. Region: "region2",
  285. Fingerprint: "fingerprint",
  286. PrivateKey: "key",
  287. },
  288. },
  289. },
  290. }
  291. for name, testCase := range testCases {
  292. t.Run(name, func(t *testing.T) {
  293. // test JSON Marshalling
  294. configJSON, err := json.Marshal(testCase.config)
  295. if err != nil {
  296. t.Errorf("failed to marshal configuration: %s", err.Error())
  297. }
  298. log.Info(string(configJSON))
  299. unmarshalledConfig := &UsageApiConfiguration{}
  300. err = json.Unmarshal(configJSON, unmarshalledConfig)
  301. if err != nil {
  302. t.Errorf("failed to unmarshal configuration: %s", err.Error())
  303. }
  304. if !testCase.config.Equals(unmarshalledConfig) {
  305. t.Error("config does not equal unmarshalled config")
  306. }
  307. })
  308. }
  309. }