haproxy-tools 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HAProxy
2
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0".freeze
3
5
  end
data/lib/haproxy_tools.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'treetop'
2
- require 'haproxy/treetop/config'
3
- require 'haproxy/config'
4
- require 'haproxy/renderer'
5
- require 'haproxy/parser'
1
+ # frozen_string_literal: true
2
+
3
+ require "treetop"
4
+ require "haproxy/treetop/config"
5
+ require "haproxy/config"
6
+ require "haproxy/renderer"
7
+ require "haproxy/parser"
@@ -1,142 +1,153 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe "HAProxy::Config" do
4
- describe 'render multi-backend config' do
6
+ describe "render multi-backend config" do
5
7
  before(:each) do
6
- @config = HAProxy::Config.parse_file('spec/fixtures/multi-pool.haproxy.cfg')
8
+ @config = HAProxy::Config.parse_file("spec/fixtures/multi-pool.haproxy.cfg")
7
9
  end
8
10
 
9
- it 'can re-render a config file with a server removed' do
10
- l = @config.backend('www_main')
11
- l.servers.delete('prd_www_1')
11
+ it "can re-render a config file with a server removed" do
12
+ l = @config.backend("www_main")
13
+ l.servers.delete("prd_www_1")
12
14
 
13
15
  new_config_text = @config.render
14
16
 
15
17
  new_config = HAProxy::Parser.new.parse(new_config_text)
16
- new_config.backend('www_main').servers['prd_www_1'].should be_nil
18
+ expect(new_config.backend("www_main").servers["prd_www_1"]).to be_nil
17
19
  end
18
20
 
19
- it 'can re-render a config file with a server attribute added' do
20
- b = @config.backend('www_main')
21
- b.servers['prd_www_1'].attributes['disabled'] = true
21
+ it "can re-render a config file with a server attribute added" do
22
+ b = @config.backend("www_main")
23
+ b.servers["prd_www_1"].attributes["disabled"] = true
22
24
  new_config_text = @config.render
23
25
 
24
26
  new_config = HAProxy::Parser.new.parse(new_config_text)
25
- s = new_config.backend('www_main').servers['prd_www_1']
26
- s.should_not be_nil
27
- s.attributes['disabled'].should be_truthy
27
+ s = new_config.backend("www_main").servers["prd_www_1"]
28
+ expect(s).not_to be_nil
29
+ expect(s.attributes["disabled"]).to be_truthy
28
30
  end
29
31
 
30
- it 'can re-render a config file with a server added' do
31
- b = @config.backend('www_main')
32
- b.add_server('prd_www_4', '99.99.99.99', :port => '8000', attributes: { :weight => 128 })
32
+ it "can re-render a config file with a server added" do
33
+ b = @config.backend("www_main")
34
+ b.add_server("prd_www_4", "99.99.99.99", port: "8000", attributes: {weight: 128})
33
35
 
34
36
  new_config_text = @config.render
35
37
 
36
38
  new_config = HAProxy::Parser.new.parse(new_config_text)
37
- s = new_config.backend('www_main').servers['prd_www_4']
38
- s.should_not be_nil
39
- s.name.should == 'prd_www_4'
40
- s.host.should == '99.99.99.99'
41
- s.port.should == '8000'
42
- s.attributes.to_a.should == [['weight', '128']]
39
+ s = new_config.backend("www_main").servers["prd_www_4"]
40
+ expect(s).not_to be_nil
41
+ expect(s.name).to eq("prd_www_4")
42
+ expect(s.host).to eq("99.99.99.99")
43
+ expect(s.port).to eq("8000")
44
+ expect(s.attributes.to_a).to eq([["weight", "128"]])
43
45
  end
44
46
 
45
- it 'can re-render a config file with a server added based on template' do
46
- b = @config.backend('www_main')
47
- b.add_server('prd_www_4', '99.99.99.99', :template => b.servers['prd_www_1'])
47
+ it "can re-render a config file with a server added based on template" do
48
+ b = @config.backend("www_main")
49
+ b.add_server("prd_www_4", "99.99.99.99", template: b.servers["prd_www_1"])
48
50
 
49
51
  new_config_text = @config.render
50
52
 
51
53
  new_config = HAProxy::Parser.new.parse(new_config_text)
52
- s = new_config.backend('www_main').servers['prd_www_4']
53
- s.should_not be_nil
54
- s.name.should == 'prd_www_4'
55
- s.host.should == '99.99.99.99'
56
- s.port.should == '8000'
57
- s.attributes.to_a.should == [
58
- ['cookie','i-prd_www_1'],
59
- ['check',true],
60
- ['inter','3000'],
61
- ['rise','2'],
62
- ['fall','3'],
63
- ['maxconn','1000']
64
- ]
54
+ s = new_config.backend("www_main").servers["prd_www_4"]
55
+ expect(s).not_to be_nil
56
+ expect(s.name).to eq("prd_www_4")
57
+ expect(s.host).to eq("99.99.99.99")
58
+ expect(s.port).to eq("8000")
59
+ expect(s.attributes.to_a).to eq([
60
+ ["cookie", "i-prd_www_1"],
61
+ ["check", true],
62
+ ["inter", "3000"],
63
+ ["rise", "2"],
64
+ ["fall", "3"],
65
+ ["maxconn", "1000"],
66
+ ])
67
+ end
68
+ end
69
+
70
+ describe "re-render multi-pool config" do
71
+ before(:each) do
72
+ @config = HAProxy::Config.parse_file("spec/fixtures/multi-pool.haproxy.cfg")
73
+ end
74
+
75
+ it "can re-render the config file" do
76
+ original_text = File.read("spec/fixtures/multi-pool.haproxy.cfg")
77
+ new_text = @config.render
78
+ expect(new_text.squeeze(" ")).to eq(original_text.squeeze(" "))
65
79
  end
66
80
  end
67
81
 
68
- describe 'render simple 1.5 config' do
82
+ describe "render simple 1.5 config" do
69
83
  before(:each) do
70
- @config = HAProxy::Config.parse_file('spec/fixtures/simple.haproxy15.cfg')
84
+ @config = HAProxy::Config.parse_file("spec/fixtures/simple.haproxy15.cfg")
71
85
  end
72
86
 
73
- it 'cen re-render a config file with an error page removed' do
74
- @config.default.config.should have_key('errorfile 400')
75
- @config.default.config.delete('errorfile 400')
87
+ it "can re-render a config file with an error page removed" do
88
+ expect(@config.default.config).to have_key("errorfile 400")
89
+ @config.default.config.delete("errorfile 400")
76
90
 
77
91
  new_config_text = @config.render
78
92
 
79
93
  new_config = HAProxy::Parser.new.parse(new_config_text)
80
- new_config.default.config.should_not have_key('errorfile 400')
94
+ expect(new_config.default.config).not_to have_key("errorfile 400")
81
95
  end
82
96
 
83
- it 'can re-render a config file with an error page added' do
84
- @config.default.config.should_not have_key('errorfile 401')
85
- @config.default.config['errorfile 401'] = '/etc/haproxy/errors/401.http'
97
+ it "can re-render a config file with an error page added" do
98
+ expect(@config.default.config).not_to have_key("errorfile 401")
99
+ @config.default.config["errorfile 401"] = "/etc/haproxy/errors/401.http"
86
100
 
87
101
  new_config_text = @config.render
88
102
 
89
103
  new_config = HAProxy::Parser.new.parse(new_config_text)
90
- new_config.default.config.should have_key('errorfile 401')
91
- new_config.default.config['errorfile 401'].should == '/etc/haproxy/errors/401.http'
104
+ expect(new_config.default.config).to have_key("errorfile 401")
105
+ expect(new_config.default.config["errorfile 401"]).to eq("/etc/haproxy/errors/401.http")
92
106
  end
93
-
94
107
  end
95
108
 
96
- describe 'render simple config' do
109
+ describe "render simple config" do
97
110
  before(:each) do
98
- @config = HAProxy::Config.parse_file('spec/fixtures/simple.haproxy.cfg')
111
+ @config = HAProxy::Config.parse_file("spec/fixtures/simple.haproxy.cfg")
99
112
  end
100
113
 
101
- it 'can re-render a config file with a config removed' do
102
- @config.default.config.should have_key('clitimeout')
103
- @config.default.config.delete('clitimeout')
114
+ it "can re-render a config file with a config removed" do
115
+ expect(@config.default.config).to have_key("clitimeout")
116
+ @config.default.config.delete("clitimeout")
104
117
 
105
118
  new_config_text = @config.render
106
119
 
107
120
  new_config = HAProxy::Parser.new.parse(new_config_text)
108
- new_config.default.config.should_not have_key('clitimeout')
109
-
121
+ expect(new_config.default.config).not_to have_key("clitimeout")
110
122
  end
111
123
 
112
- it 'can re-render a config file with a server removed' do
113
- l = @config.listener('http_proxy')
114
- l.servers.delete('web1')
124
+ it "can re-render a config file with a server removed" do
125
+ l = @config.listener("http_proxy")
126
+ l.servers.delete("web1")
115
127
 
116
128
  new_config_text = @config.render
117
129
 
118
130
  new_config = HAProxy::Parser.new.parse(new_config_text)
119
- new_config.listener('http_proxy').servers['web1'].should be_nil
131
+ expect(new_config.listener("http_proxy").servers["web1"]).to be_nil
120
132
  end
121
133
 
122
- it 'can re-render a config file with a server added' do
123
- l = @config.listener('http_proxy')
124
- l.add_server('web4', '99.99.99.99', :template => l.servers['web1'])
134
+ it "can re-render a config file with a server added" do
135
+ l = @config.listener("http_proxy")
136
+ l.add_server("web4", "99.99.99.99", template: l.servers["web1"])
125
137
 
126
138
  new_config_text = @config.render
127
139
 
128
140
  new_config = HAProxy::Parser.new.parse(new_config_text)
129
- s = new_config.listener('http_proxy').servers['web4']
130
- s.should_not be_nil
131
- s.name.should == 'web4'
132
- s.host.should == '99.99.99.99'
133
- s.port.should == '80'
134
- s.attributes.to_a.should == [
135
- ['weight','1'],
136
- ['maxconn','512'],
137
- ['check',true]
138
- ]
141
+ s = new_config.listener("http_proxy").servers["web4"]
142
+ expect(s).not_to be_nil
143
+ expect(s.name).to eq("web4")
144
+ expect(s.host).to eq("99.99.99.99")
145
+ expect(s.port).to eq("80")
146
+ expect(s.attributes.to_a).to eq([
147
+ ["weight", "1"],
148
+ ["maxconn", "512"],
149
+ ["check", true],
150
+ ])
139
151
  end
140
152
  end
141
153
  end
142
-
@@ -1,124 +1,148 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe "HAProxy::Parser" do
3
+ require "spec_helper"
4
4
 
5
- context 'multi-pool config file' do
5
+ describe "HAProxy::Parser" do
6
+ context "multi-pool config file" do
6
7
  before(:each) do
7
8
  @parser = HAProxy::Parser.new
8
- @config = @parser.parse_file('spec/fixtures/multi-pool.haproxy.cfg')
9
+ @config = @parser.parse_file("spec/fixtures/multi-pool.haproxy.cfg")
10
+ end
11
+
12
+ it "parses default_backend" do
13
+ expect(@config.frontends.size).to eq(1)
14
+ www_frontend = @config.frontend("www")
15
+ expect(www_frontend.config["default_backend"]).to eq("www_main")
16
+ end
17
+
18
+ it "parses defaults" do
19
+ expect(@config.defaults.size).to eq(1)
20
+
21
+ defaults = @config.defaults.first
22
+ expect(defaults.config["log"]).to eq("global")
23
+ expect(defaults.config["mode"]).to eq("http")
24
+ expect(defaults.config["retries"]).to eq("3")
25
+ expect(defaults.config["redispatch"]).to be_nil
26
+ expect(defaults.config["maxconn"]).to eq("10000")
27
+ expect(defaults.config["contimeout"]).to eq("5000")
28
+ expect(defaults.config["clitimeout"]).to eq("60000")
29
+ expect(defaults.config["srvtimeout"]).to eq("60000")
30
+ expect(defaults.config["stats"]).to eq("uri /haproxy-status")
31
+ expect(defaults.config["cookie"]).to eq("SERVERID insert indirect nocache")
32
+
33
+ expect(defaults.options.size).to eq(2)
34
+ expect(defaults.options).to include("httplog")
35
+ expect(defaults.options).to include("dontlognull")
9
36
  end
10
37
 
11
38
  it "parses a named backend from a config file" do
12
- @config.backends.size.should == 2
13
- logs_backend = @config.backend('logs')
14
-
15
- logs_backend.servers.size.should == 4
16
-
17
- server1 = logs_backend.servers['prd_log_1']
18
- server1.name.should == 'prd_log_1'
19
- server1.host.should == '10.245.174.75'
20
- server1.port.should == '8000'
21
-
22
- server2 = logs_backend.servers['fake_logger']
23
- server2.name.should == 'fake_logger'
24
- server2.host.should == '127.0.0.1'
25
- server2.port.should == '9999'
26
-
27
- server3 = logs_backend.servers['prd_log_2']
28
- server3.name.should == 'prd_log_2'
29
- server3.host.should == '10.215.157.10'
30
- server3.port.should == '8000'
31
-
32
- server3 = logs_backend.servers['prd_log_3']
33
- server3.name.should == 'prd_log_3'
34
- server3.host.should == 'cloudloghost1'
35
- server3.port.should == '8000'
39
+ expect(@config.backends.size).to eq(2)
40
+ logs_backend = @config.backend("logs")
41
+
42
+ expect(logs_backend.servers.size).to eq(4)
43
+
44
+ server1 = logs_backend.servers["prd_log_1"]
45
+ expect(server1.name).to eq("prd_log_1")
46
+ expect(server1.host).to eq("10.245.174.75")
47
+ expect(server1.port).to eq("8000")
48
+
49
+ server2 = logs_backend.servers["fake_logger"]
50
+ expect(server2.name).to eq("fake_logger")
51
+ expect(server2.host).to eq("127.0.0.1")
52
+ expect(server2.port).to eq("9999")
53
+
54
+ server3 = logs_backend.servers["prd_log_2"]
55
+ expect(server3.name).to eq("prd_log_2")
56
+ expect(server3.host).to eq("10.215.157.10")
57
+ expect(server3.port).to eq("8000")
58
+
59
+ server3 = logs_backend.servers["prd_log_3"]
60
+ expect(server3.name).to eq("prd_log_3")
61
+ expect(server3.host).to eq("cloudloghost1")
62
+ expect(server3.port).to eq("8000")
36
63
  end
37
64
  end
38
65
 
39
- context 'basic 1.5 config file' do
66
+ context "basic 1.5 config file" do
40
67
  before(:each) do
41
68
  @parser = HAProxy::Parser.new
42
- @config = @parser.parse_file('spec/fixtures/simple.haproxy15.cfg')
69
+ @config = @parser.parse_file("spec/fixtures/simple.haproxy15.cfg")
43
70
  end
44
71
 
45
72
  it "parses structured configs" do
46
73
  defaults = @config.defaults.first.config
47
74
 
48
- defaults['timeout connect'].should == '5000ms'
49
- defaults['timeout client'].should == '5000ms'
50
- defaults['timeout server'].should == '5000ms'
51
-
52
- defaults['errorfile 400'].should == '/etc/haproxy/errors/400.http'
53
- defaults['errorfile 504'].should == '/etc/haproxy/errors/504.http'
75
+ expect(defaults["timeout connect"]).to eq("5000ms")
76
+ expect(defaults["timeout client"]).to eq("5000ms")
77
+ expect(defaults["timeout server"]).to eq("5000ms")
54
78
 
79
+ expect(defaults["errorfile 400"]).to eq("/etc/haproxy/errors/400.http")
80
+ expect(defaults["errorfile 504"]).to eq("/etc/haproxy/errors/504.http")
55
81
  end
56
-
57
82
  end
58
83
 
59
- context 'basic config file' do
84
+ context "basic config file" do
60
85
  before(:each) do
61
86
  @parser = HAProxy::Parser.new
62
- @config = @parser.parse_file('spec/fixtures/simple.haproxy.cfg')
87
+ @config = @parser.parse_file("spec/fixtures/simple.haproxy.cfg")
63
88
  end
64
89
 
65
90
  it "parses global variables from a config file" do
66
- @config.global.size.should == 3
67
- @config.global['maxconn'].should == '4096'
68
- @config.global['daemon'].should == nil
69
- @config.global['nbproc'].should == '4'
91
+ expect(@config.global.size).to eq(3)
92
+ expect(@config.global["maxconn"]).to eq("4096")
93
+ expect(@config.global["daemon"]).to be_nil
94
+ expect(@config.global["nbproc"]).to eq("4")
70
95
  end
71
96
 
72
97
  it "parses a default set from a config file" do
73
- @config.defaults.size.should == 1
98
+ expect(@config.defaults.size).to eq(1)
74
99
 
75
100
  defaults = @config.defaults.first
76
- defaults.config['mode'].should == 'http'
77
- defaults.config['clitimeout'].should == '60000'
78
- defaults.config['srvtimeout'].should == '30000'
79
- defaults.config['contimeout'].should == '4000'
101
+ expect(defaults.config["mode"]).to eq("http")
102
+ expect(defaults.config["clitimeout"]).to eq("60000")
103
+ expect(defaults.config["srvtimeout"]).to eq("30000")
104
+ expect(defaults.config["contimeout"]).to eq("4000")
80
105
 
81
- defaults.options.size.should == 1
82
- defaults.options.should include('httpclose')
106
+ expect(defaults.options.size).to eq(1)
107
+ expect(defaults.options).to include("httpclose")
83
108
  end
84
109
 
85
- it 'parses a listener from a config file' do
86
- @config.listeners.size.should == 1
87
-
88
- listener = @config.listener('http_proxy')
89
- listener.name.should == 'http_proxy'
90
- listener.host.should == '55.55.55.55'
91
- listener.port.should == '80'
92
- listener.config['balance'].should == 'roundrobin'
93
-
94
- listener.options.size.should == 2
95
- listener.options.should include('httpchk')
96
- listener.options.should include('forwardfor')
97
-
98
- listener.servers.size.should == 3
99
-
100
- server1 = listener.servers['web1']
101
- server1.name.should == 'web1'
102
- server1.host.should == 'dnshost66'
103
- server1.port.should == '80'
104
- server1.attributes['weight'].should == '1'
105
- server1.attributes['maxconn'].should == '512'
106
- server1.attributes['check'].should == true
107
-
108
- server2 = listener.servers['web2']
109
- server2.name.should == 'web2'
110
- server2.host.should == '77.77.77.77'
111
- server2.port.should == '80'
112
- server2.attributes['weight'].should == '1'
113
- server2.attributes['maxconn'].should == '512'
114
- server2.attributes['check'].should == true
115
-
116
- server3 = listener.servers['web3']
117
- server3.name.should == 'web3'
118
- server3.host.should == '88.88.88.88'
119
- server3.port.should == '80'
120
- server3.attributes.should be_empty
110
+ it "parses a listener from a config file" do
111
+ expect(@config.listeners.size).to eq(1)
112
+
113
+ listener = @config.listener("http_proxy")
114
+ expect(listener.name).to eq("http_proxy")
115
+ expect(listener.host).to eq("55.55.55.55")
116
+ expect(listener.port).to eq("80")
117
+ expect(listener.config["balance"]).to eq("roundrobin")
118
+
119
+ expect(listener.options.size).to eq(2)
120
+ expect(listener.options).to include("httpchk")
121
+ expect(listener.options).to include("forwardfor")
122
+
123
+ expect(listener.servers.size).to eq(3)
124
+
125
+ server1 = listener.servers["web1"]
126
+ expect(server1.name).to eq("web1")
127
+ expect(server1.host).to eq("dnshost66")
128
+ expect(server1.port).to eq("80")
129
+ expect(server1.attributes["weight"]).to eq("1")
130
+ expect(server1.attributes["maxconn"]).to eq("512")
131
+ expect(server1.attributes["check"]).to eq(true)
132
+
133
+ server2 = listener.servers["web2"]
134
+ expect(server2.name).to eq("web2")
135
+ expect(server2.host).to eq("77.77.77.77")
136
+ expect(server2.port).to eq("80")
137
+ expect(server2.attributes["weight"]).to eq("1")
138
+ expect(server2.attributes["maxconn"]).to eq("512")
139
+ expect(server2.attributes["check"]).to eq(true)
140
+
141
+ server3 = listener.servers["web3"]
142
+ expect(server3.name).to eq("web3")
143
+ expect(server3.host).to eq("88.88.88.88")
144
+ expect(server3.port).to eq("80")
145
+ expect(server3.attributes).to be_empty
121
146
  end
122
147
  end
123
148
  end
124
-