mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-14 11:57:37 +00:00
Implemented first test
This commit is contained in:
parent
5a7597d999
commit
ad50694af9
5 changed files with 169 additions and 7 deletions
12
Makefile
12
Makefile
|
@ -37,6 +37,8 @@ TESTBIN := $(BINDIR)/$(TESTBINNAME)
|
||||||
RELEASE := $(GOBUILDDIR)/bin/release
|
RELEASE := $(GOBUILDDIR)/bin/release
|
||||||
GHRELEASE := $(GOBUILDDIR)/bin/github-release
|
GHRELEASE := $(GOBUILDDIR)/bin/github-release
|
||||||
|
|
||||||
|
TESTNAMESPACE := arangodb-operator-tests
|
||||||
|
|
||||||
SOURCES := $(shell find $(SRCDIR) -name '*.go' -not -path './test/*')
|
SOURCES := $(shell find $(SRCDIR) -name '*.go' -not -path './test/*')
|
||||||
|
|
||||||
.PHONY: all clean deps docker update-vendor update-generated verify-generated
|
.PHONY: all clean deps docker update-vendor update-generated verify-generated
|
||||||
|
@ -128,7 +130,7 @@ docker: $(BIN)
|
||||||
|
|
||||||
$(TESTBIN): $(GOBUILDDIR) $(SOURCES)
|
$(TESTBIN): $(GOBUILDDIR) $(SOURCES)
|
||||||
@mkdir -p $(BINDIR)
|
@mkdir -p $(BINDIR)
|
||||||
@docker run \
|
docker run \
|
||||||
--rm \
|
--rm \
|
||||||
-v $(SRCDIR):/usr/code \
|
-v $(SRCDIR):/usr/code \
|
||||||
-e GOPATH=/usr/code/.gobuild \
|
-e GOPATH=/usr/code/.gobuild \
|
||||||
|
@ -143,7 +145,11 @@ docker-test: $(TESTBIN)
|
||||||
docker build --quiet -f $(DOCKERTESTFILE) -t arangodb/arangodb-operator-test .
|
docker build --quiet -f $(DOCKERTESTFILE) -t arangodb/arangodb-operator-test .
|
||||||
|
|
||||||
run-tests: docker-test
|
run-tests: docker-test
|
||||||
kubectl run arangodb-operator-test -i --rm --quiet --restart=Never --image=arangodb/arangodb-operator-test
|
$(ROOTDIR)/scripts/kube_delete_namespace.sh $(TESTNAMESPACE)
|
||||||
|
kubectl create namespace $(TESTNAMESPACE)
|
||||||
|
kubectl --namespace=$(TESTNAMESPACE) create -f examples/deployment.yaml
|
||||||
|
kubectl --namespace $(TESTNAMESPACE) run arangodb-operator-test -i --rm --quiet --restart=Never --image=arangodb/arangodb-operator-test --env="TEST_NAMESPACE=$(TESTNAMESPACE)" -- -test.v
|
||||||
|
kubectl delete namespace $(TESTNAMESPACE) --ignore-not-found --now
|
||||||
|
|
||||||
# Release building
|
# Release building
|
||||||
|
|
||||||
|
@ -184,7 +190,7 @@ minikube-start:
|
||||||
minikube start --cpus=4 --memory=6144
|
minikube start --cpus=4 --memory=6144
|
||||||
|
|
||||||
delete-operator:
|
delete-operator:
|
||||||
kubectl delete -f examples/deployment.yaml || true
|
kubectl delete -f examples/deployment.yaml --ignore-not-found
|
||||||
|
|
||||||
redeploy-operator: delete-operator
|
redeploy-operator: delete-operator
|
||||||
kubectl create -f examples/deployment.yaml
|
kubectl create -f examples/deployment.yaml
|
||||||
|
|
12
scripts/kube_delete_namespace.sh
Executable file
12
scripts/kube_delete_namespace.sh
Executable file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Delete a namespace and wait until it is gone
|
||||||
|
|
||||||
|
NS=$1
|
||||||
|
|
||||||
|
kubectl delete namespace $NS --now --ignore-not-found
|
||||||
|
response=$(kubectl get namespace $NS --template="non-empty" --ignore-not-found)
|
||||||
|
while [ ! -z $response ]; do
|
||||||
|
sleep 1
|
||||||
|
response=$(kubectl get namespace $NS --template="non-empty" --ignore-not-found)
|
||||||
|
done
|
34
tests/predicates.go
Normal file
34
tests/predicates.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
//
|
||||||
|
// DISCLAIMER
|
||||||
|
//
|
||||||
|
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||||
|
//
|
||||||
|
// Author Ewout Prangsma
|
||||||
|
//
|
||||||
|
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
|
||||||
|
)
|
||||||
|
|
||||||
|
// deploymentHasState creates a predicate that returns true when the deployment has the given state.
|
||||||
|
func deploymentHasState(state api.DeploymentState) func(*api.ArangoDeployment) bool {
|
||||||
|
return func(obj *api.ArangoDeployment) bool {
|
||||||
|
return obj.Status.State == state
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,11 +2,30 @@ package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
"github.com/dchest/uniuri"
|
||||||
|
|
||||||
|
api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
|
||||||
|
"github.com/arangodb/k8s-operator/pkg/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestSimpleSingle tests the creating of a single server deployment
|
||||||
|
// with default settings.
|
||||||
func TestSimpleSingle(t *testing.T) {
|
func TestSimpleSingle(t *testing.T) {
|
||||||
time.Sleep(time.Second * 5)
|
c := client.MustNewInCluster()
|
||||||
t.Log("TODO")
|
ns := getNamespace(t)
|
||||||
t.Error("foo")
|
|
||||||
|
// Prepare deployment config
|
||||||
|
depl := newDeployment("test-single-" + uniuri.NewLen(4))
|
||||||
|
|
||||||
|
// Create deployment
|
||||||
|
_, err := c.DatabaseV1alpha().ArangoDeployments(ns).Create(depl)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Create deployment failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for deployment to be ready
|
||||||
|
if _, err := waitUntilDeployment(c, depl.GetName(), ns, deploymentHasState(api.DeploymentStateRunning)); err != nil {
|
||||||
|
t.Errorf("Deployment not running in time: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
91
tests/test_util.go
Normal file
91
tests/test_util.go
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
//
|
||||||
|
// DISCLAIMER
|
||||||
|
//
|
||||||
|
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||||
|
//
|
||||||
|
// Author Ewout Prangsma
|
||||||
|
//
|
||||||
|
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
|
api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
|
||||||
|
"github.com/arangodb/k8s-operator/pkg/generated/clientset/versioned"
|
||||||
|
"github.com/arangodb/k8s-operator/pkg/util/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
deploymentReadyTimeout = time.Minute * 2
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
maskAny = errors.WithStack
|
||||||
|
)
|
||||||
|
|
||||||
|
// getNamespace returns the kubernetes namespace in which to run tests.
|
||||||
|
func getNamespace(t *testing.T) string {
|
||||||
|
ns := os.Getenv("TEST_NAMESPACE")
|
||||||
|
if ns == "" {
|
||||||
|
t.Fatal("Missing environment variable TEST_NAMESPACE")
|
||||||
|
}
|
||||||
|
return ns
|
||||||
|
}
|
||||||
|
|
||||||
|
// newDeployment creates a basic ArangoDeployment with configured
|
||||||
|
// type & name.
|
||||||
|
func newDeployment(name string) *api.ArangoDeployment {
|
||||||
|
return &api.ArangoDeployment{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: api.SchemeGroupVersion.String(),
|
||||||
|
Kind: api.ArangoDeploymentResourceKind,
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: strings.ToLower(name),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// waitUntilDeployment waits until a deployment with given name in given namespace
|
||||||
|
// reached a state where the given predicate returns true.
|
||||||
|
func waitUntilDeployment(cli versioned.Interface, deploymentName, ns string, predicate func(*api.ArangoDeployment) bool) (*api.ArangoDeployment, error) {
|
||||||
|
var result *api.ArangoDeployment
|
||||||
|
op := func() error {
|
||||||
|
obj, err := cli.DatabaseV1alpha().ArangoDeployments(ns).Get(deploymentName, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
result = nil
|
||||||
|
return maskAny(err)
|
||||||
|
}
|
||||||
|
result = obj
|
||||||
|
if predicate(obj) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Predicate returns false")
|
||||||
|
}
|
||||||
|
if err := retry.Retry(op, deploymentReadyTimeout); err != nil {
|
||||||
|
return nil, maskAny(err)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
Loading…
Reference in a new issue