Skip to content

Commit 0925f14

Browse files
Merge pull request #28 from bats-core/vz
Dockerfile and CI updates
2 parents 7b336ea + ae55b75 commit 0925f14

13 files changed

+125
-67
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test
2+
on:
3+
- push
4+
- pull_request
5+
- workflow_dispatch
6+
7+
jobs:
8+
test-detik:
9+
name: Test DETIK
10+
runs-on: ubuntu-20.04
11+
steps:
12+
- name: Install BATS
13+
run: |
14+
git clone https://github.com/bats-core/bats-core.git && \
15+
cd bats-core && sudo ./install.sh /usr/local && \
16+
cd ..
17+
- uses: actions/checkout@v2
18+
- name: Test with bats
19+
run: bats tests/
20+
21+
test-docker-build:
22+
name: Build the Docker image
23+
runs-on: ubuntu-20.04
24+
steps:
25+
- uses: actions/checkout@v2
26+
- name: Execute the build script
27+
run: docker build -t bats/bats-detik .

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Contributing Guidelines
2+
3+
## Issues / PR / Ideas
4+
5+
You can create issues to share ideas or report a problem.
6+
Feel free also to submit pull requests.
7+
8+
9+
## Commit Signing
10+
11+
All the commits need to be signed with a PGP key.
12+
Your commit command should look like...
13+
14+
```bash
15+
# "S" is upper-case
16+
commit -S -m "your commit message"
17+
```
18+
19+
... unless you enabled the setting to automatically sign your commits.
20+
21+
```bash
22+
# Verify signing is enabled by default
23+
git config commit.gpgsign
24+
```
25+
26+
The following links will help you to configure your client:
27+
28+
* [Signing commits](https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification/signing-commits)
29+
* [Generating a new GPG key](https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification/generating-a-new-gpg-key)
30+
* [Telling Git about your key](https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification/telling-git-about-your-signing-key)

Dockerfile

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
FROM alpine:3.10
1+
FROM alpine:3.14
22

3-
ARG KUBECTL_VERSION=v1.14.1
4-
ARG HELM_VERSION=v2.13.1
3+
ARG KUBECTL_VERSION=v1.21.2
4+
ARG HELM_VERSION=v3.6.1
5+
ARG BATS_VERSION=1.3.0
56

67
# Add packages
78
RUN apk --no-cache add \
@@ -11,20 +12,20 @@ RUN apk --no-cache add \
1112
openssh-client \
1213
bash
1314

14-
# Install BATS 1.1.0
15-
RUN curl -LO https://github.com/bats-core/bats-core/archive/v1.1.0.zip && \
16-
unzip -q -d /tmp v1.1.0.zip && \
17-
cd /tmp/bats-core-1.1.0 && \
15+
# Install BATS
16+
RUN curl -LO "https://github.com/bats-core/bats-core/archive/refs/tags/v$BATS_VERSION.zip" && \
17+
unzip -q -d /tmp "v$BATS_VERSION.zip" && \
18+
cd "/tmp/bats-core-$BATS_VERSION" && \
1819
./install.sh /usr/local && \
19-
rm -rf /tmp/bats-core-1.1.0
20+
rm -rf "/tmp/bats-core-$BATS_VERSION"
2021

2122
# Install kubectl
2223
RUN curl -LO "https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl" && \
2324
chmod +x kubectl && \
2425
mv kubectl /usr/local/bin/
2526

2627
# Install Helm
27-
RUN curl -LO "https://kubernetes-helm.storage.googleapis.com/helm-$HELM_VERSION-linux-amd64.tar.gz" && \
28+
RUN curl -LO "https://get.helm.sh/helm-$HELM_VERSION-linux-amd64.tar.gz" && \
2829
mkdir -p "/usr/local/helm-$HELM_VERSION" && \
2930
tar -xzf "helm-$HELM_VERSION-linux-amd64.tar.gz" -C "/usr/local/helm-$HELM_VERSION" && \
3031
ln -s "/usr/local/helm-$HELM_VERSION/linux-amd64/helm" /usr/local/bin/helm && \
@@ -37,8 +38,8 @@ RUN adduser -D -u 10000 testing
3738
USER testing
3839
WORKDIR /home/testing
3940

40-
# Initialize the Helm client
41-
RUN helm init --client-only --skip-refresh
41+
# Initialize the Helm client (Helm 2.x)
42+
# RUN helm init --client-only --skip-refresh
4243

4344
# Add the library
4445
COPY ./lib /home/testing/lib

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,52 +184,76 @@ It includes...
184184

185185
## Usage
186186

187-
### Setup
187+
### Manual Setup
188188

189189
* Install [BATS](https://github.com/bats-core/bats-core), a testing framework for scripts.
190190
BATS is a test framework for BASH and other scripts.
191-
* Download the **lib/detik.bash** script.
191+
* Download the **lib/detik.bash** script.
192+
192193
```bash
193194
wget https://raw.githubusercontent.com/bats-core/bats-detik/master/lib/detik.bash
194195
wget https://raw.githubusercontent.com/bats-core/bats-detik/master/lib/linter.bash
195196
wget https://raw.githubusercontent.com/bats-core/bats-detik/master/lib/utils.bash
196197
chmod +x *.bash
197198
```
198-
* Write bats scripts with assertions.
199+
200+
* Write BATS scripts with assertions.
199201
Make sure they import the **lib/utils.bash** and **lib/detik.bash** files.
200202
* Import the **lib/linter.bash** file to verify the linting of DETIK assertions.
203+
* Use the BATS command to run your tests: `bats sources/tests/main.bats`
204+
205+
206+
### Docker Setup
201207

208+
This project does not provide any official Docker image.
209+
This is because you may need various clients (kubectl, oc, kustomize...
210+
whatever) and it all depends on your requirements.
202211

203-
### Executing Tests by Hand
212+
A sample Dockerfile is provided in this project.
213+
To build a Docker image from it:
204214

205-
Assuming you have built the image from the sample Dockerfile...
215+
```bash
216+
# Tag it with LATEST
217+
docker build -t bats/bats-detik:LATEST .
218+
219+
# Overwrite the default versions
220+
docker build \
221+
--build-arg KUBECTL_VERSION=v1.21.2 \
222+
--build-arg HELM_VERSION=v3.6.1 \
223+
--build-arg BATS_VERSION=1.3.0 \
224+
-t bats/bats-detik:LATEST \
225+
.
226+
```
227+
228+
On a development machine, you can use it this way:
206229

207230
```bash
208231
# Run the image with a volume for your project.
209232
# In this example, we show how to specify the proxy
210233
# if your organization is using one.
211234
docker run -ti \
212-
-v $(pwd):/home/testing/sources \
213-
-e http_proxy="proxy.local:3128" \
214-
-e https_proxy="proxy.local:3128" \
215-
bats-detik:LATEST
235+
-v $(pwd):/home/testing/sources \
236+
-e http_proxy="proxy.local:3128" \
237+
-e https_proxy="proxy.local:3128" \
238+
bats-detik:LATEST
216239

217240
# Log into the cluster
218241
echo "It all depends on your cluster configuration"
219242

220243
# Export the namespace for Helm (v2)
221-
export TILLER_NAMESPACE=<your namespace>
244+
# export TILLER_NAMESPACE=<your namespace>
222245

223246
# Execute the tests
224247
bats sources/tests/main.bats
225248
```
226249

250+
It can also be used in a continuous integration platform.
251+
227252

228253
### Continuous Integration
229254

230255
An example is given for Jenkins in [the examples](examples/ci).
231-
The syntax is quite simple and may be easily adapted for other solutions, such as GitLab CI,
232-
Tracis CI, etc.
256+
The syntax is quite simple and may be easily adapted for other solutions, such as GitLab CI, Tracis CI, etc.
233257

234258

235259
## Syntax Reference
@@ -251,7 +275,7 @@ See [https://kubernetes.io/docs/reference/kubectl/overview/#resource-types](http
251275

252276
This simple assertion may fail sometimes.
253277
As an example, if you count the number of PODs, run your test and then kill the POD, they will still
254-
be listed, with the TERMINATING state. So, moest of the time, you will want to verify the number of instances
278+
be listed, with the TERMINATING state. So, most of the time, you will want to verify the number of instances
255279
with a given property value. Example: count the number of PODs with a given name pattern and having the `started` status.
256280
Hence this additional syntax.
257281

docker-build.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/detik.bash

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ try() {
7070
# Start the loop
7171
echo "Valid expression. Verification in progress..."
7272
code=0
73-
for ((i=1; i<=$times; i++)); do
73+
for ((i=1; i<=times; i++)); do
7474

7575
# Verify the value
76-
verify_value $property $expected_value $resource $name "$expected_count" && code=$? || code=$?
76+
verify_value "$property" "$expected_value" "$resource" "$name" "$expected_count" && code=$? || code=$?
7777

7878
# Break the loop prematurely?
7979
if [[ "$code" == "0" ]]; then
8080
break
8181
elif [[ "$i" != "1" ]]; then
8282
code=3
83-
sleep $delay
83+
sleep "$delay"
8484
else
8585
code=3
8686
fi
@@ -158,7 +158,7 @@ verify() {
158158
name="${BASH_REMATCH[4]}"
159159

160160
echo "Valid expression. Verification in progress..."
161-
verify_value $property $expected_value $resource $name
161+
verify_value "$property" "$expected_value" "$resource" "$name"
162162

163163
if [[ "$?" != "0" ]]; then
164164
return 3
@@ -190,7 +190,7 @@ verify_value() {
190190
expected_count="$5"
191191

192192
# List the items and remove the first line (the one that contains the column names)
193-
query=$(build_k8s_request $property)
193+
query=$(build_k8s_request "$property")
194194
client_with_options=$(build_k8s_client_with_options)
195195
result=$(eval $client_with_options get $resource $query | grep $name | tail -n +1)
196196

@@ -266,7 +266,7 @@ build_k8s_request() {
266266
req="$req,PROP:'$1'"
267267
fi
268268

269-
echo $req
269+
echo "$req"
270270
}
271271

272272

@@ -275,10 +275,10 @@ build_k8s_request() {
275275
build_k8s_client_with_options() {
276276

277277
client_with_options="$DETIK_CLIENT_NAME"
278-
if [[ ! -z "$DETIK_CLIENT_NAMESPACE" ]]; then
278+
if [[ -n "$DETIK_CLIENT_NAMESPACE" ]]; then
279279
# eval does not like '-n'
280280
client_with_options="$DETIK_CLIENT_NAME --namespace=$DETIK_CLIENT_NAMESPACE"
281281
fi
282282

283-
echo $client_with_options
283+
echo "$client_with_options"
284284
}

lib/linter.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ lint() {
4343
detik_debug "Read line $current_line_number: $line"
4444

4545
# Skip empty lines and comments
46-
if [[ ! -n "$line" ]] || [[ "$line" =~ ^[[:space:]]*#.* ]]; then
46+
if [[ -z "$line" ]] || [[ "$line" =~ ^[[:space:]]*#.* ]]; then
4747
if [[ "$multi_line" == "0" ]]; then
4848
handle_error "Incomplete multi-line statement at $current_line_number."
4949
current_line=""

lib/utils.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trim_ansi_codes() {
4040
# @param {string} The debug message.
4141
# @return 0
4242
debug() {
43-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
43+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
4444
mkdir -p /tmp/detik
4545
echo -e "$1" >> "/tmp/detik/$debug_filename.debug"
4646
}
@@ -49,7 +49,7 @@ debug() {
4949
# Deletes the file that contains debug messages for a given test.
5050
# @return 0
5151
reset_debug() {
52-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
52+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
5353
rm -f "/tmp/detik/$debug_filename.debug"
5454
}
5555

tests/test.detik.try.to.find.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ mytest_with_namespace() {
239239

240240
@test "trying to find of a POD with the lower-case syntax (debug)" {
241241

242-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
242+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
243243
path="/tmp/detik/$debug_filename.debug"
244244
[ -f "$path" ] && mv "$path" "$path.backup"
245245
[ ! -f "$path" ]
@@ -293,7 +293,7 @@ mytest_with_namespace() {
293293
DETIK_CLIENT_NAME="mytest_with_namespace"
294294
DETIK_CLIENT_NAMESPACE="test_ns"
295295

296-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
296+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
297297
path="/tmp/detik/$debug_filename.debug"
298298
[ -f "$path" ] && mv "$path" "$path.backup"
299299
[ ! -f "$path" ]
@@ -354,7 +354,7 @@ my_consul_test() {
354354
echo -e "NAME PROP\nconsul-for-vault-0 Running\nconsul-for-vault-1 Running\nconsul-for-vault-2 Running"
355355
fi
356356

357-
consul_cpt=$(($consul_cpt + 1))
357+
consul_cpt=$((consul_cpt + 1))
358358
echo "$consul_cpt" > /tmp/my-consul-test.txt
359359
}
360360

tests/test.detik.try.to.verify.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ mytest() {
164164

165165
@test "trying to verify the status of a POD with the lower-case syntax (debug)" {
166166

167-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
167+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
168168
path="/tmp/detik/$debug_filename.debug"
169169
[[ -f "$path" ]] && mv "$path" "$path.backup"
170170
[ ! -f "$path" ]

tests/test.detik.verify.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mytest_with_namespace() {
170170

171171
@test "verifying the number of PODs with the lower-case syntax (debug)" {
172172

173-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
173+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
174174
path="/tmp/detik/$debug_filename.debug"
175175
[ -f "$path" ] && mv "$path" "$path.backup"
176176
[ ! -f "$path" ]
@@ -220,7 +220,7 @@ mytest_with_namespace() {
220220
DETIK_CLIENT_NAME="mytest_with_namespace"
221221
DETIK_CLIENT_NAMESPACE="test_ns"
222222

223-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
223+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
224224
path="/tmp/detik/$debug_filename.debug"
225225
[ -f "$path" ] && mv "$path" "$path.backup"
226226
[ ! -f "$path" ]
@@ -376,7 +376,7 @@ mytest_with_namespace() {
376376

377377
@test "verifying the status of a POD with the lower-case syntax (debug)" {
378378

379-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
379+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
380380
path="/tmp/detik/$debug_filename.debug"
381381
[ -f "$path" ] && mv "$path" "$path.backup"
382382
[ ! -f "$path" ]

tests/test.utils.debug.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
load "../lib/utils"
44

5-
debug_filename=$(basename -- $BATS_TEST_FILENAME)
5+
debug_filename=$(basename -- "$BATS_TEST_FILENAME")
66
target="/tmp/detik/$debug_filename.debug"
77

88
@test "testing debug" {

0 commit comments

Comments
 (0)