Class: Gitlab::QA::Component::Base
- Inherits:
-
Object
- Object
- Gitlab::QA::Component::Base
show all
- Includes:
- Scenario::Actable
- Defined in:
- lib/gitlab/qa/component/base.rb
Direct Known Subclasses
AiGateway, Alpine, Chaos, Elasticsearch, Gitaly, Gitlab, Jira, LDAP, MailHog, Minio, MockServer, Opensearch, PostgreSQL, Praefect, SAML, Selenoid, Telegraf
Constant Summary
collapse
- CERTIFICATES_PATH =
File.expand_path('../../../../tls_certificates', __dir__)
Instance Attribute Summary collapse
Instance Method Summary
collapse
#act, included
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/gitlab/qa/component/base.rb', line 22
def initialize
@docker = Docker::Engine.new
@logger = Runtime::Logger.logger
@environment = {}
@volumes = {}
@ports = []
@network_aliases = []
@exec_commands = []
@additional_hosts = []
end
|
Instance Attribute Details
#additional_hosts ⇒ Object
Returns the value of attribute additional_hosts.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def additional_hosts
@additional_hosts
end
|
#airgapped_network ⇒ Object
Returns the value of attribute airgapped_network.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def airgapped_network
@airgapped_network
end
|
#docker ⇒ Object
Returns the value of attribute docker.
11
12
13
|
# File 'lib/gitlab/qa/component/base.rb', line 11
def docker
@docker
end
|
#environment ⇒ Object
Returns the value of attribute environment.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def environment
@environment
end
|
#exec_commands=(value) ⇒ Object
Sets the attribute exec_commands
12
13
14
|
# File 'lib/gitlab/qa/component/base.rb', line 12
def exec_commands=(value)
@exec_commands = value
end
|
#logger ⇒ Object
Returns the value of attribute logger.
11
12
13
|
# File 'lib/gitlab/qa/component/base.rb', line 11
def logger
@logger
end
|
#name ⇒ Object
37
38
39
|
# File 'lib/gitlab/qa/component/base.rb', line 37
def name
raise NotImplementedError, "#{self.class.name} must specify a default name"
end
|
#network ⇒ Object
Returns the value of attribute network.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def network
@network
end
|
#network_aliases ⇒ Object
Returns the value of attribute network_aliases.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def network_aliases
@network_aliases
end
|
#ports ⇒ Object
Returns the value of attribute ports.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def ports
@ports
end
|
#runner_network ⇒ Object
Returns the value of attribute runner_network.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def runner_network
@runner_network
end
|
#volumes ⇒ Object
Returns the value of attribute volumes.
13
14
15
|
# File 'lib/gitlab/qa/component/base.rb', line 13
def volumes
@volumes
end
|
Instance Method Details
#add_network_alias(name) ⇒ Object
33
34
35
|
# File 'lib/gitlab/qa/component/base.rb', line 33
def add_network_alias(name)
@network_aliases.push(name)
end
|
#hostname ⇒ Object
41
42
43
|
# File 'lib/gitlab/qa/component/base.rb', line 41
def hostname
"#{name}.#{network}"
end
|
#image ⇒ Object
45
46
47
48
49
|
# File 'lib/gitlab/qa/component/base.rb', line 45
def image
return self.class.const_get(:DOCKER_IMAGE) if self.class.const_defined?(:DOCKER_IMAGE)
raise NotImplementedError, "#{self.class.name} must specify a docker image as DOCKER_IMAGE"
end
|
#instance(skip_teardown: false) ⇒ Object
Also known as:
launch_and_teardown_instance
61
62
63
64
65
66
67
|
# File 'lib/gitlab/qa/component/base.rb', line 61
def instance(skip_teardown: false)
instance_no_teardown do
yield self if block_given?
end
ensure
teardown unless skip_teardown
end
|
#ip_address ⇒ Object
69
70
71
|
# File 'lib/gitlab/qa/component/base.rb', line 69
def ip_address
docker.inspect(name) { |command| command << "-f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" }
end
|
#prepare ⇒ Object
75
76
77
78
79
|
# File 'lib/gitlab/qa/component/base.rb', line 75
def prepare
prepare_docker_image
prepare_docker_container
prepare_network
end
|
#prepare_airgapped_network ⇒ Object
93
94
95
96
97
|
# File 'lib/gitlab/qa/component/base.rb', line 93
def prepare_airgapped_network
return unless airgapped_network && !docker.network_exists?(network)
docker.network_create("--driver=bridge --internal #{network}")
end
|
#prepare_docker_container ⇒ Object
105
106
107
108
109
|
# File 'lib/gitlab/qa/component/base.rb', line 105
def prepare_docker_container
return unless docker.container_exists?(name)
docker.remove(name)
end
|
#prepare_docker_image ⇒ Object
81
82
83
|
# File 'lib/gitlab/qa/component/base.rb', line 81
def prepare_docker_image
pull
end
|
#prepare_network ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/gitlab/qa/component/base.rb', line 85
def prepare_network
prepare_airgapped_network
prepare_runner_network
return if docker.network_exists?(network)
docker.network_create(network)
end
|
#prepare_runner_network ⇒ Object
99
100
101
102
103
|
# File 'lib/gitlab/qa/component/base.rb', line 99
def prepare_runner_network
return unless runner_network && !docker.network_exists?(runner_network)
docker.network_create("--driver=bridge --internal #{runner_network}")
end
|
#process_exec_commands ⇒ Object
173
174
175
|
# File 'lib/gitlab/qa/component/base.rb', line 173
def process_exec_commands
exec_commands.each { |command| docker.exec(name, command) }
end
|
#pull ⇒ Object
167
168
169
170
171
|
# File 'lib/gitlab/qa/component/base.rb', line 167
def pull
return if Runtime::Env.skip_pull?
docker.pull(image: image, tag: tag)
end
|
#restart ⇒ Object
142
143
144
145
146
|
# File 'lib/gitlab/qa/component/base.rb', line 142
def restart
assert_name!
docker.restart(name)
end
|
#start ⇒ Object
rubocop:disable Metrics/AbcSize
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/gitlab/qa/component/base.rb', line 111
def start docker.run(image: image, tag: tag) do |command|
command << "-d"
command << "--name #{name}"
command << "--net #{network}"
command << "--hostname #{hostname}"
@ports.each do |mapping|
command.port(mapping)
end
@volumes.to_h.each do |to, from|
command.volume(to, from, 'Z')
end
command.volume(*log_volume.values) unless log_volume.empty?
@environment.to_h.each do |key, value|
command.env(key, value)
end
@network_aliases.to_a.each do |network_alias|
command << "--network-alias #{network_alias}"
end
@additional_hosts.each do |host|
command << "--add-host=#{host}"
end
end
end
|
#start_instance ⇒ Object
57
58
59
|
# File 'lib/gitlab/qa/component/base.rb', line 57
def start_instance
instance_no_teardown
end
|
#tag ⇒ Object
51
52
53
54
55
|
# File 'lib/gitlab/qa/component/base.rb', line 51
def tag
return self.class.const_get(:DOCKER_IMAGE_TAG) if self.class.const_defined?(:DOCKER_IMAGE_TAG)
raise NotImplementedError, "#{self.class.name} must specify a docker image tag as DOCKER_IMAGE_TAG"
end
|
#teardown ⇒ Object
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/gitlab/qa/component/base.rb', line 148
def teardown
unless teardown?
Runtime::Logger.info("The orchestrated docker containers have not been removed.")
docker.ps
return
end
teardown!
end
|
#teardown! ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/gitlab/qa/component/base.rb', line 159
def teardown!
assert_name!
return unless docker.running?(name)
docker.remove(name)
end
|