From afeef33bc16e9ec2745d7d5367a51dd0b053800d Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sun, 24 Sep 2023 11:58:11 -0700 Subject: [PATCH] Add tests for multiple service sockets --- Library/Homebrew/test/service_spec.rb | 121 +++++++++++++++++++------- 1 file changed, 90 insertions(+), 31 deletions(-) diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index bece2ae9d9..1045dfed1d 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -14,6 +14,15 @@ describe Homebrew::Service do end end + def stub_formula_with_service_sockets(sockets_var) + stub_formula do + service do + run opt_bin/"beanstalkd" + sockets sockets_var + end + end + end + describe "#std_service_path_env" do it "returns valid std_service_path_env" do f = stub_formula do @@ -102,43 +111,33 @@ describe Homebrew::Service do end describe "#sockets" do - it "throws for missing type" do - f = stub_formula do - service do - run opt_bin/"beanstalkd" - sockets "127.0.0.1:80" - end - end + let(:sockets_type_error_message) { "Service#sockets a formatted socket definition as ://:" } - expect do - f.service.manual_command - end.to raise_error TypeError, "Service#sockets a formatted socket definition as ://:" + it "throws for missing type" do + [ + stub_formula_with_service_sockets("127.0.0.1:80"), + stub_formula_with_service_sockets({ "Socket" => "127.0.0.1:80" }), + ].each do |f| + expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message + end end it "throws for missing host" do - f = stub_formula do - service do - run opt_bin/"beanstalkd" - sockets "tcp://:80" - end + [ + stub_formula_with_service_sockets("tcp://:80"), + stub_formula_with_service_sockets({ "Socket" => "tcp://:80" }), + ].each do |f| + expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message end - - expect do - f.service.manual_command - end.to raise_error TypeError, "Service#sockets a formatted socket definition as ://:" end it "throws for missing port" do - f = stub_formula do - service do - run opt_bin/"beanstalkd" - sockets "tcp://127.0.0.1" - end + [ + stub_formula_with_service_sockets("tcp://127.0.0.1"), + stub_formula_with_service_sockets({ "Socket" => "tcp://127.0.0.1" }), + ].each do |f| + expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message end - - expect do - f.service.manual_command - end.to raise_error TypeError, "Service#sockets a formatted socket definition as ://:" end end @@ -259,10 +258,59 @@ describe Homebrew::Service do end it "returns valid plist with socket" do + plist_expect = <<~EOS + + + + + \tLabel + \thomebrew.mxcl.formula_name + \tLimitLoadToSessionType + \t + \t\tAqua + \t\tBackground + \t\tLoginWindow + \t\tStandardIO + \t\tSystem + \t + \tProgramArguments + \t + \t\t#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd + \t + \tRunAtLoad + \t + \tSockets + \t + \t\tListeners + \t\t + \t\t\tSockFamily + \t\t\tIPv4v6 + \t\t\tSockNodeName + \t\t\t127.0.0.1 + \t\t\tSockProtocol + \t\t\tTCP + \t\t\tSockServiceName + \t\t\t80 + \t\t + \t + + + EOS + + [ + stub_formula_with_service_sockets("tcp://127.0.0.1:80"), + stub_formula_with_service_sockets({ "Listeners" => "tcp://127.0.0.1:80" }), + ].each do |f| + plist = f.service.to_plist + expect(plist).to eq(plist_expect) + end + end + + it "returns valid plist with multiple sockets" do f = stub_formula do service do run [opt_bin/"beanstalkd", "test"] - sockets "tcp://127.0.0.1:80" + sockets "Socket" => "tcp://0.0.0.0:80", "SocketTLS" => "tcp://0.0.0.0:443" end end @@ -291,17 +339,28 @@ describe Homebrew::Service do \t \tSockets \t - \t\tListeners + \t\tSocket \t\t \t\t\tSockFamily \t\t\tIPv4v6 \t\t\tSockNodeName - \t\t\t127.0.0.1 + \t\t\t0.0.0.0 \t\t\tSockProtocol \t\t\tTCP \t\t\tSockServiceName \t\t\t80 \t\t + \t\tSocketTLS + \t\t + \t\t\tSockFamily + \t\t\tIPv4v6 + \t\t\tSockNodeName + \t\t\t0.0.0.0 + \t\t\tSockProtocol + \t\t\tTCP + \t\t\tSockServiceName + \t\t\t443 + \t\t \t