Debian Codegraph @

spec/acceptance/mapfile_spec.rb

raw
1 # frozen_string_literal: true
2
3 require 'spec_helper_acceptance'
4
5 describe 'create mapfiles' do
6 describe 'one mapfile' do
7 let(:pp) do
8 <<-MANIFEST
9 include ::haproxy
10 haproxy::mapfile { 'single-mapfile':
11 ensure => 'present',
12 mappings => [
13 { 'example.com' => 'bk_com' },
14 { 'example.net' => 'bk_net' },
15 { 'example.edu' => 'bk_edu' },
16 ],
17 }
18 MANIFEST
19 end
20
21 it 'applies the manifest twice with no stderr' do
22 idempotent_apply(pp)
23 expect(file('/etc/haproxy/single-mapfile.map')).to be_file
24 expect(file('/etc/haproxy/single-mapfile.map').content).to match "example.com bk_com\nexample.net bk_net\nexample.edu bk_edu\n"
25 end
26 end
27
28 describe 'multiple mapfiles' do
29 let(:pp) do
30 <<-MANIFEST
31 include ::haproxy
32 haproxy::mapfile { 'multiple-mapfiles':
33 ensure => 'present',
34 }
35 haproxy::mapfile::entry { 'example.com bk_com':
36 mapfile => 'multiple-mapfiles',
37 }
38 haproxy::mapfile::entry { 'org':
39 mappings => ['example.org bk_org'],
40 mapfile => 'multiple-mapfiles',
41 order => '05',
42 }
43 haproxy::mapfile::entry { 'net':
44 mappings => ['example.net bk_net'],
45 mapfile => 'multiple-mapfiles',
46 }
47 haproxy::mapfile::entry { 'edu':
48 mappings => [{'example.edu' => 'bk_edu'}],
49 mapfile => 'multiple-mapfiles',
50 }
51 MANIFEST
52 end
53
54 it 'applies the manifest twice with no stderr' do
55 idempotent_apply(pp)
56 expect(file('/etc/haproxy/multiple-mapfiles.map')).to be_file
57 expect(file('/etc/haproxy/multiple-mapfiles.map').content).to match "example.org bk_org\nexample.edu bk_edu\nexample.com bk_com\nexample.net bk_net\n"
58 end
59 end
60
61 describe 'check selection of correct backend' do
62 describe 'single mapfile' do
63 let(:pp) do
64 <<-MANIFEST
65 $error_page_content = @(ERROR_PAGE)
66 HTTP/1.1 421 Misdirected Request
67 Cache-Control: no-cache
68 Connection: close
69 Content-Type: text/plain
70
71 Error Page
72 |ERROR_PAGE
73
74 file { '/tmp/error.html.http':
75 content => $error_page_content,
76 }
77 file_line {'localhost':
78 path => '/etc/hosts',
79 line => '127.0.0.1 localhost host2 host3',
80 match => '^127.0.0.1',
81 }
82 include ::haproxy
83 haproxy::mapfile { 'single-mapfile':
84 ensure => 'present',
85 mappings => [
86 { 'host2' => 'backend2' },
87 { 'host3' => 'backend3' },
88 ],
89 }
90 haproxy::frontend { 'test00':
91 ipaddress => '127.0.0.1',
92 ports => '5555',
93 mode => 'http',
94 options => {
95 'use_backend' => '%[req.hdr(host),lower,map_dom(/etc/haproxy/single-mapfile.map,backend1)]'
96 },
97 }
98 haproxy::backend { 'backend1':
99 mode => 'http',
100 options => [
101 {
102 'errorfile' => [
103 '503 /tmp/error.html.http',
104 ],
105 },
106 ],
107 }
108 haproxy::backend { 'backend2':
109 defaults => 'http',
110 collect_exported => false,
111 options => { 'mode' => 'http' },
112 }
113 haproxy::balancermember { 'port 5556':
114 listening_service => 'backend2',
115 server_names => 'test00.example.com',
116 defaults => 'http',
117 ports => '5556',
118 }
119 haproxy::backend { 'backend3':
120 defaults => 'http',
121 collect_exported => false,
122 options => { 'mode' => 'http' },
123 }
124 haproxy::balancermember { 'port 5557':
125 listening_service => 'backend3',
126 server_names => 'test01.example.com',
127 defaults => 'http',
128 ports => '5557',
129 }
130 MANIFEST
131 end
132
133 it 'is able to listen with a mapfile' do
134 retry_on_error_matching do
135 apply_manifest(pp, catch_failures: true)
136 end
137 end
138
139 it 'has a complete mapfile' do
140 expect(file('/etc/haproxy/single-mapfile.map')).to be_file
141 expect(file('/etc/haproxy/single-mapfile.map').content).to match "host2 backend2\nhost3 backend3\n"
142 end
143
144 it 'selects the correct backend based on host' do
145 expect(run_shell('curl localhost:5555').stdout.chomp).to match(%r{Error Page})
146 expect(run_shell('curl host2:5555').stdout.chomp).to match(%r{Response on 5556})
147 expect(run_shell('curl host3:5555').stdout.chomp).to match(%r{Response on 5557})
148 end
149 end
150
151 describe 'multiple mapfiles' do
152 let(:pp) do
153 <<-MANIFEST
154 $error_page_content = @(ERROR_PAGE)
155 HTTP/1.1 421 Misdirected Request
156 Cache-Control: no-cache
157 Connection: close
158 Content-Type: text/plain
159
160 Error Page
161 |ERROR_PAGE
162
163 file { '/tmp/error.html.http':
164 content => $error_page_content,
165 }
166 file_line {'localhost':
167 path => '/etc/hosts',
168 line => '127.0.0.1 localhost host2 host3',
169 match => '^127.0.0.1',
170 }
171 include ::haproxy
172 haproxy::mapfile { 'multiple-mapfiles':
173 ensure => 'present',
174 }
175 haproxy::mapfile::entry { 'host2 backend2':
176 mapfile => 'multiple-mapfiles',
177 }
178 haproxy::mapfile::entry { 'host3 backend3':
179 mapfile => 'multiple-mapfiles',
180 }
181 haproxy::frontend { 'test00':
182 ipaddress => '127.0.0.1',
183 ports => '5555',
184 mode => 'http',
185 options => {
186 'use_backend' => '%[req.hdr(host),lower,map_dom(/etc/haproxy/single-mapfile.map,backend1)]'
187 },
188 }
189 haproxy::backend { 'backend1':
190 mode => 'http',
191 options => [
192 {
193 'errorfile' => [
194 '503 /tmp/error.html.http',
195 ],
196 },
197 ],
198 }
199 haproxy::backend { 'backend2':
200 defaults => 'http',
201 collect_exported => false,
202 options => { 'mode' => 'http' },
203 }
204 haproxy::balancermember { 'port 5556':
205 listening_service => 'backend2',
206 server_names => 'test00.example.com',
207 defaults => 'http',
208 ports => '5556',
209 }
210 haproxy::backend { 'backend3':
211 defaults => 'http',
212 collect_exported => false,
213 options => { 'mode' => 'http' },
214 }
215 haproxy::balancermember { 'port 5557':
216 listening_service => 'backend3',
217 server_names => 'test01.example.com',
218 defaults => 'http',
219 ports => '5557',
220 }
221 MANIFEST
222 end
223
224 it 'is able to listen with a mapfile' do
225 retry_on_error_matching do
226 apply_manifest(pp, catch_failures: true)
227 end
228 end
229
230 it 'has a complete mapfile' do
231 expect(file('/etc/haproxy/multiple-mapfiles.map')).to be_file
232 expect(file('/etc/haproxy/multiple-mapfiles.map').content).to match "host2 backend2\nhost3 backend3\n"
233 end
234
235 it 'selects the correct backend based on host' do
236 expect(run_shell('curl localhost:5555').stdout.chomp).to match(%r{Error Page})
237 expect(run_shell('curl host2:5555').stdout.chomp).to match(%r{Response on 5556})
238 expect(run_shell('curl host3:5555').stdout.chomp).to match(%r{Response on 5557})
239 end
240 end
241 end
242 end