Skip to content

Commit 75911b5

Browse files
committed
Simplify if unprivileged mode is supported.
1 parent 224b0e3 commit 75911b5

File tree

5 files changed

+21
-41
lines changed

5 files changed

+21
-41
lines changed

testing/integration/upgrade_fleet_test.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,8 @@ func testUpgradeFleetManagedElasticAgent(
215215
require.NoError(t, err)
216216

217217
if unprivileged {
218-
if startParsedVersion.Less(*upgradetest.Version_8_13_0_SNAPSHOT) {
219-
t.Skipf("Starting version %s is less than 8.13-SNAPSHOT and doesn't support --unprivileged", startParsedVersion.String())
220-
}
221-
if endParsedVersion.Less(*upgradetest.Version_8_13_0_SNAPSHOT) {
222-
t.Skipf("Ending version %s is less than 8.13-SNAPSHOT and doesn't support --unprivileged", endParsedVersion.String())
223-
}
224-
if runtime.GOOS != define.Linux {
225-
if startParsedVersion.Less(*upgradetest.Version_8_14_0_SNAPSHOT) {
226-
t.Skipf("Starting version %s is less than 8.14-SNAPSHOT and doesn't support --unprivileged on Windows", startParsedVersion.String())
227-
}
228-
if endParsedVersion.Less(*upgradetest.Version_8_14_0_SNAPSHOT) {
229-
t.Skipf("Ending version %s is less than 8.14-SNAPSHOT and doesn't support --unprivileged on Windows", endParsedVersion.String())
230-
}
218+
if !upgradetest.SupportsUnprivileged(startParsedVersion, endParsedVersion) {
219+
t.Skipf("Either starting version %s or ending version %s doesn't support --unprivileged", startParsedVersion.String(), endParsedVersion.String())
231220
}
232221
}
233222

testing/integration/upgrade_standalone_same_commit_test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"os"
1919
"path"
2020
"path/filepath"
21-
"runtime"
2221
"strings"
2322
"testing"
2423
"time"
@@ -53,15 +52,9 @@ func TestStandaloneUpgradeSameCommit(t *testing.T) {
5352
}
5453

5554
unprivilegedAvailable := false
56-
// This is probably redundant: see the skip statement above
57-
if runtime.GOOS == define.Linux && !currentVersion.Less(*upgradetest.Version_8_13_0_SNAPSHOT) {
58-
// only available if version is 8.13+ on Linux
59-
unprivilegedAvailable = true
60-
} else if !currentVersion.Less(*upgradetest.Version_8_14_0_SNAPSHOT) {
61-
// 8.14+ its always available
55+
if upgradetest.SupportsUnprivileged(currentVersion) {
6256
unprivilegedAvailable = true
6357
}
64-
6558
unPrivilegedString := "unprivileged"
6659
if !unprivilegedAvailable {
6760
unPrivilegedString = "privileged"

testing/integration/upgrade_standalone_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package integration
99
import (
1010
"context"
1111
"fmt"
12-
"runtime"
1312
"testing"
1413
"time"
1514

@@ -37,11 +36,7 @@ func TestStandaloneUpgrade(t *testing.T) {
3736

3837
for _, startVersion := range versionList {
3938
unprivilegedAvailable := false
40-
if runtime.GOOS == define.Linux && !startVersion.Less(*upgradetest.Version_8_13_0_SNAPSHOT) && !endVersion.Less(*upgradetest.Version_8_13_0_SNAPSHOT) {
41-
// unprivileged available if both versions are 8.13+ on Linux
42-
unprivilegedAvailable = true
43-
} else if !startVersion.Less(*upgradetest.Version_8_14_0_SNAPSHOT) && !endVersion.Less(*upgradetest.Version_8_14_0_SNAPSHOT) {
44-
// always available if both versions are 8.14+
39+
if upgradetest.SupportsUnprivileged(startVersion, endVersion) {
4540
unprivilegedAvailable = true
4641
}
4742
t.Run(fmt.Sprintf("Upgrade %s to %s (privileged)", startVersion, define.Version()), func(t *testing.T) {

testing/upgradetest/upgrader.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"os"
1313
"path/filepath"
14-
"runtime"
1514
"strings"
1615
"time"
1716

@@ -21,7 +20,6 @@ import (
2120
v1client "github.com/elastic/elastic-agent/pkg/control/v1/client"
2221
v2proto "github.com/elastic/elastic-agent/pkg/control/v2/cproto"
2322
atesting "github.com/elastic/elastic-agent/pkg/testing"
24-
"github.com/elastic/elastic-agent/pkg/testing/define"
2523
"github.com/elastic/elastic-agent/pkg/version"
2624
)
2725

@@ -209,13 +207,7 @@ func PerformUpgrade(
209207
// in the unprivileged is unset we adjust it to use unprivileged when the version allows it
210208
// in the case that its explicitly set then we ensure the version supports it
211209
if upgradeOpts.unprivileged == nil {
212-
if !startVersion.Less(*Version_8_13_0_SNAPSHOT) && !endVersion.Less(*Version_8_13_0_SNAPSHOT) && runtime.GOOS == define.Linux {
213-
// both version support --unprivileged on Linux
214-
unprivileged := true
215-
upgradeOpts.unprivileged = &unprivileged
216-
logger.Logf("installation of Elastic Agent will use --unprivileged as both start and end version support --unprivileged mode on Linux")
217-
} else if !startVersion.Less(*Version_8_14_0_SNAPSHOT) && !endVersion.Less(*Version_8_14_0_SNAPSHOT) {
218-
// both version support --unprivileged on all platforms
210+
if SupportsUnprivileged(startVersion, endVersion) {
219211
unprivileged := true
220212
upgradeOpts.unprivileged = &unprivileged
221213
logger.Logf("installation of Elastic Agent will use --unprivileged as both start and end version support --unprivileged mode")
@@ -225,11 +217,8 @@ func PerformUpgrade(
225217
upgradeOpts.unprivileged = &unprivileged
226218
}
227219
} else if *upgradeOpts.unprivileged {
228-
if startVersion.Less(*Version_8_13_0_SNAPSHOT) {
229-
return errors.New("cannot install starting version with --unprivileged (which is default) because the it is older than 8.13")
230-
}
231-
if endVersion.Less(*Version_8_13_0_SNAPSHOT) {
232-
return errors.New("cannot upgrade to ending version as end version doesn't support running with --unprivileged (which is default) because it is older than 8.13")
220+
if !SupportsUnprivileged(startVersion, endVersion) {
221+
return fmt.Errorf("cannot install with forced --unprivileged because either start version %s or end version %s doesn't support --unprivileged mode", startVersion.String(), endVersion.String())
233222
}
234223
}
235224

testing/upgradetest/versions.go

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"path/filepath"
14+
"runtime"
1415
"sort"
1516
"strings"
1617

@@ -254,3 +255,16 @@ func EnsureSnapshot(version string) string {
254255
}
255256
return version
256257
}
258+
259+
// SupportsUnprivileged returns true when the version supports unprivileged mode.
260+
func SupportsUnprivileged(versions ...*version.ParsedSemVer) bool {
261+
for _, ver := range versions {
262+
if ver.Less(*Version_8_13_0_SNAPSHOT) {
263+
return false
264+
}
265+
if runtime.GOOS != define.Linux && ver.Less(*Version_8_14_0_SNAPSHOT) {
266+
return false
267+
}
268+
}
269+
return true
270+
}

0 commit comments

Comments
 (0)