Skip to content

Commit b5a732a

Browse files
authored
Merge pull request #177 from arangodb/test/duration
Added duration test app
2 parents 91e4475 + c7e0eb3 commit b5a732a

21 files changed

+2070
-0
lines changed

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PULSAR := $(GOBUILDDIR)/bin/pulsar$(shell go env GOEXE)
2727

2828
DOCKERFILE := Dockerfile
2929
DOCKERTESTFILE := Dockerfile.test
30+
DOCKERDURATIONTESTFILE := tests/duration/Dockerfile
3031

3132
ifndef LOCALONLY
3233
PUSHIMAGES := 1
@@ -63,6 +64,9 @@ endif
6364
ifndef TESTIMAGE
6465
TESTIMAGE := $(DOCKERNAMESPACE)/kube-arangodb-test$(IMAGESUFFIX)
6566
endif
67+
ifndef DURATIONTESTIMAGE
68+
DURATIONTESTIMAGE := $(DOCKERNAMESPACE)/kube-arangodb-durationtest$(IMAGESUFFIX)
69+
endif
6670
ifndef ENTERPRISEIMAGE
6771
ENTERPRISEIMAGE := $(DEFAULTENTERPRISEIMAGE)
6872
endif
@@ -75,6 +79,8 @@ BINNAME := $(PROJECT)
7579
BIN := $(BINDIR)/$(BINNAME)
7680
TESTBINNAME := $(PROJECT)_test
7781
TESTBIN := $(BINDIR)/$(TESTBINNAME)
82+
DURATIONTESTBINNAME := $(PROJECT)_duration_test
83+
DURATIONTESTBIN := $(BINDIR)/$(DURATIONTESTBINNAME)
7884
RELEASE := $(GOBUILDDIR)/bin/release
7985
GHRELEASE := $(GOBUILDDIR)/bin/github-release
8086

@@ -278,6 +284,28 @@ endif
278284
$(ROOTDIR)/scripts/kube_create_storage.sh $(DEPLOYMENTNAMESPACE)
279285
$(ROOTDIR)/scripts/kube_run_tests.sh $(DEPLOYMENTNAMESPACE) $(TESTIMAGE) "$(ENTERPRISEIMAGE)" $(TESTTIMEOUT) $(TESTLENGTHOPTIONS)
280286

287+
$(DURATIONTESTBIN): $(GOBUILDDIR) $(SOURCES)
288+
@mkdir -p $(BINDIR)
289+
docker run \
290+
--rm \
291+
-v $(SRCDIR):/usr/code \
292+
-v $(CACHEVOL):/usr/gocache \
293+
-e GOCACHE=/usr/gocache \
294+
-e GOPATH=/usr/code/.gobuild \
295+
-e GOOS=linux \
296+
-e GOARCH=amd64 \
297+
-e CGO_ENABLED=0 \
298+
-w /usr/code/ \
299+
golang:$(GOVERSION) \
300+
go build -installsuffix cgo -ldflags "-X main.projectVersion=$(VERSION) -X main.projectBuild=$(COMMIT)" -o /usr/code/bin/$(DURATIONTESTBINNAME) $(REPOPATH)/tests/duration
301+
302+
.PHONY: docker-duration-test
303+
docker-duration-test: $(DURATIONTESTBIN)
304+
docker build --quiet -f $(DOCKERDURATIONTESTFILE) -t $(DURATIONTESTIMAGE) .
305+
ifdef PUSHIMAGES
306+
docker push $(DURATIONTESTIMAGE)
307+
endif
308+
281309
.PHONY: cleanup-tests
282310
cleanup-tests:
283311
ifneq ($(DEPLOYMENTNAMESPACE), default)

tests/duration/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM scratch
2+
3+
ADD bin/arangodb_operator_duration_test /usr/bin/
4+
5+
ENTRYPOINT [ "/usr/bin/arangodb_operator_duration_test" ]

tests/duration/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kube-ArangoDB duration test
2+
3+
This test is a simple application that keeps accessing the database with various requests.
4+
5+
## Building
6+
7+
In root of kube-arangodb repository, run:
8+
9+
```bash
10+
make docker-duration-test
11+
```
12+
13+
## Running
14+
15+
Start an ArangoDB `Cluster` deployment.
16+
17+
Run:
18+
19+
```bash
20+
kubectl run \
21+
--image=${DOCKERNAMESPACE}/kube-arangodb-durationtest:dev \
22+
--image-pull-policy=Always duration-test \
23+
-- \
24+
--cluster=https://<deployment-name>.<namespace>.svc:8529 \
25+
--username=root
26+
```
27+
28+
To remove the test, run:
29+
30+
```bash
31+
kubectl delete -n <namespace> deployment/duration-test
32+
```

tests/duration/main.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package main
24+
25+
import (
26+
"context"
27+
"crypto/tls"
28+
"flag"
29+
"fmt"
30+
"log"
31+
"os"
32+
"os/signal"
33+
"strings"
34+
"syscall"
35+
"time"
36+
37+
driver "github.com/arangodb/go-driver"
38+
"github.com/arangodb/go-driver/http"
39+
"github.com/pkg/errors"
40+
41+
"github.com/arangodb/kube-arangodb/pkg/util/retry"
42+
)
43+
44+
const (
45+
defaultTestDuration = time.Hour * 24 * 7 // 7 days
46+
)
47+
48+
var (
49+
maskAny = errors.WithStack
50+
userName string
51+
password string
52+
clusterEndpoints string
53+
testDuration time.Duration
54+
)
55+
56+
func init() {
57+
flag.StringVar(&userName, "username", "", "Authenticating username")
58+
flag.StringVar(&password, "password", "", "Authenticating password")
59+
flag.StringVar(&clusterEndpoints, "cluster", "", "Endpoints for database cluster")
60+
flag.DurationVar(&testDuration, "duration", defaultTestDuration, "Duration of the test")
61+
}
62+
63+
func main() {
64+
flag.Parse()
65+
66+
// Create clients & wait for cluster available
67+
client, err := createClusterClient(clusterEndpoints, userName, password)
68+
if err != nil {
69+
log.Fatalf("Failed to create cluster client: %#v\n", err)
70+
}
71+
if err := waitUntilClusterUp(client); err != nil {
72+
log.Fatalf("Failed to reach cluster: %#v\n", err)
73+
}
74+
75+
// Start running tests
76+
ctx, cancel := context.WithCancel(context.Background())
77+
sigChannel := make(chan os.Signal)
78+
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
79+
go handleSignal(sigChannel, cancel)
80+
runTestLoop(ctx, client, testDuration)
81+
}
82+
83+
// createClusterClient creates a configuration, connection and client for
84+
// one of the two ArangoDB clusters in the test. It uses the go-driver.
85+
// It needs a list of endpoints.
86+
func createClusterClient(endpoints string, user string, password string) (driver.Client, error) {
87+
// This will always use HTTP, and user and password authentication
88+
config := http.ConnectionConfig{
89+
Endpoints: strings.Split(endpoints, ","),
90+
TLSConfig: &tls.Config{InsecureSkipVerify: true},
91+
}
92+
connection, err := http.NewConnection(config)
93+
if err != nil {
94+
return nil, maskAny(err)
95+
}
96+
clientCfg := driver.ClientConfig{
97+
Connection: connection,
98+
Authentication: driver.BasicAuthentication(user, password),
99+
}
100+
client, err := driver.NewClient(clientCfg)
101+
if err != nil {
102+
return nil, maskAny(err)
103+
}
104+
return client, nil
105+
}
106+
107+
func waitUntilClusterUp(c driver.Client) error {
108+
op := func() error {
109+
ctx := context.Background()
110+
if _, err := c.Version(ctx); err != nil {
111+
return maskAny(err)
112+
}
113+
return nil
114+
}
115+
if err := retry.Retry(op, time.Minute); err != nil {
116+
return maskAny(err)
117+
}
118+
return nil
119+
}
120+
121+
// handleSignal listens for termination signals and stops this process on termination.
122+
func handleSignal(sigChannel chan os.Signal, cancel context.CancelFunc) {
123+
signalCount := 0
124+
for s := range sigChannel {
125+
signalCount++
126+
fmt.Println("Received signal:", s)
127+
if signalCount > 1 {
128+
os.Exit(1)
129+
}
130+
cancel()
131+
}
132+
}

tests/duration/simple/check.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package simple
24+
25+
import "context"
26+
27+
// isDocumentEqualTo reads an existing document and checks that it is equal to the given document.
28+
// Returns: (isEqual,currentRevision,error)
29+
func (t *simpleTest) isDocumentEqualTo(c *collection, key string, expected UserDocument) (bool, string, error) {
30+
ctx := context.Background()
31+
var result UserDocument
32+
t.log.Info().Msgf("Checking existing document '%s' from '%s'...", key, c.name)
33+
col, err := t.db.Collection(ctx, c.name)
34+
if err != nil {
35+
return false, "", maskAny(err)
36+
}
37+
m, err := col.ReadDocument(ctx, key, &result)
38+
if err != nil {
39+
// This is a failure
40+
t.log.Error().Msgf("Failed to read document '%s' from '%s': %v", key, c.name, err)
41+
return false, "", maskAny(err)
42+
}
43+
// Compare document against expected document
44+
if result.Equals(expected) {
45+
// Found an exact match
46+
return true, m.Rev, nil
47+
}
48+
t.log.Info().Msgf("Document '%s' in '%s' returned different values: got %q expected %q", key, c.name, result, expected)
49+
return false, m.Rev, nil
50+
}

tests/duration/simple/error.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package simple
24+
25+
import (
26+
"github.com/pkg/errors"
27+
)
28+
29+
var (
30+
maskAny = errors.WithStack
31+
)

0 commit comments

Comments
 (0)