forked from k3s-io/kine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bootstrap testing and GitHub actions (k3s-io#14)
- Loading branch information
1 parent
d47190d
commit a2069ea
Showing
10 changed files
with
382 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !dqlite | ||
// +build !dqlite | ||
|
||
package dqlite | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !cgo | ||
// +build !cgo | ||
|
||
package sqlite | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dir-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |