-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathtest_install-dynamic-plugins.py
More file actions
3065 lines (2430 loc) · 128 KB
/
test_install-dynamic-plugins.py
File metadata and controls
3065 lines (2430 loc) · 128 KB
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
#
# Copyright (c) 2023 Red Hat, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Unit tests for install-dynamic-plugins.py
This test suite covers:
- NPMPackageMerger.parse_plugin_key() - Version stripping from NPM packages
- OciPackageMerger.parse_plugin_key() - Parsing OCI package formats
- NPMPackageMerger.merge_plugin() - Plugin config merging and override logic
- OciPackageMerger.merge_plugin() - OCI plugin merging with version inheritance
- extract_catalog_index() - Extracting plugin catalog index from OCI images
Installation:
To install test dependencies:
$ pip install -r ../python/requirements-dev.txt
Running tests:
Run all tests:
$ pytest test_install-dynamic-plugins.py -v
Run specific test class:
$ pytest test_install-dynamic-plugins.py::TestNPMPackageMergerParsePluginKey -v
Run with coverage:
$ pytest test_install-dynamic-plugins.py --cov -v
"""
import pytest
import sys
import os
import importlib.util
import json
import hashlib
import base64
# Add the current directory to path to import the module
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import from file with hyphens in name using importlib
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'install-dynamic-plugins.py')
spec = importlib.util.spec_from_file_location("install_dynamic_plugins", script_path)
install_dynamic_plugins = importlib.util.module_from_spec(spec)
spec.loader.exec_module(install_dynamic_plugins)
# Import the classes and exception from the loaded module
NPMPackageMerger = install_dynamic_plugins.NPMPackageMerger
OciPackageMerger = install_dynamic_plugins.OciPackageMerger
InstallException = install_dynamic_plugins.InstallException
# Test helper functions
import tarfile # noqa: E402
def create_test_tarball(tarball_path, mode='w:gz'): # noqa: S202
"""
Helper function to create test tarballs.
Note: This is safe for test code as we're creating controlled test data,
not opening untrusted archives. The noqa: S202 suppresses security warnings
about tarfile usage which are not applicable to test fixtures.
"""
return tarfile.open(tarball_path, mode) # NOSONAR
def create_mock_skopeo_copy(manifest_path, layer_tarball, mock_result):
"""
Helper function to create mock subprocess.run for skopeo copy operations.
Args:
manifest_path: Path to manifest.json file to copy
layer_tarball: Path to layer tarball file to copy
mock_result: Mock result object to return
Returns:
A function that can be used as side_effect for subprocess.run mock
"""
def mock_subprocess_run(cmd, **kwargs):
if 'copy' in cmd:
dest_arg = [arg for arg in cmd if arg.startswith('dir:')]
if dest_arg:
dest_dir = dest_arg[0].replace('dir:', '')
os.makedirs(dest_dir, exist_ok=True)
import shutil as sh
sh.copy(str(manifest_path), dest_dir)
sh.copy(str(layer_tarball), dest_dir)
return mock_result
return mock_subprocess_run
class TestNPMPackageMergerParsePluginKey:
"""Test cases for NPMPackageMerger.parse_plugin_key() method."""
@pytest.fixture
def npm_merger(self):
"""Create an NPMPackageMerger instance for testing."""
plugin = {'package': 'test-package'}
return NPMPackageMerger(plugin, 'test-file.yaml', {})
@pytest.mark.parametrize("input_package,expected_output", [
# Standard NPM packages with version stripping
('@npmcli/arborist@latest', '@npmcli/arborist'),
('@backstage/plugin-catalog@1.0.0', '@backstage/plugin-catalog'),
('semver@7.2.2', 'semver'),
('package-name@^1.0.0', 'package-name'),
('package-name@~2.1.0', 'package-name'),
('package-name@1.x', 'package-name'),
# Packages without version (unchanged)
('package-name', 'package-name'),
('@scope/package', '@scope/package'),
# NPM aliases with version stripping
('semver:@npm:semver@7.2.2', 'semver:@npm:semver'),
('my-alias@npm:@npmcli/semver-with-patch', 'my-alias@npm:@npmcli/semver-with-patch'),
('semver:@npm:@npmcli/semver-with-patch@1.0.0', 'semver:@npm:@npmcli/semver-with-patch'),
('alias@npm:package@1.0.0', 'alias@npm:package'),
('alias@npm:@scope/package@2.0.0', 'alias@npm:@scope/package'),
# Git URLs with ref stripping
('npm/cli#c12ea07', 'npm/cli'),
('user/repo#main', 'user/repo'),
('github:user/repo#ref', 'github:user/repo'),
('git+https://github.com/user/repo.git#branch', 'git+https://github.com/user/repo.git'),
('git+https://github.com/user/repo#branch', 'git+https://github.com/user/repo'),
('git@github.com:user/repo.git#ref', 'git@github.com:user/repo.git'),
('git+ssh://git@github.com/user/repo.git#tag', 'git+ssh://git@github.com/user/repo.git'),
('git://github.com/user/repo#commit', 'git://github.com/user/repo'),
('https://github.com/user/repo.git#v1.0.0', 'https://github.com/user/repo.git'),
# Local paths (unchanged)
('./my-local-plugin', './my-local-plugin'),
('./path/to/plugin', './path/to/plugin'),
# Tarballs (unchanged)
('package.tgz', 'package.tgz'),
('my-package-1.0.0.tgz', 'my-package-1.0.0.tgz'),
('https://example.com/package.tgz', 'https://example.com/package.tgz'),
])
def test_parse_plugin_key_success_cases(self, npm_merger, input_package, expected_output):
"""Test that parse_plugin_key correctly strips versions and refs from various package formats."""
result = npm_merger.parse_plugin_key(input_package)
assert result == expected_output, f"Expected {expected_output}, got {result}"
class TestOciPackageMergerParsePluginKey:
"""Test cases for OciPackageMerger.parse_plugin_key() method."""
@pytest.fixture
def oci_merger(self):
"""Create an OciPackageMerger instance for testing."""
plugin = {'package': 'oci://example.com:v1.0!plugin'}
return OciPackageMerger(plugin, 'test-file.yaml', {})
@pytest.mark.parametrize("input_package,expected_key,expected_version,expected_inherit", [
# Tag-based packages with explicit path
(
'oci://quay.io/user/plugin:v1.0!plugin-name',
'oci://quay.io/user/plugin:!plugin-name',
'v1.0',
False
),
(
'oci://registry.io/plugin:latest!path/to/plugin',
'oci://registry.io/plugin:!path/to/plugin',
'latest',
False
),
(
'oci://ghcr.io/org/plugin:1.2.3!my-plugin',
'oci://ghcr.io/org/plugin:!my-plugin',
'1.2.3',
False
),
(
'oci://docker.io/library/plugin:v2.0.0!plugin',
'oci://docker.io/library/plugin:!plugin',
'v2.0.0',
False
),
# Digest-based packages with different algorithms
(
'oci://quay.io/user/plugin@sha256:abc123def456!plugin',
'oci://quay.io/user/plugin:!plugin',
'sha256:abc123def456',
False
),
(
'oci://registry.io/plugin@sha512:fedcba987654!plugin',
'oci://registry.io/plugin:!plugin',
'sha512:fedcba987654',
False
),
(
'oci://example.com/plugin@blake3:1234567890abcdef!my-plugin',
'oci://example.com/plugin:!my-plugin',
'blake3:1234567890abcdef',
False
),
# Inherit version pattern
(
'oci://quay.io/user/plugin:{{inherit}}!plugin',
'oci://quay.io/user/plugin:!plugin',
'{{inherit}}',
True
),
(
'oci://registry.io/plugin:{{inherit}}!path/to/plugin',
'oci://registry.io/plugin:!path/to/plugin',
'{{inherit}}',
True
),
# Host:port registry format
(
'oci://registry.localhost:5000/rhdh-plugins/plugin:v1.0!plugin-name',
'oci://registry.localhost:5000/rhdh-plugins/plugin:!plugin-name',
'v1.0',
False
),
(
'oci://registry.localhost:5000/rhdh-plugins/rhdh-plugin-export-overlays/backstage-community-plugin-scaffolder-backend-module-quay:bs_1.45.3__2.14.0!my-plugin',
'oci://registry.localhost:5000/rhdh-plugins/rhdh-plugin-export-overlays/backstage-community-plugin-scaffolder-backend-module-quay:!my-plugin',
'bs_1.45.3__2.14.0',
False
),
(
'oci://registry.localhost:5000/path@sha256:abc123!plugin',
'oci://registry.localhost:5000/path:!plugin',
'sha256:abc123',
False
),
(
'oci://registry.localhost:5000/path:{{inherit}}!plugin',
'oci://registry.localhost:5000/path:!plugin',
'{{inherit}}',
True
),
(
'oci://10.0.0.1:5000/repo/plugin:tag!plugin', # NOSONAR
'oci://10.0.0.1:5000/repo/plugin:!plugin', # NOSONAR
'tag',
False
),
])
def test_parse_plugin_key_success_cases(
self, oci_merger, input_package, expected_key, expected_version, expected_inherit
):
"""Test that parse_plugin_key correctly parses valid OCI package formats."""
plugin_key, version, inherit_version, _ = oci_merger.parse_plugin_key(input_package)
assert plugin_key == expected_key, f"Expected key {expected_key}, got {plugin_key}"
assert version == expected_version, f"Expected version {expected_version}, got {version}"
assert inherit_version == expected_inherit, f"Expected inherit {expected_inherit}, got {inherit_version}"
@pytest.mark.parametrize("invalid_package,error_substring", [
# Missing tag/digest
('oci://registry.io/plugin!path', 'not in the expected format'),
('oci://registry.io/plugin', 'not in the expected format'),
('oci://host:1000/path', 'not in the expected format'),
# Invalid format - no tag or digest before !
('oci://registry.io!path', 'not in the expected format'),
('oci://host:1000!path', 'not in the expected format'),
# Invalid digest algorithm (md5 not in RECOGNIZED_ALGORITHMS)
('oci://registry.io/plugin@md5:abc123!plugin', 'not in the expected format'),
('oci://host:1000/path@md5:abc123!plugin', 'not in the expected format'),
# Invalid format - multiple @ symbols
('oci://registry.io/plugin@@sha256:abc!plugin', 'not in the expected format'),
('oci://host:1000/path@@sha256:abc!plugin', 'not in the expected format'),
# Invalid format - multiple : symbols in tag
('oci://registry.io/plugin:v1:v2!plugin', 'not in the expected format'),
('oci://host:1000/path:v1:v2!plugin', 'not in the expected format'),
# Empty tag
('oci://registry.io/plugin:!plugin', 'not in the expected format'),
('oci://registry.io/plugin:', 'not in the expected format'),
('oci://host:1000/path:!plugin', 'not in the expected format'),
('oci://host:1000/path:', 'not in the expected format'),
# Empty path after !
('oci://registry.io/plugin:v1.0!', 'not in the expected format'),
('oci://host:1000/path:v1.0!', 'not in the expected format'),
# No oci:// prefix (but this should fail the regex)
('registry.io/plugin:v1.0!plugin', 'not in the expected format'),
('registry.io/plugin:v1.0', 'not in the expected format'),
('host:1000/path:v1.0!plugin', 'not in the expected format'),
('host:1000/path:v1.0', 'not in the expected format'),
# Non-numeric port
('oci://host:abc/path:tag!plugin', 'not in the expected format'),
('oci://host:abc/path', 'not in the expected format'),
('oci://10.0.0.1:abc/path', 'not in the expected format'), # NOSONAR
('oci://10.0.0.1:abc/path:tag!plugin', 'not in the expected format'), # NOSONAR
])
def test_parse_plugin_key_error_cases(self, oci_merger, invalid_package, error_substring):
"""Test that parse_plugin_key raises InstallException for invalid OCI package formats."""
with pytest.raises(InstallException) as exc_info:
oci_merger.parse_plugin_key(invalid_package)
assert error_substring in str(exc_info.value), \
f"Expected error message to contain '{error_substring}', got: {str(exc_info.value)}"
def test_parse_plugin_key_complex_digest(self, oci_merger):
"""Test parsing OCI package with complex digest value."""
# Note: The pattern allows any value after @ including special strings like {{inherit}}
# though this would be semantically incorrect for digest format
input_pkg = 'oci://registry.io/plugin@sha256:abc123def456789!plugin'
plugin_key, version, inherit, resolved_path = oci_merger.parse_plugin_key(input_pkg)
assert plugin_key == 'oci://registry.io/plugin:!plugin'
assert version == 'sha256:abc123def456789'
assert inherit is False
assert resolved_path == 'plugin'
def test_parse_plugin_key_strips_version_from_key(self, oci_merger):
"""Test that the plugin key does not contain version information."""
input_pkg = 'oci://quay.io/user/plugin:v1.0.0!my-plugin'
plugin_key, version, _, resolved_path = oci_merger.parse_plugin_key(input_pkg)
# The key should not contain the version
assert ':v1.0.0' not in plugin_key
assert plugin_key == 'oci://quay.io/user/plugin:!my-plugin'
# But the version should be returned separately
assert version == 'v1.0.0'
assert resolved_path == 'my-plugin'
def test_parse_plugin_key_with_nested_path(self, oci_merger):
"""Test parsing OCI package with nested path after !."""
input_pkg = 'oci://registry.io/plugin:v1.0!path/to/nested/plugin'
plugin_key, version, inherit, resolved_path = oci_merger.parse_plugin_key(input_pkg)
assert plugin_key == 'oci://registry.io/plugin:!path/to/nested/plugin'
assert version == 'v1.0'
assert inherit is False
assert resolved_path == 'path/to/nested/plugin'
def test_parse_plugin_key_auto_detect_single_plugin(self, oci_merger, mocker):
"""Test auto-detection with single plugin in OCI image."""
# Mock get_oci_plugin_paths to return single plugin
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['auto-detected-plugin']
input_pkg = 'oci://registry.io/plugin:v1.0'
plugin_key, version, inherit, resolved_path = oci_merger.parse_plugin_key(input_pkg)
# Should resolve to the auto-detected plugin name
assert plugin_key == 'oci://registry.io/plugin:!auto-detected-plugin'
assert version == 'v1.0'
assert inherit is False
assert resolved_path == 'auto-detected-plugin'
# Package should NOT be updated here (that happens in merge_plugin)
# The fixture has a different initial package which should remain unchanged
assert oci_merger.plugin['package'] == 'oci://example.com:v1.0!plugin'
# Verify get_oci_plugin_paths was called
mock_get_paths.assert_called_once_with('oci://registry.io/plugin:v1.0')
def test_parse_plugin_key_auto_detect_with_digest(self, oci_merger, mocker):
"""Test auto-detection with digest-based reference."""
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['my-plugin']
input_pkg = 'oci://registry.io/plugin@sha256:abc123'
plugin_key, version, inherit, resolved_path = oci_merger.parse_plugin_key(input_pkg)
assert plugin_key == 'oci://registry.io/plugin:!my-plugin'
assert version == 'sha256:abc123'
assert inherit is False
assert resolved_path == 'my-plugin'
# Package should NOT be updated here (that happens in merge_plugin)
# The fixture has a different initial package which should remain unchanged
assert oci_merger.plugin['package'] == 'oci://example.com:v1.0!plugin'
def test_parse_plugin_key_auto_detect_no_plugins_error(self, oci_merger, mocker):
"""Test error when no plugins found in OCI image."""
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = []
with pytest.raises(InstallException) as exc_info:
oci_merger.parse_plugin_key('oci://registry.io/plugin:v1.0')
assert 'No plugins found' in str(exc_info.value)
def test_parse_plugin_key_auto_detect_multiple_plugins_error(self, oci_merger, mocker):
"""Test error when multiple plugins found without explicit path."""
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['plugin-one', 'plugin-two', 'plugin-three']
with pytest.raises(InstallException) as exc_info:
oci_merger.parse_plugin_key('oci://registry.io/plugin:v1.0')
error_msg = str(exc_info.value)
assert 'Multiple plugins found' in error_msg
assert 'plugin-one' in error_msg
assert 'plugin-two' in error_msg
assert 'plugin-three' in error_msg
def test_parse_plugin_key_inherit_without_path_returns_registry(self, oci_merger):
"""Test that {{inherit}} without explicit path returns registry as key with None for path."""
plugin_key, version, inherit_version, resolved_path = oci_merger.parse_plugin_key('oci://registry.io/plugin:{{inherit}}')
# Should return registry as the temporary key
assert plugin_key == 'oci://registry.io/plugin'
assert version == '{{inherit}}'
assert inherit_version is True
assert resolved_path is None # Path will be resolved during merge_plugin()
class TestEdgeCases:
"""Test edge cases and boundary conditions."""
def test_npm_merger_empty_string(self):
"""Test NPM merger with empty package string."""
plugin = {'package': ''}
merger = NPMPackageMerger(plugin, 'test.yaml', {})
result = merger.parse_plugin_key('')
assert result == ''
def test_npm_merger_special_characters_in_package(self):
"""Test NPM packages with special characters."""
plugin = {'package': 'test'}
merger = NPMPackageMerger(plugin, 'test.yaml', {})
# Package name with underscores and hyphens
result = merger.parse_plugin_key('my_special-package@1.0.0')
assert result == 'my_special-package'
def test_oci_merger_long_digest(self):
"""Test OCI package with realistic long SHA256 digest."""
plugin = {'package': 'oci://example.com:v1!plugin'}
merger = OciPackageMerger(plugin, 'test.yaml', {})
long_digest = 'sha256:' + 'a' * 64
input_pkg = f'oci://quay.io/user/plugin@{long_digest}!plugin'
plugin_key, version, inherit, resolved_path = merger.parse_plugin_key(input_pkg)
assert plugin_key == 'oci://quay.io/user/plugin:!plugin'
assert version == long_digest
assert inherit is False
assert resolved_path == 'plugin'
class TestNPMPackageMergerMergePlugin:
"""Test cases for NPMPackageMerger.merge_plugin() method."""
def test_add_new_plugin_level_0(self):
"""Test adding a new plugin at level 0."""
all_plugins = {}
plugin = {'package': 'test-package@1.0.0', 'disabled': False}
merger = NPMPackageMerger(plugin, 'test-file.yaml', all_plugins)
merger.merge_plugin(level=0)
# Check plugin was added
assert 'test-package' in all_plugins
assert all_plugins['test-package']['package'] == 'test-package@1.0.0'
assert all_plugins['test-package']['disabled'] is False
assert all_plugins['test-package']['last_modified_level'] == 0
def test_override_plugin_level_0_to_1(self):
"""Test overriding a plugin from level 0 to level 1."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'test-package@1.0.0', 'disabled': False}
merger1 = NPMPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1
plugin2 = {'package': 'test-package@2.0.0', 'disabled': True}
merger2 = NPMPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check override succeeded
assert all_plugins['test-package']['disabled'] is True
assert all_plugins['test-package']['last_modified_level'] == 1
# Package field should be overridden
assert all_plugins['test-package']['package'] == 'test-package@2.0.0'
def test_override_multiple_config_fields(self):
"""Test overriding multiple plugin config fields."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {
'package': '@scope/plugin@1.0.0',
'disabled': False,
'pullPolicy': 'IfNotPresent',
'pluginConfig': {'key1': 'value1'}
}
merger1 = NPMPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1
plugin2 = {
'package': '@scope/plugin@2.0.0',
'disabled': True,
'pullPolicy': 'Always',
'pluginConfig': {'key2': 'value2'},
'integrity': 'sha256-abc123'
}
merger2 = NPMPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check all fields were updated except package
assert all_plugins['@scope/plugin']['disabled'] is True
assert all_plugins['@scope/plugin']['pullPolicy'] == 'Always'
assert all_plugins['@scope/plugin']['pluginConfig'] == {'key2': 'value2'}
assert all_plugins['@scope/plugin']['integrity'] == 'sha256-abc123'
# Package field not overridden
assert all_plugins['@scope/plugin']['package'] == '@scope/plugin@2.0.0'
def test_duplicate_plugin_same_level_0_raises_error(self):
"""Test that duplicate plugin at same level 0 raises InstallException."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'duplicate-package@1.0.0'}
merger1 = NPMPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Try to add same plugin again at level 0
plugin2 = {'package': 'duplicate-package@2.0.0'}
merger2 = NPMPackageMerger(plugin2, 'included-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger2.merge_plugin(level=0)
assert 'Duplicate plugin configuration' in str(exc_info.value)
assert 'duplicate-package@2.0.0' in str(exc_info.value)
def test_duplicate_plugin_same_level_1_raises_error(self):
"""Test that duplicate plugin at same level 1 raises InstallException."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'test-package@1.0.0'}
merger1 = NPMPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1
plugin2 = {'package': 'test-package@2.0.0'}
merger2 = NPMPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Try to add same plugin again at level 1
plugin3 = {'package': 'test-package@3.0.0'}
merger3 = NPMPackageMerger(plugin3, 'main-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger3.merge_plugin(level=1)
assert 'Duplicate plugin configuration' in str(exc_info.value)
def test_invalid_package_field_type_raises_error(self):
"""Test that non-string package field raises InstallException."""
all_plugins = {}
plugin = {'package': 123}
merger = NPMPackageMerger(plugin, 'test-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger.merge_plugin(level=0)
assert 'must be a string' in str(exc_info.value)
def test_version_stripping_in_plugin_key(self):
"""Test that version is stripped from plugin key."""
all_plugins = {}
# Add plugin with version
plugin1 = {'package': 'my-plugin@1.0.0'}
merger1 = NPMPackageMerger(plugin1, 'test-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override with different version
plugin2 = {'package': 'my-plugin@2.0.0', 'disabled': True}
merger2 = NPMPackageMerger(plugin2, 'test-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Both should map to same key
assert 'my-plugin' in all_plugins
assert all_plugins['my-plugin']['disabled'] is True
class TestOciPackageMergerMergePlugin:
"""Test cases for OciPackageMerger.merge_plugin() method."""
def test_add_new_plugin_with_tag(self):
"""Test adding a new OCI plugin with tag."""
all_plugins = {}
plugin = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
merger.merge_plugin(level=0)
plugin_key = 'oci://registry.io/plugin:!path'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v1.0!path'
assert all_plugins[plugin_key]['version'] == 'v1.0'
assert all_plugins[plugin_key]['last_modified_level'] == 0
def test_add_new_plugin_with_digest(self):
"""Test adding a new OCI plugin with digest."""
all_plugins = {}
plugin = {'package': 'oci://registry.io/plugin@sha256:abc123!path'}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
merger.merge_plugin(level=0)
plugin_key = 'oci://registry.io/plugin:!path'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['version'] == 'sha256:abc123'
def test_merge_plugin_auto_detect_updates_package(self, mocker):
"""Test that merge_plugin updates package when path is auto-detected."""
# Mock get_oci_plugin_paths to return single plugin
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['detected-plugin']
all_plugins = {}
# Package without explicit path (will be auto-detected)
plugin = {'package': 'oci://registry.io/plugin:v1.0'}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
merger.merge_plugin(level=0)
# Verify the package was updated with the resolved path
plugin_key = 'oci://registry.io/plugin:!detected-plugin'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v1.0!detected-plugin'
assert all_plugins[plugin_key]['version'] == 'v1.0'
# Original plugin dict should also be updated
assert merger.plugin['package'] == 'oci://registry.io/plugin:v1.0!detected-plugin'
def test_merge_plugin_auto_detect_with_digest_updates_package(self, mocker):
"""Test that merge_plugin updates package with digest when path is auto-detected."""
# Mock get_oci_plugin_paths to return single plugin
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['my-plugin']
all_plugins = {}
# Package without explicit path (will be auto-detected)
plugin = {'package': 'oci://registry.io/plugin@sha256:abc123'}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
merger.merge_plugin(level=0)
# Verify the package was updated with the resolved path
plugin_key = 'oci://registry.io/plugin:!my-plugin'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin@sha256:abc123!my-plugin'
assert all_plugins[plugin_key]['version'] == 'sha256:abc123'
# Original plugin dict should also be updated
assert merger.plugin['package'] == 'oci://registry.io/plugin@sha256:abc123!my-plugin'
def test_override_plugin_version(self, capsys):
"""Test overriding OCI plugin version from level 0 to 1."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1 with new version
plugin2 = {'package': 'oci://registry.io/plugin:v2.0!path'}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check version was updated
plugin_key = 'oci://registry.io/plugin:!path'
assert all_plugins[plugin_key]['version'] == 'v2.0'
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v2.0!path'
assert all_plugins[plugin_key]['last_modified_level'] == 1
# Check that override message was printed
captured = capsys.readouterr()
assert 'Overriding version' in captured.out
assert 'v1.0' in captured.out
assert 'v2.0' in captured.out
def test_use_inherit_to_preserve_version(self):
"""Test using {{inherit}} to preserve existing version."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1 with {{inherit}}
plugin2 = {'package': 'oci://registry.io/plugin:{{inherit}}!path', 'disabled': True}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check version was preserved
plugin_key = 'oci://registry.io/plugin:!path'
assert all_plugins[plugin_key]['version'] == 'v1.0'
# Package field should NOT be updated when inheriting
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v1.0!path'
# But other config should be updated
assert all_plugins[plugin_key]['disabled'] is True
def test_override_config_with_version_inheritance(self):
"""Test overriding plugin config while preserving version with {{inherit}}."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {
'package': 'oci://registry.io/plugin:v1.0!path',
'pluginConfig': {'key1': 'value1'}
}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override config at level 1 with {{inherit}}
plugin2 = {
'package': 'oci://registry.io/plugin:{{inherit}}!path',
'pluginConfig': {'key2': 'value2'}
}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check version preserved and config updated
plugin_key = 'oci://registry.io/plugin:!path'
assert all_plugins[plugin_key]['version'] == 'v1.0'
assert all_plugins[plugin_key]['pluginConfig'] == {'key2': 'value2'}
def test_override_config_without_version_inheritance(self):
"""Test overriding both version and config."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {
'package': 'oci://registry.io/plugin:v1.0!path',
'pluginConfig': {'key1': 'value1'}
}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override both at level 1
plugin2 = {
'package': 'oci://registry.io/plugin:v2.0!path',
'pluginConfig': {'key2': 'value2'}
}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check both were updated
plugin_key = 'oci://registry.io/plugin:!path'
assert all_plugins[plugin_key]['version'] == 'v2.0'
assert all_plugins[plugin_key]['pluginConfig'] == {'key2': 'value2'}
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v2.0!path'
def test_override_from_tag_to_digest(self):
"""Test overriding from tag to digest."""
all_plugins = {}
# Add plugin with tag at level 0
plugin1 = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override with digest at level 1
plugin2 = {'package': 'oci://registry.io/plugin@sha256:abc123def456!path'}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check version updated to digest format
plugin_key = 'oci://registry.io/plugin:!path'
assert all_plugins[plugin_key]['version'] == 'sha256:abc123def456'
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin@sha256:abc123def456!path'
def test_new_plugin_with_inherit_raises_error(self):
"""Test that using {{inherit}} on a new plugin raises InstallException."""
all_plugins = {}
plugin = {'package': 'oci://registry.io/plugin:{{inherit}}!path'}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger.merge_plugin(level=0)
assert '{{inherit}}' in str(exc_info.value)
assert 'no resolved tag or digest' in str(exc_info.value)
def test_duplicate_oci_plugin_same_level_0_raises_error(self):
"""Test that duplicate OCI plugin at same level 0 raises InstallException."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Try to add same plugin again at level 0
plugin2 = {'package': 'oci://registry.io/plugin:v2.0!path'}
merger2 = OciPackageMerger(plugin2, 'included-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger2.merge_plugin(level=0)
assert 'Duplicate plugin configuration' in str(exc_info.value)
def test_duplicate_oci_plugin_same_level_1_raises_error(self):
"""Test that duplicate OCI plugin at same level 1 raises InstallException."""
all_plugins = {}
# Add plugin at level 0
plugin1 = {'package': 'oci://registry.io/plugin:v1.0!path'}
merger1 = OciPackageMerger(plugin1, 'included-file.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1
plugin2 = {'package': 'oci://registry.io/plugin:v2.0!path'}
merger2 = OciPackageMerger(plugin2, 'main-file.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Try to add same plugin again at level 1
plugin3 = {'package': 'oci://registry.io/plugin:v3.0!path'}
merger3 = OciPackageMerger(plugin3, 'main-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger3.merge_plugin(level=1)
assert 'Duplicate plugin configuration' in str(exc_info.value)
def test_invalid_package_field_type_raises_error(self):
"""Test that non-string package field raises InstallException."""
all_plugins = {}
plugin = {'package': ['not', 'a', 'string']}
merger = OciPackageMerger(plugin, 'test-file.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger.merge_plugin(level=0)
assert 'must be a string' in str(exc_info.value)
class TestOciInheritWithPathOmission:
"""Test cases for {{inherit}} with path omission feature."""
def test_inherit_version_and_path_from_single_base_plugin(self, capsys):
"""Test inheriting both version and path when exactly one base plugin exists."""
all_plugins = {}
# Add base plugin at level 0 with explicit version and path
plugin1 = {
'package': 'oci://registry.io/plugin:v1.0!my-plugin',
'disabled': False
}
merger1 = OciPackageMerger(plugin1, 'base.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Override at level 1 using {{inherit}} without path
plugin2 = {
'package': 'oci://registry.io/plugin:{{inherit}}',
'disabled': True
}
merger2 = OciPackageMerger(plugin2, 'main.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check that version and path were inherited
plugin_key = 'oci://registry.io/plugin:!my-plugin'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['version'] == 'v1.0'
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v1.0!my-plugin'
assert all_plugins[plugin_key]['disabled'] is True
# Check that inheritance message was printed
captured = capsys.readouterr()
assert 'Inheriting version `v1.0` and plugin path `my-plugin`' in captured.out
def test_inherit_version_and_path_with_digest(self, capsys):
"""Test inheriting version (digest) and path from base plugin."""
all_plugins = {}
# Add base plugin with digest
plugin1 = {'package': 'oci://registry.io/plugin@sha256:abc123!plugin-name'}
merger1 = OciPackageMerger(plugin1, 'base.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Inherit using {{inherit}} without path
plugin2 = {
'package': 'oci://registry.io/plugin:{{inherit}}',
'pluginConfig': {'custom': 'config'}
}
merger2 = OciPackageMerger(plugin2, 'main.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check inheritance
plugin_key = 'oci://registry.io/plugin:!plugin-name'
assert all_plugins[plugin_key]['version'] == 'sha256:abc123'
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin@sha256:abc123!plugin-name'
assert all_plugins[plugin_key]['pluginConfig'] == {'custom': 'config'}
def test_inherit_from_auto_detected_base_plugin(self, mocker, capsys):
"""Test inheriting from a base plugin that had its path auto-detected."""
# Mock get_oci_plugin_paths to return single plugin
mock_get_paths = mocker.patch.object(install_dynamic_plugins, 'get_oci_plugin_paths')
mock_get_paths.return_value = ['auto-detected-plugin']
all_plugins = {}
# Add base plugin without explicit path (will auto-detect)
plugin1 = {'package': 'oci://registry.io/plugin:v1.0'}
merger1 = OciPackageMerger(plugin1, 'base.yaml', all_plugins)
merger1.merge_plugin(level=0)
# Inherit both version AND the auto-detected path
plugin2 = {'package': 'oci://registry.io/plugin:{{inherit}}'}
merger2 = OciPackageMerger(plugin2, 'main.yaml', all_plugins)
merger2.merge_plugin(level=1)
# Check that auto-detected path was inherited
plugin_key = 'oci://registry.io/plugin:!auto-detected-plugin'
assert plugin_key in all_plugins
assert all_plugins[plugin_key]['version'] == 'v1.0'
assert all_plugins[plugin_key]['package'] == 'oci://registry.io/plugin:v1.0!auto-detected-plugin'
captured = capsys.readouterr()
assert 'Inheriting version `v1.0` and plugin path `auto-detected-plugin`' in captured.out
def test_inherit_without_path_no_base_plugin_error(self):
"""Test error when using {{inherit}} without path but no base plugin exists."""
all_plugins = {}
# Try to use {{inherit}} without any base plugin
plugin = {'package': 'oci://registry.io/plugin:{{inherit}}'}
merger = OciPackageMerger(plugin, 'main.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger.merge_plugin(level=0)
error_msg = str(exc_info.value)
assert '{{inherit}}' in error_msg
assert 'no existing plugin configuration found' in error_msg
assert 'oci://registry.io/plugin' in error_msg
def test_inherit_without_path_multiple_plugins_error(self):
"""Test error when using {{inherit}} without path with multiple base plugins from same image."""
all_plugins = {}
# Add two plugins from same image at level 0
plugin1 = {'package': 'oci://registry.io/bundle:v1.0!plugin-a'}
merger1 = OciPackageMerger(plugin1, 'base.yaml', all_plugins)
merger1.merge_plugin(level=0)
plugin2 = {'package': 'oci://registry.io/bundle:v1.0!plugin-b'}
merger2 = OciPackageMerger(plugin2, 'base.yaml', all_plugins)
merger2.merge_plugin(level=0)
# Try to use {{inherit}} without specifying which plugin
plugin3 = {'package': 'oci://registry.io/bundle:{{inherit}}'}
merger3 = OciPackageMerger(plugin3, 'main.yaml', all_plugins)
with pytest.raises(InstallException) as exc_info:
merger3.merge_plugin(level=1)
error_msg = str(exc_info.value)
assert '{{inherit}}' in error_msg
assert 'multiple plugins from this image are defined' in error_msg
assert 'oci://registry.io/bundle:v1.0!plugin-a' in error_msg
assert 'oci://registry.io/bundle:v1.0!plugin-b' in error_msg
assert '{{inherit}}!<plugin_path>' in error_msg
def test_inherit_without_path_works_with_explicit_path_too(self):
"""Test that {{inherit}} with explicit path still works alongside path omission."""
all_plugins = {}
# Add two plugins from same image
plugin1 = {'package': 'oci://registry.io/bundle:v1.0!plugin-a'}
merger1 = OciPackageMerger(plugin1, 'base.yaml', all_plugins)
merger1.merge_plugin(level=0)
plugin2 = {'package': 'oci://registry.io/bundle:v1.0!plugin-b'}
merger2 = OciPackageMerger(plugin2, 'base.yaml', all_plugins)
merger2.merge_plugin(level=0)
# Use {{inherit}} with explicit path for plugin-a