Adding Opkg support for system interactions

This commit is contained in:
Philippe Caseiro 2018-10-26 15:16:55 +02:00
parent db024d3c05
commit 6e84372169
2 changed files with 37 additions and 2 deletions

View File

@ -28,8 +28,7 @@ func NewSystemWithExecutor(exec Executor) *System {
}
// sysRun, private method to run the UCI command
func (s *System) sysRun(param ...string) *SysAction {
cmd := param[0]
func (s *System) sysRun(cmd string, param ...string) *SysAction {
res := s.exec.Run(cmd, param...)
return &SysAction{res, fmt.Sprintf("%s %s", cmd, param)}
}
@ -39,3 +38,9 @@ func (s *System) Service(name string, action string) *SysAction {
cmd := fmt.Sprintf("/etc/init.d/%s", name)
return s.sysRun(cmd, action)
}
// Opkg provides an interface to system command opkg
// useful for package installation or system update
func (s *System) Opkg(params ...string) *SysAction {
return s.sysRun("opkg", params...)
}

View File

@ -21,3 +21,33 @@ func TestService(t *testing.T) {
t.Error("Stderr is not empty ...")
}
}
func TestOpkg(t *testing.T) {
exec := createMockExecutor("", "", 0)
sys := NewSystemWithExecutor(exec)
res := sys.Opkg("update")
if res.ReturnCode != 0 {
t.Error("Bad Return Code !")
}
if res.Stdout != "" {
fmt.Printf("[%s] - ", res.Stdout)
t.Error("Stdout is not empty ...")
}
if res.Stderr != "" {
fmt.Printf("[%s] - ", res.Stdout)
t.Error("Stderr is not empty ...")
}
res = sys.Opkg("install", "ip")
if res.ReturnCode != 0 {
t.Error("Bad Return Code !")
}
if res.Stdout != "" {
fmt.Printf("[%s] - ", res.Stdout)
t.Error("Stdout is not empty ...")
}
if res.Stderr != "" {
fmt.Printf("[%s] - ", res.Stdout)
t.Error("Stderr is not empty ...")
}
}