Skip to content

Commit 7b5699a

Browse files
feat(tests): integration tests
1 parent bb22d4d commit 7b5699a

File tree

9 files changed

+228
-8
lines changed

9 files changed

+228
-8
lines changed

.github/workflows/docker-image.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: CI
1+
name: "build-docker-image"
22

33
on:
44
push:
55
tags:
6-
- 'v[0-9]+.[0-9]+.[0-9]+*'
6+
- "v[0-9]+.[0-9]+.[0-9]+*"
77

88
jobs:
99
bake:

.github/workflows/tests.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: "tests"
2+
3+
on:
4+
pull_request_target: # Use pull_request_target
5+
branches: [main]
6+
7+
jobs:
8+
run-specs:
9+
name: run specs
10+
runs-on: ubuntu-22.04
11+
steps:
12+
- name: deploy
13+
run: ./vagrant-deploy.sh
14+
- name: tests
15+
run: ./vagrant-tests.sh

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ ARG BASE_IMAGE="openresty-proxy-connect"
22

33
FROM ${BASE_IMAGE}
44

5-
ADD generate-certificate.sh /generate-certificate.sh
6-
ADD entrypoint.sh /entrypoint.sh
5+
COPY generate-certificate.sh /generate-certificate.sh
6+
COPY entrypoint.sh /entrypoint.sh
77

8-
ADD nginx.conf /opt/openresty/nginx/conf/nginx.conf
9-
ADD nginx.manifest.common.conf /opt/openresty/nginx/conf/nginx.manifest.common.conf
10-
ADD nginx.manifest.stale.conf /opt/openresty/nginx/conf/nginx.manifest.stale.conf
11-
ADD proxy_auth.lua /opt/openresty/nginx/conf/proxy_auth.lua
8+
COPY nginx.conf /opt/openresty/nginx/conf/nginx.conf
9+
COPY nginx.manifest.common.conf /opt/openresty/nginx/conf/nginx.manifest.common.conf
10+
COPY nginx.manifest.stale.conf /opt/openresty/nginx/conf/nginx.manifest.stale.conf
11+
COPY proxy_auth.lua /opt/openresty/nginx/conf/proxy_auth.lua
1212

1313
RUN apk add --no-cache --update bash openssl \
1414
&& mkdir -p /docker_mirror_cache /certs /opt/openresty/nginx/tmp \

Vagrantfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Vagrant.configure('2') do |config|
2+
config.vm.box = 'ubuntu/focal64'
3+
4+
config.vm.define 'docker-registry-cache' do |node|
5+
node.vm.hostname = 'docker-registry-cache'
6+
node.vm.provision 'shell', inline: '/vagrant/tests/vagrant-deploy.sh'
7+
node.vm.provision 'shell', inline: '/vagrant/tests/vagrant-tests.sh', run: 'always'
8+
node.vm.provider 'virtualbox' do |v|
9+
v.memory = 2048
10+
end
11+
end
12+
end

nginx.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ http {
101101
}
102102

103103
# A map to force http scheme for some docker registries if needed.
104+
# nosemgrep
104105
map $host $targetScheme {
105106
hostnames;
106107
include /opt/openresty/nginx/conf/docker.targetScheme.map;
@@ -306,6 +307,7 @@ http {
306307

307308
# by default, dont cache anything.
308309
location / {
310+
# nosemgrep
309311
proxy_pass $targetScheme://$targetHost;
310312
proxy_cache off;
311313
}

nginx.manifest.common.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# nginx config fragment included in every manifest-related location{} block.
22
add_header X-Docker-Registry-Proxy-Cache-Upstream-Status "$upstream_cache_status";
33
add_header X-Docker-Registry-Proxy-Cache-Type "$docker_proxy_request_type";
4+
# nosemgrep
45
proxy_pass $targetScheme://$targetHost;
56
proxy_cache cache;
67
proxy_cache_key $uri;

tests/docker_test.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
function test_success_docker_debian_pull() {
4+
function foo() {
5+
return 0
6+
}
7+
8+
docker pull debian:13-slim
9+
10+
assert_successful_code
11+
}
12+
13+
function test_success_docker_tag_debian_image() {
14+
function foo() {
15+
return 0
16+
}
17+
18+
docker tag debian:13-slim my-registry.local:443/debian:13-slim
19+
20+
assert_successful_code
21+
}
22+
23+
function test_success_docker_push_debian_image_to_local_registry_through_proxy() {
24+
function foo() {
25+
return 0
26+
}
27+
28+
docker push my-registry.local:443/debian:13-slim
29+
30+
assert_successful_code
31+
}
32+
33+
function test_success_docker_rm_local_debian_images() {
34+
function foo() {
35+
return 0
36+
}
37+
38+
docker rmi debian:13-slim my-registry.local:443/debian:13-slim
39+
40+
assert_successful_code
41+
}
42+
43+
function test_success_docker_pull_debian_image_from_local_registry_through_proxy() {
44+
function foo() {
45+
return 0
46+
}
47+
48+
docker pull my-registry.local:443/debian:13-slim
49+
50+
assert_successful_code
51+
}
52+
53+
function test_failed_docker_pull_debian_image_from_unknown_registry_through_proxy() {
54+
function foo() {
55+
return 0
56+
}
57+
58+
docker pull my-registryX.local:443/debian:13-slim
59+
60+
assert_unsuccessful_code
61+
}
62+
63+
function test_failed_curl_request_to_unknwon_registry_through_proxy() {
64+
function foo() {
65+
return 0
66+
}
67+
68+
assert_equals "403" "$(curl -I -x https://user1:password1@my-proxy.local:3128 https://my-registry-x.local:443/v2/_catalog 2> /dev/null | head -n 1 | cut -d' ' -f2)"
69+
}
70+
71+
function test_failed_curl_request_to_proxy_without_user() {
72+
function foo() {
73+
return 0
74+
}
75+
76+
assert_equals "407" "$(curl -I -x https://my-proxy.local:3128 https://my-registry.local:443/v2/_catalog 2> /dev/null | head -n 1 | cut -d' ' -f2)"
77+
}
78+
79+
function test_failed_curl_request_to_proxy_with_wrong_creds() {
80+
function foo() {
81+
return 0
82+
}
83+
84+
assert_equals "401" "$(curl -I -x https://user1:dummy@my-proxy.local:3128 https://my-registry.local:443/v2/_catalog 2> /dev/null | head -n 1 | cut -d' ' -f2)"
85+
}

tests/vagrant-deploy.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
3+
# Vars
4+
CERTS_PATH="/root/certs"
5+
DOCKER_PATH="/var/lib/docker"
6+
DOCKER_MIRROR_CACHE_PATH="/docker_mirror_cache"
7+
8+
# Add proxy to docker
9+
if [ ! -d /etc/systemd/system/docker.service.d ]; then
10+
mkdir /etc/systemd/system/docker.service.d
11+
cat << 'EOF' > /etc/systemd/system/docker.service.d/proxy.conf
12+
[Service]
13+
Environment="NO_PROXY=*.docker.io,*.cloudflarestorage.com"
14+
Environment="HTTPS_PROXY=https://user1:password1@my-proxy.local:3128"
15+
EOF
16+
fi
17+
18+
# Install packages
19+
apt-get update
20+
apt-get install -y docker.io docker-compose docker-buildx apache2-utils
21+
22+
# Start and enable docker
23+
systemctl is-active --quiet docker || systemctl enable docker
24+
systemctl is-active --quiet docker || systemctl start docker
25+
26+
if [ ! -f ${CERTS_PATH}/custom_ca.key ]; then
27+
# Generate certificates
28+
mkdir -p ${CERTS_PATH}
29+
## CA
30+
openssl genrsa -out ${CERTS_PATH}/custom_ca.key 4096
31+
openssl req -x509 -new -nodes -key ${CERTS_PATH}/custom_ca.key -sha256 -days 3650 -subj "/C=AU/ST=Some-State/O=MyOrg/CN=local" -out ${CERTS_PATH}/custom_ca.crt
32+
33+
## Proxy CRT
34+
openssl genrsa -out ${CERTS_PATH}/proxy_server.key 4096
35+
openssl req -new -sha256 \
36+
-key ${CERTS_PATH}/proxy_server.key \
37+
-subj "/C=AU/ST=Some-State/O=ORG/OU=ORG_UNIT/CN=my-proxy.local" \
38+
-reqexts SAN \
39+
-config <(cat /etc/ssl/openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:my-proxy.local")) \
40+
-out ${CERTS_PATH}/proxy_server.csr
41+
openssl x509 -req -extfile <(printf "subjectAltName=DNS:my-proxy.local") -days 3650 -in ${CERTS_PATH}/proxy_server.csr -CA ${CERTS_PATH}/custom_ca.crt -CAkey ${CERTS_PATH}/custom_ca.key -CAcreateserial -out ${CERTS_PATH}/proxy_server.crt -sha256
42+
43+
## Docker-registry CRT
44+
openssl genrsa -out ${CERTS_PATH}/server.key 4096
45+
openssl req -new -sha256 \
46+
-key ${CERTS_PATH}/server.key \
47+
-subj "/C=AU/ST=Some-State/O=ORG/OU=ORG_UNIT/CN=my-registry.local" \
48+
-reqexts SAN \
49+
-config <(cat /etc/ssl/openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:my-registry.local")) \
50+
-out ${CERTS_PATH}/server.csr
51+
openssl x509 -req -extfile <(printf "subjectAltName=DNS:my-registry.local") -days 3650 -in ${CERTS_PATH}/server.csr -CA ${CERTS_PATH}/custom_ca.crt -CAkey ${CERTS_PATH}/custom_ca.key -CAcreateserial -out ${CERTS_PATH}/server.crt -sha256
52+
53+
#Add ca-cert to system
54+
cp ${CERTS_PATH}/custom_ca.crt /usr/local/share/ca-certificates
55+
update-ca-certificates
56+
fi
57+
58+
grep -q "my-proxy.local" /etc/hosts
59+
if [[ $? != 0 ]]; then
60+
echo "127.0.0.1 my-proxy.local" >> /etc/hosts
61+
fi
62+
63+
# Build image
64+
docker buildx bake --file /vagrant/docker-bake.hcl --set *.context=/vagrant
65+
66+
# Create docker network
67+
docker network inspect registry >/dev/null 2>&1 || docker network create registry
68+
69+
# Run docker registry container
70+
if [ ! "$(docker ps -a -q -f name=my-registry)" ]; then
71+
docker run -dit --name my-registry \
72+
--hostname my-registry.local \
73+
-v ${CERTS_PATH}:/certs \
74+
-v ${DOCKER_MIRROR_CACHE_PATH}/docker-registry:/var/lib/registry \
75+
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
76+
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/server.crt \
77+
-e REGISTRY_HTTP_TLS_KEY=/certs/server.key \
78+
--network registry \
79+
registry:2
80+
fi
81+
82+
# Run docker registry cache container
83+
if [ ! "$(docker ps -a -q -f name=openresty_docker_registry_proxy)" ]; then
84+
mkdir ${DOCKER_MIRROR_CACHE_PATH}/docker-cache/
85+
chown 1001:1001 ${DOCKER_MIRROR_CACHE_PATH}/docker-cache/
86+
chown 1001:1001 ${CERTS_PATH}/*server.{crt,key}
87+
docker run -dit --name openresty_docker_registry_proxy \
88+
-p 3128:3128 \
89+
-v ${DOCKER_MIRROR_CACHE_PATH}/docker-cache/:/docker_mirror_cache \
90+
-v ${CERTS_PATH}:/certs \
91+
-e VERIFY_SSL=false \
92+
-e CACHE_MAX_SIZE=5G \
93+
-e ALLOW_PUSH=true \
94+
-e ENABLE_MANIFEST_CACHE=false \
95+
-e REGISTRIES=my-registry.local \
96+
-e ALLOW_UNKNOWN_REGISTRIES=false \
97+
-e HTPASSWD=$(htpasswd -nbB user1 password1) \
98+
--network registry \
99+
docker-registry-proxy-cache:latest
100+
fi

tests/vagrant-tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
curl -s https://bashunit.typeddevs.com/install.sh | bash
4+
5+
./lib/bashunit /vagrant/tests

0 commit comments

Comments
 (0)