Skip to content

Commit

Permalink
feat: add an oci registry for e2e test.
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Jul 28, 2023
1 parent 35d534f commit 21725c7
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ kpm

# e2e test cases dir
!kpm/
scripts/pkg_in_reg/*
13 changes: 11 additions & 2 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/usr/bin/env bash

# Usage: hack/run-e2e.sh
# Example 1: hack/run-e2e.sh (run e2e test)
# start registry at 'localhost:5001'
# include account 'test' and password '1234'
./scripts/reg.sh

# set the kpm default registry and repository
export KPM_REG="localhost:5001"
export KPM_REPO="test"
export OCI_REG_PLAIN_HTTP=on

set -o errexit
set -o nounset
Expand All @@ -14,6 +20,9 @@ GO111MODULE=on go install github.com/onsi/ginkgo/v2/[email protected]
LDFLAGS="-X kcl-lang.io/kpm/pkg/version.version=test_version"
go build -ldflags "$LDFLAGS" -o ./bin/kpm

# Prepare e2e test env
./scripts/e2e_prepare.sh

# Run e2e
set +e
ginkgo ./test/e2e/
Expand Down
14 changes: 14 additions & 0 deletions scripts/e2e_prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Check kpm version
version=$(./bin/kpm --version)
if ! echo "$version" | grep -q "kpm version test_version"; then
echo "Incorrect version: '$version'."
exit 1
fi

# pull the package 'k8s' from 'ghcr.io/kcl-lang/k8s'
./scripts/pull_pkg.sh

# push the package 'k8s' to 'localhost:5001/test'
./scripts/push_pkg.sh
24 changes: 24 additions & 0 deletions scripts/pull_pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# Check kpm version
version=$(./bin/kpm --version)
if ! echo "$version" | grep -q "kpm version test_version"; then
echo "Incorrect version: '$version'."
exit 1
fi

# set the kpm default registry and repository
export KPM_REG="ghcr.io"
export KPM_REPO="kcl-lang"
export OCI_REG_PLAIN_HTTP=off

current_dir=$(pwd)

mkdir -p ./scripts/pkg_in_reg/

cd ./scripts/pkg_in_reg/

$current_dir/bin/kpm pull k8s:1.14
$current_dir/bin/kpm pull k8s:1.27

cd "$current_dir"
30 changes: 30 additions & 0 deletions scripts/push_pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#!/usr/bin/env bash

# Check kpm version

version=$(./bin/kpm --version)
if ! echo "$version" | grep -q "kpm version test_version"; then
echo "Incorrect version: '$version'."
exit 1
fi

export KPM_REG="localhost:5001"
export KPM_REPO="test"
export OCI_REG_PLAIN_HTTP=on

# Prepare the package on the registry
current_dir=$(pwd)
echo $current_dir

$current_dir/bin/kpm login -u test -p 1234 localhost:5001

# Push the package k8s/1.14 to the registry
cd ./scripts/pkg_in_reg/ghcr.io/kcl-lang/k8s/1.14
$current_dir/bin/kpm push

cd "$current_dir"

# Push the package k8s/1.17 to the registry
cd ./scripts/pkg_in_reg/ghcr.io/kcl-lang/k8s/1.27
$current_dir/bin/kpm push
25 changes: 25 additions & 0 deletions scripts/reg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# create a directory to store user passwords
mkdir -p ./scripts/registry_auth

# use htpasswd to create an encrypted file
htpasswd -Bbn test 1234 > ./scripts/registry_auth/htpasswd

# check if there is a container named registry
if [ "$(docker ps -aq -f name=registry)" ]; then
# stop and remove the container named registry
docker stop registry
docker rm registry
fi

# start the Docker Registry with authentication
docker run -p 5001:5000 \
--restart=always \
--name registry \
-v /var/lib/registry:/var/lib/registry \
-v $PWD/scripts/registry_auth/:/auth/ \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
-d registry
2 changes: 2 additions & 0 deletions scripts/registry_auth/htpasswd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:$2y$05$7jmePmQjojuLiR4tNrCHxuJtjcA2aQof6WflherD95nNhgDayGWou

Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm pull oci://ghcr.io/kcl-lang/k8s
kpm pull oci://localhost:5001/test/k8s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kpm: start to pull 'oci://ghcr.io/kcl-lang/k8s'.
kpm: start to pull 'oci://localhost:5001/test/k8s'.
kpm: the lastest version '1.27' will be pulled.
kpm: pulling '/kcl-lang/k8s:1.27' from 'ghcr.io/kcl-lang/k8s'.
kpm: pulled 'oci://ghcr.io/kcl-lang/k8s' in '<workspace>/ghcr.io/kcl-lang/k8s' successfully.
kpm: pulling '/test/k8s:1.27' from 'localhost:5001/test/k8s'.
kpm: pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s' successfully.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm pull --tag 1.14 oci://ghcr.io/kcl-lang/k8s
kpm pull --tag 1.14 oci://localhost:5001/test/k8s
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kpm: start to pull 'oci://ghcr.io/kcl-lang/k8s' with tag '1.14'.
kpm: pulling '/kcl-lang/k8s:1.14' from 'ghcr.io/kcl-lang/k8s'.
kpm: pulled 'oci://ghcr.io/kcl-lang/k8s' in '<workspace>/ghcr.io/kcl-lang/k8s/1.14' successfully.
kpm: start to pull 'oci://localhost:5001/test/k8s' with tag '1.14'.
kpm: pulling '/test/k8s:1.14' from 'localhost:5001/test/k8s'.
kpm: pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s/1.14' successfully.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kpm: start to pull 'k8s'.
kpm: the lastest version '1.27' will be pulled.
kpm: pulling 'kcl-lang/k8s:1.27' from 'ghcr.io/kcl-lang/k8s'.
kpm: pulled 'k8s' in '<workspace>/ghcr.io/kcl-lang/k8s' successfully.
kpm: pulling 'test/k8s:1.27' from 'localhost:5001/test/k8s'.
kpm: pulled 'k8s' in '<workspace>/localhost:5001/test/k8s' successfully.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kpm: start to pull 'k8s:1.27'.
kpm: pulling 'kcl-lang/k8s:1.27' from 'ghcr.io/kcl-lang/k8s'.
kpm: pulled 'k8s:1.27' in '<workspace>/ghcr.io/kcl-lang/k8s/1.27' successfully.
kpm: pulling 'test/k8s:1.27' from 'localhost:5001/test/k8s'.
kpm: pulled 'k8s:1.27' in '<workspace>/localhost:5001/test/k8s/1.27' successfully.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm: failed to get package with 'invalid_tag' from 'ghcr.io/invalid_oci_repo'.
kpm: failed to get package with 'invalid_tag' from 'localhost:5001/invalid_oci_repo'.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm: pulling 'invalid_oci_repo:invalid_tag' from 'ghcr.io/invalid_oci_repo'.
kpm: pulling 'invalid_oci_repo:invalid_tag' from 'localhost:5001/invalid_oci_repo'.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm: failed to select latest version from 'ghcr.io/exist_but_not_tar'.
kpm: failed to select latest version from 'localhost:5001/exist_but_not_tar'.

0 comments on commit 21725c7

Please sign in to comment.