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

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- 

# 

# Copyright (C) 2015 Canonical Ltd 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License version 3 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, see <http://www.gnu.org/licenses/>. 

 

import os 

from unittest.mock import ( 

    call, 

    patch, 

) 

 

from snapcraft import ( 

    common, 

    meta, 

    tests 

) 

 

 

class ComposeTestCase(tests.TestCase): 

 

    def setUp(self): 

        super().setUp() 

        patcher = patch('snapcraft.meta._wrap_exe') 

        mock_wrap_exe = patcher.start() 

        mock_wrap_exe.return_value = 'binary.wrapped' 

        self.addCleanup(patcher.stop) 

 

        self.config_data = { 

            'name': 'my-package', 

            'version': '1.0', 

            'vendor': 'Sergio Schvezov <sergio.schvezov@canonical.com>', 

            'icon': 'my-icon.png', 

        } 

 

    def test_plain_no_binaries_or_services(self): 

        y = meta._compose_package_yaml('meta', self.config_data, 

                                       ['armhf', 'amd64']) 

 

        expected = { 

            'name': 'my-package', 

            'version': '1.0', 

            'vendor': 'Sergio Schvezov <sergio.schvezov@canonical.com>', 

            'icon': 'my-icon.png', 

            'architectures': ['armhf', 'amd64'], 

        } 

 

        self.assertEqual(y, expected) 

 

    def test_plain_no_binaries_or_services_or_arches(self): 

        y = meta._compose_package_yaml('meta', self.config_data, None) 

 

        expected = { 

            'name': 'my-package', 

            'version': '1.0', 

            'vendor': 'Sergio Schvezov <sergio.schvezov@canonical.com>', 

            'icon': 'my-icon.png', 

        } 

 

        self.assertEqual(y, expected) 

 

    def test_with_binaries(self): 

        self.config_data['binaries'] = { 

            'binary1': {'exec': 'binary1.sh go'}, 

            'binary2': {'exec': 'binary2.sh'}, 

        } 

 

        y = meta._compose_package_yaml('meta', self.config_data, 

                                       ['armhf', 'amd64']) 

 

        self.assertEqual(len(y['binaries']), 2) 

        for b in y['binaries']: 

            if b['name'] is 'binary1': 

                self.assertEqual(b['exec'], 'binary.wrapped go') 

            else: 

                self.assertEqual(b['exec'], 'binary.wrapped') 

 

    def test_with_services(self): 

        self.config_data['services'] = { 

            'service1': {'start': 'binary1'}, 

            'service2': { 

                'start': 'binary2 --start', 

                'stop': 'binary2 --stop', 

            }, 

        } 

 

        y = meta._compose_package_yaml('meta', self.config_data, 

                                       ['armhf', 'amd64']) 

 

        self.assertEqual(len(y['services']), 2) 

        for b in y['services']: 

            if b['name'] is 'service1': 

                self.assertEqual(b['start'], 'binary.wrapped') 

            else: 

                self.assertEqual(b['start'], 'binary.wrapped --start') 

                self.assertEqual(b['stop'], 'binary.wrapped --stop') 

 

    def test_plain_no_binaries_or_services_with_optionals(self): 

        self.config_data['frameworks'] = ['mir', ] 

 

        y = meta._compose_package_yaml('meta', self.config_data, 

                                       ['armhf', 'amd64']) 

 

        expected = { 

            'name': 'my-package', 

            'version': '1.0', 

            'vendor': 'Sergio Schvezov <sergio.schvezov@canonical.com>', 

            'icon': 'my-icon.png', 

            'architectures': ['armhf', 'amd64'], 

            'frameworks': ['mir', ], 

        } 

 

        self.assertEqual(y, expected) 

 

    def test_compose_readme(self): 

        self.config_data['summary'] = 'one line summary' 

        self.config_data['description'] = \ 

            'the description\nwhich can be longer' 

 

        readme_text = '''one line summary 

the description 

which can be longer 

''' 

 

        self.assertEqual(meta._compose_readme(self.config_data), readme_text) 

 

 

class Create(tests.TestCase): 

 

    def setUp(self): 

        super().setUp() 

        patcher_makedirs = patch('os.makedirs') 

        self.mock_makedirs = patcher_makedirs.start() 

        self.addCleanup(patcher_makedirs.stop) 

 

        patcher_copyfile = patch('shutil.copyfile') 

        self.mock_copyfile = patcher_copyfile.start() 

        self.addCleanup(patcher_copyfile.stop) 

 

        patcher_move = patch('shutil.move') 

        self.mock_move = patcher_move.start() 

        self.addCleanup(patcher_move.stop) 

 

        patcher_exists = patch('os.path.exists') 

        self.mock_exists = patcher_exists.start() 

        self.mock_exists.return_value = True 

        self.addCleanup(patcher_exists.stop) 

 

        self.config_data = { 

            'name': 'my-package', 

            'version': '1.0', 

            'vendor': 'Sergio Schvezov <sergio.schvezov@canonical.com>', 

            'description': 'my description', 

            'summary': 'my summary', 

            'icon': 'my-icon.png', 

            'config': 'bin/config', 

            'binaries': { 

                'bash': { 

                    'exec': 'bin/bash', 

                    'security-policy': { 

                        'apparmor': 'file.apparmor', 

                        'seccomp': 'file.seccomp', 

                    }, 

                } 

            } 

        } 

 

        self.meta_dir = os.path.join(os.path.abspath(os.curdir), 

                                     'snap', 'meta') 

        self.hooks_dir = os.path.join(self.meta_dir, 'hooks') 

 

        self.expected_open_calls = [ 

            call(os.path.join(self.meta_dir, 'package.yaml'), 'w'), 

            call().__enter__(), 

            call().__enter__().write('architectures'), 

            call().__enter__().write(':'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('-'), 

            call().__enter__().write(' '), 

            call().__enter__().write('amd64'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('binaries'), 

            call().__enter__().write(':'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('-'), 

            call().__enter__().write(' '), 

            call().__enter__().write('exec'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('bin/bash.wrapper'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('  '), 

            call().__enter__().write('name'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('bash'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('  '), 

            call().__enter__().write('security-policy'), 

            call().__enter__().write(':'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('    '), 

            call().__enter__().write('apparmor'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('meta/file.apparmor'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('    '), 

            call().__enter__().write('seccomp'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('meta/file.seccomp'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('icon'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('meta/my-icon.png'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('name'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('my-package'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('vendor'), 

            call().__enter__().write(':'), 

            call().__enter__().write(' '), 

            call().__enter__().write('Sergio'), 

            call().__enter__().write(' '), 

            call().__enter__().write('Schvezov'), 

            call().__enter__().write(' '), 

            call().__enter__().write('<sergio.schvezov@canonical.com>'), 

            call().__enter__().write('\n'), 

            call().__enter__().write('version'), 

            call().__enter__().write(':'), 

            call().__enter__().write(" '"), 

            call().__enter__().write('1.0'), 

            call().__enter__().write("'"), 

            call().__enter__().write('\n'), 

            call().__enter__().flush(), 

            call().__enter__().flush(), 

            call().__exit__(None, None, None), 

            call(os.path.join(self.meta_dir, 'readme.md'), 'w'), 

            call().__enter__(), 

            call().__enter__().write('my summary\nmy description\n'), 

            call().__exit__(None, None, None), 

        ] 

 

    @patch('snapcraft.meta._write_wrap_exe') 

    @patch('snapcraft.meta.open', create=True) 

    def test_create_meta(self, mock_the_open, mock_wrap_exe): 

        meta.create(self.config_data, ['amd64']) 

 

        self.mock_makedirs.assert_has_calls([ 

            call(self.meta_dir, exist_ok=True), 

            call(self.hooks_dir), 

        ]) 

 

        mock_the_open.assert_has_calls(self.expected_open_calls) 

        mock_wrap_exe.assert_has_calls([ 

            call( 

                '$SNAP_APP_PATH/bin/bash', 

                os.path.join(os.path.abspath(os.curdir), 

                             'snap/bin/bash.wrapper'), 

                shebang=None, 

            ), 

            call( 

                'bin/config', 

                os.path.join(self.hooks_dir, 'config'), 

                args=[], 

                cwd='$SNAP_APP_PATH', 

            ), 

        ]) 

        self.mock_copyfile.assert_has_calls([ 

            call('my-icon.png', os.path.join(self.meta_dir, 

                 'my-icon.png')), 

            call('file.apparmor', os.path.join(self.meta_dir, 

                 'file.apparmor')), 

            call('file.seccomp', os.path.join(self.meta_dir, 

                 'file.seccomp')), 

        ]) 

 

    @patch('snapcraft.meta._write_wrap_exe') 

    @patch('snapcraft.meta.open', create=True) 

    def test_create_meta_with_vararg_config(self, mock_the_open, 

                                            mock_wrap_exe): 

        self.config_data['config'] = 'python3 my.py --config' 

 

        meta.create(self.config_data, ['amd64']) 

 

        self.mock_makedirs.assert_has_calls([ 

            call(self.meta_dir, exist_ok=True), 

            call(self.hooks_dir), 

        ]) 

 

        mock_the_open.assert_has_calls(self.expected_open_calls) 

        mock_wrap_exe.assert_has_calls([ 

            call( 

                '$SNAP_APP_PATH/bin/bash', 

                os.path.join(os.path.abspath(os.curdir), 

                             'snap/bin/bash.wrapper'), 

                shebang=None, 

            ), 

            call( 

                'python3', 

                os.path.join(self.hooks_dir, 'config'), 

                args=['my.py', '--config'], 

                cwd='$SNAP_APP_PATH', 

            ), 

        ]) 

 

    @patch('snapcraft.meta._write_wrap_exe') 

    @patch('snapcraft.meta.open', create=True) 

    def test_create_meta_without_config(self, mock_the_open, mock_wrap_exe): 

        del self.config_data['config'] 

 

        meta.create(self.config_data, ['amd64']) 

 

        self.mock_makedirs.assert_called_once_with(self.meta_dir, 

                                                   exist_ok=True) 

        mock_the_open.assert_has_calls(self.expected_open_calls) 

 

 

# TODO this needs more tests. 

class WrapExeTestCase(tests.TestCase): 

 

    def test_wrap_exe_must_write_wrapper(self): 

        snapdir = common.get_snapdir() 

        os.mkdir(snapdir) 

 

        relative_exe_path = 'test_relexepath' 

        with open(os.path.join(snapdir, relative_exe_path), 'w'): 

            pass 

 

        relative_wrapper_path = meta._wrap_exe(relative_exe_path) 

        wrapper_path = os.path.join(snapdir, relative_wrapper_path) 

 

        expected = ('#!/bin/sh\n' 

                    '\n\n' 

                    'exec "$SNAP_APP_PATH/test_relexepath" $*\n') 

        with open(wrapper_path) as wrapper_file: 

            wrapper_contents = wrapper_file.read() 

 

        self.assertEqual(expected, wrapper_contents) 

 

    def test_snap_shebangs_extracted(self): 

        """Shebangs pointing to the snap's install dir get extracted. 

 

        If the exe has a shebang that points to the snap's install dir, 

        the wrapper script will execute it directly rather than relying 

        the shebang. 

 

        The shebang needs to be and absolute path, and we don't know 

        where in which directory the snap will be installed. Executing 

        it in the wrapper script allows us to use the $SNAP_APP_PATH 

        environment variable. 

        """ 

        snapdir = common.get_snapdir() 

        partsdir = common.get_partsdir() 

        os.mkdir(snapdir) 

 

        relative_exe_path = 'test_relexepath' 

        shebang_path = os.path.join( 

            partsdir, 'testsnap', 'install', 'snap_exe') 

        exe_contents = '#!{}\n'.format(shebang_path) 

        with open(os.path.join(snapdir, relative_exe_path), 'w') as exe: 

            exe.write(exe_contents) 

 

        relative_wrapper_path = meta._wrap_exe(relative_exe_path) 

        wrapper_path = os.path.join(snapdir, relative_wrapper_path) 

 

        expected = ( 

            '#!/bin/sh\n' 

            '\n\n' 

            'exec "$SNAP_APP_PATH/snap_exe"' 

            ' "$SNAP_APP_PATH/test_relexepath" $*\n') 

        with open(wrapper_path) as wrapper_file: 

            wrapper_contents = wrapper_file.read() 

 

        self.assertEqual(expected, wrapper_contents) 

        with open(os.path.join(snapdir, relative_exe_path), 'r') as exe: 

            # The shebang wasn't changed, since we don't know what the 

            # path will be on the installed system. 

            self.assertEqual(exe_contents, exe.read()) 

 

    def test_non_snap_shebangs_ignored(self): 

        """Shebangs not pointing to the snap's install dir are ignored. 

 

        If the shebang points to a system executable, there's no need to 

        interfere. 

        """ 

        snapdir = common.get_snapdir() 

        os.mkdir(snapdir) 

 

        relative_exe_path = 'test_relexepath' 

        exe_contents = '#!/bin/bash\necho hello\n' 

        with open(os.path.join(snapdir, relative_exe_path), 'w') as exe: 

            exe.write(exe_contents) 

 

        relative_wrapper_path = meta._wrap_exe(relative_exe_path) 

        wrapper_path = os.path.join(snapdir, relative_wrapper_path) 

 

        expected = ('#!/bin/sh\n' 

                    '\n\n' 

                    'exec "$SNAP_APP_PATH/test_relexepath" $*\n') 

        with open(wrapper_path) as wrapper_file: 

            wrapper_contents = wrapper_file.read() 

 

        self.assertEqual(expected, wrapper_contents) 

        with open(os.path.join(snapdir, relative_exe_path), 'r') as exe: 

            self.assertEqual(exe_contents, exe.read()) 

 

    def test_non_shebang_binaries_ignored(self): 

        """Native binaries are ignored. 

 

        If the executable is a native binary, and thus not have a 

        shebang, it's ignored. 

        """ 

        snapdir = common.get_snapdir() 

        os.mkdir(snapdir) 

 

        relative_exe_path = 'test_relexepath' 

        # Choose a content which can't be decoded with utf-8, to make 

        # sure no decoding errors happen. 

        exe_contents = b'\xf0\xf1' 

        with open(os.path.join(snapdir, relative_exe_path), 'wb') as exe: 

            exe.write(exe_contents) 

 

        relative_wrapper_path = meta._wrap_exe(relative_exe_path) 

        wrapper_path = os.path.join(snapdir, relative_wrapper_path) 

 

        expected = ('#!/bin/sh\n' 

                    '\n\n' 

                    'exec "$SNAP_APP_PATH/test_relexepath" $*\n') 

        with open(wrapper_path) as wrapper_file: 

            wrapper_contents = wrapper_file.read() 

 

        self.assertEqual(expected, wrapper_contents) 

        with open(os.path.join(snapdir, relative_exe_path), 'rb') as exe: 

            self.assertEqual(exe_contents, exe.read())