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(share_name = 'sambal_test', run_as = ENV['USER']) ⇒ TestServer

Returns a new instance of TestServer


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sambal/test_server.rb', line 38

def initialize(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 = File.expand_path(File.dirname(File.dirname(File.dirname(__FILE__))))
  @tmp_path = ENV.key?('SAMBAL_TEMP_PATH') ? ENV['SAMBAL_TEMP_PATH'] : "#{root_path}/spec_tmp"
  @share_path = "#{tmp_path}/share"
  @share_name = share_name
  @config_path = "#{tmp_path}/smb.conf"
  @lock_path = "#{tmp_path}"
  @pid_dir = "#{tmp_path}"
  @cache_dir = "#{tmp_path}"
  @state_dir = "#{tmp_path}"
  @log_path = "#{tmp_path}"
  @private_dir = "#{tmp_path}"
  @ncalrpc_dir = "#{tmp_path}"
  @port = Random.new(Time.now.to_i).rand(2345..5678).to_i
  @run_as = run_as
  FileUtils.mkdir_p @share_path
  File.chmod 0777, @share_path
  write_config
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir


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

def cache_dir
  @cache_dir
end

#config_pathObject (readonly)

Returns the value of attribute config_path


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

def config_path
  @config_path
end

#hostObject (readonly)

Returns the value of attribute host


36
37
38
# File 'lib/sambal/test_server.rb', line 36

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

#private_dirObject (readonly)

Returns the value of attribute private_dir


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

def private_dir
  @private_dir
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


35
36
37
# File 'lib/sambal/test_server.rb', line 35

def run_as
  @run_as
end

#share_nameObject (readonly)

Returns the value of attribute share_name


34
35
36
# File 'lib/sambal/test_server.rb', line 34

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

#state_dirObject (readonly)

Returns the value of attribute state_dir


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

def state_dir
  @state_dir
end

#tmp_pathObject (readonly)

Returns the value of attribute tmp_path


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

def tmp_path
  @tmp_path
end

Instance Method Details

#find_pidsObject


60
61
62
63
# File 'lib/sambal/test_server.rb', line 60

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


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sambal/test_server.rb', line 71

def start
  if RUBY_PLATFORM=="java"
    @smb_server_pid = Thread.new do
      `smbd -S -F -s #{@config_path} -p #{@port} --option="lockdir"=#{@lock_path} --option="pid directory"=#{@pid_dir} --option="private directory"=#{@private_dir} --option="cache directory"=#{@cache_dir} --option="state directory"=#{@state_dir} < /dev/null > #{@log_path}/smb.log`
    end
  else
    @smb_server_pid = fork do
      exec "smbd -S -F -s #{@config_path} -p #{@port} --option=\"lockdir\"=#{@lock_path} --option=\"pid directory\"=#{@pid_dir} --option=\"private directory\"=#{@private_dir} --option=\"cache directory\"=#{@cache_dir} --option=\"state directory\"=#{@state_dir} < /dev/null > #{@log_path}/smb.log"
    end
  end
  sleep 2 ## takes a short time to start up
end

#stopObject


84
85
86
87
88
# File 'lib/sambal/test_server.rb', line 84

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


90
91
92
93
# File 'lib/sambal/test_server.rb', line 90

def stop!
  stop
  FileUtils.rm_rf @tmp_path unless ENV.key?('KEEP_SMB_TMP')
end

#write_configObject


65
66
67
68
69
# File 'lib/sambal/test_server.rb', line 65

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, log_path: @log_path, ncalrpc_dir: @ncalrpc_dir)
  end
end