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

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

# -*- 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 logging 

import os 

import tempfile 

import unittest 

import unittest.mock 

 

import fixtures 

 

import snapcraft.common 

import snapcraft.yaml 

from snapcraft import dirs 

from snapcraft.tests import TestCase 

 

 

class TestYaml(TestCase): 

 

    def setUp(self): 

        super().setUp() 

        dirs.setup_dirs() 

 

        patcher = unittest.mock.patch('os.path.exists') 

        mock_wrap_exe = patcher.start() 

        mock_wrap_exe.return_value = True 

        self.addCleanup(patcher.stop) 

 

    def make_snapcraft_yaml(self, content): 

        tempdirObj = tempfile.TemporaryDirectory() 

        self.addCleanup(tempdirObj.cleanup) 

        os.chdir(tempdirObj.name) 

        with open('snapcraft.yaml', 'w') as fp: 

            fp.write(content) 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    @unittest.mock.patch('snapcraft.wiki.Wiki.get_part') 

    def test_config_loads_plugins(self, mock_get_part, mock_loadPlugin): 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: test 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

        snapcraft.yaml.Config() 

        mock_loadPlugin.assert_called_with('part1', 'go', { 

            'stage-packages': ['fswebcam'], 

            'stage': [], 'snap': [], 

        }) 

 

        self.assertFalse(mock_get_part.called) 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    @unittest.mock.patch('snapcraft.wiki.Wiki.compose') 

    def test_config_loads_part_from_wiki(self, mock_compose, mock_loadPlugin): 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: test 

icon: my-icon.png 

 

parts: 

  part1: 

    stage-packages: [fswebcam] 

""") 

        mock_compose.return_value = { 

            'plugin': 'go', 

            'source': 'http://source.tar.gz', 

        } 

 

        snapcraft.yaml.Config() 

 

        mock_loadPlugin.assert_called_with('part1', 'go', { 

            'source': 'http://source.tar.gz', 'stage': [], 'snap': []}) 

 

    @unittest.mock.patch('snapcraft.lifecycle.load_plugin') 

    @unittest.mock.patch('snapcraft.wiki.Wiki.get_part') 

    def test_config_with_wiki_part_after(self, mock_get_part, mock_load): 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: test 

icon: my-icon.png 

 

parts: 

  part1: 

    after: 

      - part2wiki 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

 

        def load_effect(*args, **kwargs): 

            mock_part = unittest.mock.Mock() 

            mock_part.code.build_packages = [] 

            mock_part.deps = [] 

            mock_part.name = args[0] 

 

            return mock_part 

 

        mock_load.side_effect = load_effect 

        mock_get_part.return_value = { 

            'plugin': 'go', 

            'source': 'http://somesource' 

        } 

 

        snapcraft.yaml.Config() 

 

        call1 = unittest.mock.call('part1', 'go', { 

            'stage-packages': ['fswebcam'], 'stage': [], 'snap': []}) 

        call2 = unittest.mock.call('part2wiki', 'go', { 

            'source': 'http://somesource'}) 

 

        mock_load.assert_has_calls([call1, call2]) 

        self.assertTrue(mock_get_part.called) 

 

    def test_config_raises_on_missing_snapcraft_yaml(self): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        # no snapcraft.yaml 

        with self.assertRaises( 

                snapcraft.yaml.SnapcraftYamlFileError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual(raised.exception.file, 'snapcraft.yaml') 

 

    def test_config_loop(self): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: test 

icon: my-icon.png 

 

parts: 

  p1: 

    plugin: tar-content 

    source: . 

    after: [p2] 

  p2: 

    plugin: tar-content 

    source: . 

    after: [p1] 

""") 

        with self.assertRaises(snapcraft.yaml.SnapcraftLogicError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual( 

            raised.exception.message, 

            'circular dependency chain found in parts definition') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_invalid_yaml_missing_name(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml(""" 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: nothing 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual(raised.exception.message, 

                         '\'name\' is a required property') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_invalid_yaml_invalid_name_as_number(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: 1 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: nothing 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual(raised.exception.message, 

                         '1 is not of type \'string\'') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_invalid_yaml_invalid_name_chars(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: myapp@me_1.0 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: nothing 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual( 

            raised.exception.message, 

            '\'myapp@me_1.0\' does not match \'^[a-z0-9][a-z0-9+-]*$\'') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_deprecation_for_type(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.WARNING) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: myapp 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: nothing 

icon: my-icon.png 

 

parts: 

  part1: 

    type: go 

    stage-packages: [fswebcam] 

""") 

        config = snapcraft.yaml.Config() 

        self.assertEqual(fake_logger.output, 

                         'DEPRECATED: Use "plugin" instead of "type"\n') 

        self.assertFalse('type' in config.data) 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_invalid_yaml_missing_description(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual( 

            raised.exception.message, 

            '\'description\' is a required property') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_tab_in_yaml(self, mock_loadPlugin): 

        fake_logger = fixtures.FakeLogger(level=logging.ERROR) 

        self.useFixture(fake_logger) 

 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

\tvendor: me <me@me.com> 

summary: test 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

""") 

 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml.Config() 

 

        self.assertEqual( 

            raised.exception.message, 

            'found character \'\\t\' that cannot start any token ' 

            'on line 2 of snapcraft.yaml') 

 

    @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') 

    def test_config_expands_filesets(self, mock_loadPlugin): 

        self.make_snapcraft_yaml("""name: test 

version: "1" 

vendor: me <me@me.com> 

summary: test 

description: test 

icon: my-icon.png 

 

parts: 

  part1: 

    plugin: go 

    stage-packages: [fswebcam] 

    filesets: 

      wget: 

        - /usr/lib/wget.so 

        - /usr/bin/wget 

      build-wget: 

        - /usr/lib/wget.a 

    stage: 

      - $wget 

      - $build-wget 

    snap: 

      - $wget 

      - /usr/share/my-icon.png 

""") 

        snapcraft.yaml.Config() 

 

        mock_loadPlugin.assert_called_with('part1', 'go', { 

            'snap': ['/usr/lib/wget.so', '/usr/bin/wget', 

                     '/usr/share/my-icon.png'], 

            'stage-packages': ['fswebcam'], 

            'stage': ['/usr/lib/wget.so', '/usr/bin/wget', '/usr/lib/wget.a'], 

        }) 

 

 

class TestValidation(TestCase): 

 

    def setUp(self): 

        super().setUp() 

        dirs.setup_dirs() 

 

        patcher = unittest.mock.patch('os.path.exists') 

        self.mock_path_exists = patcher.start() 

        self.mock_path_exists.return_value = True 

        self.addCleanup(patcher.stop) 

 

        self.data = { 

            'name': 'my-package-1', 

            'version': '1.0-snapcraft1~ppa1', 

            'vendor': 'Me <me@me.com>', 

            'summary': 'my summary less that 79 chars', 

            'description': 'description which can be pretty long', 

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

            'parts': { 

                'part1': { 

                    'plugin': 'project', 

                }, 

            }, 

        } 

 

    def test_required_properties(self): 

        for key in self.data: 

            data = self.data.copy() 

            with self.subTest(key=key): 

                del data[key] 

 

                with self.assertRaises( 

                        snapcraft.yaml.SnapcraftSchemaError) as raised: 

                    snapcraft.yaml._validate_snapcraft_yaml(data) 

 

                expected_message = '\'{}\' is a required property'.format(key) 

                self.assertEqual(raised.exception.message, expected_message, 

                                 msg=data) 

 

    def test_invalid_names(self): 

        invalid_names = [ 

            'package@awesome', 

            'something.another', 

            '_hideme', 

        ] 

 

        for name in invalid_names: 

            data = self.data.copy() 

            with self.subTest(key=name): 

                data['name'] = name 

 

                with self.assertRaises( 

                        snapcraft.yaml.SnapcraftSchemaError) as raised: 

                    snapcraft.yaml._validate_snapcraft_yaml(data) 

 

                expected_message = ( 

                    '\'{}\' does not match \'^[a-z0-9][a-z0-9+-]*$\'').format( 

                    name) 

                self.assertEqual(raised.exception.message, expected_message, 

                                 msg=data) 

 

    def test_summary_too_long(self): 

        self.data['summary'] = 'a' * 80 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        expected_message = '\'{}\' is too long'.format(self.data['summary']) 

        self.assertEqual(raised.exception.message, expected_message, 

                         msg=self.data) 

 

    def test_valid_types(self): 

        self.data['type'] = 'app' 

        snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        self.data['type'] = 'framework' 

        snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

    def test_invalid_types(self): 

        invalid_types = [ 

            'apps', 

            'kernel', 

            'platform', 

            'oem', 

            'os', 

        ] 

 

        for t in invalid_types: 

            data = self.data.copy() 

            with self.subTest(key=t): 

                data['type'] = t 

 

                with self.assertRaises( 

                        snapcraft.yaml.SnapcraftSchemaError) as raised: 

                    snapcraft.yaml._validate_snapcraft_yaml(data) 

 

                expected_message = ('\'{}\' is not one of ' + 

                                    '[\'app\', \'framework\']').format(t) 

                self.assertEqual(raised.exception.message, expected_message, 

                                 msg=data) 

 

    def test_valid_services(self): 

        self.data['services'] = { 

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

            'service2': { 

                'start': 'binary2', 

                'stop': 'binary2 --stop', 

            }, 

        } 

 

        snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

    def test_invalid_binary_names(self): 

        invalid_names = { 

            'qwe#rty': {'exec': '1'}, 

            'qwe_rty': {'exec': '1'}, 

            'que rty': {'exec': '1'}, 

            'que  rty': {'exec': '1'}, 

        } 

 

        for t in invalid_names: 

            data = self.data.copy() 

            with self.subTest(key=t): 

                data['binaries'] = {t: invalid_names[t]} 

 

                with self.assertRaises( 

                        snapcraft.yaml.SnapcraftSchemaError) as raised: 

                    snapcraft.yaml._validate_snapcraft_yaml(data) 

 

                expected_message = ('Additional properties are not allowed ' 

                                    '(\'{}\' was unexpected)').format(t) 

                self.assertEqual(raised.exception.message, expected_message, 

                                 msg=data) 

 

    def test_invalid_service_names(self): 

        invalid_names = { 

            'qwe#rty': {'start': '1'}, 

            'qwe_rty': {'start': '1'}, 

            'que_rty': {'start': '1'}, 

            'quer  ty': {'start': '1'}, 

        } 

 

        for t in invalid_names: 

            data = self.data.copy() 

            with self.subTest(key=t): 

                data['services'] = {t: invalid_names[t]} 

 

                with self.assertRaises( 

                        snapcraft.yaml.SnapcraftSchemaError) as raised: 

                    snapcraft.yaml._validate_snapcraft_yaml(data) 

 

                expected_message = ('Additional properties are not allowed ' 

                                    '(\'{}\' was unexpected)').format(t) 

                self.assertEqual(raised.exception.message, expected_message, 

                                 msg=data) 

 

    def test_services_required_properties(self): 

        self.data['services'] = {'service1': {}} 

 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        expected_message = '\'start\' is a required property' 

        self.assertEqual(raised.exception.message, expected_message, 

                         msg=self.data) 

 

    def test_schema_file_not_found(self): 

        mock_the_open = unittest.mock.mock_open() 

        mock_the_open.side_effect = FileNotFoundError() 

 

        with unittest.mock.patch('snapcraft.yaml.open', mock_the_open, 

                                 create=True): 

            with self.assertRaises( 

                    snapcraft.yaml.SnapcraftSchemaError) as raised: 

                snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        expected_path = os.path.join(snapcraft.common.get_schemadir(), 

                                     'snapcraft.yaml') 

        mock_the_open.assert_called_once_with(expected_path) 

        expected_message = ('snapcraft validation file is missing from ' 

                            'installation path') 

        self.assertEqual(raised.exception.message, expected_message) 

 

    def test_icon_missing(self): 

        self.mock_path_exists.return_value = False 

 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        expected_message = '\'my-icon.png\' is not a \'icon-path\'' 

        self.assertEqual(raised.exception.message, expected_message, 

                         msg=self.data) 

 

    def test_invalid_part_name_plugin_raises_exception(self): 

        self.data['parts']['plugins'] = {'type': 'go'} 

 

        with self.assertRaises(snapcraft.yaml.SnapcraftSchemaError) as raised: 

            snapcraft.yaml._validate_snapcraft_yaml(self.data) 

 

        expected_message = 'Additional properties are not allowed ' \ 

                           '(\'plugins\' was unexpected)' 

        self.assertEqual(raised.exception.message, expected_message, 

                         msg=self.data) 

 

 

class TestFilesets(TestCase): 

 

    def setUp(self): 

        super().setUp() 

 

        self.properties = { 

            'filesets': { 

                '1': ['1', '2', '3'], 

                '2': [], 

            } 

        } 

 

    def test_expand_var(self): 

        self.properties['stage'] = ['$1'] 

 

        fs = snapcraft.yaml._expand_filesets_for('stage', self.properties) 

        self.assertEqual(fs, ['1', '2', '3']) 

 

    def test_no_expansion(self): 

        self.properties['stage'] = ['1'] 

 

        fs = snapcraft.yaml._expand_filesets_for('stage', self.properties) 

        self.assertEqual(fs, ['1']) 

 

    def test_invalid_expansion(self): 

        self.properties['stage'] = ['$3'] 

 

        with self.assertRaises(snapcraft.yaml.SnapcraftLogicError) as raised: 

            snapcraft.yaml._expand_filesets_for('stage', self.properties) 

 

        self.assertEqual( 

            raised.exception.message, 

            '\'$3\' referred to in the \'stage\' fileset but it is not ' 

            'in filesets')