Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JenswBE committed Jul 2, 2024
1 parent 10b3cfa commit 07c5f5c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
43 changes: 38 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ on:
push:
pull_request:

env:
GO_VERSION: "1.22" # Keep version aligned with server/files/network-performance-file-generator/Dockerfile
PYTHON_VERSION: "3.11" # Debian 12

jobs:
lineinfile:
name: Test lineinfile
python-tests:
name: Test Python code
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout repo
uses: actions/checkout@v4

- uses: actions/setup-python@v5
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.10" # Ubuntu 22.04
python-version: ${{ env.PYTHON_VERSION }}

- name: Run unit tests for desktop/lineinfile
working-directory: desktop/lineinfile
Expand All @@ -22,3 +28,30 @@ jobs:
- name: Run unit tests for server/templates/nut/client_server/upssched-cmd
working-directory: server/templates/nut/client_server/upssched-cmd
run: python3 -m unittest

golang-tests:
name: Test Golang code
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
check-latest: true
go-version: ${{ env.GO_VERSION }}

- name: Pull common linter configs
run: wget -O .golangci.yml https://raw.githubusercontent.com/JenswBE/setup/main/programming_configs/golang/.golangci.yml

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
working-directory: server/files/network-performance-file-generator
with:
version: latest
args: --timeout=5m

- name: Run unit tests
working-directory: server/files/network-performance-file-generator
run: go test main.go main_test.go
1 change: 1 addition & 0 deletions server/files/network-performance-file-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Keep version aligned with .github/workflows/test.yml
FROM docker.io/library/golang:1.22 AS builder
WORKDIR /src/
COPY main.go main.go
Expand Down
38 changes: 34 additions & 4 deletions server/files/network-performance-file-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
"os"
Expand All @@ -13,6 +14,7 @@ import (
const Port = "8080"
const SizeMBParam = "size_mb"
const DefaultSizeMB = 10
const MB = 1024 * 1024 // 1MB

func main() {
// Flags
Expand All @@ -28,8 +30,15 @@ func main() {
}
}

// Run service
if err := run(); err != nil {
slog.Error("Starting service returned error", "error", err)
}
}

func run() error {
// Init
payload1MB := bytes.Repeat([]byte("X"), 1024*1024)
payload1MB := bytes.Repeat([]byte("X"), MB)

// Start server
slog.Info("Starting server ...", "port", Port)
Expand All @@ -55,7 +64,7 @@ func main() {
}
}
}))
slog.Error("Failed to start HTTP server", "error", err)
return fmt.Errorf("failed to start HTTP server: %w", err)
}

func parseSizeParam(input string, defaultValue int) (int, error) {
Expand All @@ -81,9 +90,30 @@ func isHealthy() bool {
checkURL := fmt.Sprintf("http://localhost:%s?%s=2", Port, SizeMBParam)
resp, err := http.Get(checkURL)
if err != nil {
slog.Warn("Http call returned error", "error", err)
slog.Warn("Healthcheck: HTTP call returned error", "error", err)
return false
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
slog.Warn("Healthcheck: Failed to close body", "error", closeErr)
}
}()

if resp.ContentLength != 2*MB {
slog.Warn("Healthcheck: HTTP call returned incorrect content length", "expected", 2048, "actual", resp.ContentLength)
return false
}

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Warn("Healthcheck: Failed to read response body")
return false
}
if len(body) != 2*MB {
slog.Warn("Healthcheck: Actual response body has different length from ContentLength", "content_length", resp.ContentLength, "body_length", len(body))
return false
}

if resp.ContentLength
slog.Info("Healthcheck successful")
return true
}
20 changes: 20 additions & 0 deletions server/files/network-performance-file-generator/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"testing"
"time"
)

func Test(t *testing.T) {
go func() {
if serviceErr := run(); serviceErr != nil {
t.Logf("service returned error: %v", serviceErr)
}
}()

time.Sleep(200 * time.Millisecond)

if !isHealthy() {
t.Fatal("Service is not healthy")
}
}

0 comments on commit 07c5f5c

Please sign in to comment.