2021-06-22 01:17:30 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2021-07-19 15:02:11 +02:00
|
|
|
"os"
|
2021-06-22 01:17:30 +02:00
|
|
|
"testing"
|
2021-08-01 19:06:01 +02:00
|
|
|
|
|
|
|
"github.com/kardianos/service"
|
2021-06-22 01:17:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_ServiceInstallCMD(t *testing.T) {
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
rootCmd.SetOut(b)
|
|
|
|
rootCmd.SetErr(b)
|
2021-06-25 11:08:16 +02:00
|
|
|
rootCmd.SetArgs([]string{
|
|
|
|
"service",
|
|
|
|
"install",
|
|
|
|
"--config",
|
|
|
|
"/tmp/config.json",
|
|
|
|
})
|
2021-06-22 01:17:30 +02:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedMSG := "Wiretrustee service has been installed"
|
|
|
|
if string(out) != expectedMSG {
|
|
|
|
t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ServiceStartCMD(t *testing.T) {
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
rootCmd.SetOut(b)
|
|
|
|
rootCmd.SetErr(b)
|
|
|
|
rootCmd.SetArgs([]string{"service", "start"})
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedMSG := "Wiretrustee service has been started"
|
|
|
|
if string(out) != expectedMSG {
|
|
|
|
t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ServiceRunCMD(t *testing.T) {
|
2021-07-19 15:02:11 +02:00
|
|
|
configFilePath := "/tmp/config.json"
|
|
|
|
if _, err := os.Stat(configFilePath); err == nil {
|
|
|
|
e := os.Remove(configFilePath)
|
|
|
|
if e != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 01:17:30 +02:00
|
|
|
rootCmd.SetArgs([]string{
|
2021-06-25 11:08:16 +02:00
|
|
|
"--config",
|
2021-07-19 15:02:11 +02:00
|
|
|
configFilePath,
|
2021-06-22 01:17:30 +02:00
|
|
|
})
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.ResetFlags()
|
|
|
|
rootCmd.SetArgs([]string{"service", "start"})
|
|
|
|
err = rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s, err := newSVC(&program{}, newSVCConfig())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
status, err := s.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != service.StatusRunning {
|
|
|
|
t.Fatalf("expected running status of \"%d\" got \"%d\"", service.StatusRunning, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
/*func Test_ServiceStopCMD(t *testing.T) {
|
2021-06-22 01:17:30 +02:00
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
rootCmd.SetOut(b)
|
|
|
|
rootCmd.SetErr(b)
|
|
|
|
rootCmd.SetArgs([]string{"service", "stop"})
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMSG := "Wiretrustee service has been stopped"
|
|
|
|
if string(out) != expectedMSG {
|
|
|
|
t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out))
|
|
|
|
}
|
2021-07-22 10:28:00 +02:00
|
|
|
}*/
|
2021-06-22 01:17:30 +02:00
|
|
|
|
|
|
|
func Test_ServiceUninstallCMD(t *testing.T) {
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
rootCmd.SetOut(b)
|
|
|
|
rootCmd.SetErr(b)
|
|
|
|
rootCmd.SetArgs([]string{"service", "uninstall"})
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedMSG := "Wiretrustee has been uninstalled"
|
|
|
|
if string(out) != expectedMSG {
|
|
|
|
t.Fatalf("expected \"%s\" got \"%s\"", expectedMSG, string(out))
|
|
|
|
}
|
|
|
|
}
|