1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class ManagementControllerTest < ActionController::TestCase
8 reset_api_fixtures :after_each_test, false
9 reset_api_fixtures :after_suite, false
12 [false, nil, 404, 'disabled'],
13 [true, nil, 401, 'authorization required'],
14 [true, 'badformatwithnoBearer', 403, 'authorization error'],
15 [true, 'Bearer wrongtoken', 403, 'authorization error'],
16 [true, 'Bearer configuredmanagementtoken', 200, '{"health":"OK"}'],
17 ].each do |enabled, header, error_code, error_msg|
18 test "health check ping when #{if enabled then 'enabled' else 'disabled' end} with header '#{header}'" do
20 Rails.configuration.ManagementToken = 'configuredmanagementtoken'
22 Rails.configuration.ManagementToken = ""
25 @request.headers['Authorization'] = header
26 get(:health, params: {check: 'ping'})
27 assert_response error_code
29 resp = JSON.parse(@response.body)
31 assert_equal(JSON.load('{"health":"OK"}'), resp)
33 assert_equal(resp['errors'], error_msg)
39 mtime = File.mtime(ENV["ARVADOS_CONFIG"])
40 hash = Digest::SHA256.hexdigest(File.read(ENV["ARVADOS_CONFIG"]))
41 Rails.configuration.ManagementToken = "configuredmanagementtoken"
42 @request.headers['Authorization'] = "Bearer configuredmanagementtoken"
44 assert_response :success
45 assert_equal 'text/plain', @response.content_type
47 assert_match /\narvados_config_source_timestamp_seconds{sha256="#{hash}"} #{Regexp.escape mtime.utc.to_f.to_s}\n/, @response.body
49 # Expect mtime < loadtime < now
50 m = @response.body.match(/\narvados_config_load_timestamp_seconds{sha256="#{hash}"} (.*?)\n/)
51 assert_operator m[1].to_f, :>, mtime.utc.to_f
52 assert_operator m[1].to_f, :<, Time.now.utc.to_f
54 assert_match /\narvados_version_running{version="#{Regexp.escape AppVersion.package_version}"} 1\n/, @response.body
57 test "metrics disabled" do
58 Rails.configuration.ManagementToken = ""
59 @request.headers['Authorization'] = "Bearer configuredmanagementtoken"
64 test "metrics bad token" do
65 Rails.configuration.ManagementToken = "configuredmanagementtoken"
66 @request.headers['Authorization'] = "Bearer asdf"
71 test "metrics unauthorized" do
72 Rails.configuration.ManagementToken = "configuredmanagementtoken"