Scale tests

This commit is contained in:
Berger Eugene 2023-07-08 02:01:48 +03:00
parent 774976406b
commit 9fb6eaa132
7 changed files with 198 additions and 17 deletions

View File

@ -0,0 +1,25 @@
version: "0.5"
processes:
process1:
command: "echo process1"
depends_on:
process2:
condition: process_completed_successfully
process2:
command: "echo process2"
availability:
restart: "on_failure"
depends_on:
process3:
condition: process_completed_successfully
process3:
command: "echo process3"
availability:
restart: "on_failure"
backoff_seconds: 2
# depends_on:
# process1:
# condition: process_completed_successfully

View File

@ -0,0 +1,23 @@
version: "0.5"
processes:
process1:
command: "echo process1 ; sleep 2"
replicas: 2
depends_on:
process2:
condition: process_completed_successfully
process2:
command: "echo process2; sleep 2"
availability:
restart: "on_failure"
depends_on:
process3:
condition: process_completed_successfully
process3:
command: "echo process3; sleep 2"
availability:
restart: "on_failure"
backoff_seconds: 2

View File

@ -45,6 +45,7 @@ type Process struct {
logger pclog.PcLogger
command Commander
done bool
timeMutex sync.Mutex
startTime time.Time
liveProber *health.Prober
readyProber *health.Prober
@ -96,8 +97,10 @@ func (p *Process) run() int {
return 1
}
p.startTime = time.Now()
p.setStartTime(time.Now())
p.stateMtx.Lock()
p.procState.Pid = p.command.Pid()
p.stateMtx.Unlock()
log.Info().Msgf("%s started", p.getName())
p.startProbes()
@ -331,13 +334,24 @@ func (p *Process) getCommand() string {
func (p *Process) updateProcState() {
if p.isRunning() {
dur := time.Since(p.startTime)
dur := time.Since(p.getStartTime())
p.procState.SystemTime = durationToString(dur)
p.procState.Age = dur
p.procState.IsRunning = true
p.procState.Name = p.getName()
}
}
func (p *Process) setStartTime(startTime time.Time) {
p.timeMutex.Lock()
defer p.timeMutex.Unlock()
p.startTime = startTime
}
func (p *Process) getStartTime() time.Time {
p.timeMutex.Lock()
defer p.timeMutex.Unlock()
return p.startTime
}
func (p *Process) handleInput(pipe io.WriteCloser) {
reader := bufio.NewReader(os.Stdin)

View File

@ -5,8 +5,8 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func getFixtures() []string {
@ -20,17 +20,6 @@ func getFixtures() []string {
func TestSystem_TestFixtures(t *testing.T) {
fixtures := getFixtures()
for _, fixture := range fixtures {
if strings.Contains(fixture, "process-compose-with-log.yaml") {
//there is a dedicated test for that TestSystem_TestComposeWithLog
continue
}
if strings.Contains(fixture, "process-compose-chain-exit.yaml") {
//there is a dedicated test for that TestSystem_TestComposeWithLog
continue
}
t.Run(fixture, func(t *testing.T) {
project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture},
@ -50,7 +39,7 @@ func TestSystem_TestFixtures(t *testing.T) {
}
func TestSystem_TestComposeWithLog(t *testing.T) {
fixture := filepath.Join("..", "..", "fixtures", "process-compose-with-log.yaml")
fixture := filepath.Join("..", "..", "fixtures-code", "process-compose-with-log.yaml")
t.Run(fixture, func(t *testing.T) {
project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture},
@ -83,7 +72,7 @@ func TestSystem_TestComposeWithLog(t *testing.T) {
}
func TestSystem_TestComposeChain(t *testing.T) {
fixture := filepath.Join("..", "..", "fixtures", "process-compose-chain.yaml")
fixture := filepath.Join("..", "..", "fixtures-code", "process-compose-chain.yaml")
t.Run(fixture, func(t *testing.T) {
project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture},
@ -119,7 +108,7 @@ func TestSystem_TestComposeChain(t *testing.T) {
}
func TestSystem_TestComposeChainExit(t *testing.T) {
fixture := filepath.Join("..", "..", "fixtures", "process-compose-chain-exit.yaml")
fixture := filepath.Join("..", "..", "fixtures-code", "process-compose-chain-exit.yaml")
t.Run(fixture, func(t *testing.T) {
project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture},
@ -140,3 +129,133 @@ func TestSystem_TestComposeChainExit(t *testing.T) {
}
})
}
//func TestSystem_TestComposeCircular(t *testing.T) {
// fixture := filepath.Join("..", "..", "fixtures", "process-compose-circular.yaml")
// t.Run(fixture, func(t *testing.T) {
// project, err := loader.Load(&loader.LoaderOptions{
// FileNames: []string{fixture},
// })
// if err != nil {
// t.Errorf(err.Error())
// return
// }
// runner, err := NewProjectRunner(project, []string{}, false)
// if err != nil {
// t.Errorf(err.Error())
// return
// }
// exitCode := runner.Run()
// want := 42
// if want != exitCode {
// t.Errorf("Project.Run() = %v, want %v", exitCode, want)
// }
// })
//}
func TestSystem_TestComposeScale(t *testing.T) {
fixture := filepath.Join("..", "..", "fixtures-code", "process-compose-scale.yaml")
t.Run(fixture, func(t *testing.T) {
project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture},
})
if err != nil {
t.Errorf(err.Error())
return
}
runner, err := NewProjectRunner(project, []string{}, false)
if err != nil {
t.Errorf(err.Error())
return
}
go runner.Run()
time.Sleep(200 * time.Millisecond)
states, err := runner.GetProcessesState()
if err != nil {
t.Errorf(err.Error())
return
}
want := 4
if len(states.States) != want {
t.Errorf("len(states.States) = %d, want %d", len(states.States), want)
}
//scale to 10
err = runner.ScaleProcess("process1-0", 10)
if err != nil {
t.Errorf(err.Error())
return
}
states, err = runner.GetProcessesState()
if err != nil {
t.Errorf(err.Error())
return
}
want = 12
if len(states.States) != want {
t.Errorf("len(states.States) = %d, want %d", len(states.States), want)
}
//check scale to 0 - should fail
err = runner.ScaleProcess("process1-00", 0)
if err == nil {
t.Errorf("should fail on scale 0")
return
}
//scale to 1 and new name with -00
err = runner.ScaleProcess("process1-00", 1)
if err != nil {
t.Errorf(err.Error())
return
}
states, err = runner.GetProcessesState()
if err != nil {
t.Errorf(err.Error())
return
}
want = 3
if len(states.States) != want {
t.Errorf("len(states.States) = %d, want %d", len(states.States), want)
}
//scale to 5 process2
err = runner.ScaleProcess("process2", 5)
if err != nil {
t.Errorf(err.Error())
return
}
states, err = runner.GetProcessesState()
if err != nil {
t.Errorf(err.Error())
return
}
want = 7
if len(states.States) != want {
t.Errorf("len(states.States) = %d, want %d", len(states.States), want)
}
//check no change
err = runner.ScaleProcess("process2-0", 5)
if err != nil {
t.Errorf(err.Error())
return
}
states, err = runner.GetProcessesState()
if err != nil {
t.Errorf(err.Error())
return
}
want = 7
if len(states.States) != want {
t.Errorf("len(states.States) = %d, want %d", len(states.States), want)
}
//wrong process name
err = runner.ScaleProcess("process2-00", 5)
if err == nil {
t.Errorf("should fail on wrong process name")
return
}
})
}