Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Campbell committed Jun 14, 2024
1 parent f0b9bc5 commit 858c39d
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 7 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ A devcontainer feature that will install and configure a kubernetes cluster (k3s
```bash
$ kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-576bfc4dc7-r76cs 1/1 Running 0 101m
kube-system local-path-provisioner-75bb9ff978-hbxsq 1/1 Running 0 101m
kube-system helm-install-traefik-crd-rtnrs 0/1 Completed 0 101m
kube-system svclb-traefik-c6687f4a-fwdhl 2/2 Running 0 101m
kube-system helm-install-traefik-qtddd 0/1 Completed 1 101m
kube-system traefik-5fb479b77-k299x 1/1 Running 0 101m
kube-system metrics-server-557ff575fb-6t882 1/1 Running 0 101m
kube-system coredns-576bfc4dc7-r76cs 1/1 Running 0 2m
kube-system local-path-provisioner-75bb9ff978-hbxsq 1/1 Running 0 2m
kube-system helm-install-traefik-crd-rtnrs 0/1 Completed 0 2m
kube-system svclb-traefik-c6687f4a-fwdhl 2/2 Running 0 2m
kube-system helm-install-traefik-qtddd 0/1 Completed 1 2m
kube-system traefik-5fb479b77-k299x 1/1 Running 0 2m
kube-system metrics-server-557ff575fb-6t882 1/1 Running 0 2m
```

The devcontainer feature also generates a KUBECONFIG yaml at `/devfeature/k3s-on-host/k3s.devcontainer.yaml` and updates the `KUBECONFIG` environment variable to enable applications like kubectl and helm.

```bash
$ echo $KUBECONFIG
/devfeature/k3s-on-host/k3s.devcontainer.yaml
```

## Example Usage
Expand All @@ -34,6 +41,15 @@ kube-system metrics-server-557ff575fb-6t882 1/1 Running 0
| criDockerd | Deploy k3s with the cri-dockerd configured | boolean | true |
| cluster_enabled | Disable provisioning of the cluster. This is useful in CI/CD scenarios that reference this devcontainer feature, but doesn't always need the kubernetes cluster deployed. | boolean | true |

## Testing

Testing the feature can be done by running the `./test/test.sh` script. A devcontainer will be deployed with and without cridocker and namespaces tested.
```bash
$ ./test/test.sh
```



## Build and Deploying

This devcontainer feature can be built and deployed using the devcontainer CLI.
Expand Down
101 changes: 101 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash
#
# Tests and validates the k3s-on-host devcontainer feature works as expected
#
# Example Usage:
#
# "bash ./test/test.sh"

set -e

WORKING_DIR="$(git rev-parse --show-toplevel)"


###################################
# Clean the current environment by removing k3s and purging docker
###################################
function reset_environment() {

if [[ -f "/usr/local/bin/k3s-uninstall.sh" ]]; then
echo "Uninstalling k3s"
sudo /usr/local/bin/k3s-uninstall.sh
fi

if docker ps -q | grep -q .; then
echo "Preexisting containers found. Stopping and removing all containers"
sudo docker stop $(docker ps -a -q)
fi

echo "Pruning docker"
sudo docker system prune --all --volumes --force

}

###################################
# Install the devcontainer cli so we can build the feature
###################################
function install_devcontainer_cli() {
sudo apt install npm
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo npm install -g @devcontainers/cli
}

###################################
# Deploy without cridockerd
###################################
function test_without_cridockerd() {
sudo devcontainer build --workspace-folder "${WORKING_DIR}" --config "${WORKING_DIR}/test/without-cridockerd/devcontainer.json"
sudo devcontainer up --workspace-folder "${WORKING_DIR}" --remove-existing-container \
--workspace-mount-consistency cached \
--id-label devcontainer.local_folder="${WORKING_DIR}" \
--id-label devcontainer.config_file="${WORKING_DIR}/test/without-cridockerd/devcontainer.json" \
--config "${WORKING_DIR}/test/without-cridockerd/devcontainer.json" \
--default-user-env-probe loginInteractiveShell \
--build-no-cache \
--remove-existing-container \
--mount type=volume,source=vscode,target=/vscode,external=true \
--update-remote-user-uid-default on \
--mount-workspace-git-root true

sudo kubectl get namespace/default
}

###################################
# Deploy with cridockerd
###################################
function test_with_cridockerd() {
sudo devcontainer build --workspace-folder "${WORKING_DIR}" --config "${WORKING_DIR}/test/with-cridockerd/devcontainer.json"
sudo devcontainer up --workspace-folder "${WORKING_DIR}" --remove-existing-container \
--workspace-mount-consistency cached \
--id-label devcontainer.local_folder="${WORKING_DIR}" \
--id-label devcontainer.config_file="${WORKING_DIR}/test/with-cridockerd/devcontainer.json" \
--config "${WORKING_DIR}/test/with-cridockerd/devcontainer.json" \
--default-user-env-probe loginInteractiveShell \
--build-no-cache \
--remove-existing-container \
--mount type=volume,source=vscode,target=/vscode,external=true \
--update-remote-user-uid-default on \
--mount-workspace-git-root true

sudo kubectl get namespace/default
}

function main() {
reset_environment
install_devcontainer_cli

test_without_cridockerd
reset_environment
test_with_cridockerd
reset_environment

echo "All tests passed"
}


main


set +e
14 changes: 14 additions & 0 deletions test/with-cridockerd/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "k3s-on-host",
"image": "mcr.microsoft.com/devcontainers/base:ubuntu22.04",
"runArgs": [
"--name",
"k3s-on-host"
],
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker": {},
"../../.devcontainer/./features/k3s-on-host": {
}
},
"remoteUser": "root"
}
15 changes: 15 additions & 0 deletions test/without-cridockerd/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "k3s-on-host",
"image": "mcr.microsoft.com/devcontainers/base:ubuntu22.04",
"runArgs": [
"--name",
"k3s-on-host"
],
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker": {},
"../../.devcontainer/./features/k3s-on-host": {
"criDockerd": "false"
}
},
"remoteUser": "root"
}

0 comments on commit 858c39d

Please sign in to comment.