forked from juju/charm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_charm_proof.py
1325 lines (1213 loc) · 43.9 KB
/
test_charm_proof.py
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Copyright (C) 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 sys
from os.path import abspath, dirname, join
from shutil import rmtree
from tempfile import mkdtemp
from textwrap import dedent
from unittest import main, TestCase
from mock import Mock, call, patch
proof_path = dirname(dirname(dirname(abspath(__file__))))
proof_path = join(proof_path, 'charmtools')
sys.path.append(proof_path)
from charmtools.charms import CharmLinter as Linter # noqa
from charmtools.charms import Charm # noqa
from charmtools.charms import validate_display_name # noqa
from charmtools.charms import validate_maintainer # noqa
from charmtools.charms import validate_categories_and_tags # noqa
from charmtools.charms import validate_storage # noqa
from charmtools.charms import validate_devices # noqa
from charmtools.charms import validate_series # noqa
from charmtools.charms import validate_min_juju_version # noqa
from charmtools.charms import validate_extra_bindings # noqa
from charmtools.charms import validate_payloads # noqa
from charmtools.charms import validate_actions # noqa
from charmtools.charms import validate_terms # noqa
from charmtools.charms import validate_resources # noqa
from charmtools.charms import validate_deployment # noqa
class TestCharmProof(TestCase):
def setUp(self):
self.charm_dir = mkdtemp()
self.linter = Linter()
def tearDown(self):
rmtree(self.charm_dir)
def write_config(self, text):
with open(join(self.charm_dir, 'config.yaml'), 'w') as f:
f.write(dedent(text))
def test_config_yaml_missing(self):
self.linter.check_config_file(self.charm_dir)
self.assertEqual(
['I: File config.yaml not found.'], self.linter.lint)
def test_clean_config(self):
self.write_config("""
options:
string_opt:
type: string
description: A string option
default: some text
int_opt:
type: int
description: An int option
default: 2
float_opt:
type: float
default: 4.2
description: This is a float option.
bool_opt:
type: boolean
default: True
description: This is a boolean option.
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual([], self.linter.lint)
def test_missing_type_defaults_to_string(self):
# A warning is issued but no failure.
self.write_config("""
options:
string_opt:
description: A string option
default: some text
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(
['W: config.yaml: option string_opt does not have the keys: '
'type'],
self.linter.lint)
def test_config_with_invalid_yaml(self):
self.write_config("""
options:
foo: 42
bar
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
message = self.linter.lint[0]
self.assertTrue(message.startswith(
'E: Cannot parse config.yaml: while scanning a simple key'),
'wrong lint message: %s' % message)
def test_config_no_root_dict(self):
self.write_config("""
this is not a dictionary
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
self.assertEqual(
'E: config.yaml not parsed into a dictionary.',
self.linter.lint[0])
def test_options_key_missing(self):
self.write_config("""
foo: bar
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
self.assertEqual(
'E: config.yaml must have an "options" key.',
self.linter.lint[0])
def test_ignored_root_keys(self):
self.write_config("""
options:
string_opt:
type: string
description: whatever
default: blah
noise: The art of - in visible silence
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
self.assertEqual(
"W: Ignored keys in config.yaml: ['noise']",
self.linter.lint[0])
def test_options_is_not_dict(self):
self.write_config("""
options: a string instead of a dict
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
self.assertEqual(
'E: config.yaml: options section is not parsed as a dictionary',
self.linter.lint[0])
def test_option_data_not_a_dict(self):
self.write_config("""
options:
foo: just a string
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
self.assertEqual(
'E: config.yaml: data for option foo is not a dict',
self.linter.lint[0])
def test_option_data_with_subset_of_allowed_keys(self):
self.write_config("""
options:
foo:
type: int
description: whatever
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'W: config.yaml: option foo does not have the keys: default')
self.assertEqual(expected, self.linter.lint[0])
def test_option_data_with_unknown_key(self):
self.write_config("""
options:
foo:
type: int
default: 3
description: whatever
something: completely different
42: the answer
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'W: config.yaml: option foo has unknown keys: 42, something')
self.assertEqual(expected, self.linter.lint[0])
def test_option_data_with_invalid_descr_type(self):
self.write_config("""
options:
foo:
type: int
default: 3
description: 1
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = ('W: config.yaml: description of option '
'foo should be a non-empty string')
self.assertEqual(expected, self.linter.lint[0])
def test_option_data_with_blank_descr(self):
self.write_config("""
options:
foo:
type: int
default: 3
description:
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = ('W: config.yaml: description of option '
'foo should be a non-empty string')
self.assertEqual(expected, self.linter.lint[0])
def test_option_data_with_missing_option_type(self):
self.write_config("""
options:
foo:
default: foo
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'W: config.yaml: option foo does not have the keys: type')
self.assertEqual(expected, self.linter.lint[0])
def test_option_data_with_invalid_option_type(self):
self.write_config("""
options:
foo:
type: strr
default: foo
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'W: config.yaml: option foo has an invalid type (strr)')
self.assertEqual(expected, self.linter.lint[0])
def test_option_type_str_conflict_with_default_value(self):
self.write_config("""
options:
foo:
type: string
default: 17
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'E: config.yaml: type of option foo is specified as string, but '
'the type of the default value is int')
self.assertEqual(expected, self.linter.lint[0])
def test_option_type_int_conflict_with_default_value(self):
self.write_config("""
options:
foo:
type: int
default: foo
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'E: config.yaml: type of option foo is specified as int, but '
'the type of the default value is str')
self.assertEqual(expected, self.linter.lint[0])
def test_option_empty_default_value_string(self):
# An empty default value is treated as INFO for strings
self.write_config("""
options:
foo:
type: string
default:
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'I: config.yaml: option foo has no default value')
self.assertEqual(expected, self.linter.lint[0])
def test_option_empty_default_value_int(self):
# An empty default value is treated as INFO for ints
self.write_config("""
options:
foo:
type: int
default:
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'I: config.yaml: option foo has no default value')
self.assertEqual(expected, self.linter.lint[0])
def test_option_empty_default_value_float(self):
# An empty default value is treated as INFO for floats
self.write_config("""
options:
foo:
type: float
default:
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'I: config.yaml: option foo has no default value')
self.assertEqual(expected, self.linter.lint[0])
def test_option_empty_default_value_boolean(self):
# An empty default value is treated as WARN for booleans
self.write_config("""
options:
foo:
type: boolean
default:
description: blah
""")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
'W: config.yaml: option foo has no default value')
self.assertEqual(expected, self.linter.lint[0])
def test_yaml_with_python_objects(self):
"""Python objects can't be loaded."""
# Try to load the YAML representation of the int() function.
self.write_config("!!python/name:__builtin__.int ''\n")
self.linter.check_config_file(self.charm_dir)
self.assertEqual(1, len(self.linter.lint))
expected = (
"E: Cannot parse config.yaml: could not determine a constructor "
"for the tag 'tag:yaml.org,2002:python/name:__builtin__.int'")
self.assertTrue(self.linter.lint[0].startswith(expected))
def test_valid_layer_yaml(self):
with open(join(self.charm_dir, 'metadata.yaml'), 'w') as f:
f.write("{}")
with open(join(self.charm_dir, 'layer.yaml'), 'w') as f:
f.write("valid: {}")
with patch.object(Charm, 'is_charm'):
charm = Charm(self.charm_dir, self.linter)
charm.proof()
assert not any(msg.startswith('W: cannot parse {}/layer.yaml: '
''.format(self.charm_dir))
for msg in self.linter.lint)
def test_invalid_layer_yaml(self):
with open(join(self.charm_dir, 'metadata.yaml'), 'w') as f:
f.write("{}")
with open(join(self.charm_dir, 'layer.yaml'), 'w') as f:
f.write("invalid: {")
Charm(self.charm_dir, self.linter).proof()
assert any(msg.startswith('W: cannot parse {}/layer.yaml: '
''.format(self.charm_dir))
for msg in self.linter.lint)
def test_load_proof_extensions(self):
mocks = {
'validate_storage': None,
'validate_devices': None,
'validate_resources': None,
'validate_payloads': None,
}
for validator in mocks.keys():
patcher = patch('charmtools.charms.{}'.format(validator))
mocks[validator] = patcher.start()
self.addCleanup(patcher.stop)
with open(join(self.charm_dir, 'metadata.yaml'), 'w') as f:
f.write("{}")
with open(join(self.charm_dir, 'layer.yaml'), 'w') as f:
f.write(dedent("""
proof:
storage:
- name: ext
type: Boolean
devices:
- name: ext
type: Boolean
resources:
- name: ext
type: Boolean
payloads:
- name: ext
type: Boolean
"""))
charm = Charm(self.charm_dir, self.linter)
charm.proof()
for mock in mocks.values():
mock.assert_called_once_with({},
self.linter,
[{'name': 'ext',
'type': 'Boolean'}])
class CategoriesTagsValidationTest(TestCase):
def test_no_categories_or_tags(self):
"""Charm has neither categories nor tags."""
linter = Mock()
charm = {}
validate_categories_and_tags(charm, linter)
linter.warn.assert_called_once_with(
'Metadata missing required field "tags"')
def test_invalid_tags(self):
"""Charm has invalid tags field"""
warning = 'Metadata field "tags" must be a non-empty list'
linter = Mock()
validate_categories_and_tags({'tags': 'foo'}, linter)
linter.warn.assert_called_once_with(warning)
linter.reset_mock()
validate_categories_and_tags({'tags': []}, linter)
linter.warn.assert_called_once_with(warning)
def test_invalid_categories(self):
"""Charm has invalid categories field"""
warning = (
'Categories metadata must be a list of one or more of: '
'applications, app-servers, databases, file-servers, '
'cache-proxy, misc'
)
linter = Mock()
validate_categories_and_tags({'categories': 'foo'}, linter)
linter.warn.assert_any_call(warning)
linter.reset_mock()
validate_categories_and_tags({'categories': []}, linter)
linter.warn.assert_any_call(warning)
def test_valid_categories(self):
"""Charm has valid categories, which should be changed to tags"""
info = (
'Categories are being deprecated in favor of tags. '
'Please rename the "categories" field to "tags".'
)
linter = Mock()
validate_categories_and_tags({'categories': ['misc']}, linter)
linter.warn.assert_called_once_with(info)
self.assertFalse(linter.info.called)
self.assertFalse(linter.err.called)
class DisplayNameValidationTest(TestCase):
def test_educates_display_name(self):
"""Charm does not have a display_name."""
linter = Mock()
charm = {
'name': 'peanutbutter'
}
validate_display_name(charm, linter)
linter.info.assert_called_once_with(
'`display-name` not provided, add for custom naming in the UI')
def test_allows_display_name(self):
"""Charm has a display_name."""
# These names are copied from the juju/names package tests.
# https://github.com/juju/names/blob/master/charm_test.go#L42
valid_names = [
'ABC',
'My Awesome Charm',
'my-charm-name',
'1-abc-2',
'underscores_allowed',
]
for name in valid_names:
linter = Mock()
charm = {
'display-name': name
}
validate_display_name(charm, linter)
linter.info.assert_not_called()
linter.err.assert_not_called()
linter.warn.assert_not_called()
def test_display_name_alphanumeric_only(self):
"""Charm had invalid display_name."""
# These names are copied from the juju/names package tests.
# https://github.com/juju/names/blob/master/charm_test.go#L42
invalid_names = [
' bad name',
'big space',
'bigger space',
'tabs not allowed',
'no\nnewlines',
'no\r\nnewlines',
]
for name in invalid_names:
linter = Mock()
charm = {
'display-name': name
}
validate_display_name(charm, linter)
linter.err.assert_called_once_with(
'display-name: not in valid format. Only letters, numbers, '
'dashes, and hyphens are permitted.'
)
class MaintainerValidationTest(TestCase):
def test_two_maintainer_fields(self):
"""Charm has maintainer AND maintainers."""
linter = Mock()
charm = {
'maintainer': 'Tester <tester@example.com>',
'maintainers': ['Tester <tester@example.com>'],
}
validate_maintainer(charm, linter)
linter.err.assert_called_once_with(
'Charm must not have both maintainer and maintainers fields')
def test_no_maintainer_fields(self):
"""Charm has neither maintainer nor maintainers field."""
linter = Mock()
charm = {}
validate_maintainer(charm, linter)
linter.err.assert_called_once_with(
'Charm must have either a maintainer or maintainers field')
def test_maintainers_not_list(self):
"""Error if maintainers field is NOT a list."""
linter = Mock()
charm = {
'maintainers': 'Tester <tester@example.com>',
}
validate_maintainer(charm, linter)
linter.err.assert_called_once_with(
'Maintainers field must be a list')
def test_maintainer_list(self):
"""Error if maintainer field IS a list."""
linter = Mock()
charm = {
'maintainer': ['Tester <tester@example.com>'],
}
validate_maintainer(charm, linter)
linter.err.assert_called_once_with(
'Maintainer field must not be a list')
def test_maintainer_bad_format(self):
"""Warn if format of maintainer string not RFC2822 compliant."""
linter = Mock()
charm = {
'maintainer': 'Tester tester@example.com',
}
validate_maintainer(charm, linter)
linter.warn.assert_called_once_with(
'Maintainer format should be "Name <Email>", not '
'"Tester tester@example.com"')
self.assertFalse(linter.err.called)
def test_maintainers_bad_format(self):
"""Warn if format of a maintainers string not RFC2822 compliant."""
linter = Mock()
charm = {
'maintainers': ['Tester tester@example.com'],
}
validate_maintainer(charm, linter)
linter.warn.assert_called_once_with(
'Maintainer format should be "Name <Email>", not '
'"Tester tester@example.com"')
self.assertFalse(linter.err.called)
def test_good_maintainer(self):
"""Maintainer field happy path."""
linter = Mock()
charm = {
'maintainer': 'Tester <tester@example.com>',
}
validate_maintainer(charm, linter)
self.assertFalse(linter.err.called)
self.assertFalse(linter.warn.called)
def test_good_maintainers(self):
"""Maintainers field happy path."""
linter = Mock()
charm = {
'maintainers': [
'Tester <tester@example.com>',
'Tester Joe H. <tester@example.com>',
]
}
validate_maintainer(charm, linter)
self.assertFalse(linter.err.called)
self.assertFalse(linter.warn.called, "linter.warn was called, sadly")
class DevicesValidationTest(TestCase):
def test_minimal_devices_config(self):
"""Charm has the minimum allowed devices configuration."""
linter = Mock()
charm = {
'devices': {
'bitcoinminer': {
'type': 'nvidia.com/gpu',
'count': 1
}
}
}
validate_devices(charm, linter)
self.assertFalse(linter.err.called)
def test_devices_with_invalid_config(self):
"""Charm has empty devices configuration."""
linter = Mock()
charm = {
'devices': 'invalid devices config'
}
validate_devices(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('devices: must be a dictionary of device definitions'),
], any_order=True)
def test_devices_with_invalid_values(self):
"""Charm has devices with invalid values."""
linter = Mock()
charm = {
'devices': {
'bitcoinminer': {
'type': 'nvidia.com/gpu',
'count': 'non number'
},
'bitcoinminer1': {
'count': 1
}
}
}
validate_devices(charm, linter)
self.assertEqual(linter.err.call_count, 2)
linter.err.assert_has_calls([
call('devices.bitcoinminer.count: "non number" is not a number'),
call('devices.bitcoinminer1.type: Required'),
], any_order=True)
def test_devices_unknown_keys(self):
"""Charm has devices with illegal keys."""
linter = Mock()
charm = {
'devices': {
'bitcoinminer': {
'type': 'nvidia.com/gpu',
'count': 1,
'unknown': True
}
}
}
validate_devices(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('devices.bitcoinminer: Unrecognized keys in mapping: '
'"{\'unknown\': True}"'),
], any_order=True)
def test_devices_proof_extensions(self):
"""Charm has devices with proof extensions."""
linter = Mock()
charm = {
'devices': {
'bitcoinminer': {
'type': 'nvidia.com/gpu',
'count': 1,
'unknown': True
}
}
}
extensions = [
{
'name': 'unknown',
'type': 'Boolean',
}
]
validate_devices(charm, linter, extensions)
self.assertEqual(linter.err.call_args_list, [])
class StorageValidationTest(TestCase):
def test_minimal_storage_config(self):
"""Charm has the minimum allowed storage configuration."""
linter = Mock()
charm = {
'storage': {
'data': {
'type': 'filesystem',
}
}
}
validate_storage(charm, linter)
self.assertFalse(linter.err.called)
def test_complete_storage_config(self):
"""Charm has a storage configuration using all options."""
linter = Mock()
charm = {
'storage': {
'data': {
'type': 'filesystem',
'description': 'my storage',
'shared': False,
'read-only': 'true',
'minimum-size': '10G',
'location': '/srv/data',
},
'disks': {
'type': 'block',
'multiple': {
'range': '10-'
}
}
}
}
validate_storage(charm, linter)
self.assertFalse(linter.err.called)
def test_storage_without_defs(self):
"""Charm has storage key but no storage definitions."""
linter = Mock()
charm = {
'storage': {}
}
validate_storage(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('storage: must be a dictionary of storage definitions'),
], any_order=True)
def test_storage_invalid_values(self):
"""Charm has storage with invalid values."""
linter = Mock()
charm = {
'storage': {
'data': {
'type': 'unknown',
'shared': 'maybe',
'read-only': 'no',
'minimum-size': '10k',
},
'disks': {
'type': 'block',
'multiple': {
'range': '10+'
}
}
}
}
validate_storage(charm, linter)
self.assertEqual(linter.err.call_count, 5)
linter.err.assert_has_calls([
call('storage.data.type: "unknown" is not one of '
'filesystem, block'),
call('storage.data.shared: "maybe" is not one of true, false'),
call('storage.data.read-only: "no" is not one of true, false'),
call('storage.data.minimum-size: must be a number followed by '
'an optional M/G/T/P, e.g. 100M'),
call('storage.disks.multiple.range: supported formats are: '
'm (a fixed number), m-n (an explicit range), and '
'm- (a minimum number)'),
], any_order=True)
def test_storage_unknown_keys(self):
"""Charm has storage with illegal keys."""
linter = Mock()
charm = {
'storage': {
'data': {
'type': 'filesystem',
'unknown': 'invalid key',
},
}
}
validate_storage(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('storage.data: Unrecognized keys in mapping: '
'"{\'unknown\': \'invalid key\'}"'),
], any_order=True)
def test_storage_proof_extensions(self):
"""Charm has storage with proof extensions."""
linter = Mock()
charm = {
'storage': {
'data': {
'type': 'filesystem',
'unknown': 'invalid key',
},
}
}
extensions = [
{
'name': 'unknown',
'type': 'String',
}
]
validate_storage(charm, linter, extensions)
self.assertEqual(linter.err.call_args_list, [])
class DeploymentValidationTest(TestCase):
def test_deployment(self):
"""Charm has valid deployment."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 'omit',
'daemonset': True,
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertFalse(linter.err.called)
def test_invalid_deployment(self):
"""Charm has invalid deployment."""
linter = Mock()
charm = {
'deployment': [],
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('deployment: must be a dict of config'),
], any_order=True)
def test_deployment_unsupported_field(self):
"""Charm has the invalid deployment field."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 'omit',
'daemonset': True,
'min-version': "1.15.0",
'unknow-field': 'xxx',
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('deployment.deployment: Unrecognized keys in mapping: "{\'unknow-field\': \'xxx\'}"'),
], any_order=True)
def test_deployment_invalid_type(self):
"""Charm has the invalid deployment type."""
linter = Mock()
charm = {
'deployment': {
'type': True,
'service': 'omit',
'daemonset': True,
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call("deployment.deployment.type: True is not a string: {'type': ''}"),
], any_order=True)
def test_deployment_unsupported_type(self):
"""Charm has the unsupported deployment type."""
linter = Mock()
charm = {
'deployment': {
'type': 'foo',
'service': 'omit',
'daemonset': True,
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('deployment.deployment.type: "foo" is not one of stateless, stateful'),
], any_order=True)
def test_deployment_invalid_service(self):
"""Charm has the invalid deployment service."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 1,
'daemonset': True,
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call("deployment.deployment.service: 1 is not a string: {'service': ''}"),
], any_order=True)
def test_deployment_unsupported_service(self):
"""Charm has the unsupported deployment service."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 'foo',
'daemonset': True,
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('deployment.deployment.service: "foo" is not one of loadbalancer, cluster, omit'),
], any_order=True)
def test_deployment_invalid_daemonset(self):
"""Charm has the invalid deployment daemonset."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 'omit',
'daemonset': 'xx',
'min-version': "1.15.0",
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('deployment.deployment.daemonset: "xx" is not one of true, false'),
], any_order=True)
def test_deployment_invalid_min_version(self):
"""Charm has the invalid deployment min-version."""
linter = Mock()
charm = {
'deployment': {
'type': 'stateful',
'service': 'omit',
'daemonset': True,
'min-version': 1.15,
}
}
validate_deployment(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call("deployment.deployment.min-version: 1.15 is not a string: {'min-version': ''}"),
], any_order=True)
class ResourcesValidationTest(TestCase):
def test_minimal_resources_config(self):
"""Charm has the minimum allowed resources configuration."""
linter = Mock()
charm = {
'resources': {
'test': {
'type': 'file',
'filename': 'file.tgz',
}
}
}
validate_resources(charm, linter)
self.assertFalse(linter.err.called)
def test_resources_without_defs(self):
"""Charm has resources key but no definitions."""
linter = Mock()
charm = {
'resources': {}
}
validate_resources(charm, linter)
self.assertEqual(linter.err.call_count, 1)
linter.err.assert_has_calls([
call('resources: must be a dictionary of resource definitions'),
], any_order=True)
def test_resources_invalid_values(self):
"""Charm has resources with invalid values."""
linter = Mock()
charm = {
'resources': {
'buzz': {
'type': 'snap',