Skip to content

Commit 4ea2cf0

Browse files
iCharlesHujakepetroules
authored andcommitted
Disable testPlatformOptionsRunAsUser on CI if we don't have privilege to create a temporary user
1 parent f9b9835 commit 4ea2cf0

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

Tests/SubprocessTests/SubprocessTests+Windows.swift

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ extension SubprocessWindowsTests {
455455
extension SubprocessWindowsTests {
456456
@Test(.enabled(if: SubprocessWindowsTests.hasAdminPrivileges()))
457457
func testPlatformOptionsRunAsUser() async throws {
458-
try await self.withTemporaryUser { username, password in
458+
try await self.withTemporaryUser { username, password, succeed in
459459
// Use public directory as working directory so the newly created user
460460
// has access to it
461461
let workingDirectory = FilePath("C:\\Users\\Public")
@@ -498,6 +498,12 @@ extension SubprocessWindowsTests {
498498
}
499499
return String(decodingCString: pointer, as: UTF16.self)
500500
}
501+
// On CI, we might failed to create user due to privilege issues
502+
// There's nothing much we can do in this case
503+
guard succeed else {
504+
// If we fail to create the user, skip this test
505+
return true
506+
}
501507
// CreateProcessWithLogonW doesn't appear to work when running in a container
502508
return whoamiResult.terminationStatus == .unhandledException(STATUS_DLL_INIT_FAILED) && userName() == "ContainerAdministrator"
503509
}
@@ -732,16 +738,16 @@ extension SubprocessWindowsTests {
732738
// MARK: - User Utils
733739
extension SubprocessWindowsTests {
734740
private func withTemporaryUser(
735-
_ work: (String, String) async throws -> Void
741+
_ work: (String, String, Bool) async throws -> Void
736742
) async throws {
737743
let username: String = "TestUser\(randomString(length: 5, lettersOnly: true))"
738744
let password: String = "Password\(randomString(length: 10))"
739745

740-
func createUser(withUsername username: String, password: String) {
741-
username.withCString(
746+
func createUser(withUsername username: String, password: String) -> Bool {
747+
return username.withCString(
742748
encodedAs: UTF16.self
743749
) { usernameW in
744-
password.withCString(
750+
return password.withCString(
745751
encodedAs: UTF16.self
746752
) { passwordW in
747753
var userInfo: USER_INFO_1 = USER_INFO_1()
@@ -762,27 +768,30 @@ extension SubprocessWindowsTests {
762768
&error
763769
)
764770
guard status == NERR_Success else {
765-
Issue.record("Failed to create user with error: \(error)")
766-
return
771+
return false
767772
}
773+
return true
768774
}
769775
}
770776
}
771777

772-
createUser(withUsername: username, password: password)
778+
let succeed = createUser(withUsername: username, password: password)
779+
773780
defer {
774-
// Now delete the user
775-
let status = username.withCString(
776-
encodedAs: UTF16.self
777-
) { usernameW in
778-
return NetUserDel(nil, usernameW)
779-
}
780-
if status != NERR_Success {
781-
Issue.record("Failed to delete user with error: \(status)")
781+
if succeed {
782+
// Now delete the user
783+
let status = username.withCString(
784+
encodedAs: UTF16.self
785+
) { usernameW in
786+
return NetUserDel(nil, usernameW)
787+
}
788+
if status != NERR_Success {
789+
Issue.record("Failed to delete user with error: \(status)")
790+
}
782791
}
783792
}
784793
// Run work
785-
try await work(username, password)
794+
try await work(username, password, succeed)
786795
}
787796

788797
private static func hasAdminPrivileges() -> Bool {

0 commit comments

Comments
 (0)