diff --git a/Project.toml b/Project.toml index 097036d..539eaec 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "OpenPolicyAgent" uuid = "8f257efb-743c-4ebc-8197-d291a1f743b4" authors = ["JuliaHub Inc.", "Tanmay Mohapatra "] -version = "0.1.0" +version = "0.1.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/server/server.jl b/src/server/server.jl index 9a64b56..27e4613 100644 --- a/src/server/server.jl +++ b/src/server/server.jl @@ -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} @@ -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), @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 3a689c7..eb67719 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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")