Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some global variables constant #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Sandbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ include("Docker.jl")
# Load the UserNamespace executor
include("UserNamespaces.jl")

all_executors = Type{<:SandboxExecutor}[
const all_executors = Type{<:SandboxExecutor}[
# We always prefer the UserNamespaces executor, if we can use it,
# and the unprivileged one most of all. Only after that do we try `docker`.
UnprivilegedUserNamespacesExecutor,
Expand Down Expand Up @@ -89,16 +89,15 @@ function select_executor(verbose::Bool)
error("Could not find any available executors for $(triplet(HostPlatform()))!")
end

_preferred_executor = nothing
const _preferred_executor = Ref{Union{Nothing, (x->Type{x}).(all_executors)...}}(nothing)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this helps anything in practice, all_executors has already three elements, with more possibly to come.

const _preferred_executor_lock = ReentrantLock()
function preferred_executor(;verbose::Bool = false)
lock(_preferred_executor_lock) do
# If we've already asked this question, return the old answer
global _preferred_executor
if _preferred_executor === nothing
_preferred_executor = select_executor(verbose)
if _preferred_executor[] === nothing
_preferred_executor[] = select_executor(verbose)
end
return _preferred_executor
return _preferred_executor[]
end
end

Expand Down
19 changes: 9 additions & 10 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,26 @@ Wrapper around libc's `getuid()` function
"""
getgid() = ccall(:getgid, Cint, ())

_sudo_cmd = nothing
const _sudo_cmd = Ref{Union{Vector{String}, Nothing}}(nothing)
function sudo_cmd()
global _sudo_cmd

# Use cached value if we've already run this
if _sudo_cmd !== nothing
return _sudo_cmd
if _sudo_cmd[] !== nothing
return _sudo_cmd[]
end

if getuid() == 0
_sudo_cmd[] = if getuid() == 0
# If we're already root, don't use any kind of sudo program
_sudo_cmd = String[]
String[]
elseif Sys.which("sudo") !== nothing success(`sudo -V`)
# If `sudo` is available, use that
_sudo_cmd = ["sudo"]
["sudo"]
elseif Sys.which("su") !== nothing
# Fall back to `su` if all else fails
_sudo_cmd = ["su", "root", "-c"]
["su", "root", "-c"]
else
@warn("No known sudo-like wrappers!")
_sudo_cmd = String[]
String[]
end
return _sudo_cmd
return _sudo_cmd[]
end
4 changes: 2 additions & 2 deletions test/Sandbox.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Test, Sandbox, SHA

all_executors = Sandbox.all_executors
const all_executors = Sandbox.all_executors

# Can we run `sudo` without a password? If not, don't attempt to test the privileged runner
if !success(`sudo -k -n true`)
Expand All @@ -17,7 +17,7 @@ function print_if_nonempty(stderr::Vector{UInt8})
return true
end

rootfs_dir = Sandbox.alpine_rootfs()
const rootfs_dir = Sandbox.alpine_rootfs()
for executor in all_executors
if !executor_available(executor)
@error("Skipping $(executor) tests, as it does not seem to be available")
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# If our test harness requests a local sandbox, make it so!
REPO_ROOT = dirname(@__DIR__)
should_build_local_sandbox = parse(Bool, get(ENV, "SANDBOX_BUILD_LOCAL_SANDBOX", "false"))
const REPO_ROOT = dirname(@__DIR__)
const should_build_local_sandbox = parse(Bool, get(ENV, "SANDBOX_BUILD_LOCAL_SANDBOX", "false"))
if should_build_local_sandbox
run(`$(Base.julia_cmd()) --project=$(REPO_ROOT) $(REPO_ROOT)/deps/build_local_sandbox.jl`)
# Ensure LocalPreferences.toml gets used by test project as well
Expand All @@ -21,7 +21,7 @@ end
using Test, Sandbox, Scratch

# If we're on a UserNSSandbox_jll-compatible system, ensure that the sandbox is coming from where we expect.
UserNSSandbox_jll = Sandbox.UserNSSandbox_jll
using UserNSSandbox_jll
if UserNSSandbox_jll.is_available()
Artifacts = Sandbox.UserNSSandbox_jll.Artifacts
sandbox_path = Sandbox.UserNSSandbox_jll.sandbox_path
Expand Down