Class: Sambal::TestServer

Inherits:
Object
  • Object
show all
Defined in:
lib/sambal/test_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = "/tmp/sambal_test_server_#{Time.now.to_i}", share_name = 'sambal_test', run_as = ENV['USER']) ⇒ TestServer

Returns a new instance of TestServer.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sambal/test_server.rb', line 34

def initialize(root_path="/tmp/sambal_test_server_#{Time.now.to_i}", share_name='sambal_test', run_as=ENV['USER'])
  @erb_path = "#{File.expand_path(File.dirname(__FILE__))}/smb.conf.erb"
  @host = "127.0.0.1" ## will always just be localhost
  @root_path = root_path
  @share_path = "#{root_path}/share"
  @share_name = share_name
  @config_path = "#{root_path}/smb.conf"
  @lock_path = "#{root_path}"
  @pid_dir = "#{root_path}"
  @port = Random.new(Time.now.to_i).rand(2345..5678).to_i
  @run_as = run_as
  FileUtils.mkdir_p @share_path
  write_config
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



29
30
31
# File 'lib/sambal/test_server.rb', line 29

def config_path
  @config_path
end

#hostObject (readonly)

Returns the value of attribute host.



32
33
34
# File 'lib/sambal/test_server.rb', line 32

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



26
27
28
# File 'lib/sambal/test_server.rb', line 26

def port
  @port
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



28
29
30
# File 'lib/sambal/test_server.rb', line 28

def root_path
  @root_path
end

#run_asObject (readonly)

Returns the value of attribute run_as.



31
32
33
# File 'lib/sambal/test_server.rb', line 31

def run_as
  @run_as
end

#share_nameObject (readonly)

Returns the value of attribute share_name.



30
31
32
# File 'lib/sambal/test_server.rb', line 30

def share_name
  @share_name
end

#share_pathObject (readonly)

Returns the value of attribute share_path.



27
28
29
# File 'lib/sambal/test_server.rb', line 27

def share_path
  @share_path
end

Instance Method Details

#find_pidsObject



49
50
51
52
# File 'lib/sambal/test_server.rb', line 49

def find_pids
  pids = `ps ax | grep smbd | grep #{@port} | grep -v grep | awk '{print \$1}'`.chomp
  pids.split("\n").map {|p| (p.nil? || p=='') ? nil : p.to_i }
end

#startObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sambal/test_server.rb', line 60

def start
  if RUBY_PLATFORM=="java"
    @smb_server_pid = Thread.new do
      `smbd -S -F -s #{@config_path} -p #{@port} --lockdir=#{@lock_path} --piddir=#{@pid_dir}`
    end
  else
    @smb_server_pid = fork do
      `smbd -S -F -s #{@config_path} -p #{@port} --lockdir=#{@lock_path} --piddir=#{@pid_dir}`
    end
  end
  sleep 2 ## takes a short time to start up
end

#stopObject



73
74
75
76
77
# File 'lib/sambal/test_server.rb', line 73

def stop
  ## stopping is done in an ugly way by grepping
  pids = find_pids
  pids.each { |ppid| `kill -9 #{ppid} 2> /dev/null` }
end

#stop!Object



79
80
81
82
# File 'lib/sambal/test_server.rb', line 79

def stop!
  stop
  FileUtils.rm_rf @root_path
end

#write_configObject



54
55
56
57
58
# File 'lib/sambal/test_server.rb', line 54

def write_config
  File.open(@config_path, 'w') do |f|
    f << Document.new(IO.binread(@erb_path)).interpolate(samba_share: @share_path, local_user: @run_as, share_name: @share_name)
  end
end