/pkg/kascfg/validation_test.go

https://gitlab.com/bauerdominic/gitlab-agent · Go · 364 lines · 350 code · 4 blank · 10 comment · 0 complexity · 75166f10da07e14ff28a88ad150f4135 MD5 · raw file

  1. package kascfg
  2. import (
  3. "testing"
  4. "gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v14/internal/tool/testing/testhelpers"
  5. "google.golang.org/protobuf/types/known/durationpb"
  6. )
  7. func TestValidation_Valid(t *testing.T) {
  8. tests := []testhelpers.ValidTestcase{
  9. {
  10. Name: "minimal",
  11. Valid: &ConfigurationFile{
  12. Gitlab: &GitLabCF{
  13. Address: "http://localhost:8080",
  14. AuthenticationSecretFile: "/some/file",
  15. },
  16. },
  17. },
  18. {
  19. Name: "GitopsCF",
  20. Valid: &GitopsCF{
  21. ProjectInfoCacheTtl: durationpb.New(0), // zero means "disabled"
  22. MaxManifestFileSize: 0, // zero means "use default value"
  23. MaxTotalManifestFileSize: 0, // zero means "use default value"
  24. MaxNumberOfPaths: 0, // zero means "use default value"
  25. MaxNumberOfFiles: 0, // zero means "use default value"
  26. },
  27. },
  28. {
  29. Name: "AgentCF",
  30. Valid: &AgentCF{
  31. InfoCacheTtl: durationpb.New(0), // zero means "disabled"
  32. },
  33. },
  34. {
  35. Name: "ObservabilityCF",
  36. Valid: &ObservabilityCF{
  37. UsageReportingPeriod: durationpb.New(0), // zero means "disabled"
  38. },
  39. },
  40. {
  41. Name: "TokenBucketRateLimitCF",
  42. Valid: &TokenBucketRateLimitCF{
  43. RefillRatePerSecond: 0, // zero means "use default value"
  44. BucketSize: 0, // zero means "use default value"
  45. },
  46. },
  47. {
  48. Name: "RedisCF",
  49. Valid: &RedisCF{
  50. RedisConfig: &RedisCF_Server{
  51. Server: &RedisServerCF{
  52. Address: "//path/to/socket.sock",
  53. },
  54. },
  55. PoolSize: 0, // zero means "use default value"
  56. KeyPrefix: "", // empty means "use default value"
  57. },
  58. },
  59. {
  60. Name: "RedisCF",
  61. Valid: &RedisCF{
  62. RedisConfig: &RedisCF_Server{
  63. Server: &RedisServerCF{
  64. Address: "address:6380",
  65. },
  66. },
  67. PoolSize: 0, // zero means "use default value"
  68. KeyPrefix: "", // empty means "use default value"
  69. },
  70. },
  71. {
  72. Name: "RedisCF",
  73. Valid: &RedisCF{
  74. RedisConfig: &RedisCF_Server{
  75. Server: &RedisServerCF{
  76. Address: "127.0.0.1:6380",
  77. },
  78. },
  79. PoolSize: 0, // zero means "use default value"
  80. KeyPrefix: "", // empty means "use default value"
  81. },
  82. },
  83. {
  84. Name: "AgentConfigurationCF",
  85. Valid: &AgentConfigurationCF{
  86. MaxConfigurationFileSize: 0, // zero means "use default value"
  87. },
  88. },
  89. {
  90. Name: "ListenAgentCF",
  91. Valid: &ListenAgentCF{
  92. ConnectionsPerTokenPerMinute: 0, // zero means "use default value"
  93. },
  94. },
  95. }
  96. testhelpers.AssertValid(t, tests)
  97. }
  98. func TestValidation_Invalid(t *testing.T) {
  99. tests := []testhelpers.InvalidTestcase{
  100. {
  101. ErrString: "invalid GitopsCF.PollPeriod: value must be greater than 0s",
  102. Invalid: &GitopsCF{
  103. PollPeriod: durationpb.New(0),
  104. },
  105. },
  106. {
  107. ErrString: "invalid GitopsCF.PollPeriod: value must be greater than 0s",
  108. Invalid: &GitopsCF{
  109. PollPeriod: durationpb.New(-1),
  110. },
  111. },
  112. {
  113. ErrString: "invalid GitopsCF.ProjectInfoCacheTtl: value must be greater than or equal to 0s",
  114. Invalid: &GitopsCF{
  115. ProjectInfoCacheTtl: durationpb.New(-1),
  116. },
  117. },
  118. {
  119. ErrString: "invalid GitopsCF.ProjectInfoCacheErrorTtl: value must be greater than 0s",
  120. Invalid: &GitopsCF{
  121. ProjectInfoCacheErrorTtl: durationpb.New(0),
  122. },
  123. },
  124. {
  125. ErrString: "invalid GitopsCF.ProjectInfoCacheErrorTtl: value must be greater than 0s",
  126. Invalid: &GitopsCF{
  127. ProjectInfoCacheErrorTtl: durationpb.New(-1),
  128. },
  129. },
  130. {
  131. ErrString: "invalid AgentCF.InfoCacheTtl: value must be greater than or equal to 0s",
  132. Invalid: &AgentCF{
  133. InfoCacheTtl: durationpb.New(-1),
  134. },
  135. },
  136. {
  137. ErrString: "invalid AgentCF.InfoCacheErrorTtl: value must be greater than 0s",
  138. Invalid: &AgentCF{
  139. InfoCacheErrorTtl: durationpb.New(0),
  140. },
  141. },
  142. {
  143. ErrString: "invalid AgentCF.InfoCacheErrorTtl: value must be greater than 0s",
  144. Invalid: &AgentCF{
  145. InfoCacheErrorTtl: durationpb.New(-1),
  146. },
  147. },
  148. {
  149. ErrString: "invalid AgentConfigurationCF.PollPeriod: value must be greater than 0s",
  150. Invalid: &AgentConfigurationCF{
  151. PollPeriod: durationpb.New(0),
  152. },
  153. },
  154. {
  155. ErrString: "invalid AgentConfigurationCF.PollPeriod: value must be greater than 0s",
  156. Invalid: &AgentConfigurationCF{
  157. PollPeriod: durationpb.New(-1),
  158. },
  159. },
  160. {
  161. ErrString: "invalid ObservabilityCF.UsageReportingPeriod: value must be greater than or equal to 0s",
  162. Invalid: &ObservabilityCF{
  163. UsageReportingPeriod: durationpb.New(-1),
  164. },
  165. },
  166. {
  167. ErrString: "invalid TokenBucketRateLimitCF.RefillRatePerSecond: value must be greater than or equal to 0",
  168. Invalid: &TokenBucketRateLimitCF{
  169. RefillRatePerSecond: -1,
  170. },
  171. },
  172. {
  173. ErrString: "invalid RedisCF.DialTimeout: value must be greater than 0s",
  174. Invalid: &RedisCF{
  175. RedisConfig: &RedisCF_Server{
  176. Server: &RedisServerCF{
  177. Address: "//path/to/socket.sock",
  178. },
  179. },
  180. DialTimeout: durationpb.New(0),
  181. },
  182. },
  183. {
  184. ErrString: "invalid RedisCF.DialTimeout: value must be greater than 0s",
  185. Invalid: &RedisCF{
  186. RedisConfig: &RedisCF_Server{
  187. Server: &RedisServerCF{
  188. Address: "//path/to/socket.sock",
  189. },
  190. },
  191. DialTimeout: durationpb.New(-1),
  192. },
  193. },
  194. {
  195. ErrString: "invalid RedisCF.ReadTimeout: value must be greater than 0s",
  196. Invalid: &RedisCF{
  197. RedisConfig: &RedisCF_Server{
  198. Server: &RedisServerCF{
  199. Address: "//path/to/socket.sock",
  200. },
  201. },
  202. ReadTimeout: durationpb.New(0),
  203. },
  204. },
  205. {
  206. ErrString: "invalid RedisCF.ReadTimeout: value must be greater than 0s",
  207. Invalid: &RedisCF{
  208. RedisConfig: &RedisCF_Server{
  209. Server: &RedisServerCF{
  210. Address: "//path/to/socket.sock",
  211. },
  212. },
  213. ReadTimeout: durationpb.New(-1),
  214. },
  215. },
  216. {
  217. ErrString: "invalid RedisCF.WriteTimeout: value must be greater than 0s",
  218. Invalid: &RedisCF{
  219. RedisConfig: &RedisCF_Server{
  220. Server: &RedisServerCF{
  221. Address: "//path/to/socket.sock",
  222. },
  223. },
  224. WriteTimeout: durationpb.New(0),
  225. },
  226. },
  227. {
  228. ErrString: "invalid RedisCF.WriteTimeout: value must be greater than 0s",
  229. Invalid: &RedisCF{
  230. RedisConfig: &RedisCF_Server{
  231. Server: &RedisServerCF{
  232. Address: "//path/to/socket.sock",
  233. },
  234. },
  235. WriteTimeout: durationpb.New(-1),
  236. },
  237. },
  238. {
  239. ErrString: "invalid RedisCF.IdleTimeout: value must be greater than 0s",
  240. Invalid: &RedisCF{
  241. RedisConfig: &RedisCF_Server{
  242. Server: &RedisServerCF{
  243. Address: "//path/to/socket.sock",
  244. },
  245. },
  246. IdleTimeout: durationpb.New(0),
  247. },
  248. },
  249. {
  250. ErrString: "invalid RedisCF.IdleTimeout: value must be greater than 0s",
  251. Invalid: &RedisCF{
  252. RedisConfig: &RedisCF_Server{
  253. Server: &RedisServerCF{
  254. Address: "//path/to/socket.sock",
  255. },
  256. },
  257. IdleTimeout: durationpb.New(-1),
  258. },
  259. },
  260. {
  261. ErrString: "invalid RedisCF.RedisConfig: value is required",
  262. Invalid: &RedisCF{},
  263. },
  264. {
  265. ErrString: "invalid RedisServerCF.Address: value length must be at least 1 bytes",
  266. Invalid: &RedisServerCF{},
  267. },
  268. {
  269. ErrString: "invalid RedisSentinelCF.MasterName: value length must be at least 1 bytes",
  270. Invalid: &RedisSentinelCF{
  271. Addresses: []string{"1:2"},
  272. },
  273. },
  274. {
  275. ErrString: "invalid RedisSentinelCF.Addresses: value must contain at least 1 item(s)",
  276. Invalid: &RedisSentinelCF{
  277. MasterName: "bla",
  278. },
  279. },
  280. {
  281. ErrString: "invalid RedisSentinelCF.Addresses[0]: value length must be at least 1 bytes",
  282. Invalid: &RedisSentinelCF{
  283. MasterName: "bla",
  284. Addresses: []string{""},
  285. },
  286. },
  287. {
  288. ErrString: "invalid ListenAgentCF.MaxConnectionAge: value must be greater than 0s",
  289. Invalid: &ListenAgentCF{
  290. MaxConnectionAge: durationpb.New(0),
  291. },
  292. },
  293. {
  294. ErrString: "invalid ListenAgentCF.MaxConnectionAge: value must be greater than 0s",
  295. Invalid: &ListenAgentCF{
  296. MaxConnectionAge: durationpb.New(-1),
  297. },
  298. },
  299. {
  300. ErrString: "invalid GitLabCF.Address: value length must be at least 1 bytes",
  301. Invalid: &GitLabCF{
  302. AuthenticationSecretFile: "/some/file",
  303. },
  304. },
  305. {
  306. ErrString: "invalid GitLabCF.Address: value must be absolute",
  307. Invalid: &GitLabCF{
  308. Address: "/path",
  309. AuthenticationSecretFile: "/some/file",
  310. },
  311. },
  312. {
  313. ErrString: "invalid GitLabCF.AuthenticationSecretFile: value length must be at least 1 bytes",
  314. Invalid: &GitLabCF{
  315. Address: "http://localhost:8080",
  316. },
  317. },
  318. // TODO uncomment when Redis becomes a hard dependency
  319. //{
  320. // ErrString: "invalid ConfigurationFile.Redis: value is required",
  321. // Invalid: &ConfigurationFile{
  322. // Gitlab: &GitLabCF{
  323. // Address: "http://localhost:8080",
  324. // AuthenticationSecretFile: "/some/file",
  325. // },
  326. // },
  327. //},
  328. {
  329. ErrString: "invalid ConfigurationFile.Gitlab: value is required",
  330. Invalid: &ConfigurationFile{},
  331. },
  332. {
  333. ErrString: "invalid ListenApiCF.AuthenticationSecretFile: value length must be at least 1 bytes",
  334. Invalid: &ListenApiCF{},
  335. },
  336. {
  337. ErrString: "invalid ListenApiCF.MaxConnectionAge: value must be greater than 0s",
  338. Invalid: &ListenApiCF{
  339. AuthenticationSecretFile: "bla",
  340. MaxConnectionAge: durationpb.New(0),
  341. },
  342. },
  343. {
  344. ErrString: "invalid ListenApiCF.MaxConnectionAge: value must be greater than 0s",
  345. Invalid: &ListenApiCF{
  346. AuthenticationSecretFile: "bla",
  347. MaxConnectionAge: durationpb.New(-1),
  348. },
  349. },
  350. {
  351. ErrString: "invalid ApiCF.Listen: value is required",
  352. Invalid: &ApiCF{},
  353. },
  354. {
  355. ErrString: "invalid PrivateApiCF.Listen: value is required",
  356. Invalid: &PrivateApiCF{},
  357. },
  358. }
  359. testhelpers.AssertInvalid(t, tests)
  360. }