Skip to content

Commit

Permalink
refactor(knuu-example): Initial commit for the refactor (#291)
Browse files Browse the repository at this point in the history
* refactor(knuu-example): Initial commit for the refactor

Signed-off-by: Jose Ramon Mañes <jose@celestia.org>

* refactor(knuu-example): add make actions

Signed-off-by: Jose Ramon Mañes <jose@celestia.org>

* chore: remove clutter

* fix: import issue

---------

Signed-off-by: Jose Ramon Mañes <jose@celestia.org>
Co-authored-by: Mojtaba <mojtaba@celestia.org>
Co-authored-by: Mojtaba <mojtaba-esk@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent 76b7c6e commit b16351a
Show file tree
Hide file tree
Showing 40 changed files with 8,306 additions and 80 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/knuu_testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Knuu Testing

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
test:

runs-on: ubuntu-latest

steps:
- name: Set up Go 1.22.1
uses: actions/setup-go@v4.0.1
with:
go-version: 1.22.1

- name: Check out code into the Go module directory
uses: actions/checkout@v4.1.2

- name: Setup kubeconfig
env:
KUBECONFIG_FILE: ${{ secrets.KNUU_KUBECONFIG_FILE }}
run: |
mkdir -p $HOME/.kube
echo "${KUBECONFIG_FILE}" > $HOME/.kube/config
- name: Run Tests
run: make test-all
env:
KNUU_NAMESPACE: "knuu-test"
KNUU_SKIP_CLEANUP: "false"
KNUU_TIMEOUT: "240m" # needed as we run all the tests
GRAFANA_ENDPOINT: ${{ secrets.GRAFANA_ENDPOINT }}
GRAFANA_USERNAME: ${{ secrets.GRAFANA_USERNAME }}
GRAFANA_TOKEN: ${{ secrets.GRAFANA_TOKEN }}
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test-basic:
go test -v ./e2e/basic -timeout 120m

test-basic-file-cache:
go test -v ./e2e/basic -run=TestFileCache -count=1 -timeout 120m

test-basic-folder-cache:
go test -v ./e2e/basic -run=TestFolderCache -count=1 -timeout 120m

test-bittwister-packetloss:
KNUU_TIMEOUT=120m go test -v ./e2e/basic --run=TestBittwister_Packetloss -timeout 60m -count=1

test-bittwister-bandwidth:
KNUU_TIMEOUT=120m go test -v ./e2e/basic --run=TestBittwister_Bandwidth -timeout 60m -count=1

test-bittwister-latency:
KNUU_TIMEOUT=120m go test -v ./e2e/basic --run=TestBittwister_Latency -timeout 60m -count=1

test-bittwister-jitter:
KNUU_TIMEOUT=120m go test -v ./e2e/basic --run=TestBittwister_Jitter -timeout 60m -count=1

test-celestia-app:
go test -v ./e2e/celestia_app

test-celestia-node:
go test -v ./e2e/celestia_node

test-all:
KNUU_TIMEOUT=300m go test -v ./e2e/... -timeout 120m

.PHONY: test-all test-basic test-basic-file-cache test-basic-folder-cache test-bittwister-packetloss test-bittwister-bandwidth test-bittwister-latency test-bittwister-jitter test-celestia-app test-celestia-node

75 changes: 69 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
![GitHub Release](https://img.shields.io/github/v/release/celestiaorg/knuu)
[![CodeQL](https://github.com/celestiaorg/knuu/workflows/CodeQL/badge.svg)](https://github.com/celestiaorg/knuu/actions) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/projects/7475/badge)](https://bestpractices.coreinfrastructure.org/projects/7475)

---

## Description

The goal of knuu is to provide a framework for writing integration tests.
The goal of Knuu is to provide a framework for writing integration tests.
The framework is written in Go and is designed to be used in Go projects.
The idea is to provide a framework that uses the power of containers and Kubernetes without the test writer having to know the details of how to use them.

We invite you to explore our codebase, contribute, and join us in developing a framework to help projects write integration tests.

---

## Features

Knuu is designed around `Instances`, which you can create, start, control, communicate with other Instances, stop, and destroy.

Some of the features of knuu are:
Some of the features of Knuu are:

- Initialize an Instance from a Container/Docker image
- Configure startup commands
Expand All @@ -34,13 +38,17 @@ Some of the features of knuu are:

> If you have feedback on the framework, want to report a bug, or suggest an improvement, please create an issue [here](https://github.com/celestiaorg/knuu/issues/new/choose).
---

## Getting Started

This section will guide you on how to set up and run **knuu**.
This section will guide you on how to set up and run **Knuu**.

---

### Prerequisites

1. **Kubernetes cluster**: Set up access to a Kubernetes cluster using a kubeconfig.
1. **Kubernetes cluster**: Set up access to a Kubernetes cluster using a `kubeconfig`.
> In case you have no Kubernetes cluster running yet, you can get more information [here](https://kubernetes.io/docs/setup/).
2. **Docker**: Knuu uses Docker by default. If `KNUU_BUILDER` is not explicitly set to `kubernetes`, Docker is required to run Knuu.
Expand All @@ -63,7 +71,7 @@ Simple example:

2. Run `go mod tidy` to download the dependencies.

3. Create a file called `main_test.go` with the following content to initialize knuu:
3. Create a file called `main_test.go` with the following content to initialize Knuu:

```go
package main
Expand Down Expand Up @@ -155,9 +163,11 @@ Simple example:

You can find more examples in the following repositories:

- [celestiaorg/knuu-example](https://github.com/celestiaorg/knuu-example)
- [celestiaorg/knuu](https://github.com/celestiaorg/knuu/e2e)
- [celestiaorg/celestia-app](https://github.com/celestiaorg/celestia-app/tree/main/test/e2e)

---

### Running Tests

You can use the built-in `go test` command to run the tests.
Expand All @@ -178,6 +188,59 @@ You can set the following environment variables to change the behavior of knuu:
| `KNUU_BUILDER` | The builder to use for building images. | `docker`, `kubernetes` | `docker` |
| `DEBUG_LEVEL` | The debug level. | `debug`, `info`, `warn`, `error` | `info` |

---

# E2E

In the folder `e2e`, you will find some examples of how to use the [knuu](https://github.com/celestiaorg/knuu) Integration Test Framework.

## Setup

1. Install [Docker](https://docs.docker.com/get-docker/).

2. Set up access to a Kubernetes cluster using your `kubeconfig` and create the `test` namespace.

> **Note:** The used namespace can be changed by setting the `KNUU_NAMESPACE` environment variable.

## Write Tests

You can find the relevant documentation in the `pkg/knuu` package at: https://pkg.go.dev/github.com/celestiaorg/knuu

## Run

```shell
make test-all
```

Or run only the basic examples:

```shell
make test-basic
```

Or run BitTwister tests:

```sh
make test-bittwister-packetloss
make test-bittwister-bandwidth
make test-bittwister-latency
make test-bittwister-jitter
```

Or the celestia-app examples:

```shell
make test-celestia-app
```

Or the celestia-node examples:

```shell
make test-celestia-node
```

---

## Contributing

We warmly welcome and appreciate contributions.
Expand Down
62 changes: 62 additions & 0 deletions e2e/basic/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package basic

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/celestiaorg/knuu/pkg/knuu"
)

func TestBasic(t *testing.T) {
t.Parallel()
// Setup

instance, err := knuu.NewInstance("alpine")
if err != nil {
t.Fatalf("Error creating instance '%v':", err)
}
err = instance.SetImage("docker.io/alpine:latest")
if err != nil {
t.Fatalf("Error setting image: %v", err)
}
err = instance.SetCommand("sleep", "infinity")
if err != nil {
t.Fatalf("Error setting command: %v", err)
}
err = instance.Commit()
if err != nil {
t.Fatalf("Error committing instance: %v", err)
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

err = instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
})

// Test logic

err = instance.Start()
if err != nil {
t.Fatalf("Error starting instance: %v", err)
}
err = instance.WaitInstanceIsRunning()
if err != nil {
t.Fatalf("Error waiting for instance to be running: %v", err)
}
wget, err := instance.ExecuteCommand("echo", "Hello World!")
if err != nil {
t.Fatalf("Error executing command '%v':", err)
}

assert.Equal(t, wget, "Hello World!\n")
}
Loading

0 comments on commit b16351a

Please sign in to comment.