| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package otelcol |
| 7 |
|
| 8 |
import ( |
| 9 |
"encoding/xml" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"path/filepath" |
| 14 |
"runtime" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
"golang.org/x/sys/windows/svc" |
| 20 |
"golang.org/x/sys/windows/svc/mgr" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
collectorServiceName = "otelcorecol" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func TestCollectorAsService(t *testing.T) { |
| 42 |
collector_executable, err := filepath.Abs(filepath.Join("..", "bin", fmt.Sprintf("otelcorecol_windows_%s", runtime.GOARCH))) |
| 43 |
require.NoError(t, err) |
| 44 |
_, err = os.Stat(collector_executable) |
| 45 |
require.NoError(t, err) |
| 46 |
|
| 47 |
scm, err := mgr.Connect() |
| 48 |
require.NoError(t, err) |
| 49 |
defer scm.Disconnect() |
| 50 |
|
| 51 |
service, err := scm.OpenService(collectorServiceName) |
| 52 |
require.NoError(t, err) |
| 53 |
defer service.Close() |
| 54 |
|
| 55 |
tests := []struct { |
| 56 |
name string |
| 57 |
configFile string |
| 58 |
expectStartFailure bool |
| 59 |
customSetup func(*testing.T) |
| 60 |
customValidation func(*testing.T) |
| 61 |
}{ |
| 62 |
{ |
| 63 |
name: "Default", |
| 64 |
configFile: filepath.Join("..", "examples", "local", "otel-config.yaml"), |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "ConfigFileNotFound", |
| 68 |
configFile: filepath.Join(".", "non", "existent", "otel-config.yaml"), |
| 69 |
expectStartFailure: true, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "LogToFile", |
| 73 |
configFile: filepath.Join(".", "testdata", "otelcol-log-to-file.yaml"), |
| 74 |
customSetup: func(t *testing.T) { |
| 75 |
|
| 76 |
programDataPath := os.Getenv("ProgramData") |
| 77 |
logsPath := filepath.Join(programDataPath, "OpenTelemetry", "Collector", "Logs") |
| 78 |
err := os.MkdirAll(logsPath, os.ModePerm) |
| 79 |
require.NoError(t, err) |
| 80 |
|
| 81 |
logFilePath := filepath.Join(logsPath, "otelcol.log") |
| 82 |
err = os.Remove(logFilePath) |
| 83 |
if err != nil && !os.IsNotExist(err) { |
| 84 |
require.NoError(t, err) |
| 85 |
} |
| 86 |
}, |
| 87 |
customValidation: func(t *testing.T) { |
| 88 |
|
| 89 |
programDataPath := os.Getenv("ProgramData") |
| 90 |
logsPath := filepath.Join(programDataPath, "OpenTelemetry", "Collector", "Logs") |
| 91 |
logFilePath := filepath.Join(logsPath, "otelcol.log") |
| 92 |
fileinfo, err := os.Stat(logFilePath) |
| 93 |
require.NoError(t, err) |
| 94 |
require.NotEmpty(t, fileinfo.Size()) |
| 95 |
}, |
| 96 |
}, |
| 97 |
} |
| 98 |
|
| 99 |
for _, tt := range tests { |
| 100 |
t.Run(tt.name, func(t *testing.T) { |
| 101 |
serviceConfig, err := service.Config() |
| 102 |
require.NoError(t, err) |
| 103 |
|
| 104 |
|
| 105 |
fullConfigPath, err := filepath.Abs(tt.configFile) |
| 106 |
require.NoError(t, err) |
| 107 |
|
| 108 |
serviceConfig.BinaryPathName = fmt.Sprintf("\"%s\" --config \"%s\"", collector_executable, fullConfigPath) |
| 109 |
err = service.UpdateConfig(serviceConfig) |
| 110 |
require.NoError(t, err) |
| 111 |
|
| 112 |
if tt.customSetup != nil { |
| 113 |
tt.customSetup(t) |
| 114 |
} |
| 115 |
|
| 116 |
startTime := time.Now() |
| 117 |
|
| 118 |
err = service.Start() |
| 119 |
require.NoError(t, err) |
| 120 |
|
| 121 |
expectedState := svc.Running |
| 122 |
if tt.expectStartFailure { |
| 123 |
expectedState = svc.Stopped |
| 124 |
} else { |
| 125 |
defer func() { |
| 126 |
_, err = service.Control(svc.Stop) |
| 127 |
require.NoError(t, err) |
| 128 |
|
| 129 |
require.Eventually(t, func() bool { |
| 130 |
status, _ := service.Query() |
| 131 |
return status.State == svc.Stopped |
| 132 |
}, 10*time.Second, 500*time.Millisecond) |
| 133 |
}() |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
require.Eventually(t, func() bool { |
| 138 |
status, _ := service.Query() |
| 139 |
return status.State == expectedState |
| 140 |
}, 10*time.Second, 500*time.Millisecond) |
| 141 |
|
| 142 |
if tt.customValidation != nil { |
| 143 |
tt.customValidation(t) |
| 144 |
} else { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
cmd := exec.Command("wevtutil.exe", "qe", "Application", "/c:1", "/rd:true", "/f:RenderedXml", "/q:*[System[Provider[@Name='otelcorecol']]]") |
| 149 |
out, err := cmd.CombinedOutput() |
| 150 |
require.NoError(t, err) |
| 151 |
|
| 152 |
var e Event |
| 153 |
require.NoError(t, xml.Unmarshal([]byte(out), &e)) |
| 154 |
|
| 155 |
eventTime, err := time.Parse("2006-01-02T15:04:05.9999999Z07:00", e.System.TimeCreated.SystemTime) |
| 156 |
require.NoError(t, err) |
| 157 |
|
| 158 |
require.True(t, eventTime.After(startTime.In(time.UTC))) |
| 159 |
} |
| 160 |
}) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
type Event struct { |
| 166 |
XMLName xml.Name `xml:"Event"` |
| 167 |
System System `xml:"System"` |
| 168 |
Data string `xml:"EventData>Data"` |
| 169 |
} |
| 170 |
|
| 171 |
type System struct { |
| 172 |
Provider Provider `xml:"Provider"` |
| 173 |
EventID int `xml:"EventID"` |
| 174 |
Version int `xml:"Version"` |
| 175 |
Level int `xml:"Level"` |
| 176 |
Task int `xml:"Task"` |
| 177 |
Opcode int `xml:"Opcode"` |
| 178 |
Keywords string `xml:"Keywords"` |
| 179 |
TimeCreated TimeCreated `xml:"TimeCreated"` |
| 180 |
EventRecordID int `xml:"EventRecordID"` |
| 181 |
Execution Execution `xml:"Execution"` |
| 182 |
Channel string `xml:"Channel"` |
| 183 |
Computer string `xml:"Computer"` |
| 184 |
} |
| 185 |
|
| 186 |
type Provider struct { |
| 187 |
Name string `xml:"Name,attr"` |
| 188 |
} |
| 189 |
|
| 190 |
type TimeCreated struct { |
| 191 |
SystemTime string `xml:"SystemTime,attr"` |
| 192 |
} |
| 193 |
|
| 194 |
type Execution struct { |
| 195 |
ProcessID string `xml:"ProcessID,attr"` |
| 196 |
ThreadID string `xml:"ThreadID,attr"` |
| 197 |
} |