1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::ManagementControllerTest < ActionController::TestCase
9 [false, nil, 404, 'disabled'],
10 [true, nil, 401, 'authorization required'],
11 [true, 'badformatwithnoBearer', 403, 'authorization error'],
12 [true, 'Bearer wrongtoken', 403, 'authorization error'],
13 [true, 'Bearer configuredmanagementtoken', 200, '{"health":"OK"}'],
14 ].each do |enabled, header, error_code, error_msg|
15 test "_health/ping when #{if enabled then 'enabled' else 'disabled' end} with header '#{header}'" do
17 Rails.configuration.ManagementToken = 'configuredmanagementtoken'
19 Rails.configuration.ManagementToken = ""
22 @request.headers['Authorization'] = header
23 get :health, params: {check: 'ping'}
24 assert_response error_code
26 resp = JSON.parse(@response.body)
28 assert_equal(JSON.load('{"health":"OK"}'), resp)
30 assert_equal(error_msg, resp['errors'])
36 mtime = File.mtime(ENV["ARVADOS_CONFIG"])
37 hash = Digest::SHA256.hexdigest(File.read(ENV["ARVADOS_CONFIG"]))
38 Rails.configuration.ManagementToken = "configuredmanagementtoken"
39 @request.headers['Authorization'] = "Bearer configuredmanagementtoken"
41 assert_response :success
42 assert_equal 'text/plain', @response.media_type
43 assert_equal 'utf-8', @response.charset
45 assert_match /\narvados_config_source_timestamp_seconds{sha256="#{hash}"} #{Regexp.escape mtime.utc.to_f.to_s}\n/, @response.body
47 # Expect mtime < loadtime < now
48 m = @response.body.match(/\narvados_config_load_timestamp_seconds{sha256="#{hash}"} (.*?)\n/)
49 assert_operator m[1].to_f, :>, mtime.utc.to_f
50 assert_operator m[1].to_f, :<, Time.now.utc.to_f
52 assert_match /\narvados_version_running{version="#{Regexp.escape AppVersion.package_version}"} 1\n/, @response.body
55 test "metrics disabled" do
56 Rails.configuration.ManagementToken = ""
57 @request.headers['Authorization'] = "Bearer configuredmanagementtoken"
62 test "metrics bad token" do
63 Rails.configuration.ManagementToken = "configuredmanagementtoken"
64 @request.headers['Authorization'] = "Bearer asdf"
69 test "metrics unauthorized" do
70 Rails.configuration.ManagementToken = "configuredmanagementtoken"