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

allow server opa command to be customized #7

Merged
merged 2 commits into from
Oct 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OpenPolicyAgent"
uuid = "8f257efb-743c-4ebc-8197-d291a1f743b4"
authors = ["JuliaHub Inc.", "Tanmay Mohapatra <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
11 changes: 9 additions & 2 deletions src/server/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct MonitoredOPAServer
port::Int
stdout::Any
stderr::Any
cmdline::CommandLine
monitor_task::Ref{Union{Nothing, Task}}
server_proc::Ref{Union{Nothing, Process}}
stopped::Ref{Bool}
Expand All @@ -41,10 +42,17 @@ struct MonitoredOPAServer
port::Int = DEFAULT_PORT,
stdout = nothing,
stderr = nothing,
cmdline = nothing,
)
if isnothing(cmdline)
cmdline = CommandLine()
end
cmdline.runopts[:wait] = false

return new(configfile,
host, port,
stdout, stderr,
cmdline,
Ref{Union{Nothing, Task}}(nothing),
Ref{Union{Nothing, Process}}(nothing),
Ref{Bool}(true),
Expand All @@ -54,8 +62,7 @@ struct MonitoredOPAServer
end

function start_opa_server!(server::MonitoredOPAServer)
ctx = CommandLine()
ctx.runopts[:wait] = false
ctx = server.cmdline
if !isnothing(server.stdout)
ctx.pipelineopts[:stdout] = server.stdout
end
Expand Down
48 changes: 42 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,21 @@ function start_bundle_server(root_path)
return server
end

function start_opa_server(root_path)
opa_server = OpenPolicyAgent.Server.MonitoredOPAServer(
joinpath(root_path, "config.yaml"),
stdout = joinpath(root_path, "server.stdout"),
stderr = joinpath(root_path, "server.stderr"),
)
function start_opa_server(root_path; change_dir::Bool=true)
if change_dir
opa_server = OpenPolicyAgent.Server.MonitoredOPAServer(
joinpath(root_path, "config.yaml");
stdout = joinpath(root_path, "server.stdout"),
stderr = joinpath(root_path, "server.stderr"),
cmdline = OpenPolicyAgent.CLI.CommandLine(; cmdopts=Dict(:dir => root_path)),
)
else
opa_server = OpenPolicyAgent.Server.MonitoredOPAServer(
joinpath(root_path, "config.yaml");
stdout = joinpath(root_path, "server.stdout"),
stderr = joinpath(root_path, "server.stderr"),
)
end
OpenPolicyAgent.Server.start!(opa_server)
return opa_server
end
Expand Down Expand Up @@ -399,6 +408,33 @@ function runtests()
@info("Stopping OPA server")
OpenPolicyAgent.Server.stop!(opa_server)
end
sleep(5)
@test opa_server.stopped[]

# run again withoug changing the directory
@info("Starting OPA server without changing the directory")
opa_server = start_opa_server(opa_server_location; change_dir=false)
try
# Wait for servers to start
sleep(15)
@test !(opa_server.stopped[])
@test !isnothing(opa_server.monitor_task[])
@test !istaskdone(opa_server.monitor_task[])

# create the client
openapi_client = OpenAPI.Clients.Client("http://localhost:8181"; escape_path_params=false)
@testset "Status API" begin
test_status_api(openapi_client)
end
@testset "Health API" begin
test_health_api(openapi_client)
end
finally
@info("Stopping OPA server")
OpenPolicyAgent.Server.stop!(opa_server)
end
sleep(5)
@test opa_server.stopped[]
end
finally
@info("Stopping bundle server")
Expand Down