/
runner_test.rb
238 lines (196 loc) · 6.85 KB
/
runner_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require_relative "../../../test_helper"
require "autoinstall/ask/runner"
require "autoinstall/ask/dialog"
require "autoinstall/ask/question"
require "autoinstall/autoinst_profile/ask_list_section"
require "autoinstall/script"
require "autoinstall/widgets/ask/dialog"
require "tmpdir"
describe Y2Autoinstall::Ask::Runner do
subject(:runner) { described_class.new(profile, stage: :initial) }
let(:profile) do
Yast::ProfileHash.new(
"general" => { "ask-list" => ask_list },
"users" => [{ "username" => "root", "user_password" => "ask" }]
)
end
let(:ask_list) do
[
{ "dialog" => 1, "question" => "Question 1" }
]
end
let(:dialogs) { [dialog1] }
let(:profile_reader) do
instance_double(Y2Autoinstall::Ask::ProfileReader, dialogs: dialogs)
end
let(:dialog1) do
Y2Autoinstall::Ask::Dialog.new(1, [question1])
end
let(:question1) do
Y2Autoinstall::Ask::Question.new("Question 1")
end
let(:ask_dialog1) do
instance_double(Y2Autoinstall::Widgets::Ask::Dialog, run: :next)
end
describe "#run" do
before do
allow(Y2Autoinstall::Ask::ProfileReader).to receive(:new)
.and_return(profile_reader)
allow(Y2Autoinstall::Widgets::Ask::Dialog).to receive(:new)
.with(dialog1, disable_back_button: true).and_return(ask_dialog1)
end
context "when :initial stage is given" do
it "runs the dialogs from the :initial stage" do
expect(Y2Autoinstall::Ask::ProfileReader).to receive(:new)
.with(Y2Autoinstall::AutoinstProfile::AskListSection, stage: :initial)
.and_return(profile_reader)
runner.run
end
end
context "when :cont stage is given" do
subject(:runner) { described_class.new(profile, stage: :cont) }
it "runs the dialogs from the :cont stage" do
expect(Y2Autoinstall::Ask::ProfileReader).to receive(:new)
.with(Y2Autoinstall::AutoinstProfile::AskListSection, stage: :cont)
.and_return(profile_reader)
runner.run
end
end
context "when a profile path is set" do
let(:question1) do
Y2Autoinstall::Ask::Question.new("Root Password").tap do |question|
question.paths << "users,0,user_password"
question.value = "secret"
end
end
it "updates the profile with the value from the question" do
runner.run
expect(profile["users"][0]).to eq("username" => "root", "user_password" => "secret")
end
end
context "when a script is set" do
let(:script) do
instance_double(Y2Autoinstallation::AskScript, environment: environment?)
end
let(:script_runner) do
instance_double(Y2Autoinstall::ScriptRunner)
end
let(:environment?) { false }
let(:question1) do
Y2Autoinstall::Ask::Question.new("Question 1").tap do |question|
question.script = script
question.value = "value"
end
end
before do
allow(Y2Autoinstall::ScriptRunner).to receive(:new).and_return(script_runner)
end
it "runs the given script" do
expect(script).to receive(:create_script_file)
expect(script_runner).to receive(:run).with(script, env: {})
runner.run
end
context "when the 'environment' attribute is set to 'true'" do
let(:environment?) { true }
it "passes the question value to the script" do
expect(script).to receive(:create_script_file)
expect(script_runner).to receive(:run).with(script, env: { "VAL" => question1.value })
runner.run
end
end
end
context "when a file is given" do
let(:script) { instance_double(Y2Autoinstall::AskScript) }
let(:question1) do
Y2Autoinstall::Ask::Question.new("MOTD").tap do |question|
question.file = "/etc/motd"
question.value = "Welcome!"
end
end
it "writes the value to the given file" do
expect(File).to receive(:write).with(question1.file, question1.value)
runner.run
end
end
context "when a /tmp/next_dialog exists" do
let(:dialogs) { [dialog1, dialog2] }
let(:dialog2) do
Y2Autoinstall::Ask::Dialog.new(2, [question2])
end
let(:question2) do
Y2Autoinstall::Ask::Question.new("Question 2")
end
let(:ask_dialog2) do
instance_double(Y2Autoinstall::Widgets::Ask::Dialog, run: :next)
end
let(:tmp_dir) { Dir.mktmpdir }
let(:next_dialog_file) { File.join(tmp_dir, "next_dialog") }
before do
stub_const("Y2Autoinstall::Ask::Runner::DIALOG_FILE", next_dialog_file)
allow(Y2Autoinstall::Widgets::Ask::Dialog).to receive(:new)
.with(dialog2, any_args).and_return(ask_dialog2)
end
after do
FileUtils.remove_entry(tmp_dir) if Dir.exist?(tmp_dir)
end
context "and it is a regular file" do
before do
File.write(next_dialog_file, "2\n")
end
it "jumps to the dialog specified in the file" do
expect(ask_dialog1).to_not receive(:run)
expect(ask_dialog2).to receive(:run)
runner.run
end
end
context "but it is not a regular file" do
let(:linked_file) { File.join(tmp_dir, "some-file") }
before do
FileUtils.ln_s(linked_file, next_dialog_file)
end
it "ignores the content and goes to the following dialog" do
expect(ask_dialog1).to receive(:run)
expect(ask_dialog2).to receive(:run)
runner.run
end
end
context "but it too big" do
before do
allow(File).to receive(:size).with(next_dialog_file).and_return(2048)
end
it "ignores the content and goes to the following dialog" do
expect(ask_dialog1).to receive(:run)
expect(ask_dialog2).to receive(:run)
runner.run
end
end
context "and the content is '-1'" do
before do
File.write(next_dialog_file, "-1\n")
end
it "finishes the sequence" do
expect(ask_dialog1).to_not receive(:run)
runner.run
end
end
end
end
end