1module Gitlab
2  #
3  # In production, gitaly-ruby configuration is derived from environment
4  # variables set by the Go gitaly parent process. During the rspec test
5  # suite this parent process is not there so we need to get configuration
6  # values from somewhere else. We used to work around this by setting
7  # variables in ENV during the rspec boot but that turned out to be
8  # fragile because Bundler.with_clean_env resets changes to ENV made
9  # after Bundler was loaded. Instead of changing ENV, the TestSetup
10  # module gives us a hacky way to set instance variables on the config
11  # objects, bypassing the ENV lookups.
12  #
13  module TestSetup
14    def test_global_ivar_override(name, value)
15      instance_variable_set("@#{name}".to_sym, value)
16    end
17  end
18
19  class Config
20    class Git
21      include TestSetup
22
23      def bin_path
24        @bin_path ||= ENV['GITALY_RUBY_GIT_BIN_PATH']
25      end
26
27      def hooks_directory
28        @hooks_directory ||= ENV['GITALY_GIT_HOOKS_DIR']
29      end
30
31      def write_buffer_size
32        @write_buffer_size ||= ENV['GITALY_RUBY_WRITE_BUFFER_SIZE'].to_i
33      end
34
35      def max_commit_or_tag_message_size
36        @max_commit_or_tag_message_size ||= ENV['GITALY_RUBY_MAX_COMMIT_OR_TAG_MESSAGE_SIZE'].to_i
37      end
38
39      def rugged_git_config_search_path
40        @rugged_git_config_search_path ||= ENV['GITALY_RUGGED_GIT_CONFIG_SEARCH_PATH']
41      end
42    end
43
44    class Gitaly
45      include TestSetup
46
47      def bin_dir
48        @bin_dir ||= ENV['GITALY_RUBY_GITALY_BIN_DIR']
49      end
50
51      def internal_socket
52        @internal_socket ||= ENV['GITALY_SOCKET']
53      end
54
55      def rbtrace_enabled?
56        @rbtrace_enabled ||= enabled?(ENV['GITALY_RUBY_RBTRACE_ENABLED'])
57      end
58
59      def objspace_trace_enabled?
60        @objspace_trace_enabled ||= enabled?(ENV['GITALY_RUBY_OBJSPACE_TRACE_ENABLED'])
61      end
62
63      def enabled?(value)
64        %w[true yes 1].include?(value&.downcase)
65      end
66    end
67
68    class Logging
69      def dir
70        @dir ||= ENV['GITALY_LOG_DIR']
71      end
72    end
73
74    def git
75      @git ||= Git.new
76    end
77
78    def logging
79      @logging ||= Logging.new
80    end
81
82    def gitaly
83      @gitaly ||= Gitaly.new
84    end
85  end
86
87  def self.config
88    @config ||= Config.new
89  end
90end
91