From 8941afb210a0f7f78b2fec352e47c596e75f0bf7 Mon Sep 17 00:00:00 2001 From: Berger Eugene Date: Sun, 3 Mar 2024 01:01:53 +0200 Subject: [PATCH] Add shutdown order test --- .../process-compose-shutdown-inorder.yaml | 14 ++++----- src/app/system_test.go | 30 ++++++++++++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/fixtures-code/process-compose-shutdown-inorder.yaml b/fixtures-code/process-compose-shutdown-inorder.yaml index 3ef791f..4c3d4cf 100644 --- a/fixtures-code/process-compose-shutdown-inorder.yaml +++ b/fixtures-code/process-compose-shutdown-inorder.yaml @@ -6,26 +6,24 @@ log_length: 1000 processes: procA: command: | + trap 'echo "A: exit"' SIGTERM echo "A: starting" + sleep 3 procB: command: | + trap 'echo "B: exit"' SIGTERM echo "B: starting" - is_daemon: true - shutdown: - command: | - echo "B: exiting" + sleep 3 depends_on: procA: condition: process_started procC: command: | + trap 'echo "C: exit"' SIGTERM echo "C: starting" - isDaemon: true - shutdown: - command: | - echo "C: exiting" + sleep 3 depends_on: procB: condition: process_started diff --git a/src/app/system_test.go b/src/app/system_test.go index df630ab..10d7dbc 100644 --- a/src/app/system_test.go +++ b/src/app/system_test.go @@ -1,10 +1,14 @@ package app import ( + "bufio" "github.com/f1bonacc1/process-compose/src/loader" + "github.com/f1bonacc1/process-compose/src/types" "os" "path/filepath" "reflect" + "slices" + "strings" "testing" "time" ) @@ -410,6 +414,7 @@ func TestSystem_TestProcListToRun(t *testing.T) { func TestSystem_TestProcListShutsDownInOrder(t *testing.T) { fixture1 := filepath.Join("..", "..", "fixtures-code", "process-compose-shutdown-inorder.yaml") t.Run("Single Proc with deps", func(t *testing.T) { + project, err := loader.Load(&loader.LoaderOptions{ FileNames: []string{fixture1}, }) @@ -436,7 +441,16 @@ func TestSystem_TestProcListShutsDownInOrder(t *testing.T) { t.Errorf("process %s is disabled", name) } } - + file, err := os.CreateTemp("/tmp", "pc_log.*.log") + defer os.Remove(file.Name()) + project.LogLocation = file.Name() + project.LoggerConfig = &types.LoggerConfig{ + FieldsOrder: []string{"message"}, + DisableJSON: true, + TimestampFormat: "", + NoMetadata: true, + FlushEachLine: true, + } go runner.Run() time.Sleep(10 * time.Millisecond) states, err := runner.GetProcessesState() @@ -470,5 +484,19 @@ func TestSystem_TestProcListShutsDownInOrder(t *testing.T) { if runningProcesses != want { t.Errorf("len(runner.runningProcesses) = %d, want %d", len(runner.runningProcesses), want) } + //read file and validate the shutdown order + scanner := bufio.NewScanner(file) + order := make([]string, 0) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "exit") { + order = append(order, line) + } + } + wantOrder := []string{"C: exit", "B: exit", "A: exit"} + if !slices.Equal(wantOrder, order) { + t.Errorf("content = %v, want %v", order, wantOrder) + return + } }) }