Skip to content

Commit

Permalink
Bootstrap testing and GitHub actions (k3s-io#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos authored Mar 13, 2023
1 parent d47190d commit a2069ea
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 17 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Go

on:
pull_request:

jobs:
code-quality:
name: Code Quality
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/[email protected]

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"

- name: go mod download
run: go mod download

- name: go fmt
run: make go.fmt

- name: check diff
run: |
if ! git diff; then
echo "Detected changes that have not been committed to the repository"
exit 1
fi
- name: go vet
run: make go.vet

tests:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Check out Code
uses: actions/[email protected]

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"

- name: go mod download
run: go mod download

- name: Run tests
run: make go.test

benchmarks:
name: Benchmarks
runs-on: ubuntu-latest

steps:
- name: Check out Code
uses: actions/[email protected]

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"

- name: go mod download
run: go mod download

- name: Run benchmarks
run: make go.bench
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
go.fmt:
go mod tidy
go fmt ./...

go.vet:
go vet ./...

go.test:
go test -v ./test

go.bench:
go test -v ./test -run "^$$" -bench "Benchmark" -benchmem
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.2
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/peterh/liner v1.2.0 // indirect
github.com/onsi/gomega v1.27.3
github.com/pkg/errors v0.9.1
github.com/rancher/wrangler v0.8.3
github.com/sirupsen/logrus v1.7.0
Expand Down
127 changes: 111 additions & 16 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/drivers/dqlite/no_dqlite.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !dqlite
// +build !dqlite

package dqlite
Expand Down
1 change: 1 addition & 0 deletions pkg/drivers/sqlite/sqlite_nocgo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !cgo
// +build !cgo

package sqlite
Expand Down
57 changes: 57 additions & 0 deletions test/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package test

import (
"context"
"fmt"
"testing"

. "github.com/onsi/gomega"
clientv3 "go.etcd.io/etcd/client/v3"
)

// TestCreate is unit testing for the create operation.
func TestCreate(t *testing.T) {
ctx := context.Background()
client := newKine(t)

t.Run("CreateOne", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision("testKey"), "=", 0)).
Then(clientv3.OpPut("testKey", "testValue")).
Commit()

g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeTrue())
})

t.Run("CreateExistingFails", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision("testKey"), "=", 0)).
Then(clientv3.OpPut("testKey", "testValue2")).
Commit()

g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeFalse())
})
}

// BenchmarkCreate is a benchmark for the Create operation.
func BenchmarkCreate(b *testing.B) {
ctx := context.Background()
client := newKine(b)

g := NewWithT(b)
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key-%d", i)
value := fmt.Sprintf("value-%d", i)
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", 0)).
Then(clientv3.OpPut(key, value)).
Commit()

g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeTrue())
}
}
72 changes: 72 additions & 0 deletions test/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package test

import (
"context"
"testing"

. "github.com/onsi/gomega"
clientv3 "go.etcd.io/etcd/client/v3"
)

// TestGet is unit testing for the Get operation.
func TestGet(t *testing.T) {
ctx := context.Background()
client := newKine(t)

t.Run("FailMissing", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Get(ctx, "testKey", clientv3.WithRange(""))

g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(BeEmpty())
})

t.Run("Success", func(t *testing.T) {
g := NewWithT(t)

// Create a key
{
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision("testKey"), "=", 0)).
Then(clientv3.OpPut("testKey", "testValue")).
Commit()
g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeTrue())
}

// Get key
{
resp, err := client.Get(ctx, "testKey", clientv3.WithRange(""))
g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(HaveLen(1))
g.Expect(resp.Kvs[0].Key).To(Equal([]byte("testKey")))
g.Expect(resp.Kvs[0].Value).To(Equal([]byte("testValue")))
}
})
}

// BenchmarkGet is a benchmark for the Get operation.
func BenchmarkGet(b *testing.B) {
ctx := context.Background()
client := newKine(b)
g := NewWithT(b)

// create a kv
{
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision("testKey"), "=", 0)).
Then(clientv3.OpPut("testKey", "testValue")).
Commit()
g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeTrue())
}

b.Run("LatestRevision", func(b *testing.B) {
g := NewWithT(b)
for i := 0; i < b.N; i++ {
resp, err := client.Get(ctx, "testKey", clientv3.WithRange(""))
g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(HaveLen(1))
}
})
}
1 change: 1 addition & 0 deletions test/testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir-*
54 changes: 54 additions & 0 deletions test/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/rancher/kine/pkg/endpoint"
"github.com/sirupsen/logrus"
clientv3 "go.etcd.io/etcd/client/v3"
)

// newKine spins up a new instance of kine. it also registers cleanup functions for temporary data
//
// newKine is currently hardcoded to using sqlite and a unix socket listener, but might be extended in the future
//
// newKine will panic in case of error
//
// newKine will return a context as well as a configured etcd client for the kine instance
func newKine(tb testing.TB) *clientv3.Client {
logrus.SetLevel(logrus.ErrorLevel)

dir, err := os.MkdirTemp("testdata", "dir-*")
if err != nil {
panic(err)
}
tb.Cleanup(func() {
os.RemoveAll(dir)
})
listener := fmt.Sprintf("unix://%s/listen.sock", dir)
ep := fmt.Sprintf("sqlite://%s/data.db", dir)
config, err := endpoint.Listen(context.Background(), endpoint.Config{
Listener: listener,
Endpoint: ep,
})
if err != nil {
panic(err)
}
tlsConfig, err := config.TLSConfig.ClientConfig()
if err != nil {
panic(err)
}
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{listener},
DialTimeout: 5 * time.Second,
TLS: tlsConfig,
})
if err != nil {
panic(err)
}
return client
}

0 comments on commit a2069ea

Please sign in to comment.