forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsul_initializer_spec.rb
More file actions
115 lines (103 loc) · 3.55 KB
/
Copy pathconsul_initializer_spec.rb
File metadata and controls
115 lines (103 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
# Copyright (C) 2015 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
require_relative '../spec_helper'
require_relative '../../config/initializers/consul'
describe ConsulInitializer do
after(:each) do
Canvas::DynamicSettings.config = nil
Canvas::DynamicSettings.reset_cache!
Canvas::DynamicSettings.fallback_data = nil
end
class FakeLogger
attr_reader :messages
def initialize
@messages = []
end
def warn(message)
messages << message
end
end
describe ".configure_with" do
include WebMock::API
it "passes provided config info to DynamicSettings" do
config_hash = {hi: "ho", host: "localhost", port: 80}
ConsulInitializer.configure_with(config_hash.with_indifferent_access)
expect(Canvas::DynamicSettings.config[:hi]).to eq("ho")
end
it "logs connection failure when trying to init data to a consul it can't find" do
config_hash = {
host: "somewhere-without-consul.gov",
port: 123456,
init_values: {
'rich-content-service' => {
'app-host' => 'rce.docker',
'cdn-host' => 'rce.docker'
}
}
}
stub_request(:put, "https://somewhere-without-consul.gov:123456/v1/kv/config/canvas/rich-content-service/app-host").
to_return(:status => 500)
logger = FakeLogger.new
ConsulInitializer.configure_with(config_hash.with_indifferent_access, logger)
message = "INITIALIZATION: can't reach consul, attempts to load DynamicSettings will fail"
expect(logger.messages).to include(message)
end
it "logs nothing if there's no config file" do
logger = FakeLogger.new
ConsulInitializer.configure_with(nil, logger)
expect(logger.messages).to eq([])
end
end
describe ".fallback to" do
let(:fallback_data) do
{
'rich-content-service' => {
'app-host' => 'rce.docker',
'cdn-host' => 'rce.docker'
},
'canvas' => {
'encryption-secret' => 'asdf',
'signing-secret' => 'fdas'
}
}
end
it "provides fallback data to DynamicSettings" do
ConsulInitializer.fallback_to(fallback_data)
s_secret = Canvas::DynamicSettings.
fallback_data['canvas']['signing-secret']
expect(s_secret).to eq('fdas')
end
it "puts the data in with indifferent access" do
ConsulInitializer.fallback_to(fallback_data)
e_secret = Canvas::DynamicSettings.
fallback_data[:canvas]["encryption-secret".to_sym]
expect(e_secret).to eq('asdf')
end
end
describe "just from loading" do
it "clears the DynamicSettings cache on reload" do
Canvas::DynamicSettings.reset_cache!
Canvas::DynamicSettings.cache["key"] = {
value: "value",
timestamp: Time.zone.now.to_i
}
expect(Canvas::DynamicSettings.from_cache("key")).to eq("value")
Canvas::Reloader.reload!
expect(Canvas::DynamicSettings.cache).to eq({})
end
end
end