spec/kontena/cli/services/exec_command_spec.rb in kontena-cli-1.3.0.rc1 vs spec/kontena/cli/services/exec_command_spec.rb in kontena-cli-1.3.0.rc2

- old
+ new

@@ -1,6 +1,6 @@ -require 'websocket-client-simple' +require 'kontena/websocket/client' require 'kontena/cli/services/exec_command' describe Kontena::Cli::Services::ExecCommand do include ClientHelpers include OutputHelpers @@ -22,10 +22,12 @@ @callbacks[:open].call } end end + def connect ; end + def receive_message(msg) @callbacks[:message].call(Event.new(JSON.dump(msg))) rescue => exc STDERR.puts exc.message end @@ -65,12 +67,12 @@ before do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) end it "Executes on the running container by default" do - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do |foo| + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do |foo| ws_client.receive_message({'stream' => 'stdout', 'chunk' => "ok\n"}) ws_client.receive_message({'exit' => 0}) end expect { @@ -103,45 +105,45 @@ ] } end it "Executes on the first running container by default" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do respond_ok(ws_client) end expect { subject.run(['test-service', 'test']) }.to output("ok\n").to_stdout end it "Executes on the first running container, even if they are ordered differently" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return({'containers' => service_containers['containers'].reverse }) - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do respond_ok(ws_client) end expect { subject.run(['test-service', 'test']) }.to output("ok\n").to_stdout end it "Executes on the first running container if given" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do respond_ok(ws_client) end expect { subject.run(['test-service', 'test']) }.to output("ok\n").to_stdout end it "Executes on the second running container if given" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-2/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-2/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do respond_ok(ws_client) end expect { subject.run(['--instance', '2', 'test-service', 'test']) }.to output("ok\n").to_stdout @@ -156,12 +158,12 @@ it "Executes on each running container" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) 3.times do |i| ws_client = ws_client_class.new - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i + 1}/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i + 1}/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do ws_client.receive_message({'stream' => 'stdout', 'chunk' => "test#{i + 1}\n"}) ws_client.receive_message({'exit' => 0}) end end @@ -170,12 +172,12 @@ }.to output("test1\ntest2\ntest3\n").to_stdout end it "Stops if the first container fails" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-1/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do respond_error(ws_client) end expect { subject.run(['--silent', '--all', 'test-service', 'test']) }.to output("error\n").to_stderr @@ -184,12 +186,12 @@ it "Stops if the second container fails" do expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) i = 1 [:ok, :err].each do |status| ws_client = ws_client_class.new - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i}/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i}/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do if status == :ok respond_ok(ws_client) else respond_error(ws_client) end @@ -205,11 +207,11 @@ expect(client).to receive(:get).with('services/test-grid/null/test-service/containers').and_return(service_containers) i = 1 [:ok, :err, :ok].each do |status| ws_client = ws_client_class.new - expect(WebSocket::Client::Simple).to receive(:connect).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i}/exec", anything).and_return(ws_client) - expect(ws_client).to receive(:send) do + expect(Kontena::Websocket::Client).to receive(:new).with("#{master_url}v1/containers/test-grid/host/test-service.container-#{i}/exec?", anything).and_return(ws_client) + expect(ws_client).to receive(:text) do if status == :ok respond_ok(ws_client) else respond_error(ws_client) end