aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/kafka-tests.yml
blob: cc4ef0348c68cf547d7805ca2effb0d1e0b76427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
name: "Kafka Gateway Tests"

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

concurrency:
  group: ${{ github.head_ref }}/kafka-tests
  cancel-in-progress: true

# Force different runners for better isolation
env:
  FORCE_RUNNER_SEPARATION: true

permissions:
  contents: read

jobs:
  kafka-unit-tests:
    name: Kafka Unit Tests
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      fail-fast: false
      matrix:
        container-id: [unit-tests-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 1.0 --memory 1g --hostname kafka-unit-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 1
      CGO_ENABLED: 0
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
      id: go

    - name: Check out code
      uses: actions/checkout@v5

    - name: Setup Container Environment
      run: |
        apk add --no-cache git
        ulimit -n 1024 || echo "Warning: Could not set file descriptor limit"
        
    - name: Get dependencies
      run: |
        cd test/kafka
        go mod download

    - name: Run Kafka Gateway Unit Tests
      run: |
        cd test/kafka
        # Set process limits for container isolation
        ulimit -n 512 || echo "Warning: Could not set file descriptor limit"
        ulimit -u 100 || echo "Warning: Could not set process limit"
        go test -v -timeout 10s ./unit/...

  kafka-integration-tests:
    name: Kafka Integration Tests (Critical)
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      fail-fast: false
      matrix:
        container-id: [integration-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 2.0 --memory 2g --ulimit nofile=1024:1024 --hostname kafka-integration-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 2
      CGO_ENABLED: 0
      KAFKA_TEST_ISOLATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
      id: go

    - name: Check out code
      uses: actions/checkout@v5

    - name: Setup Integration Container Environment
      run: |
        apk add --no-cache git procps
        ulimit -n 2048 || echo "Warning: Could not set file descriptor limit"
        
    - name: Get dependencies
      run: |
        cd test/kafka
        go mod download

    - name: Run Integration Tests
      run: |
        cd test/kafka
        # Higher limits for integration tests
        ulimit -n 1024 || echo "Warning: Could not set file descriptor limit"
        ulimit -u 200 || echo "Warning: Could not set process limit"
        go test -v -timeout 90s ./integration/...
      env:
        GOMAXPROCS: 2

  kafka-e2e-tests:
    name: Kafka End-to-End Tests (with SMQ)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        container-id: [e2e-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 2.0 --memory 2g --hostname kafka-e2e-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 2
      CGO_ENABLED: 0
      KAFKA_E2E_ISOLATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Check out code
      uses: actions/checkout@v5

    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
        cache: true
        cache-dependency-path: |
          **/go.sum
      id: go

    - name: Setup E2E Container Environment
      run: |
        apk add --no-cache git procps curl netcat-openbsd
        ulimit -n 2048 || echo "Warning: Could not set file descriptor limit"
        
    - name: Warm Go module cache
      run: |
        # Warm cache for root module
        go mod download || true
        # Warm cache for kafka test module
        cd test/kafka
        go mod download || true

    - name: Get dependencies
      run: |
        cd test/kafka
        # Use go mod download with timeout to prevent hanging
        timeout 90s go mod download || echo "Warning: Dependency download timed out, continuing with cached modules"

    - name: Build and start SeaweedFS MQ
      run: |
        set -e
        cd $GITHUB_WORKSPACE
        # Build weed binary
        go build -o /usr/local/bin/weed ./weed
        # Start SeaweedFS components with MQ brokers
        export WEED_DATA_DIR=/tmp/seaweedfs-e2e-$RANDOM
        mkdir -p "$WEED_DATA_DIR"
        
        # Start SeaweedFS server (master, volume, filer) with consistent IP advertising
        nohup weed -v 1 server \
          -ip="127.0.0.1" \
          -ip.bind="0.0.0.0" \
          -dir="$WEED_DATA_DIR" \
          -master.raftHashicorp \
          -master.port=9333 \
          -volume.port=8081 \
          -filer.port=8888 \
          -filer=true \
          -metricsPort=9325 \
          > /tmp/weed-server.log 2>&1 &
          
        # Wait for master to be ready
        for i in $(seq 1 30); do
          if curl -s http://127.0.0.1:9333/cluster/status >/dev/null; then
            echo "SeaweedFS master HTTP is up"; break
          fi
          echo "Waiting for SeaweedFS master HTTP... ($i/30)"; sleep 1
        done
        
        # Wait for master gRPC to be ready (this is what broker discovery uses)
        echo "Waiting for master gRPC port..."
        for i in $(seq 1 30); do
          if nc -z 127.0.0.1 19333; then
            echo "✓ SeaweedFS master gRPC is up (port 19333)"
            break
          fi
          echo "  Waiting for master gRPC... ($i/30)"; sleep 1
        done
        
        # Give server time to initialize all components including gRPC services
        echo "Waiting for SeaweedFS components to initialize..."
        sleep 15
        
        # Additional wait specifically for gRPC services to be ready for streaming
        echo "Allowing extra time for master gRPC streaming services to initialize..."
        sleep 10
        
        # Start MQ broker with maximum verbosity for debugging
        echo "Starting MQ broker..."
        nohup weed -v 3 mq.broker \
          -master="127.0.0.1:9333" \
          -ip="127.0.0.1" \
          -port=17777 \
          -logFlushInterval=0 \
          > /tmp/weed-mq-broker.log 2>&1 &
        
        # Wait for broker to be ready with better error reporting  
        sleep 15
        broker_ready=false
        for i in $(seq 1 20); do
          if nc -z 127.0.0.1 17777; then
            echo "SeaweedFS MQ broker is up"
            broker_ready=true
            break
          fi
          echo "Waiting for MQ broker... ($i/20)"; sleep 1
        done
        
        # Give broker additional time to register with master
        if [ "$broker_ready" = true ]; then
          echo "Allowing broker to register with master..."
          sleep 30
          
          # Check if broker is properly registered by querying cluster nodes
          echo "Cluster status after broker registration:"
          curl -s "http://127.0.0.1:9333/cluster/status" || echo "Could not check cluster status"
          
          echo "Checking cluster topology (includes registered components):"
          curl -s "http://127.0.0.1:9333/dir/status" | head -20 || echo "Could not check dir status"
          
          echo "Verifying broker discovery via master client debug:"
          echo "If broker registration is successful, it should appear in dir status"
          
          echo "Testing gRPC connectivity with weed binary:"
          echo "This simulates what the gateway does during broker discovery..."
          timeout 10s weed shell -master=127.0.0.1:9333 -filer=127.0.0.1:8888 > /tmp/shell-test.log 2>&1 || echo "weed shell test completed or timed out - checking logs..."
          echo "Shell test results:"
          cat /tmp/shell-test.log 2>/dev/null | head -10 || echo "No shell test logs"
        fi
        
        # Check if broker failed to start and show logs
        if [ "$broker_ready" = false ]; then
          echo "ERROR: MQ broker failed to start. Broker logs:"
          cat /tmp/weed-mq-broker.log || echo "No broker logs found"
          echo "Server logs:"  
          tail -20 /tmp/weed-server.log || echo "No server logs found"
          exit 1
        fi

    - name: Run End-to-End Tests
      run: |
        cd test/kafka
        # Higher limits for E2E tests
        ulimit -n 1024 || echo "Warning: Could not set file descriptor limit"
        ulimit -u 200 || echo "Warning: Could not set process limit"
        
        # Allow additional time for all background processes to settle
        echo "Allowing additional settlement time for SeaweedFS ecosystem..."
        sleep 15
        
        # Run tests and capture result
        if ! go test -v -timeout 180s ./e2e/...; then
          echo "========================================="
          echo "Tests failed! Showing debug information:"
          echo "========================================="
          echo "Server logs (last 50 lines):"
          tail -50 /tmp/weed-server.log || echo "No server logs"
          echo "========================================="
          echo "Broker logs (last 50 lines):"
          tail -50 /tmp/weed-mq-broker.log || echo "No broker logs"
          echo "========================================="
          exit 1
        fi
      env:
        GOMAXPROCS: 2
        SEAWEEDFS_MASTERS: 127.0.0.1:9333

  kafka-consumer-group-tests:
    name: Kafka Consumer Group Tests (Highly Isolated)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        container-id: [consumer-group-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 1.0 --memory 2g --ulimit nofile=512:512 --hostname kafka-consumer-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 1
      CGO_ENABLED: 0
      KAFKA_CONSUMER_ISOLATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Check out code
      uses: actions/checkout@v5

    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
        cache: true
        cache-dependency-path: |
          **/go.sum
      id: go

    - name: Setup Consumer Group Container Environment
      run: |
        apk add --no-cache git procps curl netcat-openbsd
        ulimit -n 256 || echo "Warning: Could not set file descriptor limit"
        
    - name: Warm Go module cache
      run: |
        # Warm cache for root module
        go mod download || true
        # Warm cache for kafka test module
        cd test/kafka
        go mod download || true

    - name: Get dependencies
      run: |
        cd test/kafka
        # Use go mod download with timeout to prevent hanging
        timeout 90s go mod download || echo "Warning: Dependency download timed out, continuing with cached modules"

    - name: Build and start SeaweedFS MQ
      run: |
        set -e
        cd $GITHUB_WORKSPACE
        # Build weed binary
        go build -o /usr/local/bin/weed ./weed
        # Start SeaweedFS components with MQ brokers
        export WEED_DATA_DIR=/tmp/seaweedfs-mq-$RANDOM
        mkdir -p "$WEED_DATA_DIR"
        
        # Start SeaweedFS server (master, volume, filer) with consistent IP advertising
        nohup weed -v 1 server \
          -ip="127.0.0.1" \
          -ip.bind="0.0.0.0" \
          -dir="$WEED_DATA_DIR" \
          -master.raftHashicorp \
          -master.port=9333 \
          -volume.port=8081 \
          -filer.port=8888 \
          -filer=true \
          -metricsPort=9325 \
          > /tmp/weed-server.log 2>&1 &
          
        # Wait for master to be ready
        for i in $(seq 1 30); do
          if curl -s http://127.0.0.1:9333/cluster/status >/dev/null; then
            echo "SeaweedFS master HTTP is up"; break
          fi
          echo "Waiting for SeaweedFS master HTTP... ($i/30)"; sleep 1
        done
        
        # Wait for master gRPC to be ready (this is what broker discovery uses)
        echo "Waiting for master gRPC port..."
        for i in $(seq 1 30); do
          if nc -z 127.0.0.1 19333; then
            echo "✓ SeaweedFS master gRPC is up (port 19333)"
            break
          fi
          echo "  Waiting for master gRPC... ($i/30)"; sleep 1
        done
        
        # Give server time to initialize all components including gRPC services
        echo "Waiting for SeaweedFS components to initialize..."
        sleep 15
        
        # Additional wait specifically for gRPC services to be ready for streaming
        echo "Allowing extra time for master gRPC streaming services to initialize..."
        sleep 10
        
        # Start MQ broker with maximum verbosity for debugging
        echo "Starting MQ broker..."
        nohup weed -v 3 mq.broker \
          -master="127.0.0.1:9333" \
          -ip="127.0.0.1" \
          -port=17777 \
          -logFlushInterval=0 \
          > /tmp/weed-mq-broker.log 2>&1 &
        
        # Wait for broker to be ready with better error reporting  
        sleep 15
        broker_ready=false
        for i in $(seq 1 20); do
          if nc -z 127.0.0.1 17777; then
            echo "SeaweedFS MQ broker is up"
            broker_ready=true
            break
          fi
          echo "Waiting for MQ broker... ($i/20)"; sleep 1
        done
        
        # Give broker additional time to register with master
        if [ "$broker_ready" = true ]; then
          echo "Allowing broker to register with master..."
          sleep 30
          
          # Check if broker is properly registered by querying cluster nodes
          echo "Cluster status after broker registration:"
          curl -s "http://127.0.0.1:9333/cluster/status" || echo "Could not check cluster status"
          
          echo "Checking cluster topology (includes registered components):"
          curl -s "http://127.0.0.1:9333/dir/status" | head -20 || echo "Could not check dir status"
          
          echo "Verifying broker discovery via master client debug:"
          echo "If broker registration is successful, it should appear in dir status"
          
          echo "Testing gRPC connectivity with weed binary:"
          echo "This simulates what the gateway does during broker discovery..."
          timeout 10s weed shell -master=127.0.0.1:9333 -filer=127.0.0.1:8888 > /tmp/shell-test.log 2>&1 || echo "weed shell test completed or timed out - checking logs..."
          echo "Shell test results:"
          cat /tmp/shell-test.log 2>/dev/null | head -10 || echo "No shell test logs"
        fi
        
        # Check if broker failed to start and show logs
        if [ "$broker_ready" = false ]; then
          echo "ERROR: MQ broker failed to start. Broker logs:"
          cat /tmp/weed-mq-broker.log || echo "No broker logs found"
          echo "Server logs:"  
          tail -20 /tmp/weed-server.log || echo "No server logs found"
          exit 1
        fi

    - name: Run Consumer Group Tests
      run: |
        cd test/kafka
        # Test consumer group functionality with explicit timeout
        ulimit -n 512 || echo "Warning: Could not set file descriptor limit"
        ulimit -u 100 || echo "Warning: Could not set process limit"
        timeout 240s go test -v -run "^TestConsumerGroups" -timeout 180s ./integration/... || echo "Test execution timed out or failed"
      env:
        GOMAXPROCS: 1
        SEAWEEDFS_MASTERS: 127.0.0.1:9333

  kafka-client-compatibility:
    name: Kafka Client Compatibility (with SMQ)
    runs-on: ubuntu-latest
    timeout-minutes: 25
    strategy:
      fail-fast: false
      matrix:
        container-id: [client-compat-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 1.0 --memory 1.5g --shm-size 256m --hostname kafka-client-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 1
      CGO_ENABLED: 0
      KAFKA_CLIENT_ISOLATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Check out code
      uses: actions/checkout@v5

    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
        cache: true
        cache-dependency-path: |
          **/go.sum
      id: go

    - name: Setup Client Container Environment
      run: |
        apk add --no-cache git procps curl netcat-openbsd
        ulimit -n 1024 || echo "Warning: Could not set file descriptor limit"

    - name: Warm Go module cache
      run: |
        # Warm cache for root module
        go mod download || true
        # Warm cache for kafka test module
        cd test/kafka
        go mod download || true

    - name: Get dependencies
      run: |
        cd test/kafka
        timeout 90s go mod download || echo "Warning: Dependency download timed out, continuing with cached modules"

    - name: Build and start SeaweedFS MQ
      run: |
        set -e
        cd $GITHUB_WORKSPACE
        # Build weed binary
        go build -o /usr/local/bin/weed ./weed
        # Start SeaweedFS components with MQ brokers
        export WEED_DATA_DIR=/tmp/seaweedfs-client-$RANDOM
        mkdir -p "$WEED_DATA_DIR"
        
        # Start SeaweedFS server (master, volume, filer) with consistent IP advertising
        nohup weed -v 1 server \
          -ip="127.0.0.1" \
          -ip.bind="0.0.0.0" \
          -dir="$WEED_DATA_DIR" \
          -master.raftHashicorp \
          -master.port=9333 \
          -volume.port=8081 \
          -filer.port=8888 \
          -filer=true \
          -metricsPort=9325 \
          > /tmp/weed-server.log 2>&1 &
          
        # Wait for master to be ready
        for i in $(seq 1 30); do
          if curl -s http://127.0.0.1:9333/cluster/status >/dev/null; then
            echo "SeaweedFS master HTTP is up"; break
          fi
          echo "Waiting for SeaweedFS master HTTP... ($i/30)"; sleep 1
        done
        
        # Wait for master gRPC to be ready (this is what broker discovery uses)
        echo "Waiting for master gRPC port..."
        for i in $(seq 1 30); do
          if nc -z 127.0.0.1 19333; then
            echo "✓ SeaweedFS master gRPC is up (port 19333)"
            break
          fi
          echo "  Waiting for master gRPC... ($i/30)"; sleep 1
        done
        
        # Give server time to initialize all components including gRPC services
        echo "Waiting for SeaweedFS components to initialize..."
        sleep 15
        
        # Additional wait specifically for gRPC services to be ready for streaming
        echo "Allowing extra time for master gRPC streaming services to initialize..."
        sleep 10
        
        # Start MQ broker with maximum verbosity for debugging
        echo "Starting MQ broker..."
        nohup weed -v 3 mq.broker \
          -master="127.0.0.1:9333" \
          -ip="127.0.0.1" \
          -port=17777 \
          -logFlushInterval=0 \
          > /tmp/weed-mq-broker.log 2>&1 &
        
        # Wait for broker to be ready with better error reporting  
        sleep 15
        broker_ready=false
        for i in $(seq 1 20); do
          if nc -z 127.0.0.1 17777; then
            echo "SeaweedFS MQ broker is up"
            broker_ready=true
            break
          fi
          echo "Waiting for MQ broker... ($i/20)"; sleep 1
        done
        
        # Give broker additional time to register with master
        if [ "$broker_ready" = true ]; then
          echo "Allowing broker to register with master..."
          sleep 30
          
          # Check if broker is properly registered by querying cluster nodes
          echo "Cluster status after broker registration:"
          curl -s "http://127.0.0.1:9333/cluster/status" || echo "Could not check cluster status"
          
          echo "Checking cluster topology (includes registered components):"
          curl -s "http://127.0.0.1:9333/dir/status" | head -20 || echo "Could not check dir status"
          
          echo "Verifying broker discovery via master client debug:"
          echo "If broker registration is successful, it should appear in dir status"
          
          echo "Testing gRPC connectivity with weed binary:"
          echo "This simulates what the gateway does during broker discovery..."
          timeout 10s weed shell -master=127.0.0.1:9333 -filer=127.0.0.1:8888 > /tmp/shell-test.log 2>&1 || echo "weed shell test completed or timed out - checking logs..."
          echo "Shell test results:"
          cat /tmp/shell-test.log 2>/dev/null | head -10 || echo "No shell test logs"
        fi
        
        # Check if broker failed to start and show logs
        if [ "$broker_ready" = false ]; then
          echo "ERROR: MQ broker failed to start. Broker logs:"
          cat /tmp/weed-mq-broker.log || echo "No broker logs found"
          echo "Server logs:"  
          tail -20 /tmp/weed-server.log || echo "No server logs found"
          exit 1
        fi

    - name: Run Client Compatibility Tests
      run: |
        cd test/kafka
        go test -v -run "^TestClientCompatibility" -timeout 180s ./integration/...
      env:
        GOMAXPROCS: 1
        SEAWEEDFS_MASTERS: 127.0.0.1:9333

  kafka-smq-integration-tests:
    name: Kafka SMQ Integration Tests (Full Stack)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        container-id: [smq-integration-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 1.0 --memory 2g --hostname kafka-smq-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 1
      CGO_ENABLED: 0
      KAFKA_SMQ_INTEGRATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Check out code
      uses: actions/checkout@v5

    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
        cache: true
        cache-dependency-path: |
          **/go.sum
      id: go

    - name: Setup SMQ Integration Container Environment
      run: |
        apk add --no-cache git procps curl netcat-openbsd
        ulimit -n 1024 || echo "Warning: Could not set file descriptor limit"
        
    - name: Warm Go module cache
      run: |
        # Warm cache for root module
        go mod download || true
        # Warm cache for kafka test module
        cd test/kafka
        go mod download || true

    - name: Get dependencies
      run: |
        cd test/kafka
        timeout 90s go mod download || echo "Warning: Dependency download timed out, continuing with cached modules"

    - name: Build and start SeaweedFS MQ
      run: |
        set -e
        cd $GITHUB_WORKSPACE
        # Build weed binary
        go build -o /usr/local/bin/weed ./weed
        # Start SeaweedFS components with MQ brokers
        export WEED_DATA_DIR=/tmp/seaweedfs-smq-$RANDOM
        mkdir -p "$WEED_DATA_DIR"
        
        # Start SeaweedFS server (master, volume, filer) with consistent IP advertising
        nohup weed -v 1 server \
          -ip="127.0.0.1" \
          -ip.bind="0.0.0.0" \
          -dir="$WEED_DATA_DIR" \
          -master.raftHashicorp \
          -master.port=9333 \
          -volume.port=8081 \
          -filer.port=8888 \
          -filer=true \
          -metricsPort=9325 \
          > /tmp/weed-server.log 2>&1 &
          
        # Wait for master to be ready
        for i in $(seq 1 30); do
          if curl -s http://127.0.0.1:9333/cluster/status >/dev/null; then
            echo "SeaweedFS master HTTP is up"; break
          fi
          echo "Waiting for SeaweedFS master HTTP... ($i/30)"; sleep 1
        done
        
        # Wait for master gRPC to be ready (this is what broker discovery uses)
        echo "Waiting for master gRPC port..."
        for i in $(seq 1 30); do
          if nc -z 127.0.0.1 19333; then
            echo "✓ SeaweedFS master gRPC is up (port 19333)"
            break
          fi
          echo "  Waiting for master gRPC... ($i/30)"; sleep 1
        done
        
        # Give server time to initialize all components including gRPC services
        echo "Waiting for SeaweedFS components to initialize..."
        sleep 15
        
        # Additional wait specifically for gRPC services to be ready for streaming
        echo "Allowing extra time for master gRPC streaming services to initialize..."
        sleep 10
        
        # Start MQ broker with maximum verbosity for debugging
        echo "Starting MQ broker..."
        nohup weed -v 3 mq.broker \
          -master="127.0.0.1:9333" \
          -ip="127.0.0.1" \
          -port=17777 \
          -logFlushInterval=0 \
          > /tmp/weed-mq-broker.log 2>&1 &
        
        # Wait for broker to be ready with better error reporting  
        sleep 15
        broker_ready=false
        for i in $(seq 1 20); do
          if nc -z 127.0.0.1 17777; then
            echo "SeaweedFS MQ broker is up"
            broker_ready=true
            break
          fi
          echo "Waiting for MQ broker... ($i/20)"; sleep 1
        done
        
        # Give broker additional time to register with master
        if [ "$broker_ready" = true ]; then
          echo "Allowing broker to register with master..."
          sleep 30
          
          # Check if broker is properly registered by querying cluster nodes
          echo "Cluster status after broker registration:"
          curl -s "http://127.0.0.1:9333/cluster/status" || echo "Could not check cluster status"
          
          echo "Checking cluster topology (includes registered components):"
          curl -s "http://127.0.0.1:9333/dir/status" | head -20 || echo "Could not check dir status"
          
          echo "Verifying broker discovery via master client debug:"
          echo "If broker registration is successful, it should appear in dir status"
          
          echo "Testing gRPC connectivity with weed binary:"
          echo "This simulates what the gateway does during broker discovery..."
          timeout 10s weed shell -master=127.0.0.1:9333 -filer=127.0.0.1:8888 > /tmp/shell-test.log 2>&1 || echo "weed shell test completed or timed out - checking logs..."
          echo "Shell test results:"
          cat /tmp/shell-test.log 2>/dev/null | head -10 || echo "No shell test logs"
        fi
        
        # Check if broker failed to start and show logs
        if [ "$broker_ready" = false ]; then
          echo "ERROR: MQ broker failed to start. Broker logs:"
          cat /tmp/weed-mq-broker.log || echo "No broker logs found"
          echo "Server logs:"  
          tail -20 /tmp/weed-server.log || echo "No server logs found"
          exit 1
        fi

    - name: Run SMQ Integration Tests
      run: |
        cd test/kafka
        ulimit -n 512 || echo "Warning: Could not set file descriptor limit"
        ulimit -u 100 || echo "Warning: Could not set process limit"
        # Run the dedicated SMQ integration tests
        go test -v -run "^TestSMQIntegration" -timeout 180s ./integration/...
      env:
        GOMAXPROCS: 1
        SEAWEEDFS_MASTERS: 127.0.0.1:9333

  kafka-protocol-tests:
    name: Kafka Protocol Tests (Isolated)
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      fail-fast: false
      matrix:
        container-id: [protocol-1]
    container:
      image: golang:1.24-alpine
      options: --cpus 1.0 --memory 1g --tmpfs /tmp:exec --hostname kafka-protocol-${{ matrix.container-id }}
    env:
      GOMAXPROCS: 1
      CGO_ENABLED: 0
      KAFKA_PROTOCOL_ISOLATION: "true"
      CONTAINER_ID: ${{ matrix.container-id }}
    steps:
    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ^1.24
      id: go

    - name: Check out code
      uses: actions/checkout@v5

    - name: Setup Protocol Container Environment
      run: |
        apk add --no-cache git procps
        # Ensure proper permissions for test execution
        chmod -R 755 /tmp || true
        export TMPDIR=/tmp
        export GOCACHE=/tmp/go-cache
        mkdir -p $GOCACHE
        chmod 755 $GOCACHE

    - name: Get dependencies
      run: |
        cd test/kafka
        go mod download

    - name: Run Protocol Tests
      run: |
        cd test/kafka
        export TMPDIR=/tmp
        export GOCACHE=/tmp/go-cache
        # Run protocol tests from the weed/mq/kafka directory since they test the protocol implementation
        cd ../../weed/mq/kafka
        go test -v -run "^Test.*" -timeout 10s ./...
      env:
        GOMAXPROCS: 1
        TMPDIR: /tmp
        GOCACHE: /tmp/go-cache