summaryrefslogtreecommitdiffstats
path: root/tests/pyanaconda_test/rescue_test.py
blob: b93f75ebe7a1b25dbf577464796e56ae71d18fb7 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python

import mock

class RescueTest(mock.TestCase):

    def setUp(self):
        self.setupModules(
            ['_isys', 'block', 'parted', 'storage', 'pyanaconda.storage.formats',
             'logging', 'add_drive_text', 'ConfigParser',
             'pyanaconda.storage.storage_log', 'pyanaconda.anaconda_log', 'snack'
             ])

        self.fs = mock.DiskIO()

        import pyanaconda
        pyanaconda.anaconda_log = mock.Mock()
        import snack
        snack.SnackScreen = mock.Mock()

        import pyanaconda.rescue
        pyanaconda.rescue.open = self.fs.open

    def tearDown(self):
        self.tearDownModules()

    #
    # RescueInterface class tests
    #

    def rescueinterface_waitwindow_test(self):
        import pyanaconda.rescue
        RET = 'foo1'
        pyanaconda.rescue.WaitWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.waitWindow(TITLE, TEXT)
        self.assertEqual(ret, RET)

    def rescueinterface_progresswindow_test(self):
        import pyanaconda.rescue
        RET = 'foo2'
        pyanaconda.rescue.ProgressWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        TOTAL = 100
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.progressWindow(TITLE, TEXT, TOTAL)
        self.assertEqual(ret, RET)

    def rescueinterface_detailedmessagewindow_test(self):
        import pyanaconda.rescue
        RET = 'foo3'
        pyanaconda.rescue.RescueInterface.messageWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.detailedMessageWindow(TITLE, TEXT)
        self.assertEqual(ret, RET)

    def rescueinterface_messagewindow_1_test(self):
        import pyanaconda.rescue
        pyanaconda.rescue.ButtonChoiceWindow = mock.Mock()
        TITLE = 'title'
        TEXT = 'text'
        TYPE = 'ok'
        ri = pyanaconda.rescue.RescueInterface()
        ri.detailedMessageWindow(TITLE, TEXT, TYPE)
        self.assertTrue(pyanaconda.rescue.ButtonChoiceWindow.called)

    def rescueinterface_messagewindow_2_test(self):
        import pyanaconda.rescue
        RET='yes'
        pyanaconda.rescue.ButtonChoiceWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        TYPE = 'yesno'
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.messageWindow(TITLE, TEXT, TYPE)
        self.assertEqual(ret, 1)

    def rescueinterface_messagewindow_3_test(self):
        import pyanaconda.rescue
        RET = 'barfoo'
        pyanaconda.rescue.ButtonChoiceWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        TYPE = 'custom'
        CUSTOM_BUTT = ['foo_bar', 'bar_foo']
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.messageWindow(TITLE, TEXT, TYPE, custom_buttons=CUSTOM_BUTT)
        self.assertEqual(ret, 1)

    def rescueinterface_messagewindow_4_test(self):
        import pyanaconda.rescue
        RET = 'foo4'
        pyanaconda.rescue.OkCancelWindow = mock.Mock(return_value=RET)
        TITLE = 'title'
        TEXT = 'text'
        TYPE = 'otherfoo'
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.messageWindow(TITLE, TEXT, TYPE)
        self.assertEqual(ret, RET)

    def rescueinterface_enablenetwork_1_test(self):
        import pyanaconda.rescue
        anaconda = mock.Mock()
        anaconda.network.netdevices = {}

        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.enableNetwork(anaconda)
        self.assertFalse(ret)

    def rescueinterface_passphraseentrywindow_test(self):
        import pyanaconda.rescue
        RET = ('secret', False)
        pyanaconda.rescue.PassphraseEntryWindow = mock.Mock()
        pyanaconda.rescue.PassphraseEntryWindow().run.return_value = RET
        DEVICE = 'dev'

        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.passphraseEntryWindow(DEVICE)
        self.assertEqual(ret, RET)
        self.assertTrue(pyanaconda.rescue.PassphraseEntryWindow().pop.called)

    def rescueinterface_resetinitializediskquestion_test(self):
        import pyanaconda.rescue
        ri = pyanaconda.rescue.RescueInterface()
        ri._initLabelAnswers = {'foo': 'bar'}
        ri.resetInitializeDiskQuestion()

    def rescueinterface_resetreinitinconsistentlvmquestion_test(self):
        import pyanaconda.rescue
        ri = pyanaconda.rescue.RescueInterface()
        ri._inconsistentLVMAnswers = {'foo': 'bar'}
        ri.resetReinitInconsistentLVMQuestion()
        self.assertEqual(ri._inconsistentLVMAnswers, {})

    def rescueinterface_questioninitializedisk_test(self):
        import pyanaconda.rescue
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.questionInitializeDisk('/', '', 0)
        self.assertFalse(ret)

    def rescueinterface_questionreinitinconsistentlvm_test(self):
        import pyanaconda.rescue
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.questionReinitInconsistentLVM()
        self.assertFalse(ret)

    def rescueinterface_questioninitializedasd_test(self):
        import pyanaconda.rescue
        ri = pyanaconda.rescue.RescueInterface()
        ret = ri.questionInitializeDASD('', '')
        self.assertEqual(ret, 1)

    #
    # module function tests
    #

    def makefstab_test(self):
        import pyanaconda.rescue
        INSTPATH = '/tmp'
        FSTAB = 'rootfs / rootfs rw 0 0'
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.access.return_value = True
        self.fs.open('/proc/mounts', 'w').write(FSTAB)
        self.fs.open('%s/etc/fstab' % INSTPATH, 'w')

        ret = pyanaconda.rescue.makeFStab(INSTPATH)
        self.assertEqual(self.fs['%s/etc/fstab' % INSTPATH], FSTAB)

    def makeresolvconf_1_test(self):
        import pyanaconda.rescue
        INSTPATH = '/tmp'
        RESOLV = "nameserver 10.0.0.1"
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.access.return_value = False
        pyanaconda.rescue.shutil = mock.Mock()

        pyanaconda.rescue.makeResolvConf(INSTPATH)
        self.assertFalse(pyanaconda.rescue.shutil.copyfile.called)

    def makeresolvconf_2_test(self):
        import pyanaconda.rescue
        INSTPATH = '/tmp'
        RESOLV = "nameserver 10.0.0.1"
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.access.return_value = True
        pyanaconda.rescue.shutil = mock.Mock()
        self.fs.open('%s/etc/resolv.conf' % INSTPATH, 'w').write(RESOLV)

        pyanaconda.rescue.makeResolvConf(INSTPATH)
        self.assertFalse(pyanaconda.rescue.shutil.copyfile.called)

    def makeresolvconf_3_test(self):
        import pyanaconda.rescue
        INSTPATH = '/tmp'
        RESOLV = "nameserver 10.0.0.1"
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.access.return_value = True
        pyanaconda.rescue.shutil = mock.Mock()
        self.fs.open('%s/etc/resolv.conf' % INSTPATH, 'w').write('')
        self.fs.open('/etc/resolv.conf', 'w').write('')

        pyanaconda.rescue.makeResolvConf(INSTPATH)
        self.assertFalse(pyanaconda.rescue.shutil.copyfile.called)
        self.assertEqual(self.fs['%s/etc/resolv.conf' % INSTPATH], '')

    def makeresolvconf_4_test(self):
        import pyanaconda.rescue
        INSTPATH = '/tmp'
        RESOLV = "nameserver 10.0.0.1"
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.access.return_value = True
        pyanaconda.rescue.shutil = mock.Mock()
        self.fs.open('%s/etc/resolv.conf' % INSTPATH, 'w').write('')
        self.fs.open('/etc/resolv.conf', 'w').write(RESOLV)

        pyanaconda.rescue.makeResolvConf(INSTPATH)
        self.assertTrue(pyanaconda.rescue.shutil.copyfile.called)
        self.assertEqual(self.fs['%s/etc/resolv.conf' % INSTPATH],
            'nameserver 10.0.0.1')

    def startnetworking_test(self):
        import pyanaconda.rescue
        NETWORK = mock.Mock()
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.startNetworking(NETWORK, '')
        self.assertEqual(pyanaconda.rescue.os.system.call_args,
            (('/usr/sbin/ifconfig lo 127.0.0.1',), {}))
        self.assertTrue(NETWORK.bringUp.called)

    def runshell_1_test(self):
        import pyanaconda.rescue
        import sys
        TMPFILE = '/tmp/abc'
        MSG = "foo bar"
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.path.exists.return_value = True
        pyanaconda.rescue.subprocess = mock.Mock()
        proc = mock.Mock()
        proc.returncode = 0
        pyanaconda.rescue.subprocess.Popen.return_value = proc

        stdout = sys.stdout
        sys.stdout = self.fs.open(TMPFILE, 'w')
        pyanaconda.rescue.runShell(msg=MSG)
        sys.stdout.close()
        sys.stdout = stdout

        self.assertTrue(MSG in self.fs[TMPFILE])
        self.assertEqual(pyanaconda.rescue.subprocess.Popen.call_args,
            ((['/usr/bin/firstaidkit-qs'],), {}))

    def runshell_2_test(self):
        import pyanaconda.rescue
        import sys
        TMPFILE = '/tmp/abc'
        MSG = "foo bar"

        def fake_f(filename, _=""):
            return filename == "/bin/bash"

        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.path.exists = fake_f
        pyanaconda.rescue.iutil = mock.Mock()
        proc = mock.Mock()
        proc.returncode = 0
        pyanaconda.rescue.subprocess.Popen.return_value = proc

        stdout = sys.stdout
        sys.stdout = self.fs.open(TMPFILE, 'w')
        pyanaconda.rescue.runShell(msg=MSG)
        sys.stdout.close()
        sys.stdout = stdout

        self.assertTrue(MSG in self.fs[TMPFILE])
        self.assertTrue(pyanaconda.rescue.iutil.execConsole.called)

    def runshell_3_test(self):
        import pyanaconda.rescue
        import sys
        TMPFILE = '/tmp/abc'
        SCREEN = mock.Mock()
        pyanaconda.rescue.os = mock.Mock()
        pyanaconda.rescue.os.path.exists.return_value = True
        pyanaconda.rescue.subprocess = mock.Mock()
        proc = mock.Mock()
        proc.returncode = 0
        pyanaconda.rescue.subprocess.Popen.return_value = proc

        pyanaconda.rescue.runShell(screen=SCREEN)

        self.assertTrue(SCREEN.suspend.called)
        self.assertTrue(SCREEN.finish.called)