mockws 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0fc10784350f028ff9da70f9b9bf05b81931cfdcd618e1cdc09477fccbb132eb
4
+ data.tar.gz: ff660ee38d9e40363f978c79f06d5ffee632cc2ae901a1af210d46515ea87b84
5
+ SHA512:
6
+ metadata.gz: c11fb2e102a4333cc79eb486da3d050d064a50fa866314e3cf4082f054e6faf9bd0d172d38e8e01b55ae3c9f664e575f34bdf1b577c9408f4d19a07bebdd6260
7
+ data.tar.gz: b2f5521c00364ffc72755a6907cd7b87f05319c4b7d730b5f192dea14afcc9444c07ad85936c7cf71a00ab8cb94221c12a6eea96d80c25f2b36e2ae79246b421
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org/'
2
+
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,128 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mockws (0.1.0)
5
+ carioca (~> 2.1)
6
+ sinatra (~> 3.1)
7
+ thin (~> 1.8)
8
+ thor (~> 1.2)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ base64 (0.1.1)
15
+ carioca (2.1.1)
16
+ deep_merge (~> 1.2)
17
+ i18n (~> 1.10)
18
+ locale (~> 2.1)
19
+ pastel (~> 0.8.0)
20
+ ps-ruby (~> 0.0.4)
21
+ tty-prompt (~> 0.23.1)
22
+ version (~> 1.1)
23
+ code_statistics (0.2.13)
24
+ concurrent-ruby (1.2.2)
25
+ daemons (1.4.1)
26
+ deep_merge (1.2.2)
27
+ diff-lcs (1.5.0)
28
+ eventmachine (1.2.7)
29
+ i18n (1.14.1)
30
+ concurrent-ruby (~> 1.0)
31
+ json (2.6.3)
32
+ language_server-protocol (3.17.0.3)
33
+ locale (2.1.3)
34
+ mustermann (3.0.0)
35
+ ruby2_keywords (~> 0.0.1)
36
+ parallel (1.23.0)
37
+ parser (3.2.2.3)
38
+ ast (~> 2.4.1)
39
+ racc
40
+ pastel (0.8.0)
41
+ tty-color (~> 0.5)
42
+ ps-ruby (0.0.4)
43
+ racc (1.7.1)
44
+ rack (2.2.8)
45
+ rack-protection (3.1.0)
46
+ rack (~> 2.2, >= 2.2.4)
47
+ rainbow (3.1.1)
48
+ rake (12.3.3)
49
+ regexp_parser (2.8.1)
50
+ rexml (3.2.6)
51
+ roodi (5.0.0)
52
+ ruby_parser (~> 3.2, >= 3.2.2)
53
+ rspec (3.12.0)
54
+ rspec-core (~> 3.12.0)
55
+ rspec-expectations (~> 3.12.0)
56
+ rspec-mocks (~> 3.12.0)
57
+ rspec-core (3.12.2)
58
+ rspec-support (~> 3.12.0)
59
+ rspec-expectations (3.12.3)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.12.0)
62
+ rspec-mocks (3.12.6)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.12.0)
65
+ rspec-support (3.12.1)
66
+ rubocop (1.56.4)
67
+ base64 (~> 0.1.1)
68
+ json (~> 2.3)
69
+ language_server-protocol (>= 3.17.0)
70
+ parallel (~> 1.10)
71
+ parser (>= 3.2.2.3)
72
+ rainbow (>= 2.2.2, < 4.0)
73
+ regexp_parser (>= 1.8, < 3.0)
74
+ rexml (>= 3.2.5, < 4.0)
75
+ rubocop-ast (>= 1.28.1, < 2.0)
76
+ ruby-progressbar (~> 1.7)
77
+ unicode-display_width (>= 2.4.0, < 3.0)
78
+ rubocop-ast (1.29.0)
79
+ parser (>= 3.2.1.0)
80
+ ruby-progressbar (1.13.0)
81
+ ruby2_keywords (0.0.5)
82
+ ruby_parser (3.20.3)
83
+ sexp_processor (~> 4.16)
84
+ sexp_processor (4.17.0)
85
+ sinatra (3.1.0)
86
+ mustermann (~> 3.0)
87
+ rack (~> 2.2, >= 2.2.4)
88
+ rack-protection (= 3.1.0)
89
+ tilt (~> 2.0)
90
+ thin (1.8.2)
91
+ daemons (~> 1.0, >= 1.0.9)
92
+ eventmachine (~> 1.0, >= 1.0.4)
93
+ rack (>= 1, < 3)
94
+ thor (1.2.2)
95
+ tilt (2.3.0)
96
+ tty-color (0.6.0)
97
+ tty-cursor (0.7.1)
98
+ tty-prompt (0.23.1)
99
+ pastel (~> 0.8)
100
+ tty-reader (~> 0.8)
101
+ tty-reader (0.9.0)
102
+ tty-cursor (~> 0.7)
103
+ tty-screen (~> 0.8)
104
+ wisper (~> 2.0)
105
+ tty-screen (0.8.1)
106
+ unicode-display_width (2.5.0)
107
+ version (1.1.1)
108
+ wisper (2.0.1)
109
+ yard (0.9.34)
110
+ yard-rspec (0.1)
111
+ yard
112
+
113
+ PLATFORMS
114
+ x86_64-linux
115
+
116
+ DEPENDENCIES
117
+ code_statistics (~> 0.2.13)
118
+ mockws!
119
+ rake (~> 12.0)
120
+ roodi (~> 5.0)
121
+ rspec (~> 3.0)
122
+ rubocop (~> 1.32)
123
+ version (~> 1.1)
124
+ yard (~> 0.9.27)
125
+ yard-rspec (~> 0.1)
126
+
127
+ BUNDLED WITH
128
+ 2.3.24
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ultragreen Software
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # MockWS
2
+ Webservice endpoint mock provider
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'version'
6
+ require 'rake/version_task'
7
+ require 'code_statistics'
8
+ require 'yard'
9
+ require 'yard/rake/yardoc_task'
10
+ require 'rubocop/rake_task'
11
+ require 'carioca/rake/manage'
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ Rake::VersionTask.new
16
+
17
+ RSpec::Core::RakeTask.new(:spec)
18
+
19
+ task default: :spec
20
+
21
+ YARD::Rake::YardocTask.new do |t|
22
+ t.files = ['lib/**/*.rb', '-', 'doc/**/*', 'spec/**/*_spec.rb']
23
+ t.options += ['-o', 'yardoc']
24
+ end
25
+
26
+ YARD::Config.load_plugin('yard-rspec')
27
+
28
+ namespace :yardoc do
29
+ task :clobber do
30
+ begin
31
+ rm_r 'yardoc'
32
+ rescue StandardError
33
+ nil
34
+ end
35
+ begin
36
+ rm_r '.yardoc'
37
+ rescue StandardError
38
+ nil
39
+ end
40
+ begin
41
+ rm_r 'pkg'
42
+ rescue StandardError
43
+ nil
44
+ end
45
+ end
46
+ end
47
+ task clobber: 'yardoc:clobber'
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ ---
2
+ en:
@@ -0,0 +1,2 @@
1
+ ---
2
+ fr:
data/config/mockws.yml ADDED
@@ -0,0 +1,43 @@
1
+ :mockws:
2
+ :default:
3
+ :output:
4
+ :type: :json
5
+ :services:
6
+ :main:
7
+ :services:
8
+ :mon_service:
9
+ :verb: :get
10
+ :route: "/mymockservice/data"
11
+ :type: :static
12
+ :path: "/tmp/data.csv"
13
+ :status: 206
14
+ :random_time: 5
15
+ :to: :yaml
16
+ :mon_service_2:
17
+ :verb: :get
18
+ :route: "/myotherservice/output"
19
+ :type: :inline
20
+ :data:
21
+ :toto_1: "titi"
22
+ :toto_2: "tutu"
23
+ :to: :json
24
+ :TEST1:
25
+ :verb: :get
26
+ :route: "/TEST1/data.csv"
27
+ :type: :static
28
+ :path: "/tmp/test1_data.csv"
29
+ :status: 200
30
+ :TEST2:
31
+ :verb: :get
32
+ :route: "/test2/data"
33
+ :type: :inline
34
+ :data:
35
+ - :value1: "2384"
36
+ :value2: "ok"
37
+ - :value1: "863"
38
+ :value2: "nok"
39
+ - :value1: "38927"
40
+ :value2: "partial"
41
+ :to: :json
42
+ :response_time: 2
43
+
data/config.ru ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require
5
+
6
+ require './app'
7
+
8
+ run MockWS::Service
data/exe/mockws ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'mockws'
5
+
6
+ require 'mockws/cli'
7
+
8
+ include MockWS::CLI
9
+
10
+ MainCommand.start(ARGV)
data/lib/mockws/cli.rb ADDED
@@ -0,0 +1,71 @@
1
+ module MockWS
2
+ module CLI
3
+ # The CLI Command structure for Thor
4
+ class MainCommand < Thor
5
+
6
+
7
+ class_option :debug, :desc => "Set log level to :debug", :aliases => "-d", :type => :boolean
8
+
9
+ def initialize(*args)
10
+ super
11
+ @output = Carioca::Registry.get.get_service name: :output
12
+ @finisher = Carioca::Registry.get.get_service name: :finisher
13
+ if options[:debug] then
14
+ @output.level = :debug
15
+ @output.debug "DEBUG activated"
16
+ end
17
+ end
18
+
19
+ # callback for managing ARGV errors
20
+ def self.exit_on_failure?
21
+ true
22
+ end
23
+
24
+ # Thor method : starting MockWS daemon
25
+ long_desc <<-LONGDESC
26
+ Starting MockWS Daemon\n
27
+ LONGDESC
28
+ option :foreground, :type => :boolean, :aliases => "-F"
29
+ desc 'start', 'start the MockWS service'
30
+ def start
31
+ @finisher.secure_execute! exit_case: :success_exit do
32
+ MockWS::Daemon::Controller::start options
33
+ end
34
+ end
35
+
36
+ # Thor method : running of Appifier sanitycheck
37
+ desc 'sanitycheck', 'Verify installation of MockWS for user'
38
+ def sanitycheck
39
+ @finisher.secure_execute! exit_case: :sanitycheck_success do
40
+ MockWS::Configuration::Checker.sanitycheck
41
+ end
42
+
43
+ end
44
+
45
+
46
+ # Thor method : stopping MockWS daemon
47
+ long_desc <<-LONGDESC
48
+ Stopping MockWS Daemon\n
49
+ LONGDESC
50
+ option :quiet, :type => :boolean, :aliases => "-q"
51
+ desc 'stop', 'stop the MockWS service'
52
+ def stop
53
+ @finisher.secure_execute! exit_case: :success_exit do
54
+ MockWS::Daemon::Controller::stop options
55
+ end
56
+ end
57
+
58
+ # Thor method : status for MockWS daemon
59
+ long_desc <<-LONGDESC
60
+ Status for MockWS Daemon\n
61
+ LONGDESC
62
+ desc 'status', 'Status for the MockWS service'
63
+ def status
64
+ @finisher.secure_execute! exit_case: :success_exit do
65
+ MockWS::Daemon::Controller::status options
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,111 @@
1
+ module MockWS
2
+
3
+ module Daemon
4
+ class Controller
5
+
6
+ DAEMON_NAME = "MockWS : Daemon"
7
+ MOCKWS_PATH = File::expand_path("~/.mockws")
8
+ PID_FILE = MOCKWS_PATH + "/mockws.pid"
9
+ STDOUT_TRACE = MOCKWS_PATH + "/stdout.trace"
10
+ STDERR_TRACE = MOCKWS_PATH + "/stderr.trace"
11
+
12
+ extend Carioca::Injector
13
+ inject service: :configuration
14
+ inject service: :output
15
+ inject service: :finisher
16
+ inject service: :toolbox
17
+
18
+ def self.start options
19
+ output.level = :fatal if options[:quiet]
20
+ realpid = toolbox.get_processes pattern: DAEMON_NAME
21
+
22
+ unless File::exist? PID_FILE then
23
+ unless realpid.empty? then
24
+ finisher.secure_raise error_case: :already_exist, message: "MockWS Process already launched "
25
+ end
26
+
27
+ daemon_config = {:description => DAEMON_NAME,
28
+ :pid_file => PID_FILE,
29
+ :stdout_trace => STDOUT_TRACE,
30
+ :stderr_trace => STDERR_TRACE,
31
+ :foreground => options[:foreground]
32
+ }
33
+
34
+ ["int","term","hup"].each do |type| daemon_config["sig#{type}_handler".to_sym] = Proc::new { MockWS::Service.quit! } end
35
+ res = daemonize daemon_config do
36
+ output.info "Starting MockWS Daemon"
37
+ MockWS::Service::init
38
+ MockWS::Service.run!
39
+ end
40
+ sleep 1
41
+ if res == 0 then
42
+ pid = `cat #{PID_FILE}`.to_i
43
+ output.ok "MockWS Started, with PID : #{pid}"
44
+ output.ok "MockWS successfully loaded."
45
+ else
46
+ finisher.secure_raise error_case: :unknown_error, message: "MockWS loading error, see logs for more details."
47
+ end
48
+
49
+ else
50
+ finisher.secure_raise error_case: :already_exist, message: "Pid File, please verify if MockWS is running."
51
+ end
52
+ end
53
+
54
+ # Stop MockWS daemon
55
+ # @param [Hash] options
56
+ # @option options [Symbol] :quiet activate quiet mode for log (limit to :fatal)
57
+ def self.stop(options = {})
58
+ output.level = :fatal if options[:quiet]
59
+ if File.exist?(PID_FILE) then
60
+ begin
61
+ pid = `cat #{PID_FILE}`.to_i
62
+ Process.kill("TERM", pid)
63
+ output.ok 'Splash WebAdmin stopped succesfully'
64
+ rescue Errno::ESRCH
65
+
66
+ finisher.secure_raise error_case: :not_found, message: "Process of PID : #{pid} not found, erasing Pidfile "
67
+ ensure
68
+ FileUtils::rm PID_FILE if File::exist? PID_FILE
69
+ end
70
+ return "MockWS stopped"
71
+ else
72
+ finisher.secure_raise error_case: :not_found, message: "Splash WebAdmin is not running"
73
+ end
74
+ end
75
+
76
+ # Status of the Splash WebAdmin, display status
77
+ # @param [Hash] options ignored
78
+ # @return [Hash] Exiter Case (:status_ko, :status_ok)
79
+ def self.status(options = {})
80
+ pid = realpid = ''
81
+ pid = `cat #{PID_FILE}`.to_s if File.exist?(PID_FILE)
82
+ listpid = toolbox.get_processes pattern: DAEMON_NAME
83
+ pid.chomp!
84
+ if listpid.empty? then
85
+ realpid = ''
86
+ else
87
+ realpid = listpid.first
88
+ end
89
+ unless realpid.empty? then
90
+ output.item "MockWS daemon process is running with PID #{realpid} "
91
+ else
92
+ output.item 'MockWS daemon not found '
93
+ end
94
+ unless pid.empty? then
95
+ output.item "and PID file exist with PID #{pid}"
96
+ else
97
+ output.item "and PID file don't exist"
98
+ end
99
+ if pid == realpid then
100
+ return "MockWS status clean"
101
+ elsif pid.empty? then
102
+ finisher.secure_raise error_case: :status_ko, message: "PID File error, you have to kill process manualy, with : '(sudo )kill -TERM #{realpid}'"
103
+ elsif realpid.empty? then
104
+ finisher.secure_raise error_case: :status_ko, message: "Process MockWS daemon missing, run 'mockws stop' before reload properly"
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+ end
111
+
@@ -0,0 +1,44 @@
1
+ module MockWS
2
+ class DataManager
3
+
4
+ extend Carioca::Injector
5
+ inject service: :configuration
6
+
7
+ DEFAULT_OUTPUT_TYPE = :json
8
+ @@default_output_type = (configuration.settings.output.type)? configuration.settings.output.type : DEFAULT_OUTPUT_TYPE
9
+ TYPE_MAP = { json: "application/json", csv: "application/csv", yaml: "application/x-yaml" }
10
+
11
+
12
+ def default_output_type
13
+ return @@default_output_type
14
+ end
15
+
16
+ def self.from_csv(file)
17
+ return CSV.read file
18
+ end
19
+
20
+
21
+ def self.from_json(file)
22
+ return JSON.load file
23
+ end
24
+
25
+ def self.from_yaml(file)
26
+ return YAML.load_file file
27
+ end
28
+
29
+
30
+ def self.to_json(data)
31
+ return data.to_json
32
+ end
33
+
34
+ def self.to_csv(data)
35
+ return data.to_csv
36
+ end
37
+
38
+ def self.to_yaml(data)
39
+ return data.to_yaml
40
+ end
41
+
42
+
43
+ end
44
+ end
@@ -0,0 +1,72 @@
1
+ module MockWS
2
+ class RouteManager
3
+
4
+ extend Carioca::Injector
5
+ inject service: :output
6
+ inject service: :configuration
7
+
8
+ DEFAULT_STATUS = 200
9
+
10
+ def self.get_response_time(value)
11
+ return {value: value[:response_time], type: :static} if value.include? :response_time
12
+ return {value: rand(1..value[:random_time]), type: :random} if value.include? :random_time
13
+ return {value: 0, type: :instant}
14
+ end
15
+
16
+
17
+ def self.configure(service)
18
+ @service = service
19
+ output.info "Mock routes initialisation : "
20
+ configuration.settings.services.each do |_key, definition|
21
+ create_route(definition)
22
+ end
23
+ end
24
+
25
+ # build a session number
26
+ # @return [String] Session number
27
+ def self.get_session
28
+ return "#{Time.now.to_i.to_s}#{rand(999)}"
29
+ end
30
+
31
+
32
+ private
33
+
34
+
35
+ def self.create_route(definition)
36
+ output.item "Adding #{definition[:type]} route #{definition[:route]} on verb #{definition[:verb]}"
37
+ unless self.respond_to? "#{definition[:type]}_content".to_sym, true then
38
+ output.error 'type not defined or type not recognize'
39
+ return false
40
+ end
41
+ @service.send(definition[:verb], definition[:route]) do
42
+ session = MockWS::RouteManager::get_session
43
+ output.debug "[#{session}] Executing route #{definition[:route]} on verb #{definition[:verb]}"
44
+ response_time = MockWS::RouteManager::get_response_time definition
45
+ output.debug "[#{session}] Response time #{response_time[:type]} value #{response_time[:value]}" unless response_time[:type] == :instant
46
+ sleep(response_time[:value]) if response_time[:value]
47
+ return_status = (definition[:status])? definition[:status] : DEFAULT_STATUS
48
+ output.debug "[#{session}] status : #{return_status}"
49
+ status return_status
50
+ data = MockWS::RouteManager.send "#{definition[:type]}_content".to_sym, definition
51
+ if definition.include? :to then
52
+ raise "Output type not supported" unless MockWS::DataManager::TYPE_MAP.keys.include? definition[:to]
53
+ return MockWS::DataManager.send "to_#{definition[:to]}", data
54
+ else
55
+ return MockWS::DataManager.send "to_#{MockWS::DataManager.default_output_type}", data
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.static_content(definition)
61
+ ext = File.extname(definition[:path])[1..-1].to_sym
62
+ return MockWS::DataManager.send "from_#{ext}", definition[:path]
63
+ end
64
+
65
+
66
+ def self.inline_content(definition)
67
+ return definition[:data]
68
+ end
69
+
70
+
71
+ end
72
+ end
@@ -0,0 +1,53 @@
1
+ module MockWS
2
+ module Configuration
3
+ def self.setup(force: false)
4
+ if File.exist?(File.expand_path(MockWS::DEFAULT_CONFIG_PATH)) && !force
5
+ puts 'MockWS already configured'
6
+ else
7
+ FileUtils.rm_rf File.expand_path(MockWS::DEFAULT_PATH)
8
+ config_file = search_file_in_gem('mockws', 'config/mockws.yml')
9
+ [ MockWS::DEFAULT_LOGS_PATH, MockWS::DEFAULT_CONFIG_PATH].each do |path|
10
+ FileUtils.mkdir_p File.expand_path(path)
11
+ end
12
+ File.open(File.expand_path("#{MockWS::DEFAULT_LOGS_PATH}/#{MockWS::DEFAULT_LOG_FILENAME}"), 'w') { |file| file.write("# MockWS : beginning of log file\n") }
13
+ FileUtils.cp config_file, File.expand_path(MockWS::DEFAULT_CONFIG_PATH)
14
+ puts '[OK] Building config folder and initialize settings'
15
+ end
16
+ end
17
+
18
+
19
+
20
+
21
+ class Checker
22
+
23
+ extend Carioca::Injector
24
+ inject service: :output
25
+ inject service: :finisher
26
+
27
+ def self.sanitycheck
28
+ global_status = []
29
+ output.info "Checking path for #{Etc.getpwuid(Process.uid).name} : "
30
+ status = { true => :ok, false => :error }
31
+ [DEFAULT_PATH,DEFAULT_CONFIG_PATH,DEFAULT_LOGS_PATH].each do |path|
32
+ res = status[File::exist?(File::expand_path(path))]
33
+ output.send res, path
34
+ global_status.push res
35
+ end
36
+ output.info "Checking file for #{Etc.getpwuid(Process.uid).name} : "
37
+
38
+ ["#{DEFAULT_CONFIG_PATH}/#{DEFAULT_SETTINGS_FILENAME}","#{DEFAULT_LOGS_PATH}/#{DEFAULT_LOG_FILENAME}"].each do |file|
39
+ res = status[File::exist?(File::expand_path(file))]
40
+ output.send res, file
41
+ global_status.push res
42
+
43
+ end
44
+ finisher.secure_raise error_case: :sanitycheck_error,
45
+ message: "MocKWS configuration error for #{Etc.getpwuid(Process.uid).name}" if global_status.include? :error
46
+
47
+
48
+ end
49
+ end
50
+
51
+
52
+ end
53
+ end
data/lib/mockws.rb ADDED
@@ -0,0 +1,186 @@
1
+ require 'sinatra'
2
+ require 'yaml'
3
+ require 'json'
4
+ require 'carioca'
5
+ require 'thor'
6
+ require 'csv'
7
+ require 'etc'
8
+
9
+ require_relative 'mockws/setup'
10
+
11
+
12
+ # facility to find a file in gem path
13
+ # @param [String] gem a Gem name
14
+ # @param [String] file a file relative path in the gem
15
+ # @return [String] the path of the file, if found.
16
+ # @return [False] if not found
17
+ def search_file_in_gem(gem, file)
18
+ if Gem::Specification.respond_to?(:find_by_name)
19
+ begin
20
+ spec = Gem::Specification.find_by_name(gem)
21
+ rescue LoadError
22
+ spec = nil
23
+ end
24
+ else
25
+ spec = Gem.searcher.find(gem)
26
+ end
27
+ if spec
28
+ res = if Gem::Specification.respond_to?(:find_by_name)
29
+ spec.lib_dirs_glob.split('/')
30
+ else
31
+ Gem.searcher.lib_dirs_for(spec).split('/')
32
+ end
33
+ res.pop
34
+ services_path = res.join('/').concat("/#{file}")
35
+ return services_path if File.exist?(services_path)
36
+
37
+ end
38
+ false
39
+ end
40
+
41
+ module MockWS
42
+
43
+ DEFAULT_PATH = '~/.mockws'
44
+ DEFAULT_CONFIG_PATH = "#{DEFAULT_PATH}/config"
45
+ DEFAULT_LOGS_PATH = "#{DEFAULT_PATH}/logs"
46
+
47
+ DEFAULT_LOG_FILENAME = "mockws.log"
48
+ DEFAULT_SETTINGS_FILENAME = "mockws.yml"
49
+
50
+ end
51
+
52
+
53
+ unless File.exist? File.expand_path(MockWS::DEFAULT_CONFIG_PATH)
54
+ puts "[W] MockWS not initialized for user #{Etc.getpwuid(Process.uid).name}, running setup"
55
+ MockWS::Configuration.setup
56
+ end
57
+
58
+ context = ENV['RACK_ENV'] ? ENV['RACK_ENV'].to_sym : :main
59
+
60
+ Carioca::Registry.configure do |spec|
61
+ spec.debug = true
62
+ spec.init_from_file = false
63
+ spec.log_file = File.expand_path("#{MockWS::DEFAULT_LOGS_PATH}/mockws.log")
64
+ spec.config_file = File.expand_path("#{MockWS::DEFAULT_CONFIG_PATH}/#{MockWS::DEFAULT_SETTINGS_FILENAME}")
65
+ spec.config_root = :mockws
66
+ spec.environment = context
67
+ spec.default_locale = :en
68
+ spec.log_level = :info
69
+ spec.locales_load_path << Dir[search_file_in_gem('mockws', './config/locales') + "/*.yml"]
70
+ spec.debugger_tracer = :logger
71
+ end
72
+
73
+
74
+ require_relative 'mockws/route_manager'
75
+ require_relative 'mockws/data_manager'
76
+ require_relative 'mockws/daemon_controller'
77
+
78
+
79
+ class ApplicationController < Carioca::Container
80
+ inject service: :configuration
81
+ inject service: :i18n
82
+ end
83
+
84
+
85
+ module MockWS
86
+ class Service < Sinatra::Base
87
+ extend Carioca::Injector
88
+ inject service: :output
89
+ inject service: :configuration
90
+
91
+
92
+
93
+ def self.init
94
+ MockWS::RouteManager::configure(self)
95
+ end
96
+
97
+
98
+
99
+
100
+ end
101
+ end
102
+
103
+
104
+
105
+ # method for daemonize blocks
106
+ # @param [Hash] options the list of options, keys are symbols
107
+ # @option options [String] :description the description of the process, use for $0
108
+ # @option options [String] :pid_file the pid filename
109
+ # @option options [String] :daemon_user the user to change privileges
110
+ # @option options [String] :daemon_group the group to change privileges
111
+ # @option options [String] :stderr_trace the path of the file where to redirect STDERR
112
+ # @option options [String] :stdout_trace the path of the file where to redirect STDOUT
113
+ # @option options [Proc] :sigint_handler handler Proc for SIGINT signal
114
+ # @option options [Proc] :sigterm_handler handler Proc for SIGTERM signal
115
+ # @option options [Proc] :sighup_handler handler Proc for SIGHuP signal
116
+ # @option options [Bool] :foreground option to run foreground
117
+ # @yield a process definion or block given
118
+ # @example usage inline
119
+ # class Test
120
+ # include Splash::Helpers
121
+ # private :daemonize
122
+ # def initialize
123
+ # @loop = Proc::new do
124
+ # loop do
125
+ # sleep 1
126
+ # end
127
+ # end
128
+ # end
129
+ #
130
+ # def run
131
+ # daemonize({:description => "A loop daemon", :pid_file => '/tmp/pid.file'}, &@loop)
132
+ # end
133
+ # end
134
+ #
135
+ # @example usage block
136
+ # class Test
137
+ # include Splash::Helpers
138
+ # include Dorsal::Privates
139
+ # private :daemonize
140
+ # def initialize
141
+ # end
142
+ #
143
+ # def run
144
+ # daemonize :description => "A loop daemon", :pid_file => '/tmp/pid.file' do
145
+ # loop do
146
+ # sleep 1
147
+ # end
148
+ # end
149
+ # end
150
+ # end
151
+ # @return [Fixnum] pid the pid of the forked processus
152
+ def daemonize(options)
153
+ {
154
+ :sighup_handler => 'SIGHUP',
155
+ :sigint_handler => 'SIGINT',
156
+ :sigterm_handler => 'SIGTERM',
157
+ }.each do |key,value|
158
+ trap(value){
159
+ if options[:sighup_handler].include? key then
160
+ options[:sighup_handler].call
161
+ else
162
+ exit! 0
163
+ end
164
+ }
165
+ end
166
+ if options[:foreground]
167
+ Process.setproctitle options[:description] if options[:description]
168
+ return yield
169
+ end
170
+ fork do
171
+ File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file]
172
+ if options[:daemon_user] and options[:daemon_group] then
173
+ uid = Etc.getpwnam(options[:daemon_user]).uid
174
+ gid = Etc.getgrnam(options[:daemon_group]).gid
175
+ Process::UID.change_privilege(uid)
176
+ Process::GID.change_privilege(gid)
177
+ end
178
+ $stdout.reopen(options[:stdout_trace], "w") if options[:stdout_trace]
179
+ $stderr.reopen(options[:stderr_trace], "w") if options[:stderr_trace]
180
+ Process.setproctitle options[:description] if options[:description]
181
+ yield
182
+ end
183
+ return 0
184
+ end
185
+
186
+
data/mockws.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'mockws'
5
+ spec.version = `cat VERSION`.chomp
6
+ spec.authors = ['Camille Paquet', 'Romain GEORGES', 'Pierre Alphonse']
7
+ spec.email = ['gems@ultragreen.net']
8
+
9
+ spec.summary = 'MockWS : Web services mocking utility'
10
+ spec.description = 'MockWS : Web services mocking utility'
11
+ spec.homepage = 'https://github.com/Ultragreen/mockws'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'code_statistics', '~> 0.2.13'
29
+ spec.add_development_dependency 'rake', '~> 12.0'
30
+ spec.add_development_dependency 'roodi', '~> 5.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'rubocop', '~> 1.32'
33
+ spec.add_development_dependency 'version', '~> 1.1'
34
+ spec.add_development_dependency 'yard', '~> 0.9.27'
35
+ spec.add_development_dependency 'yard-rspec', '~> 0.1'
36
+ spec.metadata['rubygems_mfa_required'] = 'false'
37
+ spec.add_dependency 'carioca', '~> 2.1'
38
+ spec.add_dependency 'thor', '~> 1.2'
39
+ spec.add_dependency 'thin', '~> 1.8'
40
+ spec.add_dependency 'sinatra', '~> 3.1'
41
+ end
metadata ADDED
@@ -0,0 +1,236 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mockws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Camille Paquet
8
+ - Romain GEORGES
9
+ - Pierre Alphonse
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2023-10-03 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: code_statistics
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.13
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 0.2.13
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '12.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '12.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: roodi
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '5.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '5.0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '3.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rubocop
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.32'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '1.32'
85
+ - !ruby/object:Gem::Dependency
86
+ name: version
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.1'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '1.1'
99
+ - !ruby/object:Gem::Dependency
100
+ name: yard
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: 0.9.27
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: 0.9.27
113
+ - !ruby/object:Gem::Dependency
114
+ name: yard-rspec
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '0.1'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '0.1'
127
+ - !ruby/object:Gem::Dependency
128
+ name: carioca
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '2.1'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: '2.1'
141
+ - !ruby/object:Gem::Dependency
142
+ name: thor
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '1.2'
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - "~>"
153
+ - !ruby/object:Gem::Version
154
+ version: '1.2'
155
+ - !ruby/object:Gem::Dependency
156
+ name: thin
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - "~>"
160
+ - !ruby/object:Gem::Version
161
+ version: '1.8'
162
+ type: :runtime
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - "~>"
167
+ - !ruby/object:Gem::Version
168
+ version: '1.8'
169
+ - !ruby/object:Gem::Dependency
170
+ name: sinatra
171
+ requirement: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - "~>"
174
+ - !ruby/object:Gem::Version
175
+ version: '3.1'
176
+ type: :runtime
177
+ prerelease: false
178
+ version_requirements: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - "~>"
181
+ - !ruby/object:Gem::Version
182
+ version: '3.1'
183
+ description: 'MockWS : Web services mocking utility'
184
+ email:
185
+ - gems@ultragreen.net
186
+ executables:
187
+ - mockws
188
+ extensions: []
189
+ extra_rdoc_files: []
190
+ files:
191
+ - Gemfile
192
+ - Gemfile.lock
193
+ - LICENSE
194
+ - README.md
195
+ - Rakefile
196
+ - VERSION
197
+ - config.ru
198
+ - config/locales/en.yml
199
+ - config/locales/fr.yml
200
+ - config/mockws.yml
201
+ - exe/mockws
202
+ - lib/mockws.rb
203
+ - lib/mockws/cli.rb
204
+ - lib/mockws/daemon_controller.rb
205
+ - lib/mockws/data_manager.rb
206
+ - lib/mockws/route_manager.rb
207
+ - lib/mockws/setup.rb
208
+ - mockws.gemspec
209
+ homepage: https://github.com/Ultragreen/mockws
210
+ licenses:
211
+ - MIT
212
+ metadata:
213
+ homepage_uri: https://github.com/Ultragreen/mockws
214
+ source_code_uri: https://github.com/Ultragreen/mockws
215
+ changelog_uri: https://github.com/Ultragreen/mockws
216
+ rubygems_mfa_required: 'false'
217
+ post_install_message:
218
+ rdoc_options: []
219
+ require_paths:
220
+ - lib
221
+ required_ruby_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: 2.6.0
226
+ required_rubygems_version: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ">="
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ requirements: []
232
+ rubygems_version: 3.3.5
233
+ signing_key:
234
+ specification_version: 4
235
+ summary: 'MockWS : Web services mocking utility'
236
+ test_files: []