Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

import unittest 

import os 

import mock 

import errno 

 

import testlib 

 

 

class TestTestContext(unittest.TestCase): 

    def test_generate_inventory_file(self): 

        context = testlib.TestContext() 

        context.inventory = dict(key='value') 

 

        self.assertEquals("key='value'", context.generate_inventory_contents()) 

 

    @testlib.with_context 

    def test_adapter_adds_scsi_host_entry(self, context): 

        context.add_adapter(testlib.SCSIAdapter()) 

 

        self.assertEquals(['host0'], os.listdir('/sys/class/scsi_host')) 

 

    @testlib.with_context 

    def test_add_disk_adds_scsi_disk_entry(self, context): 

        import glob 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        adapter.add_disk() 

 

        self.assertEquals( 

            ['/sys/class/scsi_disk/0:0:0:0'], 

            glob.glob('/sys/class/scsi_disk/0*')) 

 

    @testlib.with_context 

    def test_add_disk_adds_scsibus_entry(self, context): 

        import glob 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        adapter.long_id = 'HELLO' 

        adapter.add_disk() 

 

        self.assertEquals( 

            ['/dev/disk/by-scsibus/HELLO-0:0:0:0'], 

            glob.glob('/dev/disk/by-scsibus/*')) 

 

    @testlib.with_context 

    def test_add_disk_adds_device(self, context): 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        adapter.add_disk() 

 

        self.assertEquals( 

            ['sda'], 

            os.listdir('/sys/class/scsi_disk/0:0:0:0/device/block')) 

 

    @testlib.with_context 

    def test_add_disk_adds_disk_by_id_entry(self, context): 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        disk = adapter.add_disk() 

        disk.long_id = 'SOMEID' 

 

        self.assertEquals(['SOMEID'], os.listdir('/dev/disk/by-id')) 

 

    @testlib.with_context 

    def test_add_disk_adds_glob(self, context): 

        import glob 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        disk = adapter.add_disk() 

 

        self.assertEquals(['/dev/disk/by-id'], glob.glob('/dev/disk/by-id')) 

 

    @testlib.with_context 

    def test_add_disk_path_exists(self, context): 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        disk = adapter.add_disk() 

 

        self.assertTrue(os.path.exists('/dev/disk/by-id')) 

 

    @testlib.with_context 

    def test_add_parameter_parameter_file_exists(self, context): 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        disk = adapter.add_disk() 

        adapter.add_parameter('fc_host', {'node_name': 'ignored'}) 

 

        self.assertTrue(os.path.exists('/sys/class/fc_host/host0/node_name')) 

 

    @testlib.with_context 

    def test_add_parameter_parameter_file_contents(self, context): 

        adapter = context.add_adapter(testlib.SCSIAdapter()) 

        disk = adapter.add_disk() 

        adapter.add_parameter('fc_host', {'node_name': 'value'}) 

 

        param_file = open('/sys/class/fc_host/host0/node_name') 

        param_value = param_file.read() 

        param_file.close() 

 

        self.assertEquals('value', param_value) 

 

    @testlib.with_context 

    def test_uname_explicitly_defined(self, context): 

        context.kernel_version = 'HELLO' 

        import os 

 

        result = os.uname() 

 

        self.assertEquals('HELLO', result[2]) 

 

    @testlib.with_context 

    def test_uname_default_kernel_version(self, context): 

        import os 

 

        result = os.uname() 

 

        self.assertEquals('3.1', result[2]) 

 

    @testlib.with_context 

    def test_inventory(self, context): 

        context.inventory = {} 

 

        inventory_file = open('/etc/xensource-inventory', 'rb') 

        inventory = inventory_file.read() 

        inventory_file.close() 

 

        self.assertEquals('', inventory) 

 

    @testlib.with_context 

    def test_default_inventory(self, context): 

        inventory_file = open('/etc/xensource-inventory', 'rb') 

        inventory = inventory_file.read() 

        inventory_file.close() 

 

        self.assertEquals("PRIMARY_DISK='/dev/disk/by-id/primary'", inventory) 

 

    @testlib.with_context 

    def test_exists_returns_false_for_non_existing(self, context): 

        self.assertFalse(os.path.exists('somefile')) 

 

    @testlib.with_context 

    def test_exists_returns_true_for_root(self, context): 

        self.assertTrue(os.path.exists('/')) 

 

    @testlib.with_context 

    def test_stat_nonexistent_file_throws_oserror(self, context): 

        self.assertRaises( 

            OSError, 

            lambda: os.stat('/nonexistingstuff')) 

 

    @testlib.with_context 

    def test_stat_does_not_fail_with_existing_file(self, context): 

        os.makedirs('/existingstuff') 

 

        os.stat('/existingstuff') 

 

    @testlib.with_context 

    def test_error_codes_read(self, context): 

        context.setup_error_codes() 

        errorcodes_file = open('/opt/xensource/sm/XE_SR_ERRORCODES.xml', 'rb') 

        errorcodes = errorcodes_file.read() 

        errorcodes_file.close() 

 

        self.assertTrue("<SM-errorcodes>" in errorcodes) 

 

    @testlib.with_context 

    def test_executable_shows_up_on_filesystem(self, context): 

        context.add_executable('/something', None) 

 

        self.assertTrue(os.path.exists('/something')) 

 

    @testlib.with_context 

    def test_subprocess_execution(self, context): 

        context.add_executable( 

            'something', 

            lambda args, inp: (1, inp + ' out', ','.join(args))) 

        import subprocess 

 

        proc = subprocess.Popen( 

            ['something', 'a', 'b'], 

            stdin=subprocess.PIPE, 

            stdout=subprocess.PIPE, 

            stderr=subprocess.PIPE, 

            close_fds=True) 

 

        out, err = proc.communicate('in') 

        rc = proc.returncode 

 

        self.assertEquals(1, rc) 

        self.assertEquals('in out', out) 

        self.assertEquals('something,a,b', err) 

 

    @testlib.with_context 

    def test_modinfo(self, context): 

        import subprocess 

 

        proc = subprocess.Popen( 

            ['/sbin/modinfo', '-d', 'somemodule'], 

            stdin=subprocess.PIPE, 

            stdout=subprocess.PIPE, 

            stderr=subprocess.PIPE, 

            close_fds=True) 

 

        out, err = proc.communicate('in') 

        rc = proc.returncode 

 

        self.assertEquals(0, rc) 

        self.assertEquals('somemodule-description', out) 

        self.assertEquals('', err) 

 

    @testlib.with_context 

    def test_makedirs_mocked_out(self, context): 

        import os 

 

        os.makedirs('/blah/subdir') 

 

        self.assertTrue(os.path.exists('/blah/subdir')) 

 

    @testlib.with_context 

    def test_makedirs_raises_if_exists(self, context): 

        import os 

 

        os.makedirs('/blah/subdir') 

 

        self.assertRaises(OSError, os.makedirs, '/blah/subdir') 

 

    @testlib.with_context 

    def test_setup_error_codes(self, context): 

        context.setup_error_codes() 

 

        self.assertTrue( 

            os.path.exists('/opt/xensource/sm/XE_SR_ERRORCODES.xml')) 

 

    @testlib.with_context 

    def test_write_a_file(self, context): 

        import os 

 

        os.makedirs('/blah/subdir') 

 

        f = open('/blah/subdir/somefile', 'w+') 

        f.write('hello') 

        f.close() 

 

        self.assertTrue( 

            ('/blah/subdir/somefile', 'hello') 

            in list(context.generate_path_content())) 

 

    @testlib.with_context 

    def test_write_a_file_in_non_existing_dir(self, context): 

        with self.assertRaises(IOError) as cm: 

            open('/blah/subdir/somefile', 'w') 

        self.assertEquals(errno.ENOENT, cm.exception.errno) 

 

    @testlib.with_context 

    def test_file_returns_an_object_with_fileno_callable(self, context): 

        f = file('/file', 'w+') 

 

        self.assertTrue(hasattr(f, 'fileno')) 

        self.assertTrue(callable(f.fileno)) 

 

    @testlib.with_context 

    def test_filenos_are_unique(self, context): 

        import os 

 

        os.makedirs('/blah/subdir') 

 

        file_1 = file('/blah/subdir/somefile', 'w+') 

        fileno_1 = file_1.fileno() 

 

        file_2 = file('/blah/subdir/somefile2', 'w+') 

        fileno_2 = file_2.fileno() 

 

        self.assertTrue(fileno_1 != fileno_2) 

 

    def test_get_created_directories(self): 

        context = testlib.TestContext() 

 

        context.fake_makedirs('/some/path') 

 

        self.assertEquals([ 

            '/', 

            '/some', 

            '/some/path'], 

            context.get_created_directories()) 

 

    def test_popen_raises_error(self): 

        import subprocess 

        context = testlib.TestContext() 

 

        self.assertRaises( 

            testlib.ContextSetupError, 

            context.fake_popen, 

            ['something'], 

            subprocess.PIPE, 

            subprocess.PIPE, 

            subprocess.PIPE, 

            True 

        ) 

 

    def test_glob_requests_logged(self): 

        context = testlib.TestContext() 

        context.log = mock.Mock() 

 

        context.fake_glob('/dir/*') 

 

        self.assertEquals( 

            [ 

                mock.call('no glob', '/dir/*'), 

            ], 

            context.log.mock_calls 

        ) 

 

    def test_fake_open_logged(self): 

        context = testlib.TestContext() 

        context.log = mock.Mock() 

 

        try: 

            context.fake_open('/nonexisting_file', 'r') 

        except: 

            pass 

 

        self.assertEquals( 

            [ 

                mock.call('tried to open file', '/nonexisting_file'), 

            ], 

            context.log.mock_calls 

        ) 

 

    def test_context_stops_mocking_on_failures(self): 

        original_open = os.open 

 

        @testlib.with_context 

        def somefunction(firstparam, context): 

            raise Exception() 

 

        try: 

            somefunction(None) 

        except: 

            pass 

 

        self.assertEquals(original_open, os.open) 

 

    @testlib.with_context 

    def test_rmdir_is_replaced_with_a_fake(self, context): 

        self.assertEquals(context.fake_rmdir, os.rmdir) 

 

    def test_rmdir_raises_error_if_dir_not_found(self): 

        context = testlib.TestContext() 

 

        with self.assertRaises(OSError) as cm: 

            context.fake_rmdir('nonexisting') 

        self.assertEquals(errno.ENOENT, cm.exception.errno) 

 

    def test_rmdir_removes_dir_if_found(self): 

        context = testlib.TestContext() 

 

        context.fake_makedirs('/existing_dir') 

 

        context.fake_rmdir('/existing_dir') 

 

        self.assertFalse(context.fake_exists('/existing_dir')) 

 

    def test_rmdir_raises_exception_if_dir_is_not_empty(self): 

        context = testlib.TestContext() 

 

        context.fake_makedirs('/existing_dir/somefile') 

 

        with self.assertRaises(OSError) as cm: 

            context.fake_rmdir('/existing_dir') 

        self.assertEquals(errno.ENOTEMPTY, cm.exception.errno) 

 

 

class TestFilesystemFor(unittest.TestCase): 

    def test_returns_single_item_for_root(self): 

        fs = testlib.filesystem_for('/') 

 

        self.assertEquals(['/'], fs) 

 

    def test_returns_multiple_items_for_path(self): 

        fs = testlib.filesystem_for('/somedir') 

 

        self.assertEquals(['/', '/somedir'], fs) 

 

 

class TestXmlMixIn(unittest.TestCase, testlib.XmlMixIn): 

 

    def test_assertXML_doesn_t_care_about_spaces(self): 

        self.assertXML( 

            """ 

 

            <?xml version="1.0" ?> 

                <something/> 

 

            """, 

            '<?xml version="1.0" ?><something/>')