|
7 | 7 | require "io/wait" |
8 | 8 |
|
9 | 9 | RSpec.describe "Terminalwire Install", type: :system do |
10 | | - let(:binary_name) { "hello" } |
11 | | - let(:gem_path) { File.expand_path('../../../gem/terminalwire', __FILE__) } |
12 | | - let(:exe_path) { File.join(gem_path, "exe") } |
13 | | - |
14 | | - PORT = 3000 |
| 10 | + DOCKER_IMAGE = "terminalwire-rails-server" |
| 11 | + BINARY_NAME = "bin/hello" |
| 12 | + PORT = 3000 |
15 | 13 |
|
16 | 14 | before(:all) do |
17 | | - `docker buildx -t terminalwire-rails-server -f containers/rails/Dockerfile .` |
18 | | - @docker_id = `docker run -p 3000:#{PORT} -d terminalwire-rails-server`.chomp |
19 | | - wait_for_server("0.0.0.0", PORT) |
20 | | - |
21 | | - @path = Pathname.new(Dir.mktmpdir) |
22 | | - @bin_path = @path.join("bin").tap(&:mkdir) |
| 15 | + build_command = "docker build -t #{DOCKER_IMAGE} -f containers/rails/Dockerfile ." |
| 16 | + system(build_command) or raise "Docker build failed: #{build_command}" |
23 | 17 |
|
24 | | - Terminalwire::Binary.write( |
25 | | - url: "http://localhost:#{PORT}/terminal", |
26 | | - to: @bin_path.join("hello") |
27 | | - ) |
| 18 | + # Run the container without external port binding and capture the container ID. |
| 19 | + @docker_id = `docker run --rm -d #{DOCKER_IMAGE}`.chomp |
| 20 | + raise "Docker run failed" if @docker_id.empty? |
28 | 21 |
|
29 | | - ENV["PATH"] = "#{@bin_path.to_s}:#{ENV["PATH"]}" |
30 | | - |
31 | | - Dir.chdir(@path) do |
32 | | - Bundler.with_unbundled_env do |
33 | | - `bundle install --path #{@path} --binstubs=#{@bin_path} --quiet` |
34 | | - end |
35 | | - end |
| 22 | + wait_for_server_in_container(timeout: 15) |
36 | 23 | end |
37 | 24 |
|
38 | 25 | after(:all) do |
39 | | - `docker stop #{@docker_id}` if @docker_id |
| 26 | + system("docker stop #{@docker_id}") if @docker_id |
40 | 27 | end |
41 | 28 |
|
42 | 29 | it "runs Terminalwire client against server" do |
43 | | - # Run the binary and capture output |
44 | | - output, status = Open3.capture2e("#{binary_name} hello World") |
| 30 | + command = "docker exec #{@docker_id} #{BINARY_NAME} hello World" |
| 31 | + output, status = Open3.capture2e(command) |
45 | 32 | expect(output.strip).to eql "Hello World" |
46 | 33 | expect(status).to be_success |
47 | 34 | end |
48 | 35 |
|
49 | 36 | it "logs in successfully" do |
50 | | - PTY.spawn("#{binary_name} login") do |stdout, stdin, pid| |
51 | | - # stdout.readpartial("Email: ".size) |
52 | | - # Simulate entering email and password |
| 37 | + command = "docker exec -i #{@docker_id} #{BINARY_NAME} login" |
| 38 | + PTY.spawn(command) do |stdout, stdin, pid| |
| 39 | + sleep 0.5 |
53 | 40 | |
54 | | - |
55 | | - # stdout.readpartial("Password: ".size) |
| 41 | + sleep 0.5 |
56 | 42 | stdin.puts "password123" |
57 | | - |
58 | 43 | output = stdout.read |
59 | 44 | expect(output).to include("Successfully logged in as [email protected].") |
60 | | - |
61 | | - # Ensure the process was successful |
62 | 45 | Process.wait(pid) |
63 | 46 | expect($?.success?).to be_truthy |
64 | 47 | end |
65 | 48 | end |
66 | 49 |
|
67 | 50 | private |
68 | 51 |
|
69 | | - def wait_for_server(host, port, timeout: 5) |
| 52 | + def wait_for_server_in_container(timeout:) |
70 | 53 | start_time = Time.now |
71 | 54 | until Time.now - start_time > timeout |
72 | | - begin |
73 | | - TCPSocket.new(host, port).close |
74 | | - return true |
75 | | - rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH |
76 | | - sleep 0.1 |
77 | | - end |
| 55 | + response = `docker exec #{@docker_id} curl -s http://localhost:3000/health` |
| 56 | + return if !response.strip.empty? |
| 57 | + sleep 0.5 |
78 | 58 | end |
79 | 59 | raise "Server did not start within #{timeout} seconds" |
80 | 60 | end |
|
0 commit comments