Added map validations.

This commit is contained in:
Glenn Y. Rolland 2015-08-12 09:29:59 +02:00
parent b835a8f587
commit f6d90e96f3
6 changed files with 93 additions and 35 deletions

View file

@ -2,9 +2,9 @@
require 'qasim/constants'
module Qasim
autoload :Config, 'qasim/config'
autoload :Map, 'qasim/map'
autoload :Ui, 'qasim/ui'
autoload :Cli, 'qasim/cli'
autoload :Config, 'qasim/config'
autoload :Map, 'qasim/map'
autoload :Ui, 'qasim/ui'
autoload :Cli, 'qasim/cli'
end

View file

@ -1,12 +1,24 @@
require 'fileutils'
require 'qasim/map/generic'
require 'qasim/map/ssh'
require 'qasim/map/webdav'
module Qasim ; module Map
class ParseError < RuntimeError ; end
class ConnectError < RuntimeError ; end
def class_for type
plugin = nil
ObjectSpace.each_object(Class) do |cls|
if cls < Qasim::Map::Generic then
plugin = cls if cls.handles.include? type.to_sym
end
end
plugin
end
#
# replace magic values withing map lines
#
@ -42,7 +54,9 @@ module Qasim ; module Map
# Load description from file and create a Map object
#
def from_file appcfg, filename
config = {}
config = {
type: :ssh # for config V1, we assume SSHFS
}
map = nil
f = File.open filename
@ -93,6 +107,8 @@ module Qasim ; module Map
f.puts "REMOTE_CYPHER=%s" % @cypher
end
end
module_function :from_file, :env_substitute
module_function :from_file,
:env_substitute,
:class_for
end ; end

View file

@ -1,11 +1,10 @@
require 'fileutils'
require 'qasim/map'
module Qasim ; module Map
end ; end
class Qasim::Map::Generic
def initialize config
def initialize params
end
@ -14,6 +13,7 @@ class Qasim::Map::Generic
# Format :
# Hash of (name:Symbol * [value:Object, optional:Boolean])
def self.parameters
{
map_name: [nil , true],
@ -22,4 +22,28 @@ class Qasim::Map::Generic
}
end
#
# Test map liveness (connected & working)
#
def alive?
end
#
# Test map
#
def mounted?
end
#
# Mount
#
def mount
end
#
# Umount
#
def umount
end
end

View file

@ -12,13 +12,20 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
:name
CYPHER_ARCFOUR = :arcfour
CYPHER_AES256CBC = "aes-256-cbc".to_sym
CYPHERS = [ CYPHER_ARCFOUR, CYPHER_AES256CBC ]
CYPHER_AES256CBC = :"aes-256-cbc"
CYPHERS = [
CYPHER_ARCFOUR,
CYPHER_AES256CBC
]
def self.parameters
super
end
def self.handles
[ :ssh, :sshfs ]
end
#
# Set defaults properties for maps
#
@ -37,21 +44,11 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
self.load @path
end
#
# Test map liveness (how ?)
# FIXME: not implemented
#
def online?
#rdebug "testing online? %s " % self.inspect
#FIXME: test liveness
end
#
# Test if map is connected / mounted
#
def connected?
def mounted?
f = File.open("/proc/mounts")
sshfs_mounted = (f.readlines.select do |line|
line =~ /\s+fuse.sshfs\s+/
@ -79,14 +76,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
#
# Connect map
#
def connect &block
puts "[#{File.basename @path}] Connecting..."
puts " #{@user}@#{@host}:#{@port}"
#puts " links = %s" % @links.map{ |k,v| "%s => %s" % [ k, v ] }.join(', ')
# do something
# test server connection
# mount
#
def mount &block
# FIXME: test connexion with Net::SSH + timeout or ask password
@links.each do |name, remotepath|
localpath = File.join ENV['HOME'], "mnt", name
@ -111,7 +101,6 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
"-o","Port=%s" % @port,
"%s@%s:%s" % [@user,@host,remotepath],
localpath ]
#rdebug "command: %s" % [ cmd, cmd_args ].flatten.join(' ')
if block_given? then
yield name, cmd, cmd_args
else
@ -127,8 +116,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
#
# Disconnect map
#
def disconnect &block
puts "Disconnecting map #{@path}"
def umount &block
@links.each do |name, remotepath|
localpath = File.join ENV['HOME'], "mnt", name
cmd = "fusermount"
@ -148,3 +136,4 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
end
end
end

View file

@ -6,7 +6,7 @@ class Qasim::Map::Webdav < Qasim::Map::Generic
def initialize
end
def requirements
def self.parameters
req = super
req << :webdav_user # ex: foo
req << :webdav_password # ex: bar
@ -14,5 +14,9 @@ class Qasim::Map::Webdav < Qasim::Map::Generic
req << :webdav_protocol # ex: http, https
req
end
def self.handles
[ :webdav, :fusedav ]
end
end

View file

@ -1,9 +1,17 @@
require_relative 'spec_helper'
require 'minitest/spec'
require 'qasim/map'
require 'securerandom'
class Qasim::Map::Fake < Qasim::Map::Generic
def self.handles
[:fake]
end
end
describe Qasim::Map do
describe 'env_substitute' do
describe 'env_substitute' do# {{{
it "returns a normal string unchanged" do
str = "5cb0c49325df2d526116ef7b49eb7329"
assert_equal Qasim::Map.env_substitute(str), str
@ -35,10 +43,27 @@ describe Qasim::Map do
ref = "SOMETHING = OK"
assert_equal ref, Qasim::Map.env_substitute(str)
end
end# }}}
describe 'class_for' do
it 'must return a class for given type' do
expected = Qasim::Map::Fake
result = Qasim::Map.class_for 'fake'
assert_equal expected, result
end
it 'must return nil when no type found' do
random_string = SecureRandom.hex
expected = nil
result = Qasim::Map.class_for random_string
assert_equal expected, result
end
end
describe 'from_file' do
end
end