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
Instance Attribute Summary collapse
Instance Method Summary
collapse
#act, included
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
11
12
13
14
15
16
17
18
|
# File 'lib/gitlab/qa/component/base.rb', line 11
def initialize
@docker = Docker::Engine.new
@environment = {}
@volumes = {}
@network_aliases = []
self.exec_commands = []
end
|
Instance Attribute Details
#docker ⇒ Object
Returns the value of attribute docker.
7
8
9
|
# File 'lib/gitlab/qa/component/base.rb', line 7
def docker
@docker
end
|
#environment ⇒ Object
Returns the value of attribute environment.
8
9
10
|
# File 'lib/gitlab/qa/component/base.rb', line 8
def environment
@environment
end
|
#exec_commands=(value) ⇒ Object
Sets the attribute exec_commands
9
10
11
|
# File 'lib/gitlab/qa/component/base.rb', line 9
def exec_commands=(value)
@exec_commands = value
end
|
#name ⇒ Object
24
25
26
|
# File 'lib/gitlab/qa/component/base.rb', line 24
def name
raise NotImplementedError, "#{self.class.name} must specify a default name"
end
|
#network ⇒ Object
Returns the value of attribute network.
8
9
10
|
# File 'lib/gitlab/qa/component/base.rb', line 8
def network
@network
end
|
#runner_network ⇒ Object
Returns the value of attribute runner_network.
8
9
10
|
# File 'lib/gitlab/qa/component/base.rb', line 8
def runner_network
@runner_network
end
|
#volumes ⇒ Object
Returns the value of attribute volumes.
8
9
10
|
# File 'lib/gitlab/qa/component/base.rb', line 8
def volumes
@volumes
end
|
Instance Method Details
#add_network_alias(name) ⇒ Object
20
21
22
|
# File 'lib/gitlab/qa/component/base.rb', line 20
def add_network_alias(name)
@network_aliases.push(name)
end
|
#hostname ⇒ Object
28
29
30
|
# File 'lib/gitlab/qa/component/base.rb', line 28
def hostname
"#{name}.#{network}"
end
|
#image ⇒ Object
32
33
34
35
36
|
# File 'lib/gitlab/qa/component/base.rb', line 32
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
44
45
46
47
48
49
50
|
# File 'lib/gitlab/qa/component/base.rb', line 44
def instance(skip_teardown: false)
instance_no_teardown do
yield self if block_given?
end
ensure
teardown unless skip_teardown
end
|
#prepare ⇒ Object
54
55
56
57
|
# File 'lib/gitlab/qa/component/base.rb', line 54
def prepare
prepare_docker_image
prepare_network
end
|
#prepare_docker_image ⇒ Object
59
60
61
|
# File 'lib/gitlab/qa/component/base.rb', line 59
def prepare_docker_image
pull
end
|
#prepare_network ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/gitlab/qa/component/base.rb', line 63
def prepare_network
if runner_network && !docker.network_exists?(runner_network)
docker.network_create("--driver=bridge --internal #{runner_network}")
end
return if docker.network_exists?(network)
docker.network_create(network)
end
|
#process_exec_commands ⇒ Object
128
129
130
|
# File 'lib/gitlab/qa/component/base.rb', line 128
def process_exec_commands
exec_commands.each { |command| docker.exec(name, command) }
end
|
#pull ⇒ Object
122
123
124
125
126
|
# File 'lib/gitlab/qa/component/base.rb', line 122
def pull
return if Runtime::Env.skip_pull?
docker.pull(image, tag)
end
|
#restart ⇒ Object
96
97
98
99
100
|
# File 'lib/gitlab/qa/component/base.rb', line 96
def restart
assert_name!
docker.restart(name)
end
|
#start ⇒ Object
rubocop:disable Metrics/AbcSize
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/gitlab/qa/component/base.rb', line 73
def start docker.run(image, tag) do |command|
command << "-d"
command << "--name #{name}"
command << "--net #{network}"
command << "--hostname #{hostname}"
@volumes.to_h.each do |to, from|
command.volume(to, from, 'Z')
end
command.volume(File.join(Runtime::Env.host_artifacts_dir, name, 'logs'), '/var/log/gitlab', 'Z')
@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
end
end
|
#tag ⇒ Object
38
39
40
41
42
|
# File 'lib/gitlab/qa/component/base.rb', line 38
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
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/gitlab/qa/component/base.rb', line 102
def teardown
unless teardown?
puts "The orchestrated docker containers have not been removed."
docker.ps
return
end
teardown!
end
|
#teardown! ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/gitlab/qa/component/base.rb', line 113
def teardown!
assert_name!
return unless docker.running?(name)
docker.stop(name)
docker.remove(name)
end
|