Skip to content

Commit

Permalink
expand tests and docs (#2721)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark McCormick <mark.mccormick@chainguard.dev>
  • Loading branch information
mamccorm authored May 22, 2024
1 parent 1dfe262 commit ab28b3a
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
46 changes: 46 additions & 0 deletions images/cassandra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,50 @@ docker pull cgr.dev/chainguard/cassandra:latest
<!--getting:end-->

<!--body:start-->

## Deploying
Deploy a new instance of Cassandra using this image:

```bash
docker run -d \
--name cassandra \
-e CASSANDRA_START_RPC=true \
-p 9042:9042 cgr.dev/chainguard/cassandra:latest
```

You can use `nodetool status` command to check if Cassandra is running properly.
Note, it'll take a couple of minutes for Cassandra to become fully operational:

```bash
docker exec -it cassandra nodetool status

Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 127.0.0.1 104.38 KiB 16 100.0% 0e75f72d-d273-4fac-807e-2b230583458c rack1
```

`cqlsh` is available on the image:

```bash
docker exec -i cassandra cqlsh -e "
CREATE KEYSPACE testkeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
USE testkeyspace;
CREATE TABLE users (user_id UUID PRIMARY KEY, name text);
INSERT INTO users (user_id, name) VALUES (uuid(), 'Chainguard');
SELECT * FROM users;
"

user_id | name
--------------------------------------+------------
3f13c6b4-4a22-4de7-a1f6-a3ac6e887ddb | Chainguard
67e3be15-07f9-4dd6-b9b9-c00037d705ac | Chainguard

(2 rows)
```

For more information, refer to the [Cassandra documentation](https://cassandra.apache.org/_/quickstart.html).

<!--body:end-->
33 changes: 32 additions & 1 deletion images/cassandra/tests/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
terraform {
required_providers {
oci = { source = "chainguard-dev/oci" }
oci = { source = "chainguard-dev/oci" }
imagetest = { source = "chainguard-dev/imagetest" }

kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.28.1"
Expand Down Expand Up @@ -197,3 +199,32 @@ resource "kubernetes_stateful_set" "cassandra" {
service_name = kubernetes_service.cassandra.metadata[0].name
}
}

data "imagetest_inventory" "this" {}

resource "imagetest_harness_docker" "docker" {
name = "cassandra"
inventory = data.imagetest_inventory.this

mounts = [{
source = path.module
destination = "/tests"
}]

envs = {
IMAGE_NAME : var.digest
}
}

resource "imagetest_feature" "basic" {
harness = imagetest_harness_docker.docker
name = "Basic"
description = "Run Cassandra functionality tests."

steps = [
{
name = "Cassandra tests"
cmd = "/tests/run-tests.sh"
}
]
}
54 changes: 54 additions & 0 deletions images/cassandra/tests/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail -x

# Start a Cassandra container
CASSANDRA_CONTAINER=$(docker run -d -e CASSANDRA_START_RPC=true $IMAGE_NAME)

# Wait for Cassandra to start
MAX_RETRIES=10
RETRY_INTERVAL=10

for ((i=0; i<MAX_RETRIES; i++)); do
if docker exec "$CASSANDRA_CONTAINER" nodetool status; then
echo "Cassandra is running"
break
else
echo "Waiting for Cassandra to start..."
sleep $RETRY_INTERVAL
fi

if [[ $i -eq $((MAX_RETRIES-1)) ]]; then
echo "Error: Cassandra did not start within the expected time"
docker logs "$CASSANDRA_CONTAINER"
docker stop "$CASSANDRA_CONTAINER"
docker rm "$CASSANDRA_CONTAINER"
exit 1
fi
done

# Create a keyspace and table, insert data, and query it
docker exec -i "$CASSANDRA_CONTAINER" cqlsh <<EOF
CREATE KEYSPACE testkeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
USE testkeyspace;
CREATE TABLE users (user_id UUID PRIMARY KEY, name text);
INSERT INTO users (user_id, name) VALUES (uuid(), 'Chainguard');
SELECT * FROM users;
EOF
sleep 5

# Check if the data was inserted and queried correctly
RESULT=$(docker exec "$CASSANDRA_CONTAINER" cqlsh -e "USE testkeyspace; SELECT * FROM users;")
if [[ "$RESULT" != *"Chainguard"* ]]; then
echo "Error: Data insertion/query failed"
docker logs "$CASSANDRA_CONTAINER"
docker stop "$CASSANDRA_CONTAINER"
docker rm "$CASSANDRA_CONTAINER"
exit 1
fi

# Clean up
docker stop "$CASSANDRA_CONTAINER"
docker rm "$CASSANDRA_CONTAINER"

echo "Cassandra functionality test passed!"

0 comments on commit ab28b3a

Please sign in to comment.