Skip to content

Commit 36a87fa

Browse files
authored
Merge pull request #234 from couchbase/action-test
Fix code samples and add CI test framwork
2 parents 8ed7dc8 + ca1b870 commit 36a87fa

File tree

76 files changed

+1939
-1148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1939
-1148
lines changed

.github/workflows/test-samples.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test Python Code Samples
2+
3+
on:
4+
pull_request:
5+
branches: ["release/4.*"]
6+
7+
jobs:
8+
Run-Tests:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout actions
12+
uses: actions/checkout@v3
13+
14+
- name: Start Docker test system
15+
run: docker compose --profile prod up --abort-on-container-exit
16+
17+
# Destroy any remaining containers
18+
- name: Cleanup
19+
run: docker stop $(docker ps -a -q)
20+
21+
#- name: Notify slack
22+
# uses: 8398a7/action-slack@v3
23+
# with:
24+
# status: ${{ job.status }}
25+
# author_name: ":octocat: Python SDK Automation Test"
26+
# text: "https://tenor.com/en-GB/view/spanish-inquisition-surprise-monty-python-nobody-expects-it-laugh-gif-4970776"
27+
# env:
28+
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_STORE
2+
.vscode
3+
*.out
4+
*.log

Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.9-slim-bullseye
2+
3+
COPY . /python-docs-repo
4+
WORKDIR /python-docs-repo/tests
5+
6+
RUN apt-get update && apt-get install -y \
7+
build-essential \
8+
cmake \
9+
libssl-dev \
10+
jq curl \
11+
npm
12+
13+
RUN pip install -r ../requirements.txt
14+
RUN npm install -g bats
15+
16+
ENTRYPOINT [ "./wait-for-couchbase.sh", "1" ]

README.adoc

+86-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This repository hosts the documentation source for the Couchbase Python SDK.
1313

1414
== Contributing
1515

16-
Check out our {url-contribute}[contributing guide] to learn how to:
16+
Check out the {url-contribute}[contributing guide] to learn how to:
1717

1818
* submit a bug or feedback issue
1919
* set up your documentation workspace
@@ -27,14 +27,93 @@ Thank you for helping to make the documentation better.
2727
This repository contains an Antora docs component.
2828
Keep in mind these key repository features:
2929

30-
* Component name, version, and start page are configured in each branch's _antora.yml_ file.
31-
* The navigation for all of the modules is stored in the ROOT module's _nav.adoc_ file.
32-
* Production branches use the *release/X.Y* naming pattern (e.g., release/5.5, release/6.0).
30+
* Each branch's _antora.yml_ file configures the component name, version, and start page.
31+
* The ROOT module's _nav.adoc_ file stores the navigation for all of the modules.
32+
* Production branches use the *release/X.Y* naming pattern (for example, release/5.5, release/6.0).
3333
** The {url-playbook}[docs site playbook] instructs Antora to automatically aggregate any branch names that start with *release/*.
3434

3535
== Documentation Site Toolchain
3636

37-
The documentation source files are marked up with AsciiDoc.
38-
Once merged into a version branch, the source files and their assets are aggregated, converted to HTML, and published by Antora to our staging and production sites.
39-
The docs components and {url-ui}[site UI] are orchestrated by the {url-playbook}[docs site playbook].
37+
The documentation source files written with AsciiDoc markup.
38+
Once merged into a version branch Antora aggregates the source files and their assets, converts to HTML, and publishes them to the staging and production sites.
39+
The {url-playbook}[docs site playbook] orchestrates the docs components and {url-ui}[site UI].
4040
See the contributing guide to learn more.
41+
42+
== Automated Testing
43+
44+
This repository performs a check on the sample code upon opening a Pull Request.
45+
These tests need to succeed to perform the merge.
46+
47+
=== Test Framework Structure
48+
49+
image::TestInfra.png[]
50+
51+
The following steps conduct the check:
52+
53+
1. The user opens a PR, triggering the GitHub Action
54+
2. The action creates a GitHub runner VM, and copies the repository over
55+
3. The runner starts the Docker test framework with `docker compose --profile prod up --abort-on-container-exit`. The flag is to verify the database container is also killed when the tests finish.
56+
4. The database and SDK containers start. The following two steps occur concurrently.
57+
** Database Container: Uses the `server/sandbox` image from Docker Hub. This image configures and starts Couchbase, setting up a one node cluster with the travel-sample bucket installed.
58+
** SDK Container: Builds the SDK image from a debian python base image, copying the repository over, and installing dependencies. It then uses `wait-for-couchbase` to ping the Couchbase container, until it confirms the database is fully configured and running.
59+
5. Once the SDK confirms the database is ready for tests, it runs the bats test file. Bats retries each test up to three times upon failure.
60+
6. When the tests have completed, both the SDK and Couchbase containers should die. If they persist, such as in a case where the test framework crashes, the GitHub Action instead destroys them.
61+
62+
=== Testing Code Samples Locally
63+
64+
You may want to run tests locally if you are adding/updating a code sample.
65+
The only prerequisites are a local copy of this repository, and https://www.docker.com/[Docker].
66+
You can start the local test environment by running the following command:
67+
68+
[source, console]
69+
----
70+
docker compose --profile local up
71+
----
72+
73+
This creates two Docker containers with the same structure as described in <<Test Framework Structure>>, with the following changes:
74+
75+
* The bats tests aren't automatically performed once Couchbase has finished configuring
76+
* The repository files in the container mount onto the SDK repository in your local machine.
77+
This means any changes you make to the files on your local machine are instantly reflected in the container.
78+
79+
To run tests, you either need to use the
80+
https://docs.docker.com/desktop/use-desktop/container/#integrated-terminal[Integrated Terminal]
81+
feature on Docker Desktop, or SSH into the container from the command line.
82+
You can use the following command to use the SDK container shell:
83+
84+
[source, console]
85+
----
86+
docker exec -it python-sdk-local /bin/bash
87+
----
88+
89+
Once within the container, run the following command to run a single test:
90+
91+
[source, console]
92+
----
93+
bats -f "<test_name>" test.bats
94+
----
95+
96+
The `-f` flag only runs tests that match the given regular expression.
97+
As test names are in a standard format, you can also run the following to test all the files in a given module:
98+
99+
[source, console]
100+
----
101+
bats -f "<module name>" test.bats
102+
----
103+
104+
=== Creating a New Test
105+
106+
To add a new test, append the following to the `test.bats` file.
107+
108+
[source, bats]
109+
----
110+
@test "[<module>] - <code_file>" {
111+
runExample $<module_dir_var> <code_file>
112+
assert_success
113+
}
114+
----
115+
116+
You can find the module directory variables in `test_helper.bash`.
117+
The test name is `"[<module>] - <code_file>"`.
118+
The name can be anything, but this standard format allows you to carry out single tests, as described in the preceding section.
119+
For more information about creating tests, see the https://bats-core.readthedocs.io/en/stable/writing-tests.html[bats documentation].

TestInfra.png

31.3 KB
Loading

docker-compose.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: "3.9"
2+
3+
services:
4+
# Docker uses services names as DNS records.
5+
# Calling the db container this means the
6+
# connection strings in the samples work but,
7+
# still look like placeholders.
8+
your-ip:
9+
image: couchbase/server-sandbox:7.1.1
10+
ports:
11+
- "8091-8095:8091-8095"
12+
- "9102:9102"
13+
- "11210:11210"
14+
expose:
15+
- "8091"
16+
- "8094"
17+
container_name: Couchbase-7.1.1
18+
profiles:
19+
- prod
20+
- local
21+
22+
# The GH Action uses this profile to run tests.
23+
python-sdk-repo:
24+
build: .
25+
depends_on:
26+
- your-ip
27+
container_name: python-sdk
28+
profiles:
29+
- prod
30+
31+
# This profile is for local use creating tests.
32+
local-python-sdk-repo:
33+
build:
34+
dockerfile: local-tests.Dockerfile
35+
depends_on:
36+
- your-ip
37+
container_name: python-sdk-local
38+
volumes:
39+
- .:/app
40+
profiles:
41+
- local

local-tests.Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM python:3.9-slim-bullseye
2+
3+
WORKDIR /app/tests
4+
# The volume is not created until the container is run,
5+
# so we have to copy this over separately.
6+
COPY requirements.txt /app/tests
7+
8+
RUN apt-get update && apt-get install -y \
9+
build-essential \
10+
cmake \
11+
libssl-dev \
12+
jq curl \
13+
npm
14+
15+
RUN pip install -r requirements.txt
16+
RUN npm install -g bats
17+
18+
ENTRYPOINT [ "./wait-for-couchbase.sh" ]

modules/hello-world/examples/hello_world_t.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# Get a reference to our cluster
2424
# NOTE: For TLS/SSL connection use 'couchbases://<your-ip-address>' instead
25-
cluster = Cluster('couchbase://localhost', ClusterOptions(auth))
25+
cluster = Cluster('couchbase://your-ip', ClusterOptions(auth))
2626

2727
# Wait until the cluster is ready for use.
2828
cluster.wait_until_ready(timedelta(seconds=5))

modules/hello-world/examples/index_hello_world.py

-104
This file was deleted.

modules/hello-world/examples/kv_bulk_hello_world.py

-58
This file was deleted.

0 commit comments

Comments
 (0)