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

test: add integration tests for "pebble run" #497

Merged
merged 15 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Integration Test
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
go: ['1.22']

name: Go ${{ matrix.go }}
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Integration test
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
run: go test -count=1 -tags=integration ./tests/
145 changes: 32 additions & 113 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package tests

import (
"errors"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
Expand All @@ -42,13 +40,13 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}

func createLayer(t *testing.T, pebbleDir string, layerFileName string, layerYAML string) {
func createLayer(t *testing.T, pebbleDir, layerFileName, layerYAML string) {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

layersDir := filepath.Join(pebbleDir, "layers")
err := os.MkdirAll(layersDir, 0o755)
if err != nil {
t.Fatalf("Cannot create layers directory: pipe: %v", err)
t.Fatalf("Cannot create layers directory: %v", err)
}

layerPath := filepath.Join(layersDir, layerFileName)
Expand All @@ -58,31 +56,15 @@ func createLayer(t *testing.T, pebbleDir string, layerFileName string, layerYAML
}
}

func createIdentitiesFile(t *testing.T, pebbleDir string, identitiesFileName string, identitiesYAML string) {
func pebbleRun(t *testing.T, pebbleDir string, args ...string) (<-chan servicelog.Entry, <-chan servicelog.Entry) {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

identitiesPath := filepath.Join(pebbleDir, identitiesFileName)
if err := os.WriteFile(identitiesPath, []byte(identitiesYAML), 0o755); err != nil {
t.Fatalf("Cannot create layers file: %v", err)
}
}

func pebbleRun(t *testing.T, pebbleDir string, args ...string) <-chan servicelog.Entry {
t.Helper()

logsCh := make(chan servicelog.Entry)
stdoutCh := make(chan servicelog.Entry)
stderrCh := make(chan servicelog.Entry)

cmd := exec.Command("../pebble", append([]string{"run"}, args...)...)
cmd.Env = append(os.Environ(), "PEBBLE="+pebbleDir)

t.Cleanup(func() {
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
t.Errorf("Error sending SIGINT/Ctrl+C to pebble: %v", err)
}
cmd.Wait()
})

stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
t.Fatalf("Cannot create stdout pipe: %v", err)
Expand All @@ -97,17 +79,26 @@ func pebbleRun(t *testing.T, pebbleDir string, args ...string) <-chan servicelog
t.Fatalf("Error starting 'pebble run': %v", err)
}

t.Cleanup(func() {
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
t.Errorf("Error sending SIGINT/Ctrl+C to pebble: %v", err)
}
cmd.Wait()
})

done := make(chan struct{})
go func() {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
defer close(logsCh)
defer close(stdoutCh)
defer close(stderrCh)

readLogs := func(parser *servicelog.Parser) {
readLogs := func(parser *servicelog.Parser, ch chan servicelog.Entry) {
for parser.Next() {
if err := parser.Err(); err != nil {
t.Errorf("Cannot parse Pebble logs: %v", err)
}
select {
case logsCh <- parser.Entry():
case ch <- parser.Entry():
case <-done:
return
}
Expand All @@ -119,125 +110,59 @@ func pebbleRun(t *testing.T, pebbleDir string, args ...string) <-chan servicelog
stderrParser := servicelog.NewParser(stderrPipe, 4*1024)
stdoutParser := servicelog.NewParser(stdoutPipe, 4*1024)

// Channel to signal completion and close logsCh
done := make(chan struct{})
defer close(done)

go readLogs(stderrParser)
go readLogs(stdoutParser)
go readLogs(stdoutParser, stdoutCh)
go readLogs(stderrParser, stderrCh)

// Wait for both parsers to finish
<-done
<-done
}()

return logsCh
return stdoutCh, stderrCh
}

func waitForLogs(logsCh <-chan servicelog.Entry, expectedLogs []string, timeout time.Duration) error {
receivedLogs := make(map[string]struct{})
func waitForLog(t *testing.T, logsCh <-chan servicelog.Entry, expectedLog string, timeout time.Duration) {
t.Helper()

timeoutCh := time.After(timeout)
for {
select {
case log, ok := <-logsCh:
if !ok {
return errors.New("channel closed before all expected logs were received")
t.Error("channel closed before all expected logs were received")
}

for _, expectedLog := range expectedLogs {
if _, ok := receivedLogs[expectedLog]; !ok && containsSubstring(log.Message, expectedLog) {
receivedLogs[expectedLog] = struct{}{}
break
}
}

allLogsReceived := true
for _, log := range expectedLogs {
if _, ok := receivedLogs[log]; !ok {
allLogsReceived = false
break
}
}

if allLogsReceived {
return nil
if strings.Contains(log.Message, expectedLog) {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
return
}

case <-timeoutCh:
missingLogs := []string{}
for _, log := range expectedLogs {
if _, ok := receivedLogs[log]; !ok {
missingLogs = append(missingLogs, log)
}
}
return errors.New("timed out waiting for log: " + strings.Join(missingLogs, ", "))
t.Fatalf("timed out after %v waiting for log %s", 3*time.Second, expectedLog)
}
}
}

func containsSubstring(s, substr string) bool {
return strings.Contains(s, substr)
}

func waitForServices(t *testing.T, pebbleDir string, expectedServices []string, timeout time.Duration) {
t.Helper()

for _, service := range expectedServices {
waitForService(t, pebbleDir, service, timeout)
}
}

func waitForService(t *testing.T, pebbleDir string, service string, timeout time.Duration) {
func waitForFile(t *testing.T, file string, timeout time.Duration) {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

serviceFilePath := filepath.Join(pebbleDir, service)
timeoutCh := time.After(timeout)
ticker := time.NewTicker(time.Millisecond)
for {
select {
case <-timeoutCh:
t.Errorf("timeout waiting for service %s", service)
return
t.Fatalf("timeout waiting for file %s", file)

case <-ticker.C:
stat, err := os.Stat(serviceFilePath)
stat, err := os.Stat(file)
if err == nil && stat.Mode().IsRegular() {
os.Remove(serviceFilePath)
os.Remove(file)
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
return
}
}
}
}

func isPortUsedByProcess(t *testing.T, port string, processName string) bool {
t.Helper()

conn, err := net.Listen("tcp", ":"+port)
if err == nil {
conn.Close()
return false
}
if conn != nil {
conn.Close()
}

cmd := exec.Command("lsof", "-i", ":"+port)
output, err := cmd.Output()
if err != nil {
t.Errorf("Error running lsof command: %v", err)
return false
}

outputStr := string(output)
if strings.Contains(outputStr, processName) {
return true
}

return false
}

func runPebbleCmdAndCheckOutput(t *testing.T, pebbleDir string, expectedOutput []string, args ...string) {
func runPebbleCommand(t *testing.T, pebbleDir string, args ...string) string {
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

cmd := exec.Command("../pebble", args...)
Expand All @@ -248,11 +173,5 @@ func runPebbleCmdAndCheckOutput(t *testing.T, pebbleDir string, expectedOutput [
t.Fatalf("error executing pebble command: %v", err)
}

outputStr := string(output)

for _, expected := range expectedOutput {
if !strings.Contains(outputStr, expected) {
t.Errorf("Expected output %s not found in command output", expected)
}
}
return string(output)
}
Loading
Loading