Skip to content

Commit

Permalink
Merge pull request #7 from calyptia/ci
Browse files Browse the repository at this point in the history
github/workflows: go
  • Loading branch information
niedbalski authored May 3, 2022
2 parents 1e5fab5 + d53cad0 commit 8493e34
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Go

on:
push:
branches: [master, main]
tags: [v*]
pull_request:
workflow_dispatch:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

# Required by golangci-lint-action@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.45.2

test:
runs-on: ubuntu-latest
steps:
# Required by the tests
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ secrets.CI_USERNAME }}
password: ${{ secrets.CI_PAT }}

- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Test
run: go test -v -race ./...
32 changes: 24 additions & 8 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugin

import (
"bytes"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -32,15 +33,22 @@ func testPlugin(t *testing.T, pool *dockertest.Pool) {
t.Cleanup(func() { _ = f.Close() })
t.Cleanup(func() { _ = f.Truncate(0) })

err = pool.Client.BuildImage(dc.BuildImageOptions{
buildOpts := dc.BuildImageOptions{
Name: "fluent-bit-go.localhost",
ContextDir: filepath.Join(pwd, ".."),
Dockerfile: filepath.Join(pwd, "./testdata/Dockerfile"),
ContextDir: ".",
Dockerfile: "./testdata/Dockerfile",
Platform: "linux/amd64",
OutputStream: io.Discard,
ErrorStream: io.Discard,
Pull: true,
AuthConfigs: *auths,
})
}

if testing.Verbose() {
buildOpts.ErrorStream = os.Stderr
}

err = pool.Client.BuildImage(buildOpts)
wantNoErr(t, err)

fbit, err := pool.Client.CreateContainer(dc.CreateContainerOptions{
Expand Down Expand Up @@ -89,10 +97,12 @@ func testPlugin(t *testing.T, pool *dockertest.Pool) {
contents, err := io.ReadAll(f)
wantNoErr(t, err)

contents = bytes.TrimSpace(contents)
lines := strings.Split(string(contents), "\n")

// after 5 seconds of fluentbit running, there should be at least one record.
if d := len(lines); d < 1 {
// after 5 seconds of fluentbit running, there should be at least 1 record
// and at most 10 record due to the 5s of timeout to shutdown.
if d := len(lines); d < 1 || d > 10 {
t.Fatalf("expected at least 1 lines, got %d", d)
}

Expand All @@ -111,8 +121,14 @@ func testPlugin(t *testing.T, pool *dockertest.Pool) {
// fmt.Fprintf(f, "message=\"got record\" tag=%s time=%s record=%+v\n", msg.Tag(), msg.Time.Format(time.RFC3339), msg.Record)
re := regexp.MustCompile(`^message="got record" tag=test-input time=[^\s]+ record=map\[foo:bar message:hello from go-test-input-plugin]$`)

// fluentbit runs for 5 seconds, so at most we could get 5 records if they are collected every one second.
for _, line := range lines[0:5] {
// fluentbit runs for 5s and with a timeout to shutdown of 5s,
// so at most we could get 10 records if they are collected every one second.
for i := 0; i < 10; i++ {
if len(lines) == i {
break
}

line := lines[i]
if line == "" {
break
}
Expand Down

0 comments on commit 8493e34

Please sign in to comment.