svc_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package svc_test
  6. import (
  7. "fmt"
  8. "io/ioutil"
  9. "math/rand"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. "testing"
  15. "time"
  16. "golang.org/x/sys/windows/svc"
  17. "golang.org/x/sys/windows/svc/mgr"
  18. )
  19. func getState(t *testing.T, s *mgr.Service) svc.State {
  20. status, err := s.Query()
  21. if err != nil {
  22. t.Fatalf("Query(%s) failed: %s", s.Name, err)
  23. }
  24. return status.State
  25. }
  26. func testState(t *testing.T, s *mgr.Service, want svc.State) {
  27. have := getState(t, s)
  28. if have != want {
  29. t.Fatalf("%s state is=%d want=%d", s.Name, have, want)
  30. }
  31. }
  32. func waitState(t *testing.T, s *mgr.Service, want svc.State) {
  33. for i := 0; ; i++ {
  34. have := getState(t, s)
  35. if have == want {
  36. return
  37. }
  38. if i > 10 {
  39. t.Fatalf("%s state is=%d, waiting timeout", s.Name, have)
  40. }
  41. time.Sleep(300 * time.Millisecond)
  42. }
  43. }
  44. func TestExample(t *testing.T) {
  45. if testing.Short() {
  46. t.Skip("skipping test in short mode - it modifies system services")
  47. }
  48. const name = "myservice"
  49. m, err := mgr.Connect()
  50. if err != nil {
  51. t.Fatalf("SCM connection failed: %s", err)
  52. }
  53. defer m.Disconnect()
  54. dir, err := ioutil.TempDir("", "svc")
  55. if err != nil {
  56. t.Fatalf("failed to create temp directory: %v", err)
  57. }
  58. defer os.RemoveAll(dir)
  59. exepath := filepath.Join(dir, "a.exe")
  60. o, err := exec.Command("go", "build", "-o", exepath, "golang.org/x/sys/windows/svc/example").CombinedOutput()
  61. if err != nil {
  62. t.Fatalf("failed to build service program: %v\n%v", err, string(o))
  63. }
  64. s, err := m.OpenService(name)
  65. if err == nil {
  66. err = s.Delete()
  67. if err != nil {
  68. s.Close()
  69. t.Fatalf("Delete failed: %s", err)
  70. }
  71. s.Close()
  72. }
  73. s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: "my service"}, "is", "auto-started")
  74. if err != nil {
  75. t.Fatalf("CreateService(%s) failed: %v", name, err)
  76. }
  77. defer s.Close()
  78. args := []string{"is", "manual-started", fmt.Sprintf("%d", rand.Int())}
  79. testState(t, s, svc.Stopped)
  80. err = s.Start(args...)
  81. if err != nil {
  82. t.Fatalf("Start(%s) failed: %s", s.Name, err)
  83. }
  84. waitState(t, s, svc.Running)
  85. time.Sleep(1 * time.Second)
  86. // testing deadlock from issues 4.
  87. _, err = s.Control(svc.Interrogate)
  88. if err != nil {
  89. t.Fatalf("Control(%s) failed: %s", s.Name, err)
  90. }
  91. _, err = s.Control(svc.Interrogate)
  92. if err != nil {
  93. t.Fatalf("Control(%s) failed: %s", s.Name, err)
  94. }
  95. time.Sleep(1 * time.Second)
  96. _, err = s.Control(svc.Stop)
  97. if err != nil {
  98. t.Fatalf("Control(%s) failed: %s", s.Name, err)
  99. }
  100. waitState(t, s, svc.Stopped)
  101. err = s.Delete()
  102. if err != nil {
  103. t.Fatalf("Delete failed: %s", err)
  104. }
  105. out, err := exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='myservice']]]", "/rd:true", "/c:10").CombinedOutput()
  106. if err != nil {
  107. t.Fatalf("wevtutil failed: %v\n%v", err, string(out))
  108. }
  109. want := strings.Join(append([]string{name}, args...), "-")
  110. // Test context passing (see servicemain in sys_386.s and sys_amd64.s).
  111. want += "-123456"
  112. if !strings.Contains(string(out), want) {
  113. t.Errorf("%q string does not contain %q", string(out), want)
  114. }
  115. }