Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor basic tests to use the new knuu obj #548

Merged
merged 13 commits into from
Aug 27, 2024
52 changes: 13 additions & 39 deletions e2e/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,25 @@ package basic
import (
"context"
"strings"

"github.com/stretchr/testify/assert"
)

const (
testImage = "alpine:latest"
)

func (ts *TestSuite) TestBasic() {
ts.T().Parallel()
// Setup

func (s *Suite) TestBasic() {
const namePrefix = "basic"
ctx := context.Background()

target, err := ts.Knuu.NewInstance("alpine")
ts.Require().NoError(err)
target, err := s.Knuu.NewInstance(namePrefix + "-target")
s.Require().NoError(err)

ts.Require().NoError(target.Build().SetImage(ctx, testImage))
ts.Require().NoError(target.Build().SetStartCommand("sleep", "infinity"))
ts.Require().NoError(target.Build().Commit(ctx))

ts.T().Cleanup(func() {
if err := target.Execution().Destroy(ctx); err != nil {
ts.T().Logf("error destroying instance: %v", err)
}
})

// Test Logic
ts.Require().NoError(target.Execution().Start(ctx))
s.Require().NoError(target.Build().SetImage(ctx, alpineImage))
s.Require().NoError(target.Build().SetStartCommand("sleep", "infinity"))
s.Require().NoError(target.Build().Commit(ctx))
s.Require().NoError(target.Execution().Start(ctx))

// Perform the test
tt := []struct {
name string
}{
{name: "Hello World"},
}

for _, tc := range tt {
tc := tc
ts.Run(tc.name, func() {
output, err := target.Execution().ExecuteCommand(ctx, "echo", tc.name)
ts.Require().NoError(err)
expectedOutput := "Hello World"
output, err := target.Execution().ExecuteCommand(ctx, "echo", expectedOutput)
s.Require().NoError(err)

output = strings.TrimSpace(output)
assert.Contains(ts.T(), output, tc.name)
})
}
output = strings.TrimSpace(output)
s.Assert().Equal(expectedOutput, output)
}
20 changes: 0 additions & 20 deletions e2e/basic/main_test.go

This file was deleted.

96 changes: 39 additions & 57 deletions e2e/basic/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ package basic
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

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

Expand All @@ -21,26 +16,30 @@ const (
prometheusConfig = "/etc/prometheus/prometheus.yml"
prometheusArgs = "--config.file=/etc/prometheus/prometheus.yml"

targetImage = "curlimages/curl:latest"
otlpPort = observability.DefaultOtelOtlpPort
curlImage = "curlimages/curl:latest"
otlpPort = observability.DefaultOtelOtlpPort
)

// TestObservabilityCollector is a test function that verifies the functionality of the otel collector setup
func TestObservabilityCollector(t *testing.T) {
t.Parallel()
func (s *Suite) TestObservabilityCollector() {
const (
namePrefix = "observability"
targetStartCommand = "while true; do curl -X POST http://localhost:8888/v1/traces; sleep 5; done"
)
ctx := context.Background()

// Setup Prometheus
prometheus, err := knuu.NewInstance("prometheus")
require.NoError(t, err)
prometheus, err := s.Knuu.NewInstance(namePrefix + "-prometheus")
s.Require().NoError(err)

require.NoError(t, prometheus.SetImage(prometheusImage))
require.NoError(t, prometheus.AddPortTCP(prometheusPort))
s.Require().NoError(prometheus.Build().SetImage(ctx, prometheusImage))
s.Require().NoError(prometheus.Network().AddPortTCP(prometheusPort))

// enable proxy for this port
err, prometheusEndpoint := prometheus.AddHost(prometheusPort)
require.NoError(t, err)
prometheusEndpoint, err := prometheus.Network().AddHost(ctx, prometheusPort)
s.Require().NoError(err)

require.NoError(t, prometheus.Commit())
s.Require().NoError(prometheus.Build().Commit(ctx))

// Add Prometheus config file
prometheusConfigContent := fmt.Sprintf(`
Expand All @@ -51,51 +50,34 @@ scrape_configs:
static_configs:
- targets: ['otel-collector:%d']
`, otlpPort)
require.NoError(t, prometheus.AddFileBytes([]byte(prometheusConfigContent), prometheusConfig, "0:0"))
s.Require().NoError(prometheus.Storage().AddFileBytes([]byte(prometheusConfigContent), prometheusConfig, "0:0"))

require.NoError(t, prometheus.SetArgs(prometheusArgs))
require.NoError(t, prometheus.Start())
s.Require().NoError(prometheus.Build().SetArgs(prometheusArgs))
s.Require().NoError(prometheus.Execution().Start(ctx))

// Setup observabilitySidecar collector
observabilitySidecar := observability.New()

require.NoError(t, observabilitySidecar.SetOtelEndpoint(4318))

err = observabilitySidecar.SetPrometheusEndpoint(otlpPort, fmt.Sprintf("knuu-%s", knuu.Scope()), "10s")
require.NoError(t, err)

require.NoError(t, observabilitySidecar.SetJaegerEndpoint(14250, 6831, 14268))

require.NoError(t, observabilitySidecar.SetOtlpExporter("prometheus:9090", "", ""))
s.Require().NoError(observabilitySidecar.SetOtelEndpoint(4318))
s.Require().NoError(observabilitySidecar.SetPrometheusEndpoint(otlpPort, fmt.Sprintf("knuu-%s", s.Knuu.Scope), "10s"))
s.Require().NoError(observabilitySidecar.SetJaegerEndpoint(14250, 6831, 14268))
s.Require().NoError(observabilitySidecar.SetOtlpExporter("prometheus:9090", "", ""))

// Create and start a target pod and configure it to use the obsySidecar to push metrics
target, err := knuu.NewInstance("target")
require.NoError(t, err, "Error creating target instance")

err = target.SetImage(targetImage)
require.NoError(t, err, "Error setting target image")

err = target.SetStartCommand("sh", "-c", "while true; do curl -X POST http://localhost:8888/v1/traces; sleep 5; done")
require.NoError(t, err, "Error setting target command")

require.NoError(t, target.AddSidecar(context.Background(), observabilitySidecar))
target, err := s.Knuu.NewInstance(namePrefix + "target")
s.Require().NoError(err)

require.NoError(t, target.Commit(), "Error committing target instance")
s.Require().NoError(target.Build().SetImage(ctx, curlImage))

require.NoError(t, target.Start(), "Error starting target instance")
err = target.Build().SetStartCommand("sh", "-c", targetStartCommand)
s.Require().NoError(err)

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}
err := knuu.BatchDestroy(prometheus, target)
if err != nil {
t.Log("Error destroying instances: ", err)
}
})
s.Require().NoError(target.Sidecars().Add(ctx, observabilitySidecar))
s.Require().NoError(target.Build().Commit(ctx))
s.Require().NoError(target.Execution().Start(ctx))

// Wait for the target pod to push data to the otel collector
s.T().Log("Waiting one minute for the target pod to push data to the otel collector")
time.Sleep(1 * time.Minute)

// Verify that data has been pushed to Prometheus
Expand All @@ -105,16 +87,16 @@ scrape_configs:
defer cancel()

req, err := http.NewRequestWithContext(ctx, "GET", prometheusURL, nil)
require.NoError(t, err)
s.Require().NoError(err)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode, "Prometheus API is not accessible")
s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode, "Prometheus API is not accessible")

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "otel-collector", "otel-collector data source not found in Prometheus")
body, err := io.ReadAll(resp.Body)
s.Require().NoError(err)
s.Require().Contains(string(body), "otel-collector", "otel-collector data source not found in Prometheus")

t.Log("otel-collector data source is available in Prometheus")
s.T().Log("otel-collector data source is available in Prometheus")
}
97 changes: 20 additions & 77 deletions e2e/basic/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,46 @@ package basic

import (
"context"
"testing"
"strings"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

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

func TestProbe(t *testing.T) {
t.Parallel()
// Setup
func (s *Suite) TestProbe() {
const namePrefix = "probe"
ctx := context.Background()

// Ideally this has to be defined in the test suit setup
exe := e2e.Executor{
Kn: knuu.GetKnuuObj(),
}
executor, err := s.Executor.NewInstance(ctx, namePrefix+"-executor")
s.Require().NoError(err)

ctx := context.Background()
executor, err := exe.NewInstance(ctx, "prob-executor")
if err != nil {
t.Fatalf("Error creating executor: %v", err)
}
web := s.createNginxInstanceWithVolume(ctx, namePrefix+"-web")

err = web.Storage().AddFile(resourcesHTML+"/index.html", nginxHTMLPath+"/index.html", "0:0")
s.Require().NoError(err)

web, err := knuu.NewInstance("web")
if err != nil {
t.Fatalf("Error creating instance '%v':", err)
}
err = web.SetImage("docker.io/nginx:latest")
if err != nil {
t.Fatalf("Error setting image '%v':", err)
}
web.AddPortTCP(80)
err = web.AddExecuteCommand("mkdir", "-p", "/usr/share/nginx/html")
if err != nil {
t.Fatalf("Error executing command '%v':", err)
}
err = web.AddVolumeWithOwner("/usr/share/nginx/html", "1Gi", 0)
if err != nil {
t.Fatalf("Error adding volume: %v", err)
}
err = web.AddFile("../system/resources/html/index.html", "/usr/share/nginx/html/index.html", "0:0")
if err != nil {
t.Fatalf("Error adding file '%v':", err)
}
livenessProbe := v1.Probe{
ProbeHandler: v1.ProbeHandler{
HTTPGet: &v1.HTTPGetAction{
Path: "/",
Port: intstr.IntOrString{Type: intstr.Int, IntVal: 80},
Port: intstr.IntOrString{Type: intstr.Int, IntVal: nginxPort},
},
},
InitialDelaySeconds: 10,
}
err = web.SetLivenessProbe(&livenessProbe)
if err != nil {
t.Fatalf("Error setting readiness probe '%v':", err)
}
err = web.Commit()
if err != nil {
t.Fatalf("Error committing instance: %v", err)
}

t.Cleanup(func() {
// after refactor, we can use instance.BatchDestroy for simplicity
err := executor.Execution().Destroy(ctx)
if err != nil {
t.Logf("Error destroying instance: %v", err)
}

err = web.Destroy()
if err != nil {
t.Logf("Error destroying instance: %v", err)
}
})
s.Require().NoError(web.Monitoring().SetLivenessProbe(&livenessProbe))
s.Require().NoError(web.Build().Commit(ctx))

// Test logic
webIP, err := web.Network().GetIP(ctx)
s.Require().NoError(err)

webIP, err := web.GetIP()
if err != nil {
t.Fatalf("Error getting IP '%v':", err)
}
s.Require().NoError(web.Execution().Start(ctx))

err = web.Start()
if err != nil {
t.Fatalf("Error starting instance: %v", err)
}
err = web.WaitInstanceIsRunning()
if err != nil {
t.Fatalf("Error waiting for instance to be running: %v", err)
}

wget, err := executor.Execution().ExecuteCommand(ctx, "wget", "-q", "-O", "-", webIP)
if err != nil {
t.Fatalf("Error executing command '%v':", err)
}
wgetOutput, err := executor.Execution().ExecuteCommand(ctx, "wget", "-q", "-O", "-", webIP)
s.Require().NoError(err)

assert.Contains(t, wget, "Hello World!")
wgetOutput = strings.TrimSpace(wgetOutput)
s.Assert().Contains(wgetOutput, "Hello World!")
}
Loading
Loading