|
| 1 | +// Copyright Splunk, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build windows |
| 16 | + |
| 17 | +package windows_install_script |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "golang.org/x/sys/windows/registry" |
| 29 | + "golang.org/x/sys/windows/svc" |
| 30 | + "golang.org/x/sys/windows/svc/mgr" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + // Old version to install first, this version by default is not installed as machine-wide. |
| 35 | + oldCollectorVersion = "0.94.0" |
| 36 | + // Service name |
| 37 | + serviceName = "splunk-otel-collector" |
| 38 | + // Service display name |
| 39 | + serviceDisplayName = "Splunk OpenTelemetry Collector" |
| 40 | +) |
| 41 | + |
| 42 | +func TestUpgradeFromNonMachineWideVersion(t *testing.T) { |
| 43 | + t.Setenv("VERIFY_ACCESS_TOKEN", "false") |
| 44 | + |
| 45 | + requireNoPendingFileOperations(t) |
| 46 | + |
| 47 | + scm, err := mgr.Connect() |
| 48 | + require.NoError(t, err) |
| 49 | + defer scm.Disconnect() |
| 50 | + |
| 51 | + t.Logf(" *** Installing old collector version %s", oldCollectorVersion) |
| 52 | + installCollector(t, oldCollectorVersion, "") |
| 53 | + verifyServiceExists(t, scm) |
| 54 | + verifyServiceState(t, scm, svc.Running) |
| 55 | + legacySvcVersion := getCurrentServiceVersion(t) |
| 56 | + require.Equal(t, oldCollectorVersion, legacySvcVersion) |
| 57 | + |
| 58 | + msiInstallerPath := getFilePathFromEnvVar(t, "MSI_COLLECTOR_PATH") |
| 59 | + t.Logf(" *** Installing collector from %q", msiInstallerPath) |
| 60 | + installCollector(t, "", msiInstallerPath) |
| 61 | + verifyServiceExists(t, scm) |
| 62 | + verifyServiceState(t, scm, svc.Running) |
| 63 | + latestSvcVersion := getCurrentServiceVersion(t) |
| 64 | + require.NotEqual(t, oldCollectorVersion, latestSvcVersion) |
| 65 | + requireNoPendingFileOperations(t) |
| 66 | +} |
| 67 | + |
| 68 | +func installCollector(t *testing.T, version string, msiPath string) { |
| 69 | + require.False(t, version == "" && msiPath == "", "Either version or msiPath must be provided") |
| 70 | + require.False(t, version != "" && msiPath != "", "Only one of version or msiPath should be provided") |
| 71 | + args := []string{ |
| 72 | + "-ExecutionPolicy", "Bypass", |
| 73 | + "-File", getFilePathFromEnvVar(t, "INSTALL_SCRIPT_PATH"), |
| 74 | + "-access_token", "fake-token", |
| 75 | + } |
| 76 | + |
| 77 | + if version != "" { |
| 78 | + args = append(args, "-collector_version", version) |
| 79 | + } else if msiPath != "" { |
| 80 | + args = append(args, "-msi_path", msiPath) |
| 81 | + } else { |
| 82 | + require.Fail(t, "Either version or msiPath must be provided") |
| 83 | + } |
| 84 | + |
| 85 | + cmd := exec.Command("powershell.exe", args...) |
| 86 | + |
| 87 | + output, err := cmd.CombinedOutput() |
| 88 | + t.Logf("Install output: %s", string(output)) |
| 89 | + require.NoError(t, err, "Failed to install collector (version:%q msiPath:%q)", version, msiPath) |
| 90 | +} |
| 91 | + |
| 92 | +func verifyServiceExists(t *testing.T, scm *mgr.Mgr) { |
| 93 | + service, err := scm.OpenService(serviceName) |
| 94 | + require.NoError(t, err) |
| 95 | + service.Close() |
| 96 | +} |
| 97 | + |
| 98 | +func verifyServiceState(t *testing.T, scm *mgr.Mgr, desiredState svc.State) { |
| 99 | + service, err := scm.OpenService(serviceName) |
| 100 | + require.NoError(t, err) |
| 101 | + defer service.Close() |
| 102 | + |
| 103 | + // Wait for the service to reach the running state |
| 104 | + require.Eventually(t, func() bool { |
| 105 | + status, err := service.Query() |
| 106 | + require.NoError(t, err) |
| 107 | + return status.State == desiredState |
| 108 | + }, 10*time.Second, 500*time.Millisecond, "Service failed to reach the desired state") |
| 109 | +} |
| 110 | + |
| 111 | +func getCurrentServiceVersion(t *testing.T) string { |
| 112 | + // Read the service version from the registry, need to find the GUID registry key |
| 113 | + // given the service name. |
| 114 | + key, err := registry.OpenKey(registry.LOCAL_MACHINE, `Software\Microsoft\Windows\CurrentVersion\Uninstall`, registry.ALL_ACCESS) |
| 115 | + require.NoError(t, err) |
| 116 | + defer key.Close() |
| 117 | + |
| 118 | + // Enumerate all subkeys to find the one that matches the service name |
| 119 | + subKeys, err := key.ReadSubKeyNames(0) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + for _, subKey := range subKeys { |
| 123 | + subKeyPath := fmt.Sprintf(`Software\Microsoft\Windows\CurrentVersion\Uninstall\%s`, subKey) |
| 124 | + subKeyHandle, err := registry.OpenKey(registry.LOCAL_MACHINE, subKeyPath, registry.QUERY_VALUE) |
| 125 | + if err != nil { |
| 126 | + continue |
| 127 | + } |
| 128 | + defer subKeyHandle.Close() |
| 129 | + |
| 130 | + displayName, _, err := subKeyHandle.GetStringValue("DisplayName") |
| 131 | + if err == nil && strings.Contains(displayName, serviceDisplayName) { |
| 132 | + // Found the subkey for the service, now get the version |
| 133 | + version, _, err := subKeyHandle.GetStringValue("DisplayVersion") |
| 134 | + require.NoError(t, err) |
| 135 | + return version |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + require.Fail(t, "Failed to find service version in registry") |
| 140 | + return "" |
| 141 | +} |
| 142 | + |
| 143 | +func requireNoPendingFileOperations(t *testing.T) { |
| 144 | + // Check for pending file rename operations |
| 145 | + pendingFileRenameKey, err := registry.OpenKey( |
| 146 | + registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager`, registry.QUERY_VALUE) |
| 147 | + require.NoError(t, err) |
| 148 | + defer pendingFileRenameKey.Close() |
| 149 | + pendingFileRenameEntries, _, err := pendingFileRenameKey.GetStringsValue("PendingFileRenameOperations") |
| 150 | + if err != nil { |
| 151 | + require.ErrorIs(t, err, registry.ErrNotExist) |
| 152 | + } |
| 153 | + |
| 154 | + for _, fileName := range pendingFileRenameEntries { |
| 155 | + if strings.Contains(strings.ToLower(fileName), "splunk") { |
| 156 | + require.Fail(t, "Found pending file rename: %s", fileName) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func getFilePathFromEnvVar(t *testing.T, envVar string) string { |
| 162 | + filePath := os.Getenv(envVar) |
| 163 | + require.NotEmpty(t, filePath, "%s environment variable is not set", envVar) |
| 164 | + _, err := os.Stat(filePath) |
| 165 | + require.NoError(t, err, "File %s does not exist", filePath) |
| 166 | + if strings.Contains(filePath, " ") { |
| 167 | + filePath = "\"" + filePath + "\"" |
| 168 | + } |
| 169 | + return filePath |
| 170 | +} |
0 commit comments