Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions integration-tests/goss/windows/tests/service.goss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ service:
MSDTC:
enabled: true
running: true

# needs implementation
skip: true
54 changes: 54 additions & 0 deletions system/service_win.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"os"
"os/exec"
"runtime"
"strconv"
"sync"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down