diff --git a/integration-tests/goss/windows/tests/service.goss.yaml b/integration-tests/goss/windows/tests/service.goss.yaml index f29e31286..1fe258ce4 100644 --- a/integration-tests/goss/windows/tests/service.goss.yaml +++ b/integration-tests/goss/windows/tests/service.goss.yaml @@ -3,6 +3,3 @@ service: MSDTC: enabled: true running: true - - # needs implementation - skip: true diff --git a/system/service_win.go b/system/service_win.go new file mode 100644 index 000000000..14e617cd0 --- /dev/null +++ b/system/service_win.go @@ -0,0 +1,54 @@ +package system + +import ( + "context" + "fmt" + "strings" + + "github.com/goss-org/goss/util" +) + +type ServiceWindows struct { + service string +} + +func NewServiceWindows(_ context.Context, service string, system *System, config util.Config) Service { + return &ServiceWindows{ + service: service, + } +} + +func (s *ServiceWindows) Service() string { + return s.service +} + +func (s *ServiceWindows) Exists() (bool, error) { + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("Get-Service -Name %s", s.service)) + cmd.Run() + if strings.Contains(cmd.Stderr.String(), "Cannot find any service with service name") { + return false, nil + } + return true, cmd.Err +} + +func (s *ServiceWindows) Enabled() (bool, error) { + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("$(Get-Service -Name %s).StartType", s.service), "-eq", "\"Automatic\"") + cmd.Run() + if strings.Contains(cmd.Stdout.String(), "True") { + return true, cmd.Err + } + return false, cmd.Err +} + +func (s *ServiceWindows) Running() (bool, error) { + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("$(Get-Service -Name %s).Status", s.service), "-eq", "\"Running\"") + cmd.Run() + if strings.Contains(cmd.Stdout.String(), "True") { + return true, cmd.Err + } + return false, cmd.Err +} + +func (s *ServiceWindows) RunLevels() ([]string, error) { + return nil, nil +} diff --git a/system/system.go b/system/system.go index 6c6083558..287717903 100644 --- a/system/system.go +++ b/system/system.go @@ -5,6 +5,7 @@ import ( "context" "os" "os/exec" + "runtime" "strconv" "sync" @@ -109,6 +110,8 @@ func (sys *System) detectService() { sys.NewService = NewServiceSystemdLegacy case "alpineinit": sys.NewService = NewAlpineServiceInit + case "windows": + sys.NewService = NewServiceWindows default: sys.NewService = NewServiceInit } @@ -166,6 +169,9 @@ func DetectService() string { } return "systemd" } + if runtime.GOOS == "windows" { + return "windows" + } // Centos Docker container doesn't run systemd, so we detect it or use init. switch DetectDistro() { case "ubuntu": diff --git a/system/system_test.go b/system/system_test.go index f8644915c..b0622346c 100644 --- a/system/system_test.go +++ b/system/system_test.go @@ -37,7 +37,7 @@ func TestDetectService(t *testing.T) { t.Parallel() testOutputs( DetectService, - []string{"systemd", "init", "alpineinit", "upstart", ""}, + []string{"systemd", "init", "alpineinit", "upstart", "windows"}, t, ) }