-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathtest_commands.py
More file actions
1347 lines (1156 loc) · 55 KB
/
test_commands.py
File metadata and controls
1347 lines (1156 loc) · 55 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
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://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.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.
import datetime
import json
import tempfile
import uuid
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from unittest import mock
from django.apps import apps
from django.contrib.auth import get_user_model
from django.core.management import CommandError
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.test import TestCase
from django.test import override_settings
from django.utils import timezone
import openpyxl
from scanpipe.management import commands
from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredPackage
from scanpipe.models import Project
from scanpipe.models import Run
from scanpipe.models import WebhookSubscription
from scanpipe.pipes import flag
from scanpipe.pipes import purldb
from scanpipe.tests import filter_warnings
from scanpipe.tests import make_package
from scanpipe.tests import make_project
from scanpipe.tests import make_resource_file
scanpipe_app = apps.get_app_config("scanpipe")
def task_success(run_pk):
run = Run.objects.get(pk=run_pk)
run.task_exitcode = 0
run.save()
def task_failure(run_pk):
run = Run.objects.get(pk=run_pk)
run.task_output = "Error log"
run.task_exitcode = 1
run.save()
def raise_interrupt(run_pk):
raise KeyboardInterrupt
class ScanPipeManagementCommandTest(TestCase):
data = Path(__file__).parent / "data"
pipeline_name = "analyze_docker_image"
pipeline_class = scanpipe_app.pipelines.get(pipeline_name)
def test_scanpipe_management_command_create_project_base(self):
out = StringIO()
expected = "Error: the following arguments are required: name"
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project")
call_command("create-project", "my_project", stdout=out)
self.assertIn("Project my_project created", out.getvalue())
self.assertTrue(Project.objects.get(name="my_project"))
expected = "Project with this Name already exists."
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project", "my_project")
def test_scanpipe_management_command_create_project_verbosity(self):
out = StringIO()
call_command("create-project", "my_project", verbosity=0, stdout=out)
self.assertEqual("", out.getvalue())
self.assertTrue(Project.objects.get(name="my_project"))
def test_scanpipe_management_command_create_project_labels(self):
out = StringIO()
options = ["--label", "label1", "--label", "label2"]
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
project = Project.objects.get(name="my_project")
self.assertEqual(["label1", "label2"], list(project.labels.names()))
def test_scanpipe_management_command_create_project_notes(self):
out = StringIO()
notes = "Some notes about my project"
options = ["--notes", notes]
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
project = Project.objects.get(name="my_project")
self.assertEqual(notes, project.notes)
def test_scanpipe_management_command_create_project_pipelines(self):
out = StringIO()
options = ["--pipeline", "non-existing"]
expected = "non-existing is not a valid pipeline"
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project", "my_project", *options)
options = [
"--pipeline",
self.pipeline_name,
"--pipeline",
"analyze_root_filesystem_or_vm_image:group1,group2",
"--pipeline",
"scan_package", # old name backward compatibility
]
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
project = Project.objects.get(name="my_project")
expected = [
self.pipeline_name,
"analyze_root_filesystem_or_vm_image",
"scan_single_package",
]
self.assertEqual(expected, [run.pipeline_name for run in project.runs.all()])
run = project.runs.get(pipeline_name="analyze_root_filesystem_or_vm_image")
self.assertEqual(["group1", "group2"], run.selected_groups)
def test_scanpipe_management_command_create_project_inputs(self):
out = StringIO()
options = ["--input-file", "non-existing"]
expected = "non-existing not found or not a file"
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project", "my_project", *options)
parent_path = Path(__file__).parent
options = [
"--input-file",
str(parent_path / "test_commands.py"),
"--input-file",
str(parent_path / "test_models.py:tag"),
]
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
project = Project.objects.get(name="my_project")
expected = sorted(["test_commands.py", "test_models.py"])
self.assertEqual(expected, sorted(project.input_files))
tagged_source = project.inputsources.get(filename="test_models.py")
self.assertEqual("tag", tagged_source.tag)
def test_scanpipe_management_command_create_project_execute(self):
options = ["--execute"]
expected = "The --execute option requires one or more pipelines."
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project", "my_project", *options)
pipeline = "load_inventory"
options = [
"--pipeline",
pipeline,
"--execute",
]
out = StringIO()
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_success):
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
self.assertIn(f"Start the {pipeline} pipeline execution...", out.getvalue())
self.assertIn("successfully executed on project my_project", out.getvalue())
options.append("--async")
out = StringIO()
expected = "SCANCODEIO_ASYNC=False is not compatible with --async option."
with override_settings(SCANCODEIO_ASYNC=False):
with self.assertRaisesMessage(CommandError, expected):
call_command("create-project", "other_project", *options, stdout=out)
self.assertIn(
"Project other_project created with work directory", out.getvalue()
)
@override_settings(SCANCODEIO_GLOBAL_WEBHOOK={"target_url": "https://webhook.url"})
@mock.patch.object(Project, "setup_global_webhook")
def test_scanpipe_management_command_create_project_no_global_webhook(
self, mock_setup_webhook
):
out = StringIO()
options = ["--no-global-webhook"]
call_command("create-project", "my_project", *options, stdout=out)
self.assertIn("Project my_project created", out.getvalue())
mock_setup_webhook.assert_not_called()
options = []
call_command("create-project", "my_project_v2", *options, stdout=out)
self.assertIn("Project my_project_v2 created", out.getvalue())
mock_setup_webhook.assert_called()
def test_scanpipe_management_command_batch_create(self):
expected = "You must provide either --input-directory or --input-list as input."
with self.assertRaisesMessage(CommandError, expected):
call_command("batch-create")
input_directory = self.data / "commands" / "batch-create-directory"
options = [
"--input-directory",
str(input_directory),
"--pipeline",
"scan_package",
"--note",
"Some notes",
"--label",
"label1",
"--label",
"label2",
"--project-name-suffix",
"suffix",
]
out = StringIO()
call_command("batch-create", *options, stdout=out)
self.assertIn("Project a.txt suffix created", out.getvalue())
self.assertIn("Project b.txt suffix created", out.getvalue())
self.assertIn("2 projects created.", out.getvalue())
self.assertEqual(2, Project.objects.count())
project = Project.objects.get(name="a.txt suffix")
self.assertEqual("Some notes", project.notes)
self.assertEqual(["label1", "label2"], list(project.labels.names()))
self.assertEqual("scan_single_package", project.runs.get().pipeline_name)
self.assertEqual(["a.txt"], project.input_files)
def test_scanpipe_management_command_batch_create_input_list_csv(self):
input_list = self.data / "commands" / "batch-create-list" / "project_list.csv"
options = [
"--input-list",
str(input_list),
"--pipeline",
"map_deploy_to_develop",
]
out = StringIO()
call_command("batch-create", *options, stdout=out)
self.assertIn("Project project-v1", out.getvalue())
self.assertIn("Project project-v2", out.getvalue())
self.assertIn("URL(s) added as project input sources", out.getvalue())
self.assertIn("https://example.com/source.zip#from", out.getvalue())
self.assertIn("https://example.com/binary.bin#to", out.getvalue())
self.assertIn("https://example.com/filename.zip", out.getvalue())
self.assertIn("2 projects created.", out.getvalue())
self.assertEqual(2, Project.objects.count())
project1 = Project.objects.filter(name__contains="project-v1")[0]
self.assertEqual("map_deploy_to_develop", project1.runs.get().pipeline_name)
input_source1 = project1.inputsources.get(
download_url="https://example.com/source.zip#from"
)
self.assertFalse(input_source1.is_uploaded)
self.assertEqual("from", input_source1.tag)
self.assertFalse(input_source1.exists())
input_source2 = project1.inputsources.get(
download_url="https://example.com/binary.bin#to"
)
self.assertFalse(input_source2.is_uploaded)
self.assertEqual("to", input_source2.tag)
self.assertFalse(input_source2.exists())
project2 = Project.objects.filter(name__contains="project-v2")[0]
self.assertEqual("map_deploy_to_develop", project1.runs.get().pipeline_name)
input_source3 = project2.inputsources.get()
self.assertEqual("https://example.com/filename.zip", input_source3.download_url)
self.assertFalse(input_source3.is_uploaded)
self.assertEqual("", input_source3.tag)
self.assertFalse(input_source3.exists())
@override_settings(SCANCODEIO_GLOBAL_WEBHOOK={"target_url": "https://webhook.url"})
@mock.patch.object(Project, "setup_global_webhook")
def test_scanpipe_management_command_batch_create_global_webhook(
self, mock_setup_webhook
):
input_directory = self.data / "commands" / "batch-create-directory"
options = ["--input-directory", str(input_directory)]
out = StringIO()
call_command("batch-create", *options, stdout=out)
self.assertIn("2 projects created.", out.getvalue())
mock_setup_webhook.assert_not_called()
options += [
"--create-global-webhook",
"--project-name-suffix",
"with-webhook",
]
out = StringIO()
call_command("batch-create", *options, stdout=out)
self.assertIn("2 projects created.", out.getvalue())
mock_setup_webhook.assert_called()
def test_scanpipe_management_command_add_input_file(self):
out = StringIO()
project = Project.objects.create(name="my_project")
parent_path = Path(__file__).parent
options = [
"--input-file",
str(parent_path / "test_commands.py"),
"--input-file",
str(parent_path / "test_models.py:tag"),
]
expected = "the following arguments are required: --project"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-input", *options, stdout=out)
options.extend(["--project", project.name])
call_command("add-input", *options, stdout=out)
self.assertIn("Files copied to the project inputs directory", out.getvalue())
expected = sorted(["test_commands.py", "test_models.py"])
self.assertEqual(expected, sorted(project.input_files))
tagged_source = project.inputsources.get(filename="test_models.py")
self.assertEqual("tag", tagged_source.tag)
options = ["--project", project.name, "--input-file", "non-existing.py"]
expected = "non-existing.py not found or not a file"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-input", *options, stdout=out)
def test_scanpipe_management_command_add_input_url(self):
project = Project.objects.create(name="my_project")
options = [
"--input-url",
"https://example.com/archive.zip",
"--project",
project.name,
]
out = StringIO()
call_command("add-input", *options, stdout=out)
self.assertIn("URL(s) added as project input sources:", out.getvalue())
self.assertIn("- https://example.com/archive.zip", out.getvalue())
input_source = project.inputsources.get()
self.assertEqual("https://example.com/archive.zip", input_source.download_url)
self.assertEqual("", input_source.filename)
self.assertFalse(input_source.is_uploaded)
self.assertEqual("", input_source.tag)
self.assertFalse(input_source.exists())
def test_scanpipe_management_command_add_input_copy_codebase(self):
out = StringIO()
project = Project.objects.create(name="my_project")
options = ["--copy-codebase", "non-existing", "--project", project.name]
expected = "non-existing not found"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-input", *options)
parent_path = Path(__file__).parent
options = [
"--copy-codebase",
str(parent_path / "data" / "codebase"),
"--project",
project.name,
]
call_command("add-input", *options, stdout=out)
self.assertIn("content copied in", out.getvalue())
expected = ["a.txt", "b.txt", "c.txt"]
self.assertEqual(
expected, sorted([path.name for path in project.codebase_path.iterdir()])
)
@filter_warnings("ignore", category=DeprecationWarning, module="scanpipe")
def test_scanpipe_management_command_add_pipeline(self):
out = StringIO()
project = Project.objects.create(name="my_project")
pipelines = [
self.pipeline_name,
"analyze_root_filesystem_or_vm_image:group1,group2",
"scan_package", # old name backward compatibility
]
options = pipelines[:]
expected = "the following arguments are required: --project"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-pipeline", *options, stdout=out)
options.extend(["--project", project.name])
call_command("add-pipeline", *options, stdout=out)
expected = (
"Pipelines analyze_docker_image, analyze_root_filesystem_or_vm_image, "
"scan_single_package added to the project"
)
self.assertIn(expected, out.getvalue())
expected = [
"analyze_docker_image",
"analyze_root_filesystem_or_vm_image",
"scan_single_package",
]
self.assertEqual(expected, [run.pipeline_name for run in project.runs.all()])
run = project.runs.get(pipeline_name="analyze_root_filesystem_or_vm_image")
self.assertEqual(["group1", "group2"], run.selected_groups)
options = ["--project", project.name, "non-existing"]
expected = "non-existing is not a valid pipeline"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-pipeline", *options, stdout=out)
def test_scanpipe_management_command_add_webhook(self):
out = StringIO()
project = Project.objects.create(name="my_project")
options = ["https://example.com/webhook"]
expected = "the following arguments are required: --project"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-webhook", *options, stdout=out)
options = ["invalid-url", "--project", project.name]
expected = "Invalid URL: invalid-url"
with self.assertRaisesMessage(CommandError, expected):
call_command("add-webhook", *options, stdout=out)
options = ["https://example.com/webhook", "--project", project.name]
call_command("add-webhook", *options, stdout=out)
self.assertIn(
f"Webhook successfully added to project '{project.name}' (active).",
out.getvalue(),
)
webhook = WebhookSubscription.objects.get(project=project)
self.assertEqual(webhook.target_url, "https://example.com/webhook")
self.assertTrue(webhook.is_active)
self.assertFalse(webhook.trigger_on_each_run)
self.assertFalse(webhook.include_summary)
self.assertFalse(webhook.include_results)
# Test adding a webhook with all options enabled and inactive
out = StringIO()
options.extend(
[
"--trigger-on-each-run",
"--include-summary",
"--include-results",
"--inactive",
]
)
call_command("add-webhook", *options, stdout=out)
self.assertIn(
f"Webhook successfully added to project '{project.name}' (inactive).",
out.getvalue(),
)
webhook = WebhookSubscription.objects.filter(project=project).first()
self.assertEqual(webhook.target_url, "https://example.com/webhook")
self.assertFalse(webhook.is_active)
self.assertTrue(webhook.trigger_on_each_run)
self.assertTrue(webhook.include_summary)
self.assertTrue(webhook.include_results)
def test_scanpipe_management_command_show_pipeline(self):
pipeline_names = [
self.pipeline_name,
"analyze_root_filesystem_or_vm_image",
]
project = Project.objects.create(name="my_project")
for pipeline_name in pipeline_names:
project.add_pipeline(pipeline_name)
options = ["--project", project.name, "--no-color"]
out = StringIO()
call_command("show-pipeline", *options, stdout=out)
expected = (
" [NOT_STARTED] analyze_docker_image\n"
" [NOT_STARTED] analyze_root_filesystem_or_vm_image\n"
)
self.assertEqual(expected, out.getvalue())
project.runs.filter(pipeline_name=pipeline_names[0]).update(
task_exitcode=0, selected_groups=["group1", "group2"]
)
project.runs.filter(pipeline_name=pipeline_names[1]).update(task_exitcode=1)
out = StringIO()
call_command("show-pipeline", *options, stdout=out)
expected = (
" [SUCCESS] analyze_docker_image (group1,group2)\n"
" [FAILURE] analyze_root_filesystem_or_vm_image\n"
)
self.assertEqual(expected, out.getvalue())
def test_scanpipe_management_command_execute(self):
project = Project.objects.create(name="my_project")
options = ["--project", project.name]
out = StringIO()
expected = "No pipelines to run on project my_project"
with self.assertRaisesMessage(CommandError, expected):
call_command("execute", *options, stdout=out)
out = StringIO()
run1 = project.add_pipeline(self.pipeline_name)
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_success):
call_command("execute", *options, stdout=out)
expected = "Start the analyze_docker_image pipeline execution..."
self.assertIn(expected, out.getvalue())
expected = "successfully executed on project my_project"
self.assertIn(expected, out.getvalue())
run1.refresh_from_db()
self.assertTrue(run1.task_succeeded)
self.assertEqual("", run1.task_output)
run1.delete()
err = StringIO()
run2 = project.add_pipeline(self.pipeline_name)
expected = "Error during analyze_docker_image execution:\nError log"
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_failure):
with self.assertRaisesMessage(CommandError, expected):
call_command("execute", *options, stdout=out, stderr=err)
run2.refresh_from_db()
self.assertTrue(run2.task_failed)
self.assertEqual("Error log", run2.task_output)
run2.delete()
err = StringIO()
run3 = project.add_pipeline(self.pipeline_name)
with mock.patch("scanpipe.tasks.execute_pipeline_task", raise_interrupt):
with self.assertRaisesMessage(CommandError, "Pipeline execution stopped."):
call_command("execute", *options, stdout=out, stderr=err)
run3.refresh_from_db()
run3 = Run.objects.get(pk=run3.pk)
self.assertTrue(run3.task_stopped)
self.assertEqual("", run3.task_output)
def test_scanpipe_management_command_execute_project_function(self):
project = Project.objects.create(name="my_project")
expected = "No pipelines to run on project my_project"
with self.assertRaisesMessage(CommandError, expected):
commands.execute_project(project)
run1 = project.add_pipeline(self.pipeline_name)
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_success):
returned_value = commands.execute_project(project, run_async=False)
self.assertIsNone(returned_value)
run1.refresh_from_db()
self.assertTrue(run1.task_succeeded)
run1.delete()
project.add_pipeline(self.pipeline_name)
expected = "SCANCODEIO_ASYNC=False is not compatible with --async option."
with override_settings(SCANCODEIO_ASYNC=False):
with self.assertRaisesMessage(CommandError, expected):
commands.execute_project(project, run_async=True)
with override_settings(SCANCODEIO_ASYNC=True):
with mock.patch("scanpipe.models.Run.start") as mock_start:
returned_value = commands.execute_project(project, run_async=True)
mock_start.assert_called_once()
self.assertIsNone(returned_value)
def test_scanpipe_management_command_status(self):
project = Project.objects.create(name="my_project")
run = project.add_pipeline(self.pipeline_name)
options = ["--project", project.name, "--no-color"]
out = StringIO()
call_command("status", *options, stdout=out)
output = out.getvalue()
self.assertIn("my_project", output)
self.assertIn("- CodebaseResource: 0", output)
self.assertIn("- DiscoveredPackage: 0", output)
self.assertIn("- ProjectMessage: 0", output)
self.assertIn("[NOT_STARTED] analyze_docker_image", output)
run.task_id = uuid.uuid4()
run.save()
out = StringIO()
call_command("status", *options, stdout=out)
output = out.getvalue()
self.assertIn("[QUEUED] analyze_docker_image", output)
run.task_start_date = timezone.now()
run.log = (
"[1611839665826870/start/1 (pid 65890)] Task finished successfully.\n"
"[1611839665826870/extract_images/2 (pid 65914)] Task is starting.\n"
)
run.save()
out = StringIO()
call_command("status", *options, stdout=out)
output = out.getvalue()
self.assertIn("[RUNNING] analyze_docker_image", output)
for line in run.log.splitlines():
self.assertIn(line, output)
run.task_end_date = run.task_start_date + datetime.timedelta(0, 42)
run.task_exitcode = 0
run.save()
out = StringIO()
call_command("status", *options, stdout=out)
output = out.getvalue()
expected = (
f"[SUCCESS] analyze_docker_image (executed in {run.execution_time} seconds)"
)
self.assertIn(expected, output)
def test_scanpipe_management_command_list_pipelines(self):
options = []
out = StringIO()
call_command("list-pipelines", *options, stdout=out)
output = out.getvalue()
self.assertIn("analyze_docker_image", output)
self.assertIn("Analyze Docker images.", output)
self.assertIn("(addon)", output)
self.assertNotIn("[extract_images]", output)
self.assertNotIn("Extract images from input tarballs.", output)
options = ["--verbosity=2"]
out = StringIO()
call_command("list-pipelines", *options, stdout=out)
output = out.getvalue()
self.assertIn("[extract_images]", output)
self.assertIn("Extract images from input tarballs.", output)
options = ["--verbosity=0"]
out = StringIO()
call_command("list-pipelines", *options, stdout=out)
output = out.getvalue()
self.assertIn("analyze_docker_image", output)
self.assertNotIn("Analyze Docker images.", output)
self.assertIn("(addon)", output)
def test_scanpipe_management_command_list_project(self):
project1 = Project.objects.create(name="project1")
project2 = Project.objects.create(name="project2")
project3 = Project.objects.create(name="archived", is_archived=True)
options = []
out = StringIO()
call_command("list-project", *options, stdout=out)
output = out.getvalue()
self.assertIn(project1.name, output)
self.assertIn(project2.name, output)
self.assertNotIn(project3.name, output)
options = ["--search", project2.name]
out = StringIO()
call_command("list-project", *options, stdout=out)
output = out.getvalue()
self.assertNotIn(project1.name, output)
self.assertIn(project2.name, output)
self.assertNotIn(project3.name, output)
options = ["--include-archived"]
out = StringIO()
call_command("list-project", *options, stdout=out)
output = out.getvalue()
self.assertIn(project1.name, output)
self.assertIn(project2.name, output)
self.assertIn(project3.name, output)
def test_scanpipe_management_command_output(self):
project = Project.objects.create(name="my_project")
make_package(project, package_url="pkg:generic/name@1.0")
out = StringIO()
options = ["--project", project.name, "--no-color"]
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
self.assertTrue(out_value.endswith(".json"))
filename = out_value.split("/")[-1]
self.assertIn(filename, project.output_root)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "csv"])
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
for output_file in out_value.splitlines():
filename = output_file.split("/")[-1]
self.assertIn(filename, project.output_root)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "spdx", "xlsx"])
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
for output_file in out_value.splitlines():
filename = output_file.split("/")[-1]
self.assertIn(filename, project.output_root)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "WRONG"])
message = "Error: argument --format: invalid choice: 'WRONG'"
with self.assertRaisesMessage(CommandError, message):
call_command("output", *options, stdout=out)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "xlsx", "--print"])
message = "--print is not compatible with xlsx and csv formats."
with self.assertRaisesMessage(CommandError, message):
call_command("output", *options, stdout=out)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "json", "--print"])
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
self.assertIn('"tool_name": "scanpipe"', out_value)
self.assertIn('"notice": "Generated with ScanCode.io', out_value)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "cyclonedx", "--print"])
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
self.assertIn('"bomFormat": "CycloneDX"', out_value)
self.assertIn('"specVersion": "1.6",', out_value)
out = StringIO()
options = ["--project", project.name, "--no-color"]
options.extend(["--format", "cyclonedx:1.5", "--print"])
call_command("output", *options, stdout=out)
out_value = out.getvalue().strip()
self.assertIn('"bomFormat": "CycloneDX"', out_value)
self.assertIn('"specVersion": "1.5",', out_value)
def test_scanpipe_management_command_delete_project(self):
project = Project.objects.create(name="my_project")
work_path = project.work_path
self.assertTrue(work_path.exists())
out = StringIO()
options = ["--project", project.name, "--no-color", "--no-input"]
call_command("delete-project", *options, stdout=out)
out_value = out.getvalue().strip()
expected = "All the my_project project data have been removed."
self.assertEqual(expected, out_value)
self.assertFalse(Project.objects.filter(name="my_project").exists())
self.assertFalse(work_path.exists())
def test_scanpipe_management_command_archive_project(self):
project = Project.objects.create(name="my_project")
(project.input_path / "input_file").touch()
(project.codebase_path / "codebase_file").touch()
self.assertEqual(1, len(Project.get_root_content(project.input_path)))
self.assertEqual(1, len(Project.get_root_content(project.codebase_path)))
out = StringIO()
options = [
"--project",
project.name,
"--remove-codebase",
"--no-color",
"--no-input",
]
call_command("archive-project", *options, stdout=out)
out_value = out.getvalue().strip()
project.refresh_from_db()
self.assertTrue(project.is_archived)
expected = "The my_project project has been archived."
self.assertEqual(expected, out_value)
self.assertEqual(1, len(Project.get_root_content(project.input_path)))
self.assertEqual(0, len(Project.get_root_content(project.codebase_path)))
def test_scanpipe_management_command_reset_project(self):
project = Project.objects.create(name="my_project")
project.add_pipeline("analyze_docker_image")
CodebaseResource.objects.create(project=project, path="filename.ext")
DiscoveredPackage.objects.create(project=project)
self.assertEqual(1, project.runs.count())
self.assertEqual(1, project.codebaseresources.count())
self.assertEqual(1, project.discoveredpackages.count())
(project.input_path / "input_file").touch()
(project.codebase_path / "codebase_file").touch()
self.assertEqual(1, len(Project.get_root_content(project.input_path)))
self.assertEqual(1, len(Project.get_root_content(project.codebase_path)))
out = StringIO()
options = [
"--project",
project.name,
"--no-color",
"--no-input",
]
call_command("reset-project", *options, stdout=out)
out_value = out.getvalue().strip()
expected = (
"All data, except inputs, for the my_project project have been removed."
)
self.assertEqual(expected, out_value)
self.assertEqual(0, project.runs.count())
self.assertEqual(0, project.codebaseresources.count())
self.assertEqual(0, project.discoveredpackages.count())
self.assertEqual(1, len(Project.get_root_content(project.input_path)))
self.assertEqual(0, len(Project.get_root_content(project.codebase_path)))
def test_scanpipe_management_command_flush_projects(self):
project1 = Project.objects.create(name="project1")
project2 = Project.objects.create(name="project2")
ten_days_ago = timezone.now() - datetime.timedelta(days=10)
project2.update(created_date=ten_days_ago)
out = StringIO()
options = ["--retain-days", 7, "--no-color", "--no-input"]
call_command("flush-projects", *options, stdout=out)
out_value = out.getvalue().strip()
expected = "1 project and its related data have been removed."
self.assertEqual(expected, out_value)
self.assertEqual(project1, Project.objects.get())
Project.objects.create(name="project2")
out = StringIO()
options = ["--no-color", "--no-input"]
call_command("flush-projects", *options, stdout=out)
out_value = out.getvalue().strip()
expected = "2 projects and their related data have been removed."
self.assertEqual(expected, out_value)
def test_scanpipe_management_command_create_user(self):
out = StringIO()
expected = "Error: the following arguments are required: username"
with self.assertRaisesMessage(CommandError, expected):
call_command("create-user", "--no-input")
username = "my_username"
call_command("create-user", "--no-input", username, stdout=out)
self.assertIn(f"User {username} created with API key:", out.getvalue())
user = get_user_model().objects.get(username=username)
self.assertTrue(user.auth_token)
self.assertFalse(user.is_staff)
self.assertFalse(user.is_superuser)
expected = "Error: That username is already taken."
with self.assertRaisesMessage(CommandError, expected):
call_command("create-user", "--no-input", username)
username = "^&*"
expected = (
"Enter a valid username. This value may contain only letters, numbers, "
"and @/./+/-/_ characters."
)
with self.assertRaisesMessage(CommandError, expected):
call_command("create-user", "--no-input", username)
def test_scanpipe_management_command_create_user_admin_superuser(self):
out = StringIO()
username = "my_username"
call_command("create-user", "--no-input", "--admin", username, stdout=out)
user = get_user_model().objects.get(username=username)
self.assertTrue(user.is_staff)
self.assertFalse(user.is_superuser)
out = StringIO()
username = "user2"
call_command("create-user", "--no-input", "--super", username, stdout=out)
user = get_user_model().objects.get(username=username)
self.assertTrue(user.is_staff)
self.assertTrue(user.is_superuser)
def test_scanpipe_management_command_run(self):
expected = (
"Error: the following arguments are required: PIPELINE_NAME, input_location"
)
with self.assertRaisesMessage(CommandError, expected):
call_command("run")
expected = "wrong_pipeline is not a valid pipeline."
with self.assertRaisesMessage(CommandError, expected):
call_command("run", "wrong_pipeline", str(self.data))
expected = "bad_location not found."
with self.assertRaisesMessage(CommandError, expected):
call_command("run", "scan_single_package", "bad_location")
out = StringIO()
input_location = self.data / "codebase"
with redirect_stdout(out):
call_command("run", "inspect_packages", input_location)
json_data = json.loads(out.getvalue())
self.assertEqual(3, len(json_data["files"]))
# Multiple pipeline and selected_groups are supported
out = StringIO()
with redirect_stdout(out):
options = [
"inspect_packages:StaticResolver",
"do_nothing:Group1,Group2",
input_location,
]
call_command("run", *options)
json_data = json.loads(out.getvalue())
runs = json_data["headers"][0]["runs"]
self.assertEqual("inspect_packages", runs[0]["pipeline_name"])
self.assertEqual(["StaticResolver"], runs[0]["selected_groups"])
self.assertEqual("do_nothing", runs[1]["pipeline_name"])
self.assertEqual(["Group1", "Group2"], runs[1]["selected_groups"])
@mock.patch("scanpipe.models.Project.get_latest_output")
@mock.patch("requests.post")
@mock.patch("requests.sessions.Session.get")
@mock.patch("scanpipe.pipes.purldb.request_get")
def test_scanpipe_management_command_purldb_scan_queue_worker(
self,
mock_request_get,
mock_download_get,
mock_request_post,
mock_get_latest_output,
):
scannable_uri_uuid = "97627c6e-9acb-43e0-b8df-28bd92f2b7e5"
download_url = "https://registry.npmjs.org/asdf/-/asdf-1.2.2.tgz"
webhook_url = "http://server/api/scan_queue/index_package_scan/IjEi:1sZEvv:_br-G-VikC19M5RS2ToNZTGt81GLc6co7w72XHRmCKY/"
mock_request_get.return_value = {
"scannable_uri_uuid": scannable_uri_uuid,
"download_url": download_url,
"pipelines": ["scan_single_package"],
"webhook_url": webhook_url,
}
mock_request_post.return_value = {
"status": f"scan results for scannable_uri {scannable_uri_uuid} "
"have been queued for indexing"
}
mock_get_latest_output.return_value = (
self.data / "scancode" / "is-npm-1.0.0_summary.json"
)
mock_download_get.return_value = mock.Mock(
content=b"\x00",
headers={},
status_code=200,
url=download_url,
)
self.assertFalse(WebhookSubscription.objects.exists())
options = [
"--max-loops",
1,
]
out = StringIO()
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_success):
call_command("purldb-scan-worker", *options, stdout=out)
out_value = out.getvalue()
self.assertIn(
"Project httpsregistrynpmjsorgasdf-asdf-122tgz-97627c6e created", out_value
)
self.assertIn(
"scan_single_package successfully executed on project "
"httpsregistrynpmjsorgasdf-asdf-122tgz-97627c6e",
out_value,
)
project_name = purldb.create_project_name(download_url, scannable_uri_uuid)
project = Project.objects.get(name=project_name)
self.assertEqual(scannable_uri_uuid, project.extra_data["scannable_uri_uuid"])
# Ensure a webhook subscription is created
self.assertEqual(1, project.webhooksubscriptions.count())
webhook_subscription = project.webhooksubscriptions.first()