Skip to content

Commit cd350a4

Browse files
authored
make run.sh executable from within e2e (#619)
1 parent 1d45a6a commit cd350a4

20 files changed

+169
-61
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ before_install:
1515
- go get github.com/mattn/goveralls
1616

1717
install:
18-
- make deps e2e-tools e2e-build
18+
- make deps
1919

2020
script:
2121
- hack/verify-codegen.sh
2222
- travis_wait 20 goveralls -service=travis-ci -package ./pkg/... -v
23-
- make e2e-run
23+
- make e2e

Makefile

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: clean local test linux macos docker push scm-source.json e2e-run e2e-tools e2e-build
1+
.PHONY: clean local test linux macos docker push scm-source.json e2e
22

33
BINARY ?= postgres-operator
44
BUILD_FLAGS ?= -v
@@ -34,7 +34,10 @@ ifdef CDP_PULL_REQUEST_NUMBER
3434
CDP_TAG := -${CDP_BUILD_VERSION}
3535
endif
3636

37-
KIND_PATH := $(GOPATH)/bin
37+
ifndef GOPATH
38+
GOPATH := $(HOME)/go
39+
endif
40+
3841
PATH := $(GOPATH)/bin:$(PATH)
3942
SHELL := env PATH=$(PATH) $(SHELL)
4043

@@ -92,15 +95,5 @@ test:
9295
hack/verify-codegen.sh
9396
@go test ./...
9497

95-
e2e-build:
96-
docker build --tag="postgres-operator-e2e-tests" -f e2e/Dockerfile .
97-
98-
e2e-tools:
99-
# install pinned version of 'kind'
100-
# leave the name as is to avoid overwriting official binary named `kind`
101-
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.3.0/kind-linux-amd64
102-
chmod +x kind-linux-amd64
103-
mv kind-linux-amd64 $(KIND_PATH)
104-
105-
e2e-run: docker
106-
e2e/run.sh
98+
e2e:
99+
cd e2e; make tools test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ There is a browser-friendly version of this documentation at
6464
* [The Postgres experience on K8s](docs/user.md)
6565
* [The Postgres Operator UI](docs/operator-ui.md)
6666
* [DBA options - from RBAC to backup](docs/administrator.md)
67-
* [Debug and extend the operator](docs/developer.md)
67+
* [Build, debug and extend the operator](docs/developer.md)
6868
* [Configuration options](docs/reference/operator_parameters.md)
6969
* [Postgres manifest reference](docs/reference/cluster_manifest.md)
7070
* [Command-line options and environment variables](docs/reference/command_line_and_environment.md)

delivery.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pipeline:
4444
- desc: 'Run e2e tests'
4545
cmd: |
4646
cd $OPERATOR_TOP_DIR/postgres-operator
47-
make e2e-tools e2e-build e2e-run
47+
make e2e
4848
- desc: 'Push docker image'
4949
cmd: |
5050
export PATH=$PATH:$HOME/go/bin

docs/administrator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ on `configmaps` resources). This is also done intentionally to avoid breaking
9898
things if someone decides to configure the same service account in the
9999
operator's ConfigMap to run Postgres clusters.
100100

101-
### Give K8S users access to create/list `postgresqls`
101+
### Give K8s users access to create/list `postgresqls`
102102

103103
By default `postgresql` custom resources can only be listed and changed by
104104
cluster admins. To allow read and/or write access to other human users apply
@@ -363,7 +363,7 @@ used internally in K8s.
363363

364364
## Logical backups
365365

366-
The operator can manage k8s cron jobs to run logical backups of Postgres
366+
The operator can manage K8s cron jobs to run logical backups of Postgres
367367
clusters. The cron job periodically spawns a batch job that runs a single pod.
368368
The backup script within this pod's container can connect to a DB for a logical
369369
backup. The operator updates cron jobs during Sync if the job schedule changes;

docs/developer.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ kubectl get pod -l name=postgres-operator
9696
The operator employs K8s-provided code generation to obtain deep copy methods
9797
and K8s-like APIs for its custom resource definitions, namely the
9898
Postgres CRD and the operator CRD. The usage of the code generation follows
99-
conventions from the k8s community. Relevant scripts live in the `hack`
99+
conventions from the K8s community. Relevant scripts live in the `hack`
100100
directory:
101101
* `update-codegen.sh` triggers code generation for the APIs defined in `pkg/apis/acid.zalan.do/`,
102102
* `verify-codegen.sh` checks if the generated code is up-to-date (to be used within CI).
@@ -247,23 +247,20 @@ kubectl logs acid-minimal-cluster-0
247247

248248
## End-to-end tests
249249

250-
The operator provides reference e2e (end-to-end) tests to ensure various infra
251-
parts work smoothly together. Each e2e execution tests a Postgres Operator image
252-
built from the current git branch. The test runner starts a [kind](https://kind.sigs.k8s.io/)
253-
(local k8s) cluster and Docker container with tests. The k8s API client from
254-
within the container connects to the `kind` cluster using the standard Docker
255-
`bridge` network. The tests utilize examples from `/manifests` (ConfigMap is
256-
used for the operator configuration) to avoid maintaining yet another set of
257-
configuration files. The kind cluster is deleted if tests complete successfully.
250+
The operator provides reference end-to-end tests (e2e) (as Docker image) to
251+
ensure various infrastructure parts work smoothly together. Each e2e execution
252+
tests a Postgres Operator image built from the current git branch. The test
253+
runner creates a new local K8s cluster using [kind](https://kind.sigs.k8s.io/),
254+
utilizes provided manifest examples, and runs e2e tests contained in the `tests`
255+
folder. The K8s API client in the container connects to the `kind` cluster via
256+
the standard Docker `bridge` network. The kind cluster is deleted if tests
257+
finish successfully or on each new run in case it still exists.
258258

259-
End-to-end tests are executed automatically during builds:
259+
End-to-end tests are executed automatically during builds (for more details,
260+
see the [README](../e2e/README.md) in the `e2e` folder):
260261

261262
```bash
262-
# invoke them from the project's top directory
263-
make e2e-run
264-
265-
# install kind and build test image before first run
266-
make e2e-tools e2e-build
263+
make e2e
267264
```
268265

269266
End-to-end tests are written in Python and use `flake8` for code quality.

docs/diagrams/pod.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
node[k8s-label] (app-label) {App}
3939
node[k8s-label, right=.25cm of app-label] (role-label) {Role}
4040
node[k8s-label, right=.25cm of role-label] (custom-label) {Custom}
41-
node[label, below of=role-label] (k8s-label-label) {K8S Labels}
41+
node[label, below of=role-label] (k8s-label-label) {K8s Labels}
4242
node[border, behind path,
4343
fit=(app-label)(role-label)(custom-label)(k8s-label-label)
4444
] (k8s-labels) {}; \& \&

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ used to complement it.
4646
Here is a diagram, that summarizes what would be created by the operator, when a
4747
new Postgres cluster CRD is submitted:
4848

49-
![postgresql-operator](diagrams/operator.png "K8S resources, created by operator")
49+
![postgresql-operator](diagrams/operator.png "K8s resources, created by operator")
5050

5151
This picture is not complete without an overview of what is inside a single
5252
cluster pod, so let's zoom in:

docs/reference/cluster_manifest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ These parameters are grouped directly under the `spec` key in the manifest.
135135
to S3. Default: false. Optional.
136136

137137
* **logicalBackupSchedule**
138-
Schedule for the logical backup k8s cron job. Please take
138+
Schedule for the logical backup K8s cron job. Please take
139139
[the reference schedule format](https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#schedule)
140140
into account. Optional. Default is: "30 00 \* \* \*"
141141

docs/reference/operator_parameters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ configuration they are grouped under the `kubernetes` key.
158158

159159
* **pod_service_account_role_binding_definition**
160160
This definition must bind pod service account to a role with permission
161-
sufficient for the pods to start and for Patroni to access k8s endpoints;
162-
service account on its own lacks any such rights starting with k8s v1.8. If
161+
sufficient for the pods to start and for Patroni to access K8s endpoints;
162+
service account on its own lacks any such rights starting with K8s v1.8. If
163163
not explicitly defined by the user, a simple definition that binds the
164164
account to the operator's own 'zalando-postgres-operator' cluster role will
165165
be used. The default is empty.
@@ -416,7 +416,7 @@ yet officially supported.
416416

417417
## Logical backup
418418

419-
These parameters configure a k8s cron job managed by the operator to produce
419+
These parameters configure a K8s cron job managed by the operator to produce
420420
Postgres logical backups. In the CRD-based configuration those parameters are
421421
grouped under the `logical_backup` key.
422422

e2e/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
FROM ubuntu:18.04
22
LABEL maintainer="Team ACID @ Zalando <[email protected]>"
33

4-
WORKDIR /e2e
5-
64
COPY manifests ./manifests
7-
COPY e2e/requirements.txt e2e/tests ./
5+
COPY requirements.txt tests ./
86

97
RUN apt-get update \
10-
&& apt-get install --no-install-recommends -y \
8+
&& apt-get install --no-install-recommends -y \
119
python3 \
1210
python3-setuptools \
1311
python3-pip \
@@ -19,4 +17,7 @@ RUN apt-get update \
1917
&& apt-get clean \
2018
&& rm -rf /var/lib/apt/lists/*
2119

22-
CMD ["python3", "-m", "unittest", "discover", "--start-directory", ".", "-v"]
20+
ARG VERSION=dev
21+
RUN sed -i "s/__version__ = .*/__version__ = '${VERSION}'/" ./__init__.py
22+
23+
CMD ["python3", "-m", "unittest", "discover", "--start-directory", ".", "-v"]

e2e/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.PHONY: clean copy docker push tools test
2+
3+
BINARY ?= postgres-operator-e2e-tests
4+
BUILD_FLAGS ?= -v
5+
CGO_ENABLED ?= 0
6+
ifeq ($(RACE),1)
7+
BUILD_FLAGS += -race -a
8+
CGO_ENABLED=1
9+
endif
10+
11+
LOCAL_BUILD_FLAGS ?= $(BUILD_FLAGS)
12+
LDFLAGS ?= -X=main.version=$(VERSION)
13+
14+
IMAGE ?= registry.opensource.zalan.do/acid/$(BINARY)
15+
VERSION ?= $(shell git describe --tags --always --dirty)
16+
TAG ?= $(VERSION)
17+
GITHEAD = $(shell git rev-parse --short HEAD)
18+
GITURL = $(shell git config --get remote.origin.url)
19+
GITSTATU = $(shell git status --porcelain || echo 'no changes')
20+
TTYFLAGS = $(shell test -t 0 && echo '-it')
21+
22+
ifndef GOPATH
23+
GOPATH := $(HOME)/go
24+
endif
25+
26+
KIND_PATH := $(GOPATH)/bin
27+
PATH := $(GOPATH)/bin:$(PATH)
28+
29+
default: tools
30+
31+
clean:
32+
rm -fr manifests
33+
34+
copy: clean
35+
mkdir manifests
36+
cp ../manifests -r .
37+
38+
docker: copy
39+
docker build --build-arg "VERSION=$(VERSION)" -t "$(IMAGE):$(TAG)" .
40+
41+
push: docker
42+
docker push "$(IMAGE):$(TAG)"
43+
44+
tools: docker
45+
# install pinned version of 'kind'
46+
# leave the name as is to avoid overwriting official binary named `kind`
47+
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-linux-amd64
48+
chmod +x kind-linux-amd64
49+
mv kind-linux-amd64 $(KIND_PATH)
50+
51+
test:
52+
./run.sh

e2e/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Postgres Operator end-to-end tests
2+
3+
End-to-end tests shall ensure that the Postgres Operator does its job when
4+
applying manifests against a Kubernetes (K8s) environment. A test runner
5+
Dockerfile is provided to run e2e tests without the need to install K8s and
6+
its runtime `kubectl` in advance. The test runner uses
7+
[kind](https://kind.sigs.k8s.io/) to create a local K8s cluster which runs on
8+
Docker.
9+
10+
## Prerequisites
11+
12+
Docker
13+
Go
14+
15+
## Build test runner
16+
17+
In the directory of the cloned Postgres Operator repository change to the e2e
18+
folder and run:
19+
20+
```bash
21+
make
22+
```
23+
24+
This will build the `postgres-operator-e2e-tests` image and download the kind
25+
runtime.
26+
27+
## Run tests
28+
29+
In the e2e folder you can invoke tests either with `make test` or with:
30+
31+
```bash
32+
./run.sh
33+
```
34+
35+
To run both the build and test step you can invoke `make e2e` from the parent
36+
directory.
37+
38+
## Covered use cases
39+
40+
The current tests are all bundled in [`test_e2e.py`](tests/test_e2e.py):
41+
42+
* support for multiple namespaces
43+
* scale Postgres cluster up and down
44+
* taint-based eviction of Postgres pods
45+
* invoking logical backup cron job
46+
* uniqueness of master pod

e2e/run.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ set -o nounset
66
set -o pipefail
77
IFS=$'\n\t'
88

9+
cd $(dirname "$0");
10+
911
readonly cluster_name="postgres-operator-e2e-tests"
10-
readonly operator_image=$(docker images --filter=reference="registry.opensource.zalan.do/acid/postgres-operator" --format "{{.Repository}}:{{.Tag}}" | head -1)
11-
readonly e2e_test_image=${cluster_name}
1212
readonly kubeconfig_path="/tmp/kind-config-${cluster_name}"
1313

14+
function pull_images(){
15+
16+
operator_tag=$(git describe --tags --always --dirty)
17+
if [[ -z $(docker images -q registry.opensource.zalan.do/acid/postgres-operator:${operator_tag}) ]]
18+
then
19+
docker pull registry.opensource.zalan.do/acid/postgres-operator:latest
20+
fi
21+
if [[ -z $(docker images -q registry.opensource.zalan.do/acid/postgres-operator-e2e-tests:${operator_tag}) ]]
22+
then
23+
docker pull registry.opensource.zalan.do/acid/postgres-operator-e2e-tests:latest
24+
fi
25+
26+
operator_image=$(docker images --filter=reference="registry.opensource.zalan.do/acid/postgres-operator" --format "{{.Repository}}:{{.Tag}}" | head -1)
27+
e2e_test_image=$(docker images --filter=reference="registry.opensource.zalan.do/acid/postgres-operator-e2e-tests" --format "{{.Repository}}:{{.Tag}}" | head -1)
28+
}
1429

1530
function start_kind(){
1631

@@ -20,8 +35,9 @@ function start_kind(){
2035
kind-linux-amd64 delete cluster --name ${cluster_name}
2136
fi
2237

23-
kind-linux-amd64 create cluster --name ${cluster_name} --config ./e2e/kind-cluster-postgres-operator-e2e-tests.yaml
38+
kind-linux-amd64 create cluster --name ${cluster_name} --config kind-cluster-postgres-operator-e2e-tests.yaml
2439
kind-linux-amd64 load docker-image "${operator_image}" --name ${cluster_name}
40+
kind-linux-amd64 load docker-image "${e2e_test_image}" --name ${cluster_name}
2541
KUBECONFIG="$(kind-linux-amd64 get kubeconfig-path --name=${cluster_name})"
2642
export KUBECONFIG
2743
}
@@ -36,11 +52,12 @@ function set_kind_api_server_ip(){
3652
}
3753

3854
function run_tests(){
55+
3956
docker run --rm --mount type=bind,source="$(readlink -f ${kubeconfig_path})",target=/root/.kube/config -e OPERATOR_IMAGE="${operator_image}" "${e2e_test_image}"
4057
}
4158

4259
function clean_up(){
43-
unset KUBECONFIG
60+
unset KUBECONFIG
4461
kind-linux-amd64 delete cluster --name ${cluster_name}
4562
rm -rf ${kubeconfig_path}
4663
}
@@ -49,6 +66,7 @@ function main(){
4966

5067
trap "clean_up" QUIT TERM EXIT
5168

69+
pull_images
5270
start_kind
5371
set_kind_api_server_ip
5472
run_tests

e2e/tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This version is replaced during release process.
2+
__version__ = '2019.0.dev1'

e2e/tests/test_e2e.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class EndToEndTestCase(unittest.TestCase):
1313
'''
14-
Test interaction of the operator with multiple k8s components.
14+
Test interaction of the operator with multiple K8s components.
1515
'''
1616

1717
# `kind` pods may stuck in the `Terminating` phase for a few minutes; hence high test timeout
@@ -21,15 +21,15 @@ class EndToEndTestCase(unittest.TestCase):
2121
@timeout_decorator.timeout(TEST_TIMEOUT_SEC)
2222
def setUpClass(cls):
2323
'''
24-
Deploy operator to a "kind" cluster created by /e2e/run.sh using examples from /manifests.
24+
Deploy operator to a "kind" cluster created by run.sh using examples from /manifests.
2525
This operator deployment is to be shared among all tests.
2626
27-
/e2e/run.sh deletes the 'kind' cluster after successful run along with all operator-related entities.
27+
run.sh deletes the 'kind' cluster after successful run along with all operator-related entities.
2828
In the case of test failure the cluster will stay to enable manual examination;
29-
next invocation of "make e2e-run" will re-create it.
29+
next invocation of "make test" will re-create it.
3030
'''
3131

32-
# set a single k8s wrapper for all tests
32+
# set a single K8s wrapper for all tests
3333
k8s = cls.k8s = K8s()
3434

3535
# operator deploys pod service account there on start up

0 commit comments

Comments
 (0)