boaconfiguration_test.go 6.4 KB

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