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

import unittest 

import trim_util 

import testlib 

 

import mock 

 

EMPTY_VG_SPACE = 4 * 1024 * 1024 

 

class AlwaysBusyLock(object): 

    def acquireNoblock(self): 

        return False 

 

 

class AlwaysFreeLock(object): 

    def __init__(self): 

        self.acquired = False 

 

    def acquireNoblock(self): 

        self.acquired = True 

        return True 

 

    def release(self): 

        self.acquired = False 

 

 

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

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_error_code_trim_not_supported(self, 

                                                   context, 

                                                   sr_get_capability): 

        sr_get_capability.return_value = [] 

        context.setup_error_codes() 

 

        result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertXML(""" 

        <?xml version="1.0" ?> 

        <trim_response> 

            <key_value_pair> 

                <key>errcode</key> 

                <value>UnsupportedSRForTrim</value> 

            </key_value_pair> 

            <key_value_pair> 

                <key>errmsg</key> 

                <value>Trim on [some-uuid] not supported</value> 

            </key_value_pair> 

        </trim_response> 

        """, result) 

 

    @mock.patch('time.sleep', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_unable_to_obtain_lock_on_sr(self, 

                                                 context, 

                                                 sr_get_capability, 

                                                 MockLock, 

                                                 sleep): 

        MockLock.return_value = AlwaysBusyLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertXML(""" 

        <?xml version="1.0" ?> 

        <trim_response> 

            <key_value_pair> 

                <key>errcode</key> 

                <value>SRUnavailable</value> 

            </key_value_pair> 

            <key_value_pair> 

                <key>errmsg</key> 

                <value>Unable to get SR lock [some-uuid]</value> 

            </key_value_pair> 

        </trim_response> 

        """, result) 

 

    @mock.patch('time.sleep', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_sleeps_a_sec_and_retries_three_times(self, 

                                                          context, 

                                                          sr_get_capability, 

                                                          MockLock, 

                                                          sleep): 

        MockLock.return_value = AlwaysBusyLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertEquals([ 

                mock.call(1), 

                mock.call(1), 

                mock.call(1) 

            ], 

            sleep.mock_calls 

        ) 

 

    @mock.patch("trim_util.lvutil.LVM_SIZE_INCREMENT", EMPTY_VG_SPACE) 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_creates_an_lv(self, 

                                   context, 

                                   sr_get_capability, 

                                   MockLock, 

                                   lvutil): 

        lvutil._getVGstats.return_value = {'physical_size': 0, 

                                           'physical_utilisation': 0, 

                                           'freespace': EMPTY_VG_SPACE} 

        MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        lvutil.create.assert_called_once_with( 

            'some-uuid_trim_lv', 0, 'VG_XenStorage-some-uuid', 

            size_in_percentage='100%F' 

        ) 

 

    @mock.patch("trim_util.lvutil.LVM_SIZE_INCREMENT", EMPTY_VG_SPACE) 

    @mock.patch('util.pread2', autospec=True) 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_removes_lv_no_leftover_trim_vol(self, 

                                                     context, 

                                                     sr_get_capability, 

                                                     MockLock, 

                                                     lvutil, 

                                                     pread2): 

        lvutil._getVGstats.return_value = {'physical_size': 0, 

                                           'physical_utilisation': 0, 

                                           'freespace': EMPTY_VG_SPACE} 

        lvutil.exists.return_value = False 

        MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        pread2.assert_called_once_with( 

            ["/usr/sbin/blkdiscard","-v", 

            "/dev/VG_XenStorage-some-uuid/some-uuid_trim_lv"]) 

 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_releases_lock(self, 

                                   context, 

                                   sr_get_capability, 

                                   MockLock, 

                                   lvutil): 

        lvutil.exists.return_value = False 

        sr_lock = MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertFalse(sr_lock.acquired) 

 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_removes_lv_with_leftover_trim_vol(self, 

                                                      context, 

                                                      sr_get_capability, 

                                                      MockLock, 

                                                      lvutil): 

        lvutil.exists.return_value = True 

        MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertEquals([ 

                mock.call('/dev/VG_XenStorage-some-uuid/some-uuid_trim_lv'), 

                mock.call( 

                    '/dev/VG_XenStorage-some-uuid/some-uuid_trim_lv') 

            ], lvutil.remove.mock_calls) 

 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_lock_released_even_if_exception_raised(self, 

                                                            context, 

                                                            sr_get_capability, 

                                                            MockLock, 

                                                            lvutil): 

        lvutil.create.side_effect = Exception('blah') 

        srlock = AlwaysFreeLock() 

        MockLock.return_value = srlock 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertFalse(srlock.acquired) 

 

    @mock.patch("trim_util.lvutil.LVM_SIZE_INCREMENT", EMPTY_VG_SPACE) 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_when_exception_then_returns_generic_err(self, 

                                                             context, 

                                                             sr_get_capability, 

                                                             MockLock, 

                                                             lvutil): 

        lvutil._getVGstats.return_value = {'physical_size': 0, 

                                           'physical_utilisation': 0, 

                                           'freespace': EMPTY_VG_SPACE} 

        lvutil.create.side_effect = Exception('blah') 

        srlock = AlwaysFreeLock() 

        MockLock.return_value = srlock 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertXML(""" 

        <?xml version="1.0" ?> 

        <trim_response> 

            <key_value_pair> 

                <key>errcode</key> 

                <value>UnknownTrimException</value> 

            </key_value_pair> 

            <key_value_pair> 

                <key>errmsg</key> 

                <value>Unknown Exception: trim failed on SR [some-uuid]</value> 

            </key_value_pair> 

        </trim_response> 

        """, result) 

 

    @mock.patch("trim_util.lvutil.LVM_SIZE_INCREMENT", EMPTY_VG_SPACE) 

    @mock.patch('util.pread2', autospec=True) 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_when_trim_succeeded_returns_true(self, 

                                                      context, 

                                                      sr_get_capability, 

                                                      MockLock, 

                                                      lvutil, 

                                                      pread2): 

        lvutil._getVGstats.return_value = {'physical_size': 0, 

                                           'physical_utilisation': 0, 

                                           'freespace': EMPTY_VG_SPACE} 

        MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertEquals('True', result) 

 

    @mock.patch('trim_util.time.time', autospec=True) 

    def test_log_last_triggered_no_key(self, mock_time): 

        session = mock.Mock() 

        mock_time.return_value = 0 

        session.xenapi.SR.get_by_uuid.return_value = 'sr_ref' 

        session.xenapi.SR.get_other_config.return_value = {} 

 

        trim_util._log_last_triggered(session, 'sr_uuid') 

 

        session.xenapi.SR.add_to_other_config.assert_called_with( 

            'sr_ref', trim_util.TRIM_LAST_TRIGGERED_KEY, '0') 

        self.assertEqual(0, session.xenapi.SR.remove_from_other_config.call_count) 

 

    @mock.patch('trim_util.time.time', autospec=True) 

    def test_log_last_triggered_has_key(self, mock_time): 

        session = mock.Mock() 

        mock_time.return_value = 0 

        session.xenapi.SR.get_by_uuid.return_value = 'sr_ref' 

        other_config = {trim_util.TRIM_LAST_TRIGGERED_KEY: '0'} 

        session.xenapi.SR.get_other_config.return_value = other_config 

 

        trim_util._log_last_triggered(session, 'sr_uuid') 

 

        session.xenapi.SR.remove_from_other_config.assert_called_with( 

            'sr_ref', trim_util.TRIM_LAST_TRIGGERED_KEY) 

        session.xenapi.SR.add_to_other_config.assert_called_with( 

            'sr_ref', trim_util.TRIM_LAST_TRIGGERED_KEY, '0') 

 

    @mock.patch('trim_util.time.time', autospec=True) 

    @mock.patch('trim_util.util.logException', autospec=True) 

    def test_log_last_triggered_exc_logged(self, mock_log_exc, mock_time): 

        session = mock.Mock() 

        mock_time.return_value = 0 

        session.xenapi.SR.get_by_uuid.side_effect = Exception() 

 

        # This combination ensures that an exception does not cause the log 

        # function to throw, but the exception is still logged 

        trim_util._log_last_triggered(session, 'sr_uuid') 

 

        self.assertEqual(1, mock_log_exc.call_count) 

 

    @mock.patch("trim_util.lvutil.LVM_SIZE_INCREMENT", EMPTY_VG_SPACE) 

    @mock.patch('trim_util.lvutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    @mock.patch('util.sr_get_capability', autospec=True) 

    @testlib.with_context 

    def test_do_trim_returns_exception_when_sr_full(self, 

                                   context, 

                                   sr_get_capability, 

                                   MockLock, 

                                   lvutil): 

        lvutil._getVGstats.return_value = {'physical_size': 0, 

                                           'physical_utilisation': 0, 

                                           'freespace': 0} 

        MockLock.return_value = AlwaysFreeLock() 

        sr_get_capability.return_value = [trim_util.TRIM_CAP] 

        context.setup_error_codes() 

 

        result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) 

 

        self.assertXML(""" 

        <?xml version="1.0" ?> 

        <trim_response> 

            <key_value_pair> 

                <key>errcode</key> 

                <value>Trim failed on full SR</value> 

            </key_value_pair> 

            <key_value_pair> 

                <key>errmsg</key> 

                <value>No space to claim on a full SR</value> 

            </key_value_pair> 

        </trim_response> 

        """, result)