diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..3aac200 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,67 @@ +name: CD + +on: + push: + tags: [ "v*.*.*" ] + +permissions: + contents: write + +env: + CGO_ENABLED: 0 + APP: ${{ github.event.repository.name }} + VERSION: ${{ github.ref_name }} + +jobs: + build: + name: Build and package + runs-on: ubuntu-latest + steps: + - name: Setup go + uses: actions/setup-go@v3.5.0 + with: + go-version: 1.24 + - name: Clone repo + uses: actions/checkout@v3 + - name: Build and package (linux/amd64) + run: | + GOOS=linux GOARCH=amd64 go build -ldflags="-X 'main.commit=${{ github.sha }}' -X 'main.tag=${{ env.VERSION }}'" -o build/linux-amd64/${{ env.APP }} + tar -czvf build/${{ env.APP }}-${{ env.VERSION }}-linux-amd64.tar.gz -C build/linux-amd64 ${{ env.APP }} + - name: Build and package (darwin/amd64) + run: | + GOOS=darwin GOARCH=amd64 go build -ldflags="-X 'main.commit=${{ github.sha }}' -X 'main.tag=${{ env.VERSION }}'" -o build/darwin-amd64/${{ env.APP }} + tar -czvf build/${{ env.APP }}-${{ env.VERSION }}-darwin-amd64.tar.gz -C build/darwin-amd64 ${{ env.APP }} + - name: Build and package (darwin/arm64) + run: | + GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'main.commit=${{ github.sha }}' -X 'main.tag=${{ env.VERSION }}'" -o build/darwin-arm64/${{ env.APP }} + tar -czvf build/${{ env.APP }}-${{ env.VERSION }}-darwin-arm64.tar.gz -C build/darwin-arm64 ${{ env.APP }} + - name: Upload build artifacts + uses: actions/upload-artifact@v4 + with: + name: build-artifacts + if-no-files-found: error + path: build/*.tar.gz + retention-days: 1 + overwrite: true + release: + name: Release + runs-on: ubuntu-latest + needs: build + steps: + - name: Download build artifacts + uses: actions/download-artifact@v4 + with: + name: build-artifacts + path: build + - name: Create release + id: create_release + if: github.ref_type == 'tag' + uses: softprops/action-gh-release@v2 + with: + files: | + build/${{ env.APP }}-${{ env.VERSION }}-linux-amd64.tar.gz + build/${{ env.APP }}-${{ env.VERSION }}-darwin-amd64.tar.gz + build/${{ env.APP }}-${{ env.VERSION }}-darwin-arm64.tar.gz + - name: Report + run: | + echo "release url: ${{ steps.create_release.outputs.url }}" \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f0a6d0..a93dd0c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,9 +11,21 @@ jobs: - name: Setup go uses: actions/setup-go@v3.5.0 with: - go-version: 1.23 + go-version: 1.24 - name: Clone repo uses: actions/checkout@v3 - name: Build - run: make build + run: CGO_ENABLED=0;go build -o /dev/null + test: + runs-on: ubuntu-latest + steps: + - name: Setup go + uses: actions/setup-go@v3.5.0 + with: + go-version: 1.24 + - name: Clone repo + uses: actions/checkout@v3 + - name: Integration Test + run: tests/integration-tests.sh + \ No newline at end of file diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..a243118 --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,15 @@ +# Installing Klient + +If `go` is available on the machine, `klient` can be installed quickly by running the below. + +```bash +go install github.com/comradequinn/klient +``` + +Alternatively, you can install `klient` by downloading the appropriate tarball for your `os` from the [releases](https://github.com/comradequinn/klient/releases) page. Extract the binary and place it somewhere accessible to your `$PATH` variable. + +Optionally, `linux` users can have the below script quickly do this for them. + +```bash +export VERSION="v1.2.0"; export OS="linux-amd64"; wget "https://github.com/comradequinn/klient/releases/download/${VERSION}/klient-${VERSION}-${OS}.tar.gz" && tar -xf "klient-${VERSION}-${OS}.tar.gz" && rm -f "klient-${VERSION}-${OS}.tar.gz" && chmod +x klient && sudo mv klient /usr/local/bin/ +``` \ No newline at end of file diff --git a/Makefile b/Makefile index c1c7038..f10387f 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ build : @CGO_ENABLED=0;go build -o bin/klient stop-kafka: - -@docker rm -f kafka-broker 2> /dev/null + -@podman container rm -f kafka-broker -local-kafka: stop-kafka - @docker run -d -p 9092:9092 --rm --name kafka-broker apache/kafka:latest +start-kafka: stop-kafka + @podman container run -d -p 9092:9092 --rm --name kafka-broker docker.io/apache/kafka:latest BOOTSTRAPPERS="localhost:9092" info : build @@ -29,4 +29,4 @@ create-topic : build @bin/klient -c ${TOPIC} -p ${PARTITIONS} -n ${REPLICAS} -b ${BOOTSTRAPPERS} delete-topic : build - -@bin/klient -d ${TOPIC} -b ${BOOTSTRAPPERS} + -@bin/klient -d ${TOPIC} -b ${BOOTSTRAPPERS} \ No newline at end of file diff --git a/README.md b/README.md index ccffb68..970e158 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,14 @@ The `klient` utility exposes simplifed kafka cluster administration operations f ## Installation -If `go` is available on the machine, `klient` can be installed quickly by running the below. - -```bash -go install github.com/comradequinn/klient -``` - -Alternatively, there is a `amd64/linux` binary in this repo's `/bin` directory. Copy this repo to a location in your `$PATH` variable and grant it execute permissions. +Installation instructions are available [here](./INSTALL.md) ### Local Kafka Broker The `Makefile` contains a convenience target to spin up a local `kafka broker` which will be available at `localhost:9092`, as shown below. ```bash -make local-kafka +make start-kafka ``` The broker can be stopped by running the below @@ -95,7 +89,7 @@ Alternatively, specify these attributes as required: ```bash # specify a partition count of 3 and a replication factor of 1 -klient -c "my-topic" -p 3 -r 1 -b "localhost:9092" +klient -c "my-topic" -p 3 -n 1 -b "localhost:9092" ``` ## Topic Deletion diff --git a/bin/klient b/bin/klient index 59b4f16..5906f9c 100755 Binary files a/bin/klient and b/bin/klient differ diff --git a/cli/cli.go b/cli/cli.go index bb91131..ee4eb63 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -29,7 +29,7 @@ var ( writerFunc WriterFunc ) -// generate me a unit test for this +// generate me a unit test for this func Init(k KafkaReaderWriter, wf WriterFunc) { kafka = k writerFunc = wf diff --git a/go.mod b/go.mod index b847563..d973a27 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/comradequinn/klient -require github.com/segmentio/kafka-go v0.4.32 +require github.com/segmentio/kafka-go v0.4.48 require ( - github.com/klauspost/compress v1.15.9 // indirect - github.com/pierrec/lz4/v4 v4.1.15 // indirect + github.com/klauspost/compress v1.18.0 // indirect + github.com/pierrec/lz4/v4 v4.1.22 // indirect ) -go 1.23 +go 1.24 diff --git a/go.sum b/go.sum index 53e5d02..c027cea 100644 --- a/go.sum +++ b/go.sum @@ -5,31 +5,84 @@ github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi2 github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE= github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0= github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU= +github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/segmentio/kafka-go v0.4.32 h1:Ohr+9E+kDv/Ld2UPJN9hnKZRd2qgiqCmI8v2e1qlfLM= github.com/segmentio/kafka-go v0.4.32/go.mod h1:JAPPIiY3MQIwVHj64CWOP0LsFFfQ7H0w69kuoxnMIS0= +github.com/segmentio/kafka-go v0.4.48 h1:9jyu9CWK4W5W+SroCe8EffbrRZVqAOkuaLd/ApID4Vs= +github.com/segmentio/kafka-go v0.4.48/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= +github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v1.0.0 h1:d9X0esnoa3dFsV0FG35rAT0RIhYFlPq7MiP+DW89La0= github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 h1:rlLehGeYg6jfoyz/eDqDU1iRXLKfR42nnNh57ytKEWo= golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc= gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kafio/kafio.go b/kafio/kafio.go index 28f5736..add3201 100644 --- a/kafio/kafio.go +++ b/kafio/kafio.go @@ -17,7 +17,7 @@ import ( type ( Connection struct { - controller *kafka.Conn + driver *kafka.Conn bootstrappers []string } Message struct { @@ -53,7 +53,7 @@ func Connect(bootstrappers []string) (*Connection, error) { ) for _, bs := range bootstrappers { - if connection.controller, err = kafka.Dial("tcp", bs); err != nil { + if connection.driver, err = kafka.Dial("tcp", bs); err != nil { log.Printf("error connecting to bootstrapper [%v]: %v", bs, err) continue } @@ -64,11 +64,11 @@ func Connect(bootstrappers []string) (*Connection, error) { return nil, fmt.Errorf("unable to connect to any of the specified bootstrappers [%+v]: %v", bootstrappers, err) } - if controllerBroker, err = connection.controller.Controller(); err != nil { + if controllerBroker, err = connection.driver.Controller(); err != nil { return nil, fmt.Errorf("unable to ascertain the cluster controller: %v", err) } - if connection.controller, err = kafka.Dial("tcp", net.JoinHostPort(controllerBroker.Host, strconv.Itoa(controllerBroker.Port))); err != nil { + if connection.driver, err = kafka.Dial("tcp", net.JoinHostPort(controllerBroker.Host, strconv.Itoa(controllerBroker.Port))); err != nil { return nil, fmt.Errorf("unable to connect to the cluster controller: %v", err) } @@ -78,7 +78,7 @@ func Connect(bootstrappers []string) (*Connection, error) { // Topics returns a map keyed on the cluster's topics with // the value being the partition count func (conn *Connection) Topics() (map[string]int, error) { - partitions, err := conn.controller.ReadPartitions() + partitions, err := conn.driver.ReadPartitions() if err != nil { return nil, fmt.Errorf("error reading cluster info. %v", err) @@ -119,7 +119,7 @@ func (conn *Connection) NewTopic(name string, partitions, replicas int) error { return err } - err := conn.controller.CreateTopics(kafka.TopicConfig{ + err := conn.driver.CreateTopics(kafka.TopicConfig{ Topic: name, NumPartitions: partitions, ReplicationFactor: replicas}) @@ -144,7 +144,7 @@ func (conn *Connection) DeleteTopic(name string) error { return fmt.Errorf("cannot delete topic. topic does not exist") } - if err := conn.controller.DeleteTopics(name); err != nil { + if err := conn.driver.DeleteTopics(name); err != nil { return fmt.Errorf("unable to delete topic. %v", name) } diff --git a/klient.go b/klient.go index a58751e..8025333 100644 --- a/klient.go +++ b/klient.go @@ -10,11 +10,17 @@ import ( "github.com/comradequinn/klient/kafio" ) +var ( + commit = "dev-build" + tag = "v0.0.0-dev.0" +) + func main() { log.SetFlags(0) log.SetOutput(os.Stdout) var ( + versionArg = flag.Bool("version", false, "print klient version") bootstrappersArg = flag.String("b", "localhost:9092", "the comma separated list of kafka bootstrappers to use to connect") writeTopicArg = flag.String("w", "", "the topic to write to") writeTopicHeadersArg = flag.String("h", "", "the comma separated set of 'key=value' formatted headers to use when writing to a topic. eg \"k1=v1,k2=v2\"") @@ -33,6 +39,11 @@ func main() { flag.Parse() + if *versionArg { + log.Printf("klient version %v (commit %v)\n", tag, commit) + os.Exit(0) + } + bootstrappers := strings.Split(*bootstrappersArg, ",") if len(bootstrappers) == 0 { diff --git a/tests/bin/klient b/tests/bin/klient new file mode 100755 index 0000000..fcf84f9 Binary files /dev/null and b/tests/bin/klient differ diff --git a/tests/integration-tests.sh b/tests/integration-tests.sh new file mode 100755 index 0000000..d30f422 --- /dev/null +++ b/tests/integration-tests.sh @@ -0,0 +1,124 @@ +#! /bin/bash + +export TEST_BROKER_NAME=kafka-broker +export TEST_TOPIC="klient-test" +export TEST_TOPIC_REPLICAS="4" +export BOOTSTRAPPERS="localhost:9092" +export KLIENT=tests/bin/klient +export TEST_KEY="test-key" +export TEST_VALUE="test-data" +export TEST_HEADER_1="test-header1" +export TEST_HEADER_VALUE_1="test-value1" +export TEST_HEADER_2="test-header2" +export TEST_HEADER_VALUE_2="test-value2" + +function assert_contains() { + term="$1" + string="$2" + + if ! printf "%s" "$string" | grep -q "$term"; then + echo "- test assertion failed: expected '$string' to contain '$term'" + exit 1 + fi +} + +function assert_line_count() { + expected_lines="$1" + string="$2" + actual_lines="$(printf "%s\n" "$string" | wc -l)" + + if [[ "$actual_lines" != "$expected_lines" ]]; then + echo "- test assertion failed: expected '$string' to contain $expected_lines lines. got $actual_lines" + exit 1 + fi +} + +function assert_no_error() { + exit_code="$?" + output="$1" + + if [[ "$exit_code" != "0" ]]; then + echo "- test assertion failed: expected exit code of 0. got $exit_code. command output was '$output'" + exit 1 + fi +} + +function assert_exit_code() { + actual_exit_code="$?" + expected_exit_code="$1" + output="$2" + + if [[ "$actual_exit_code" != "$expected_exit_code" ]]; then + echo "- test assertion failed: expected exit code of $expected_exit_code. got $actual_exit_code. command output was '$output'" + exit 1 + fi +} + +printf "%s" "building klient... " +CGO_ENABLED=0;go build -o $KLIENT +printf "%s\n" "klient built to $KLIENT" + +echo "preparing kafka broker for integration tests named '$TEST_BROKER_NAME'..." +printf "%s" "- removing any orphaned brokers from previous tests..." +podman container rm -f $TEST_BROKER_NAME +printf "\n%s" "- creating kafka test broker... " +podman container run -d -p 9092:9092 --rm --name $TEST_BROKER_NAME docker.io/apache/kafka:latest +printf "%s\n\n" "- waiting to allow '$TEST_BROKER_NAME' to be ready..." +sleep 5s + +echo "test 1: list topics, expect none" +TEST_RESULT="$($KLIENT -i -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +assert_contains "NAME" "$TEST_RESULT" +assert_contains "PARTITIONS" "$TEST_RESULT" +assert_line_count "1" "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 2: create topic named '${TEST_TOPIC}', expect no error" +TEST_RESULT="$($KLIENT -c "${TEST_TOPIC}" -p 4 -n 1 -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 3: list topics, expect one named '${TEST_TOPIC}' with ${TEST_TOPIC_REPLICAS} replicas" +TEST_RESULT="$($KLIENT -i -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +assert_contains "NAME" "$TEST_RESULT" +assert_contains "PARTITIONS" "$TEST_RESULT" +assert_contains "${TEST_TOPIC}" "$TEST_RESULT" +assert_contains "${TEST_TOPIC_REPLICAS}" "$TEST_RESULT" +assert_line_count "2" "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 4: write message to '${TEST_TOPIC}' topic, expect no error" +TEST_RESULT="$($KLIENT -w "${TEST_TOPIC}" -k "${TEST_KEY}" -v "${TEST_VALUE}" -h "${TEST_HEADER_1}=${TEST_HEADER_VALUE_1},${TEST_HEADER_2}=${TEST_HEADER_VALUE_2}" -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +assert_contains "data written to topic" "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 5: read from '${TEST_TOPIC}' topic, expect message from test 4" +TEST_RESULT="$(timeout 10s $KLIENT -r "${TEST_TOPIC}" -a -b "${BOOTSTRAPPERS}")" +assert_exit_code "124" "$TEST_RESULT" # 124 = timeout +assert_contains "reading from topic" "$TEST_RESULT" +assert_contains "${TEST_HEADER_1}: ${TEST_HEADER_VALUE_1}" "$TEST_RESULT" +assert_contains "${TEST_HEADER_2}: ${TEST_HEADER_VALUE_2}" "$TEST_RESULT" +assert_contains "${TEST_VALUE}" "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 6: delete topic named '${TEST_TOPIC}', expect no error" +TEST_RESULT="$($KLIENT -d "${TEST_TOPIC}" -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +printf "%s\n\n" "- OK" + +echo "test 7: list topics, expect only system topic named '__consumer_offsets'" +TEST_RESULT="$($KLIENT -i -b "${BOOTSTRAPPERS}")" +assert_no_error "$TEST_RESULT" +assert_contains "NAME" "$TEST_RESULT" +assert_contains "PARTITIONS" "$TEST_RESULT" +assert_contains "__consumer_offsets" "$TEST_RESULT" +assert_line_count "2" "$TEST_RESULT" +printf "%s\n\n" "- OK" + +printf "%s" "removing test broker... " +podman container rm -f $TEST_BROKER_NAME + +printf "\n%s\n" "integration tests completed successfully" \ No newline at end of file