mirror of
https://github.com/F1bonacc1/process-compose.git
synced 2024-08-17 15:30:30 +03:00
Scale tests
This commit is contained in:
parent
774976406b
commit
9fb6eaa132
25
fixtures-code/process-compose-circular.yaml
Normal file
25
fixtures-code/process-compose-circular.yaml
Normal 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
|
||||||
|
|
23
fixtures-code/process-compose-scale.yaml
Normal file
23
fixtures-code/process-compose-scale.yaml
Normal 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
|
||||||
|
|
@ -45,6 +45,7 @@ type Process struct {
|
|||||||
logger pclog.PcLogger
|
logger pclog.PcLogger
|
||||||
command Commander
|
command Commander
|
||||||
done bool
|
done bool
|
||||||
|
timeMutex sync.Mutex
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
liveProber *health.Prober
|
liveProber *health.Prober
|
||||||
readyProber *health.Prober
|
readyProber *health.Prober
|
||||||
@ -96,8 +97,10 @@ func (p *Process) run() int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
p.startTime = time.Now()
|
p.setStartTime(time.Now())
|
||||||
|
p.stateMtx.Lock()
|
||||||
p.procState.Pid = p.command.Pid()
|
p.procState.Pid = p.command.Pid()
|
||||||
|
p.stateMtx.Unlock()
|
||||||
log.Info().Msgf("%s started", p.getName())
|
log.Info().Msgf("%s started", p.getName())
|
||||||
|
|
||||||
p.startProbes()
|
p.startProbes()
|
||||||
@ -331,13 +334,24 @@ func (p *Process) getCommand() string {
|
|||||||
|
|
||||||
func (p *Process) updateProcState() {
|
func (p *Process) updateProcState() {
|
||||||
if p.isRunning() {
|
if p.isRunning() {
|
||||||
dur := time.Since(p.startTime)
|
dur := time.Since(p.getStartTime())
|
||||||
p.procState.SystemTime = durationToString(dur)
|
p.procState.SystemTime = durationToString(dur)
|
||||||
p.procState.Age = dur
|
p.procState.Age = dur
|
||||||
p.procState.IsRunning = true
|
p.procState.IsRunning = true
|
||||||
p.procState.Name = p.getName()
|
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) {
|
func (p *Process) handleInput(pipe io.WriteCloser) {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getFixtures() []string {
|
func getFixtures() []string {
|
||||||
@ -20,17 +20,6 @@ func getFixtures() []string {
|
|||||||
func TestSystem_TestFixtures(t *testing.T) {
|
func TestSystem_TestFixtures(t *testing.T) {
|
||||||
fixtures := getFixtures()
|
fixtures := getFixtures()
|
||||||
for _, fixture := range fixtures {
|
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) {
|
t.Run(fixture, func(t *testing.T) {
|
||||||
project, err := loader.Load(&loader.LoaderOptions{
|
project, err := loader.Load(&loader.LoaderOptions{
|
||||||
FileNames: []string{fixture},
|
FileNames: []string{fixture},
|
||||||
@ -50,7 +39,7 @@ func TestSystem_TestFixtures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSystem_TestComposeWithLog(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) {
|
t.Run(fixture, func(t *testing.T) {
|
||||||
project, err := loader.Load(&loader.LoaderOptions{
|
project, err := loader.Load(&loader.LoaderOptions{
|
||||||
FileNames: []string{fixture},
|
FileNames: []string{fixture},
|
||||||
@ -83,7 +72,7 @@ func TestSystem_TestComposeWithLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSystem_TestComposeChain(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) {
|
t.Run(fixture, func(t *testing.T) {
|
||||||
project, err := loader.Load(&loader.LoaderOptions{
|
project, err := loader.Load(&loader.LoaderOptions{
|
||||||
FileNames: []string{fixture},
|
FileNames: []string{fixture},
|
||||||
@ -119,7 +108,7 @@ func TestSystem_TestComposeChain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSystem_TestComposeChainExit(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) {
|
t.Run(fixture, func(t *testing.T) {
|
||||||
project, err := loader.Load(&loader.LoaderOptions{
|
project, err := loader.Load(&loader.LoaderOptions{
|
||||||
FileNames: []string{fixture},
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user