Skip to content

Commit

Permalink
feat: foreman testing
Browse files Browse the repository at this point in the history
Signed-off-by: nabil salah <[email protected]>
  • Loading branch information
Nabil-Salah committed Aug 22, 2024
1 parent 74ff026 commit ec620ec
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ import (
func main() {
f, err := foreman.InitForeman()
if err != nil {
fmt.Println("err")
fmt.Println(err)
return
}
err = f.RunServices()
if err != nil {

fmt.Println("err")
fmt.Println(err)
}
// serviceCmd := exec.Command("bash", "-c", "ping -c 1 google.com")
// serviceCmd.SysProcAttr = &syscall.SysProcAttr{
// Setpgid: true,
// Pgid: 0,
// }
// serviceCmd.Start()
// x := serviceCmd.Process.Pid
// fmt.Printf("[%d] process started [%v]\n", x, time.Now())
}
18 changes: 14 additions & 4 deletions pkg/foremanHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"syscall"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -54,19 +55,27 @@ func (foreman *Foreman)parseProcfile () error {
newInfo.runOnce = runOnce
}

if checks, ok := info["checks"].(map[string]interface{}); ok {
if checks, ok := info["checks"].(map[interface{}]interface{}); ok {
if cmd, ok := checks["cmd"].(string); ok {
newInfo.checks.cmd = cmd
}
if tcpPorts, ok := checks["tcp_ports"].([]interface{}); ok {
for _, ports := range tcpPorts {
if port, ok := ports.(int); ok {
newInfo.checks.tcpPorts = append(newInfo.checks.tcpPorts, strconv.Itoa(port))
continue
}
if port, ok := ports.(string); ok {
newInfo.checks.tcpPorts = append(newInfo.checks.tcpPorts, port)
}
}
}
if udpPorts, ok := checks["udp_ports"].([]interface{}); ok {
for _, ports := range udpPorts {
if port, ok := ports.(int); ok {
newInfo.checks.udpPorts = append(newInfo.checks.udpPorts, strconv.Itoa(port))
continue
}
if port, ok := ports.(string); ok {
newInfo.checks.udpPorts = append(newInfo.checks.udpPorts, port)
}
Expand Down Expand Up @@ -113,10 +122,11 @@ func (foreman *Foreman) runService(serviceName string) error{
service := foreman.services[serviceName]
serviceCmd := exec.Command("bash", "-c", service.info.cmd)
serviceCmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
Setpgid: true,
Pgid: 0,
}
err := serviceCmd.Start()

if err != nil {
if !service.info.runOnce {
return foreman.runService(serviceName)
Expand All @@ -130,8 +140,8 @@ func (foreman *Foreman) runService(serviceName string) error{
}
return err
}
service.id = serviceCmd.SysProcAttr.Pgid
fmt.Printf("[%s] process started\n", service.name)
service.id = serviceCmd.Process.Pid
fmt.Printf("[%d] process [%s] started\n", service.id, service.name)
foreman.services[serviceName] = service
return nil
}
Expand Down
69 changes: 44 additions & 25 deletions pkg/foreman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,48 @@ func TestForeman(t *testing.T) {
// Create necessary files for testing
tempDir := t.TempDir()
const (
valid_procfile = `web:
cmd: "echo Starting web server"
deps: []`
valid_procfile = `app1:
cmd: ping -c 1 google.com
checks:
cmd: sleep 3
deps:
- app2
app2:
cmd: ping -c 10 yahoo.com
run_once: true
checks:
cmd: sleep 4
tcp_ports: [8080]
udp_ports: [80]
app3:
run_once: true
cmd: sleep 10
checks:
tcp_ports: ["8090"]
udp_ports: ["90"]
deps:
- app1`
cycle_procfile = `web:
cmd: "echo Starting web server"
deps: ["db"]
db:
cmd: "echo Starting database"
deps: ["web"]`
cycle_procfiles2 = `app1:
cmd: ping -c 1 google.com
run_once: true
checks:
cmd: sleep 3
deps:
- app2
app2:
cmd: ping -c 10 yahoo.com
run_once: true
deps:
- app1
`
invalid_command_procfile = `web:
cmd: "nonexistentcommand"
run_once: true`
Expand All @@ -30,17 +62,6 @@ db:
deps: ["db"
db:
cmd: "echo Starting database"`
// port_conflict_procfile = `web1:
// cmd: "nc -l 8080"
// run_once: true
// checks:
// tcp_ports: ["8080"]

// web2:
// cmd: "nc -l 8080"
// run_once: true
// checks:
// tcp_ports: ["8080"]`
)

validProcfilePath := filepath.Join(tempDir, "valid_procfile.yaml")
Expand All @@ -57,13 +78,11 @@ db:

invalidformedProcfilePath := filepath.Join(tempDir, "malformed_procfile.yaml")
err = os.WriteFile(invalidformedProcfilePath, []byte(invalid_format_procfile), 0644)
assert.NoError(t, err)
assert.NoError(t, err)

// portConflictProcfilePath := filepath.Join(tempDir, "port_conflict_procfile.yaml")
// err = os.WriteFile(portConflictProcfilePath, []byte(port_conflict_procfile), 0644)
//assert.NoError(t, err)


cycleProcfile2Path := filepath.Join(tempDir, "malformed_procfile.yaml")
err = os.WriteFile(invalidformedProcfilePath, []byte(cycle_procfiles2), 0644)
assert.NoError(t, err)

// Define the test cases
tests := []struct {
Expand All @@ -81,6 +100,11 @@ db:
procfile: cycleProcfilePath,
expectErr: true,
},
{
name: "DependenciesFormCycle another example",
procfile: cycleProcfile2Path,
expectErr: true,
},
{
name: "InvalidCommand",
procfile: invalidCommandProcfilePath,
Expand All @@ -96,11 +120,6 @@ db:
procfile: invalidformedProcfilePath,
expectErr: true,
},
// {
// name: "PortConflict",
// procfile: portConflictProcfilePath,
// expectErr: true,
// },
}

for _, test := range tests {
Expand Down
29 changes: 3 additions & 26 deletions procfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,9 @@ app1:
checks:
cmd: sleep 3
deps:
- redis6010
- app2
app2:
cmd: ping -c 50 yahoo.com
cmd: ping -c 10 yahoo.com
run_once: true
checks:
cmd: sleep 4
deps:
- redis8080

app3:
run_once: true
cmd: sleep 10
deps:
- app1

redis6010:
cmd: redis-server --port 6010
run_once: true
checks:
cmd: redis-cli -p 6010 ping
tcp_ports: [6010]

redis8080:
cmd: redis-server --port 8080
run_once: true
checks:
cmd: redis-cli -p 8080 ping
tcp_ports: [8080]
udp_ports: [80]
- app1

0 comments on commit ec620ec

Please sign in to comment.