admin_test.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. package sarama
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. )
  7. func TestClusterAdmin(t *testing.T) {
  8. seedBroker := NewMockBroker(t, 1)
  9. defer seedBroker.Close()
  10. seedBroker.SetHandlerByMap(map[string]MockResponse{
  11. "MetadataRequest": NewMockMetadataResponse(t).
  12. SetController(seedBroker.BrokerID()).
  13. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  14. })
  15. config := NewConfig()
  16. config.Version = V1_0_0_0
  17. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. err = admin.Close()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. }
  26. func TestClusterAdminInvalidController(t *testing.T) {
  27. seedBroker := NewMockBroker(t, 1)
  28. defer seedBroker.Close()
  29. seedBroker.SetHandlerByMap(map[string]MockResponse{
  30. "MetadataRequest": NewMockMetadataResponse(t).
  31. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  32. })
  33. config := NewConfig()
  34. config.Version = V1_0_0_0
  35. _, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  36. if err == nil {
  37. t.Fatal(errors.New("controller not set still cluster admin was created"))
  38. }
  39. if err != ErrControllerNotAvailable {
  40. t.Fatal(err)
  41. }
  42. }
  43. func TestClusterAdminCreateTopic(t *testing.T) {
  44. seedBroker := NewMockBroker(t, 1)
  45. defer seedBroker.Close()
  46. seedBroker.SetHandlerByMap(map[string]MockResponse{
  47. "MetadataRequest": NewMockMetadataResponse(t).
  48. SetController(seedBroker.BrokerID()).
  49. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  50. "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
  51. })
  52. config := NewConfig()
  53. config.Version = V0_10_2_0
  54. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. err = admin.CreateTopic("my_topic", &TopicDetail{NumPartitions: 1, ReplicationFactor: 1}, false)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. err = admin.Close()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. }
  67. func TestClusterAdminCreateTopicWithInvalidTopicDetail(t *testing.T) {
  68. seedBroker := NewMockBroker(t, 1)
  69. defer seedBroker.Close()
  70. seedBroker.SetHandlerByMap(map[string]MockResponse{
  71. "MetadataRequest": NewMockMetadataResponse(t).
  72. SetController(seedBroker.BrokerID()).
  73. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  74. "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
  75. })
  76. config := NewConfig()
  77. config.Version = V0_10_2_0
  78. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. err = admin.CreateTopic("my_topic", nil, false)
  83. if err.Error() != "you must specify topic details" {
  84. t.Fatal(err)
  85. }
  86. err = admin.Close()
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. }
  91. func TestClusterAdminCreateTopicWithoutAuthorization(t *testing.T) {
  92. seedBroker := NewMockBroker(t, 1)
  93. defer seedBroker.Close()
  94. seedBroker.SetHandlerByMap(map[string]MockResponse{
  95. "MetadataRequest": NewMockMetadataResponse(t).
  96. SetController(seedBroker.BrokerID()).
  97. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  98. "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
  99. })
  100. config := NewConfig()
  101. config.Version = V0_11_0_0
  102. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. err = admin.CreateTopic("_internal_topic", &TopicDetail{NumPartitions: 1, ReplicationFactor: 1}, false)
  107. want := "insufficient permissions to create topic with reserved prefix"
  108. if !strings.HasSuffix(err.Error(), want) {
  109. t.Fatal(err)
  110. }
  111. err = admin.Close()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. }
  116. func TestClusterAdminListTopics(t *testing.T) {
  117. seedBroker := NewMockBroker(t, 1)
  118. defer seedBroker.Close()
  119. seedBroker.SetHandlerByMap(map[string]MockResponse{
  120. "MetadataRequest": NewMockMetadataResponse(t).
  121. SetController(seedBroker.BrokerID()).
  122. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  123. SetLeader("my_topic", 0, seedBroker.BrokerID()),
  124. "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
  125. })
  126. config := NewConfig()
  127. config.Version = V1_1_0_0
  128. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. entries, err := admin.ListTopics()
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if len(entries) <= 0 {
  137. t.Fatal(errors.New("no resource present"))
  138. }
  139. topic, found := entries["my_topic"]
  140. if !found {
  141. t.Fatal(errors.New("topic not found in response"))
  142. }
  143. _, found = topic.ConfigEntries["max.message.bytes"]
  144. if found {
  145. t.Fatal(errors.New("default topic config entry incorrectly found in response"))
  146. }
  147. value := topic.ConfigEntries["retention.ms"]
  148. if value == nil || *value != "5000" {
  149. t.Fatal(errors.New("non-default topic config entry not found in response"))
  150. }
  151. err = admin.Close()
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. if topic.ReplicaAssignment == nil || topic.ReplicaAssignment[0][0] != 1 {
  156. t.Fatal(errors.New("replica assignment not found in response"))
  157. }
  158. }
  159. func TestClusterAdminDeleteTopic(t *testing.T) {
  160. seedBroker := NewMockBroker(t, 1)
  161. defer seedBroker.Close()
  162. seedBroker.SetHandlerByMap(map[string]MockResponse{
  163. "MetadataRequest": NewMockMetadataResponse(t).
  164. SetController(seedBroker.BrokerID()).
  165. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  166. "DeleteTopicsRequest": NewMockDeleteTopicsResponse(t),
  167. })
  168. config := NewConfig()
  169. config.Version = V0_10_2_0
  170. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. err = admin.DeleteTopic("my_topic")
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. err = admin.Close()
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. }
  183. func TestClusterAdminDeleteEmptyTopic(t *testing.T) {
  184. seedBroker := NewMockBroker(t, 1)
  185. defer seedBroker.Close()
  186. seedBroker.SetHandlerByMap(map[string]MockResponse{
  187. "MetadataRequest": NewMockMetadataResponse(t).
  188. SetController(seedBroker.BrokerID()).
  189. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  190. "DeleteTopicsRequest": NewMockDeleteTopicsResponse(t),
  191. })
  192. config := NewConfig()
  193. config.Version = V0_10_2_0
  194. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. err = admin.DeleteTopic("")
  199. if err != ErrInvalidTopic {
  200. t.Fatal(err)
  201. }
  202. err = admin.Close()
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. }
  207. func TestClusterAdminCreatePartitions(t *testing.T) {
  208. seedBroker := NewMockBroker(t, 1)
  209. defer seedBroker.Close()
  210. seedBroker.SetHandlerByMap(map[string]MockResponse{
  211. "MetadataRequest": NewMockMetadataResponse(t).
  212. SetController(seedBroker.BrokerID()).
  213. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  214. "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
  215. })
  216. config := NewConfig()
  217. config.Version = V1_0_0_0
  218. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. err = admin.CreatePartitions("my_topic", 3, nil, false)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. err = admin.Close()
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. }
  231. func TestClusterAdminCreatePartitionsWithDiffVersion(t *testing.T) {
  232. seedBroker := NewMockBroker(t, 1)
  233. defer seedBroker.Close()
  234. seedBroker.SetHandlerByMap(map[string]MockResponse{
  235. "MetadataRequest": NewMockMetadataResponse(t).
  236. SetController(seedBroker.BrokerID()).
  237. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  238. "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
  239. })
  240. config := NewConfig()
  241. config.Version = V0_10_2_0
  242. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. err = admin.CreatePartitions("my_topic", 3, nil, false)
  247. if err != ErrUnsupportedVersion {
  248. t.Fatal(err)
  249. }
  250. err = admin.Close()
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. }
  255. func TestClusterAdminCreatePartitionsWithoutAuthorization(t *testing.T) {
  256. seedBroker := NewMockBroker(t, 1)
  257. defer seedBroker.Close()
  258. seedBroker.SetHandlerByMap(map[string]MockResponse{
  259. "MetadataRequest": NewMockMetadataResponse(t).
  260. SetController(seedBroker.BrokerID()).
  261. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  262. "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
  263. })
  264. config := NewConfig()
  265. config.Version = V1_0_0_0
  266. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  267. if err != nil {
  268. t.Fatal(err)
  269. }
  270. err = admin.CreatePartitions("_internal_topic", 3, nil, false)
  271. want := "insufficient permissions to create partition on topic with reserved prefix"
  272. if !strings.HasSuffix(err.Error(), want) {
  273. t.Fatal(err)
  274. }
  275. err = admin.Close()
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. }
  280. func TestClusterAdminAlterPartitionReassignments(t *testing.T) {
  281. seedBroker := NewMockBroker(t, 1)
  282. defer seedBroker.Close()
  283. secondBroker := NewMockBroker(t, 2)
  284. defer secondBroker.Close()
  285. seedBroker.SetHandlerByMap(map[string]MockResponse{
  286. "MetadataRequest": NewMockMetadataResponse(t).
  287. SetController(secondBroker.BrokerID()).
  288. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  289. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  290. })
  291. secondBroker.SetHandlerByMap(map[string]MockResponse{
  292. "AlterPartitionReassignmentsRequest": NewMockAlterPartitionReassignmentsResponse(t),
  293. })
  294. config := NewConfig()
  295. config.Version = V2_4_0_0
  296. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. var topicAssignment = make([][]int32, 0, 3)
  301. err = admin.AlterPartitionReassignments("my_topic", topicAssignment)
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. err = admin.Close()
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. }
  310. func TestClusterAdminAlterPartitionReassignmentsWithDiffVersion(t *testing.T) {
  311. seedBroker := NewMockBroker(t, 1)
  312. defer seedBroker.Close()
  313. secondBroker := NewMockBroker(t, 2)
  314. defer secondBroker.Close()
  315. seedBroker.SetHandlerByMap(map[string]MockResponse{
  316. "MetadataRequest": NewMockMetadataResponse(t).
  317. SetController(secondBroker.BrokerID()).
  318. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  319. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  320. })
  321. secondBroker.SetHandlerByMap(map[string]MockResponse{
  322. "AlterPartitionReassignmentsRequest": NewMockAlterPartitionReassignmentsResponse(t),
  323. })
  324. config := NewConfig()
  325. config.Version = V2_3_0_0
  326. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. var topicAssignment = make([][]int32, 0, 3)
  331. err = admin.AlterPartitionReassignments("my_topic", topicAssignment)
  332. if !strings.ContainsAny(err.Error(), ErrUnsupportedVersion.Error()) {
  333. t.Fatal(err)
  334. }
  335. err = admin.Close()
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. }
  340. func TestClusterAdminListPartitionReassignments(t *testing.T) {
  341. seedBroker := NewMockBroker(t, 1)
  342. defer seedBroker.Close()
  343. secondBroker := NewMockBroker(t, 2)
  344. defer secondBroker.Close()
  345. seedBroker.SetHandlerByMap(map[string]MockResponse{
  346. "MetadataRequest": NewMockMetadataResponse(t).
  347. SetController(secondBroker.BrokerID()).
  348. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  349. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  350. })
  351. secondBroker.SetHandlerByMap(map[string]MockResponse{
  352. "ListPartitionReassignmentsRequest": NewMockListPartitionReassignmentsResponse(t),
  353. })
  354. config := NewConfig()
  355. config.Version = V2_4_0_0
  356. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  357. if err != nil {
  358. t.Fatal(err)
  359. }
  360. response, err := admin.ListPartitionReassignments("my_topic", []int32{0, 1})
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. partitionStatus, ok := response["my_topic"]
  365. if !ok {
  366. t.Fatalf("topic missing in response")
  367. } else {
  368. if len(partitionStatus) != 2 {
  369. t.Fatalf("partition missing in response")
  370. }
  371. }
  372. err = admin.Close()
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. }
  377. func TestClusterAdminListPartitionReassignmentsWithDiffVersion(t *testing.T) {
  378. seedBroker := NewMockBroker(t, 1)
  379. defer seedBroker.Close()
  380. secondBroker := NewMockBroker(t, 2)
  381. defer secondBroker.Close()
  382. seedBroker.SetHandlerByMap(map[string]MockResponse{
  383. "MetadataRequest": NewMockMetadataResponse(t).
  384. SetController(secondBroker.BrokerID()).
  385. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  386. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  387. })
  388. secondBroker.SetHandlerByMap(map[string]MockResponse{
  389. "ListPartitionReassignmentsRequest": NewMockListPartitionReassignmentsResponse(t),
  390. })
  391. config := NewConfig()
  392. config.Version = V2_3_0_0
  393. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  394. if err != nil {
  395. t.Fatal(err)
  396. }
  397. var partitions = make([]int32, 0)
  398. _, err = admin.ListPartitionReassignments("my_topic", partitions)
  399. if !strings.ContainsAny(err.Error(), ErrUnsupportedVersion.Error()) {
  400. t.Fatal(err)
  401. }
  402. err = admin.Close()
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. }
  407. func TestClusterAdminDeleteRecords(t *testing.T) {
  408. topicName := "my_topic"
  409. seedBroker := NewMockBroker(t, 1)
  410. defer seedBroker.Close()
  411. seedBroker.SetHandlerByMap(map[string]MockResponse{
  412. "MetadataRequest": NewMockMetadataResponse(t).
  413. SetController(seedBroker.BrokerID()).
  414. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  415. SetLeader(topicName, 1, 1).
  416. SetLeader(topicName, 2, 1).
  417. SetLeader(topicName, 3, 1),
  418. "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
  419. })
  420. config := NewConfig()
  421. config.Version = V1_0_0_0
  422. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. partitionOffsetFake := make(map[int32]int64)
  427. partitionOffsetFake[4] = 1000
  428. errFake := admin.DeleteRecords(topicName, partitionOffsetFake)
  429. if errFake == nil {
  430. t.Fatal(err)
  431. }
  432. partitionOffset := make(map[int32]int64)
  433. partitionOffset[1] = 1000
  434. partitionOffset[2] = 1000
  435. partitionOffset[3] = 1000
  436. err = admin.DeleteRecords(topicName, partitionOffset)
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. err = admin.Close()
  441. if err != nil {
  442. t.Fatal(err)
  443. }
  444. }
  445. func TestClusterAdminDeleteRecordsWithInCorrectBroker(t *testing.T) {
  446. topicName := "my_topic"
  447. seedBroker := NewMockBroker(t, 1)
  448. secondBroker := NewMockBroker(t, 2)
  449. defer seedBroker.Close()
  450. defer secondBroker.Close()
  451. seedBroker.SetHandlerByMap(map[string]MockResponse{
  452. "MetadataRequest": NewMockMetadataResponse(t).
  453. SetController(seedBroker.BrokerID()).
  454. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  455. SetBroker(secondBroker.Addr(), secondBroker.brokerID).
  456. SetLeader(topicName, 1, 1).
  457. SetLeader(topicName, 2, 1).
  458. SetLeader(topicName, 3, 2),
  459. "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
  460. })
  461. secondBroker.SetHandlerByMap(map[string]MockResponse{
  462. "MetadataRequest": NewMockMetadataResponse(t).
  463. SetController(seedBroker.BrokerID()).
  464. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  465. SetBroker(secondBroker.Addr(), secondBroker.brokerID).
  466. SetLeader(topicName, 1, 1).
  467. SetLeader(topicName, 2, 1).
  468. SetLeader(topicName, 3, 2),
  469. "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
  470. })
  471. config := NewConfig()
  472. config.Version = V1_0_0_0
  473. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. partitionOffset := make(map[int32]int64)
  478. partitionOffset[1] = 1000
  479. partitionOffset[2] = 1000
  480. partitionOffset[3] = 1000
  481. err = admin.DeleteRecords(topicName, partitionOffset)
  482. if err != nil {
  483. t.Fatal(err)
  484. }
  485. err = admin.Close()
  486. if err != nil {
  487. t.Fatal(err)
  488. }
  489. }
  490. func TestClusterAdminDeleteRecordsWithDiffVersion(t *testing.T) {
  491. topicName := "my_topic"
  492. seedBroker := NewMockBroker(t, 1)
  493. defer seedBroker.Close()
  494. seedBroker.SetHandlerByMap(map[string]MockResponse{
  495. "MetadataRequest": NewMockMetadataResponse(t).
  496. SetController(seedBroker.BrokerID()).
  497. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  498. SetLeader(topicName, 1, 1).
  499. SetLeader(topicName, 2, 1).
  500. SetLeader(topicName, 3, 1),
  501. "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
  502. })
  503. config := NewConfig()
  504. config.Version = V0_10_2_0
  505. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. partitionOffset := make(map[int32]int64)
  510. partitionOffset[1] = 1000
  511. partitionOffset[2] = 1000
  512. partitionOffset[3] = 1000
  513. err = admin.DeleteRecords(topicName, partitionOffset)
  514. if !strings.HasPrefix(err.Error(), "kafka server: failed to delete records") {
  515. t.Fatal(err)
  516. }
  517. deleteRecordsError, ok := err.(ErrDeleteRecords)
  518. if !ok {
  519. t.Fatal(err)
  520. }
  521. for _, err := range *deleteRecordsError.Errors {
  522. if err != ErrUnsupportedVersion {
  523. t.Fatal(err)
  524. }
  525. }
  526. err = admin.Close()
  527. if err != nil {
  528. t.Fatal(err)
  529. }
  530. }
  531. func TestClusterAdminDescribeConfig(t *testing.T) {
  532. seedBroker := NewMockBroker(t, 1)
  533. defer seedBroker.Close()
  534. seedBroker.SetHandlerByMap(map[string]MockResponse{
  535. "MetadataRequest": NewMockMetadataResponse(t).
  536. SetController(seedBroker.BrokerID()).
  537. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  538. "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
  539. })
  540. var tests = []struct {
  541. saramaVersion KafkaVersion
  542. requestVersion int16
  543. includeSynonyms bool
  544. }{
  545. {V1_0_0_0, 0, false},
  546. {V1_1_0_0, 1, true},
  547. {V1_1_1_0, 1, true},
  548. {V2_0_0_0, 2, true},
  549. }
  550. for _, tt := range tests {
  551. config := NewConfig()
  552. config.Version = tt.saramaVersion
  553. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  554. if err != nil {
  555. t.Fatal(err)
  556. }
  557. defer func() {
  558. _ = admin.Close()
  559. }()
  560. resource := ConfigResource{
  561. Name: "r1",
  562. Type: TopicResource,
  563. ConfigNames: []string{"my_topic"},
  564. }
  565. entries, err := admin.DescribeConfig(resource)
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. history := seedBroker.History()
  570. describeReq, ok := history[len(history)-1].Request.(*DescribeConfigsRequest)
  571. if !ok {
  572. t.Fatal("failed to find DescribeConfigsRequest in mockBroker history")
  573. }
  574. if describeReq.Version != tt.requestVersion {
  575. t.Fatalf(
  576. "requestVersion %v did not match expected %v",
  577. describeReq.Version, tt.requestVersion)
  578. }
  579. if len(entries) <= 0 {
  580. t.Fatal(errors.New("no resource present"))
  581. }
  582. if tt.includeSynonyms {
  583. if len(entries[0].Synonyms) == 0 {
  584. t.Fatal("expected synonyms to have been included")
  585. }
  586. }
  587. }
  588. }
  589. func TestClusterAdminDescribeConfigWithErrorCode(t *testing.T) {
  590. seedBroker := NewMockBroker(t, 1)
  591. defer seedBroker.Close()
  592. seedBroker.SetHandlerByMap(map[string]MockResponse{
  593. "MetadataRequest": NewMockMetadataResponse(t).
  594. SetController(seedBroker.BrokerID()).
  595. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  596. "DescribeConfigsRequest": NewMockDescribeConfigsResponseWithErrorCode(t),
  597. })
  598. config := NewConfig()
  599. config.Version = V1_1_0_0
  600. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  601. if err != nil {
  602. t.Fatal(err)
  603. }
  604. defer func() {
  605. _ = admin.Close()
  606. }()
  607. resource := ConfigResource{
  608. Name: "r1",
  609. Type: TopicResource,
  610. ConfigNames: []string{"my_topic"},
  611. }
  612. _, err = admin.DescribeConfig(resource)
  613. if err == nil {
  614. t.Fatal(errors.New("ErrorCode present but no Error returned"))
  615. }
  616. }
  617. // TestClusterAdminDescribeBrokerConfig ensures that a describe broker config
  618. // is sent to the broker in the resource struct, _not_ the controller
  619. func TestClusterAdminDescribeBrokerConfig(t *testing.T) {
  620. controllerBroker := NewMockBroker(t, 1)
  621. defer controllerBroker.Close()
  622. configBroker := NewMockBroker(t, 2)
  623. defer configBroker.Close()
  624. controllerBroker.SetHandlerByMap(map[string]MockResponse{
  625. "MetadataRequest": NewMockMetadataResponse(t).
  626. SetController(controllerBroker.BrokerID()).
  627. SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
  628. SetBroker(configBroker.Addr(), configBroker.BrokerID()),
  629. })
  630. configBroker.SetHandlerByMap(map[string]MockResponse{
  631. "MetadataRequest": NewMockMetadataResponse(t).
  632. SetController(controllerBroker.BrokerID()).
  633. SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
  634. SetBroker(configBroker.Addr(), configBroker.BrokerID()),
  635. "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
  636. })
  637. config := NewConfig()
  638. config.Version = V1_0_0_0
  639. admin, err := NewClusterAdmin(
  640. []string{
  641. controllerBroker.Addr(),
  642. configBroker.Addr(),
  643. }, config)
  644. if err != nil {
  645. t.Fatal(err)
  646. }
  647. for _, resourceType := range []ConfigResourceType{BrokerResource, BrokerLoggerResource} {
  648. resource := ConfigResource{Name: "2", Type: resourceType}
  649. entries, err := admin.DescribeConfig(resource)
  650. if err != nil {
  651. t.Fatal(err)
  652. }
  653. if len(entries) <= 0 {
  654. t.Fatal(errors.New("no resource present"))
  655. }
  656. }
  657. err = admin.Close()
  658. if err != nil {
  659. t.Fatal(err)
  660. }
  661. }
  662. func TestClusterAdminAlterConfig(t *testing.T) {
  663. seedBroker := NewMockBroker(t, 1)
  664. defer seedBroker.Close()
  665. seedBroker.SetHandlerByMap(map[string]MockResponse{
  666. "MetadataRequest": NewMockMetadataResponse(t).
  667. SetController(seedBroker.BrokerID()).
  668. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  669. "AlterConfigsRequest": NewMockAlterConfigsResponse(t),
  670. })
  671. config := NewConfig()
  672. config.Version = V1_0_0_0
  673. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  674. if err != nil {
  675. t.Fatal(err)
  676. }
  677. var value string
  678. entries := make(map[string]*string)
  679. value = "60000"
  680. entries["retention.ms"] = &value
  681. err = admin.AlterConfig(TopicResource, "my_topic", entries, false)
  682. if err != nil {
  683. t.Fatal(err)
  684. }
  685. err = admin.Close()
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. }
  690. func TestClusterAdminAlterConfigWithErrorCode(t *testing.T) {
  691. seedBroker := NewMockBroker(t, 1)
  692. defer seedBroker.Close()
  693. seedBroker.SetHandlerByMap(map[string]MockResponse{
  694. "MetadataRequest": NewMockMetadataResponse(t).
  695. SetController(seedBroker.BrokerID()).
  696. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  697. "AlterConfigsRequest": NewMockAlterConfigsResponseWithErrorCode(t),
  698. })
  699. config := NewConfig()
  700. config.Version = V1_0_0_0
  701. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  702. if err != nil {
  703. t.Fatal(err)
  704. }
  705. defer func() {
  706. _ = admin.Close()
  707. }()
  708. var value string
  709. entries := make(map[string]*string)
  710. value = "60000"
  711. entries["retention.ms"] = &value
  712. err = admin.AlterConfig(TopicResource, "my_topic", entries, false)
  713. if err == nil {
  714. t.Fatal(errors.New("ErrorCode present but no Error returned"))
  715. }
  716. }
  717. func TestClusterAdminAlterBrokerConfig(t *testing.T) {
  718. controllerBroker := NewMockBroker(t, 1)
  719. defer controllerBroker.Close()
  720. configBroker := NewMockBroker(t, 2)
  721. defer configBroker.Close()
  722. controllerBroker.SetHandlerByMap(map[string]MockResponse{
  723. "MetadataRequest": NewMockMetadataResponse(t).
  724. SetController(controllerBroker.BrokerID()).
  725. SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
  726. SetBroker(configBroker.Addr(), configBroker.BrokerID()),
  727. })
  728. configBroker.SetHandlerByMap(map[string]MockResponse{
  729. "MetadataRequest": NewMockMetadataResponse(t).
  730. SetController(controllerBroker.BrokerID()).
  731. SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
  732. SetBroker(configBroker.Addr(), configBroker.BrokerID()),
  733. "AlterConfigsRequest": NewMockAlterConfigsResponse(t),
  734. })
  735. config := NewConfig()
  736. config.Version = V1_0_0_0
  737. admin, err := NewClusterAdmin(
  738. []string{
  739. controllerBroker.Addr(),
  740. configBroker.Addr(),
  741. }, config)
  742. if err != nil {
  743. t.Fatal(err)
  744. }
  745. var value string
  746. entries := make(map[string]*string)
  747. value = "3"
  748. entries["min.insync.replicas"] = &value
  749. for _, resourceType := range []ConfigResourceType{BrokerResource, BrokerLoggerResource} {
  750. resource := ConfigResource{Name: "2", Type: resourceType}
  751. err = admin.AlterConfig(
  752. resource.Type,
  753. resource.Name,
  754. entries,
  755. false)
  756. if err != nil {
  757. t.Fatal(err)
  758. }
  759. }
  760. err = admin.Close()
  761. if err != nil {
  762. t.Fatal(err)
  763. }
  764. }
  765. func TestClusterAdminCreateAcl(t *testing.T) {
  766. seedBroker := NewMockBroker(t, 1)
  767. defer seedBroker.Close()
  768. seedBroker.SetHandlerByMap(map[string]MockResponse{
  769. "MetadataRequest": NewMockMetadataResponse(t).
  770. SetController(seedBroker.BrokerID()).
  771. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  772. "CreateAclsRequest": NewMockCreateAclsResponse(t),
  773. })
  774. config := NewConfig()
  775. config.Version = V1_0_0_0
  776. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  777. if err != nil {
  778. t.Fatal(err)
  779. }
  780. r := Resource{ResourceType: AclResourceTopic, ResourceName: "my_topic"}
  781. a := Acl{Host: "localhost", Operation: AclOperationAlter, PermissionType: AclPermissionAny}
  782. err = admin.CreateACL(r, a)
  783. if err != nil {
  784. t.Fatal(err)
  785. }
  786. err = admin.Close()
  787. if err != nil {
  788. t.Fatal(err)
  789. }
  790. }
  791. func TestClusterAdminListAcls(t *testing.T) {
  792. seedBroker := NewMockBroker(t, 1)
  793. defer seedBroker.Close()
  794. seedBroker.SetHandlerByMap(map[string]MockResponse{
  795. "MetadataRequest": NewMockMetadataResponse(t).
  796. SetController(seedBroker.BrokerID()).
  797. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  798. "DescribeAclsRequest": NewMockListAclsResponse(t),
  799. "CreateAclsRequest": NewMockCreateAclsResponse(t),
  800. })
  801. config := NewConfig()
  802. config.Version = V1_0_0_0
  803. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  804. if err != nil {
  805. t.Fatal(err)
  806. }
  807. r := Resource{ResourceType: AclResourceTopic, ResourceName: "my_topic"}
  808. a := Acl{Host: "localhost", Operation: AclOperationAlter, PermissionType: AclPermissionAny}
  809. err = admin.CreateACL(r, a)
  810. if err != nil {
  811. t.Fatal(err)
  812. }
  813. resourceName := "my_topic"
  814. filter := AclFilter{
  815. ResourceType: AclResourceTopic,
  816. Operation: AclOperationRead,
  817. ResourceName: &resourceName,
  818. }
  819. rAcls, err := admin.ListAcls(filter)
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. if len(rAcls) <= 0 {
  824. t.Fatal("no acls present")
  825. }
  826. err = admin.Close()
  827. if err != nil {
  828. t.Fatal(err)
  829. }
  830. }
  831. func TestClusterAdminDeleteAcl(t *testing.T) {
  832. seedBroker := NewMockBroker(t, 1)
  833. defer seedBroker.Close()
  834. seedBroker.SetHandlerByMap(map[string]MockResponse{
  835. "MetadataRequest": NewMockMetadataResponse(t).
  836. SetController(seedBroker.BrokerID()).
  837. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  838. "DeleteAclsRequest": NewMockDeleteAclsResponse(t),
  839. })
  840. config := NewConfig()
  841. config.Version = V1_0_0_0
  842. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  843. if err != nil {
  844. t.Fatal(err)
  845. }
  846. resourceName := "my_topic"
  847. filter := AclFilter{
  848. ResourceType: AclResourceTopic,
  849. Operation: AclOperationAlter,
  850. ResourceName: &resourceName,
  851. }
  852. _, err = admin.DeleteACL(filter, false)
  853. if err != nil {
  854. t.Fatal(err)
  855. }
  856. err = admin.Close()
  857. if err != nil {
  858. t.Fatal(err)
  859. }
  860. }
  861. func TestDescribeTopic(t *testing.T) {
  862. seedBroker := NewMockBroker(t, 1)
  863. defer seedBroker.Close()
  864. seedBroker.SetHandlerByMap(map[string]MockResponse{
  865. "MetadataRequest": NewMockMetadataResponse(t).
  866. SetController(seedBroker.BrokerID()).
  867. SetLeader("my_topic", 0, seedBroker.BrokerID()).
  868. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  869. })
  870. config := NewConfig()
  871. config.Version = V1_0_0_0
  872. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  873. if err != nil {
  874. t.Fatal(err)
  875. }
  876. topics, err := admin.DescribeTopics([]string{"my_topic"})
  877. if err != nil {
  878. t.Fatal(err)
  879. }
  880. if len(topics) != 1 {
  881. t.Fatalf("Expected 1 result, got %v", len(topics))
  882. }
  883. if topics[0].Name != "my_topic" {
  884. t.Fatalf("Incorrect topic name: %v", topics[0].Name)
  885. }
  886. err = admin.Close()
  887. if err != nil {
  888. t.Fatal(err)
  889. }
  890. }
  891. func TestDescribeTopicWithVersion0_11(t *testing.T) {
  892. seedBroker := NewMockBroker(t, 1)
  893. defer seedBroker.Close()
  894. seedBroker.SetHandlerByMap(map[string]MockResponse{
  895. "MetadataRequest": NewMockMetadataResponse(t).
  896. SetController(seedBroker.BrokerID()).
  897. SetLeader("my_topic", 0, seedBroker.BrokerID()).
  898. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  899. })
  900. config := NewConfig()
  901. config.Version = V0_11_0_0
  902. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  903. if err != nil {
  904. t.Fatal(err)
  905. }
  906. topics, err := admin.DescribeTopics([]string{"my_topic"})
  907. if err != nil {
  908. t.Fatal(err)
  909. }
  910. if len(topics) != 1 {
  911. t.Fatalf("Expected 1 result, got %v", len(topics))
  912. }
  913. if topics[0].Name != "my_topic" {
  914. t.Fatalf("Incorrect topic name: %v", topics[0].Name)
  915. }
  916. err = admin.Close()
  917. if err != nil {
  918. t.Fatal(err)
  919. }
  920. }
  921. func TestDescribeConsumerGroup(t *testing.T) {
  922. seedBroker := NewMockBroker(t, 1)
  923. defer seedBroker.Close()
  924. expectedGroupID := "my-group"
  925. seedBroker.SetHandlerByMap(map[string]MockResponse{
  926. "DescribeGroupsRequest": NewMockDescribeGroupsResponse(t).AddGroupDescription(expectedGroupID, &GroupDescription{
  927. GroupId: expectedGroupID,
  928. }),
  929. "MetadataRequest": NewMockMetadataResponse(t).
  930. SetController(seedBroker.BrokerID()).
  931. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  932. "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, expectedGroupID, seedBroker),
  933. })
  934. config := NewConfig()
  935. config.Version = V1_0_0_0
  936. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  937. if err != nil {
  938. t.Fatal(err)
  939. }
  940. result, err := admin.DescribeConsumerGroups([]string{expectedGroupID})
  941. if err != nil {
  942. t.Fatal(err)
  943. }
  944. if len(result) != 1 {
  945. t.Fatalf("Expected 1 result, got %v", len(result))
  946. }
  947. if result[0].GroupId != expectedGroupID {
  948. t.Fatalf("Expected groupID %v, got %v", expectedGroupID, result[0].GroupId)
  949. }
  950. err = admin.Close()
  951. if err != nil {
  952. t.Fatal(err)
  953. }
  954. }
  955. func TestListConsumerGroups(t *testing.T) {
  956. seedBroker := NewMockBroker(t, 1)
  957. defer seedBroker.Close()
  958. seedBroker.SetHandlerByMap(map[string]MockResponse{
  959. "MetadataRequest": NewMockMetadataResponse(t).
  960. SetController(seedBroker.BrokerID()).
  961. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  962. "ListGroupsRequest": NewMockListGroupsResponse(t).
  963. AddGroup("my-group", "consumer"),
  964. })
  965. config := NewConfig()
  966. config.Version = V1_0_0_0
  967. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  968. if err != nil {
  969. t.Fatal(err)
  970. }
  971. groups, err := admin.ListConsumerGroups()
  972. if err != nil {
  973. t.Fatal(err)
  974. }
  975. if len(groups) != 1 {
  976. t.Fatalf("Expected %v results, got %v", 1, len(groups))
  977. }
  978. protocolType, ok := groups["my-group"]
  979. if !ok {
  980. t.Fatal("Expected group to be returned, but it did not")
  981. }
  982. if protocolType != "consumer" {
  983. t.Fatalf("Expected protocolType %v, got %v", "consumer", protocolType)
  984. }
  985. err = admin.Close()
  986. if err != nil {
  987. t.Fatal(err)
  988. }
  989. }
  990. func TestListConsumerGroupsMultiBroker(t *testing.T) {
  991. seedBroker := NewMockBroker(t, 1)
  992. defer seedBroker.Close()
  993. secondBroker := NewMockBroker(t, 2)
  994. defer secondBroker.Close()
  995. firstGroup := "first"
  996. secondGroup := "second"
  997. nonExistingGroup := "non-existing-group"
  998. seedBroker.SetHandlerByMap(map[string]MockResponse{
  999. "MetadataRequest": NewMockMetadataResponse(t).
  1000. SetController(seedBroker.BrokerID()).
  1001. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  1002. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  1003. "ListGroupsRequest": NewMockListGroupsResponse(t).
  1004. AddGroup(firstGroup, "consumer"),
  1005. })
  1006. secondBroker.SetHandlerByMap(map[string]MockResponse{
  1007. "MetadataRequest": NewMockMetadataResponse(t).
  1008. SetController(seedBroker.BrokerID()).
  1009. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
  1010. SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
  1011. "ListGroupsRequest": NewMockListGroupsResponse(t).
  1012. AddGroup(secondGroup, "consumer"),
  1013. })
  1014. config := NewConfig()
  1015. config.Version = V1_0_0_0
  1016. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  1017. if err != nil {
  1018. t.Fatal(err)
  1019. }
  1020. groups, err := admin.ListConsumerGroups()
  1021. if err != nil {
  1022. t.Fatal(err)
  1023. }
  1024. if len(groups) != 2 {
  1025. t.Fatalf("Expected %v results, got %v", 1, len(groups))
  1026. }
  1027. if _, found := groups[firstGroup]; !found {
  1028. t.Fatalf("Expected group %v to be present in result set, but it isn't", firstGroup)
  1029. }
  1030. if _, found := groups[secondGroup]; !found {
  1031. t.Fatalf("Expected group %v to be present in result set, but it isn't", secondGroup)
  1032. }
  1033. if _, found := groups[nonExistingGroup]; found {
  1034. t.Fatalf("Expected group %v to not exist, but it exists", nonExistingGroup)
  1035. }
  1036. err = admin.Close()
  1037. if err != nil {
  1038. t.Fatal(err)
  1039. }
  1040. }
  1041. func TestListConsumerGroupOffsets(t *testing.T) {
  1042. seedBroker := NewMockBroker(t, 1)
  1043. defer seedBroker.Close()
  1044. group := "my-group"
  1045. topic := "my-topic"
  1046. partition := int32(0)
  1047. expectedOffset := int64(0)
  1048. seedBroker.SetHandlerByMap(map[string]MockResponse{
  1049. "OffsetFetchRequest": NewMockOffsetFetchResponse(t).SetOffset(group, "my-topic", partition, expectedOffset, "", ErrNoError).SetError(ErrNoError),
  1050. "MetadataRequest": NewMockMetadataResponse(t).
  1051. SetController(seedBroker.BrokerID()).
  1052. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  1053. "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, group, seedBroker),
  1054. })
  1055. config := NewConfig()
  1056. config.Version = V1_0_0_0
  1057. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  1058. if err != nil {
  1059. t.Fatal(err)
  1060. }
  1061. response, err := admin.ListConsumerGroupOffsets(group, map[string][]int32{
  1062. topic: {0},
  1063. })
  1064. if err != nil {
  1065. t.Fatalf("ListConsumerGroupOffsets failed with error %v", err)
  1066. }
  1067. block := response.GetBlock(topic, partition)
  1068. if block == nil {
  1069. t.Fatalf("Expected block for topic %v and partition %v to exist, but it doesn't", topic, partition)
  1070. }
  1071. if block.Offset != expectedOffset {
  1072. t.Fatalf("Expected offset %v, got %v", expectedOffset, block.Offset)
  1073. }
  1074. err = admin.Close()
  1075. if err != nil {
  1076. t.Fatal(err)
  1077. }
  1078. }
  1079. func TestDeleteConsumerGroup(t *testing.T) {
  1080. seedBroker := NewMockBroker(t, 1)
  1081. defer seedBroker.Close()
  1082. group := "my-group"
  1083. seedBroker.SetHandlerByMap(map[string]MockResponse{
  1084. // "OffsetFetchRequest": NewMockOffsetFetchResponse(t).SetOffset(group, "my-topic", partition, expectedOffset, "", ErrNoError),
  1085. "DeleteGroupsRequest": NewMockDeleteGroupsRequest(t).SetDeletedGroups([]string{group}),
  1086. "MetadataRequest": NewMockMetadataResponse(t).
  1087. SetController(seedBroker.BrokerID()).
  1088. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  1089. "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, group, seedBroker),
  1090. })
  1091. config := NewConfig()
  1092. config.Version = V1_1_0_0
  1093. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  1094. if err != nil {
  1095. t.Fatal(err)
  1096. }
  1097. err = admin.DeleteConsumerGroup(group)
  1098. if err != nil {
  1099. t.Fatalf("DeleteConsumerGroup failed with error %v", err)
  1100. }
  1101. }
  1102. // TestRefreshMetaDataWithDifferentController ensures that the cached
  1103. // controller can be forcibly updated from Metadata by the admin client
  1104. func TestRefreshMetaDataWithDifferentController(t *testing.T) {
  1105. seedBroker1 := NewMockBroker(t, 1)
  1106. seedBroker2 := NewMockBroker(t, 2)
  1107. defer seedBroker1.Close()
  1108. defer seedBroker2.Close()
  1109. seedBroker1.SetHandlerByMap(map[string]MockResponse{
  1110. "MetadataRequest": NewMockMetadataResponse(t).
  1111. SetController(seedBroker1.BrokerID()).
  1112. SetBroker(seedBroker1.Addr(), seedBroker1.BrokerID()).
  1113. SetBroker(seedBroker2.Addr(), seedBroker2.BrokerID()),
  1114. })
  1115. config := NewConfig()
  1116. config.Version = V1_1_0_0
  1117. client, err := NewClient([]string{seedBroker1.Addr()}, config)
  1118. if err != nil {
  1119. t.Fatal(err)
  1120. }
  1121. ca := clusterAdmin{client: client, conf: config}
  1122. if b, _ := ca.Controller(); seedBroker1.BrokerID() != b.ID() {
  1123. t.Fatalf("expected cached controller to be %d rather than %d",
  1124. seedBroker1.BrokerID(), b.ID())
  1125. }
  1126. seedBroker1.SetHandlerByMap(map[string]MockResponse{
  1127. "MetadataRequest": NewMockMetadataResponse(t).
  1128. SetController(seedBroker2.BrokerID()).
  1129. SetBroker(seedBroker1.Addr(), seedBroker1.BrokerID()).
  1130. SetBroker(seedBroker2.Addr(), seedBroker2.BrokerID()),
  1131. })
  1132. if b, _ := ca.refreshController(); seedBroker2.BrokerID() != b.ID() {
  1133. t.Fatalf("expected refreshed controller to be %d rather than %d",
  1134. seedBroker2.BrokerID(), b.ID())
  1135. }
  1136. if b, _ := ca.Controller(); seedBroker2.BrokerID() != b.ID() {
  1137. t.Fatalf("expected cached controller to be %d rather than %d",
  1138. seedBroker2.BrokerID(), b.ID())
  1139. }
  1140. }
  1141. func TestDescribeLogDirs(t *testing.T) {
  1142. seedBroker := NewMockBroker(t, 1)
  1143. defer seedBroker.Close()
  1144. seedBroker.SetHandlerByMap(map[string]MockResponse{
  1145. "MetadataRequest": NewMockMetadataResponse(t).
  1146. SetController(seedBroker.BrokerID()).
  1147. SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
  1148. "DescribeLogDirsRequest": NewMockDescribeLogDirsResponse(t).
  1149. SetLogDirs("/tmp/logs", map[string]int{"topic1": 2, "topic2": 2}),
  1150. })
  1151. config := NewConfig()
  1152. config.Version = V1_0_0_0
  1153. admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
  1154. if err != nil {
  1155. t.Fatal(err)
  1156. }
  1157. logDirsPerBroker, err := admin.DescribeLogDirs([]int32{seedBroker.BrokerID()})
  1158. if err != nil {
  1159. t.Fatal(err)
  1160. }
  1161. if len(logDirsPerBroker) != 1 {
  1162. t.Fatalf("Expected %v results, got %v", 1, len(logDirsPerBroker))
  1163. }
  1164. logDirs := logDirsPerBroker[seedBroker.BrokerID()]
  1165. if len(logDirs) != 1 {
  1166. t.Fatalf("Expected log dirs for broker %v to be returned, but it did not, got %v", seedBroker.BrokerID(), len(logDirs))
  1167. }
  1168. logDirsBroker := logDirs[0]
  1169. if logDirsBroker.ErrorCode != ErrNoError {
  1170. t.Fatalf("Expected no error for broker %v, but it was %v", seedBroker.BrokerID(), logDirsBroker.ErrorCode)
  1171. }
  1172. if logDirsBroker.Path != "/tmp/logs" {
  1173. t.Fatalf("Expected log dirs for broker %v to be '/tmp/logs', but it was %v", seedBroker.BrokerID(), logDirsBroker.Path)
  1174. }
  1175. if len(logDirsBroker.Topics) != 2 {
  1176. t.Fatalf("Expected log dirs for broker %v to have 2 topics, but it had %v", seedBroker.BrokerID(), len(logDirsBroker.Topics))
  1177. }
  1178. err = admin.Close()
  1179. if err != nil {
  1180. t.Fatal(err)
  1181. }
  1182. }