File: test_client.py

package info (click to toggle)
refstack-client 0.0.0~2023.09.19.b60a7e41f7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 592 kB
  • sloc: python: 1,998; sh: 220; makefile: 25
file content (940 lines) | stat: -rwxr-xr-x 37,566 bytes parent folder | download
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
#
# Copyright (c) 2014 Piston Cloud Computing, Inc. All Rights Reserved.
#
#    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.
#

import hashlib
import json
import logging
import os
import tempfile

import httmock
from unittest import mock
from unittest.mock import MagicMock
import unittest

from refstack_client import refstack_client as rc


class TestRefstackClient(unittest.TestCase):

    test_path = os.path.dirname(os.path.realpath(__file__))
    conf_file_name = '%s/refstack-client.test.conf' % test_path

    def patch(self, name, **kwargs):
        """
        :param name: Name of class to be patched
        :param kwargs: directly passed to mock.patch
        :return: mock
        """
        patcher = mock.patch(name, **kwargs)
        thing = patcher.start()
        self.addCleanup(patcher.stop)
        return thing

    def mock_argv(self, command='test', **kwargs):
        """
        Build argv for test.
        :param conf_file_name: Configuration file name
        :param verbose: verbosity level
        :return: argv
        """
        argv = [command]
        if kwargs.get('verbose', None):
            argv.append(kwargs.get('verbose', None))
        if kwargs.get('silent', None):
            argv.append(kwargs.get('silent', None))
        argv.extend(['--url', 'http://127.0.0.1', '-y'])
        if kwargs.get('priv_key', None):
            argv.extend(('-i', kwargs.get('priv_key', None)))
        if command == 'test':
            argv.extend(
                ('-c', kwargs.get('conf_file_name', self.conf_file_name)))
            if kwargs.get('test_cases', None):
                argv.extend(('--', kwargs.get('test_cases', None)))
        return argv

    def mock_data(self):
        """
        Mock the Keystone client methods.
        """
        self.mock_identity_service_v2 = {'type': 'identity',
                                         'endpoints': [{'id': 'test-id'}]}
        self.mock_identity_service_v3 = {'type': 'identity',
                                         'id': 'test-id'}
        self.v2_config = {'auth_url': 'http://0.0.0.0:35357/v2.0/tokens',
                          'auth_version': 'v2',
                          'domain_name': 'Default',
                          'password': 'test',
                          'tenant_id': 'admin_project_id',
                          'project_id': 'admin_project_id',
                          'tenant_name': 'project_name',
                          'project_name': 'project_name',
                          'username': 'admin'}

    def setUp(self):
        """
        Test case setup
        """
        logging.disable(logging.CRITICAL)

    def test_verbose(self):
        """
        Test different verbosity levels.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        self.assertEqual(client.logger.level, logging.INFO)

        args = rc.parse_cli_args(self.mock_argv(verbose='-v'))
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        self.assertEqual(client.logger.level, logging.DEBUG)

        args = rc.parse_cli_args(self.mock_argv(verbose='-vv'))
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        self.assertEqual(client.logger.level, logging.DEBUG)

        args = rc.parse_cli_args(self.mock_argv(silent='-s'))
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        self.assertEqual(client.logger.level, logging.WARNING)

    def test_get_next_stream_subunit_output_file(self):
        """
        Test getting the subunit file from an existing .testrepository
        directory that has a next-stream file.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        output_file = client._get_next_stream_subunit_output_file(
            self.test_path)

        # The next-stream file contains a "1".
        expected_file = expected_file = self.test_path + "/.testrepository/1"
        self.assertEqual(expected_file, output_file)

    def test_get_next_stream_subunit_output_file_nonexistent(self):
        """
        Test getting the subunit output file from a nonexistent
        .testrepository directory.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        output_file = client._get_next_stream_subunit_output_file(
            "/tempest/path")
        expected_file = "/tempest/path/.testrepository/0"
        self.assertEqual(expected_file, output_file)

    def test_get_cpid_account_file_not_found(self):
        """
        Test that the client will exit if an accounts file is specified,
        but does not exist.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()

        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/some-file.yaml' % self.test_path)

        self.mock_data()
        with self.assertRaises(SystemExit):
            client._get_keystone_config(client.conf)

    def test_get_keystone_config_account_file_empty(self):
        """
        Test that the client will exit if an accounts file exists,
        but is empty.
        """
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=None)

        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()

        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/some-file.yaml' % self.test_path)

        self.mock_data()
        with self.assertRaises(SystemExit):
            client._get_keystone_config(client.conf)

    def test_get_keystone_config_no_accounts_file(self):
        """
        Test that the client will exit if accounts file
        is not specified.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()

        self.mock_data()
        with self.assertRaises(SystemExit):
            client._get_keystone_config(client.conf)

    def test_get_keystone_config(self):
        """
        Test that keystone configs properly parsed.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/test-accounts.yaml' % self.test_path)
        self.mock_data()
        accounts = [
            {
                'username': 'admin',
                'project_name': 'project_name',
                'project_id': 'admin_project_id',
                'password': 'test'
            }
        ]
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=accounts)
        actual_result = client._get_keystone_config(client.conf)
        expected_result = self.v2_config
        self.assertEqual(expected_result, actual_result)

    def test_get_cpid_from_keystone_by_tenant_name_from_account_file(self):
        """
        Test getting a CPID from Keystone using an admin tenant name
        from an accounts file.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/test-accounts.yaml' % self.test_path)
        self.mock_data()
        actual_result = client._get_keystone_config(client.conf)
        expected_result = None
        self.assertEqual(expected_result, actual_result['tenant_id'])
        accounts = [
            {
                'username': 'admin',
                'tenant_id': 'tenant_id',
                'password': 'test'
            }
        ]
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=accounts)
        actual_result = client._get_keystone_config(client.conf)
        self.assertEqual('tenant_id', actual_result['tenant_id'])

    def test_generate_keystone_data(self):
        """Test that correct data is generated."""
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/test-accounts.yaml' % self.test_path)
        self.mock_data()
        accounts = [
            {
                'username': 'admin',
                'tenant_id': 'admin_tenant_id',
                'password': 'test'
            }
        ]
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=accounts)
        configs = client._get_keystone_config(client.conf)
        actual_results = client._generate_keystone_data(configs)
        expected_results = ('v2', 'http://0.0.0.0:35357/v2.0/tokens',
                            {'auth':
                                {'passwordCredentials':
                                    {
                                        'username': 'admin', 'password': 'test'
                                    },
                                 'tenantId': 'admin_tenant_id'}})
        self.assertEqual(expected_results, actual_results)

    def test_get_cpid_from_keystone_v3_varying_catalogs(self):
        """
        Test getting the CPID from keystone API v3 with varying catalogs.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        client.conf.set('identity-feature-enabled', 'api_v3', 'true')
        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/test-accounts.yaml' % self.test_path)
        self.mock_data()
        accounts = [
            {
                'tenant_name': 'tenant_name'
            }
        ]
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=accounts)
        configs = client._get_keystone_config(client.conf)
        auth_version, auth_url, content = \
            client._generate_keystone_data(configs)
        client._generate_cpid_from_endpoint = MagicMock()

        # Test when the identity ID is None.
        ks3_ID_None = {'token': {'catalog':
                                 [{'type': 'identity', 'id': None}]}}

        @httmock.all_requests
        def keystone_api_v3_mock(url, request):
            return httmock.response(201, ks3_ID_None)
        with httmock.HTTMock(keystone_api_v3_mock):
            client._get_cpid_from_keystone(auth_version, auth_url, content)
            client._generate_cpid_from_endpoint.assert_called_with(auth_url)

        # Test when the catalog is empty.
        ks3_catalog_empty = {'token': {'catalog': []}}
        client._generate_cpid_from_endpoint = MagicMock()

        @httmock.all_requests
        def keystone_api_v3_mock2(url, request):
            return httmock.response(201, ks3_catalog_empty)
        with httmock.HTTMock(keystone_api_v3_mock2):
            client._get_cpid_from_keystone(auth_version, auth_url, content)
            client._generate_cpid_from_endpoint.assert_called_with(auth_url)

        # Test when there is no service catalog.
        ks3_no_catalog = {'token': {}}
        client._generate_cpid_from_endpoint = MagicMock()

        @httmock.all_requests
        def keystone_api_v3_mock3(url, request):
            return httmock.response(201, ks3_no_catalog)
        with httmock.HTTMock(keystone_api_v3_mock3):
            client._get_cpid_from_keystone(auth_version, auth_url, content)
            client._generate_cpid_from_endpoint.assert_called_with(auth_url)

        # Test when catalog has other non-identity services.
        ks3_other_services = {'token': {
                              'catalog': [{'type': 'compute',
                                           'id': 'test-id1'},
                                          {'type': 'identity',
                                           'id': 'test-id2'}]
                              }}
        client._generate_cpid_from_endpoint = MagicMock()

        @httmock.all_requests
        def keystone_api_v3_mock4(url, request):
            return httmock.response(201, ks3_other_services)
        with httmock.HTTMock(keystone_api_v3_mock4):
            cpid = client._get_cpid_from_keystone(auth_version,
                                                  auth_url,
                                                  content)
            self.assertFalse(client._generate_cpid_from_endpoint.called)
            self.assertEqual('test-id2', cpid)

    def test_get_cpid_from_keystone_failure_handled(self):
        """Test that get cpid from keystone API failure handled."""
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        client.logger.warning = MagicMock()
        client._generate_cpid_from_endpoint = MagicMock()
        client.conf.add_section('auth')
        client.conf.set('auth',
                        'test_accounts_file',
                        '%s/test-accounts.yaml' % self.test_path)
        self.mock_data()
        accounts = [
            {
                'tenant_name': 'tenant_name',
                'tenant_id': 'admin_tenant_id',
                'password': 'test'
            }
        ]
        self.patch(
            'refstack_client.refstack_client.read_accounts_yaml',
            return_value=accounts)
        configs = client._get_keystone_config(client.conf)
        auth_version, url, content = client._generate_keystone_data(configs)

        @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v2/tokens')
        def keystone_api_mock(auth_version, url, request):
            return None
        with httmock.HTTMock(keystone_api_mock):
            client._get_cpid_from_keystone(auth_version, url, content)
            client._generate_cpid_from_endpoint.assert_called_with(url)

    def test_generate_cpid_from_endpoint(self):
        """
        Test that an endpoint's hostname is properly hashed.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        cpid = client._generate_cpid_from_endpoint('http://some.url:5000/v2')
        expected = hashlib.md5('some.url'.encode('utf-8')).hexdigest()
        self.assertEqual(expected, cpid)

        with self.assertRaises(ValueError):
            client._generate_cpid_from_endpoint('some.url:5000/v2')

    def test_form_result_content(self):
        """
        Test that the request content is formed into the expected format.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        content = client._form_result_content(1, 1, ['tempest.sample.test'])
        expected = {'cpid': 1,
                    'duration_seconds': 1,
                    'results': ['tempest.sample.test']}
        self.assertEqual(expected, content)

    def test_save_json_result(self):
        """
        Test that the results are properly written to a JSON file.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        results = {'cpid': 1,
                   'duration_seconds': 1,
                   'results': ['tempest.sample.test']}
        temp_file = tempfile.NamedTemporaryFile()
        client._save_json_results(results, temp_file.name)

        # Get the JSON that was written to the file and make sure it
        # matches the expected value.
        json_file = open(temp_file.name)
        json_data = json.load(json_file)
        json_file.close()
        self.assertEqual(results, json_data)

    def test_get_passed_tests(self):
        """
        Test that only passing tests are retrieved from a subunit file.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        subunit_file = self.test_path + "/.testrepository/0"
        results = client.get_passed_tests(subunit_file)
        expected = [
            {'name': 'tempest.passed.test'},
            {'name': 'tempest.tagged_passed.test',
             'uuid': '0146f675-ffbd-4208-b3a4-60eb628dbc5e'}
        ]
        self.assertEqual(expected, results)

    @mock.patch('six.moves.input')
    def test_user_query(self, mock_input):
        client = rc.RefstackClient(rc.parse_cli_args(self.mock_argv()))
        self.assertTrue(client._user_query('42?'))

        mock_input.return_value = 'n'
        cli_args = self.mock_argv()
        cli_args.remove('-y')
        client = rc.RefstackClient(rc.parse_cli_args(cli_args))
        self.assertFalse(client._user_query('42?'))
        mock_input.return_value = 'yes'
        self.assertTrue(client._user_query('42?'))

    def test_upload_prompt(self):
        """
        Test the _upload_prompt method.
        """
        client = rc.RefstackClient(rc.parse_cli_args(self.mock_argv()))

        # When user says yes.
        client._user_query = MagicMock(return_value=True)
        client.post_results = MagicMock()
        client._upload_prompt({'some': 'data'})
        client.post_results.assert_called_with(
            'http://127.0.0.1', {'some': 'data'}, sign_with=None
        )

        # When user says no.
        client._user_query = MagicMock(return_value=False)
        client.post_results = MagicMock()
        client._upload_prompt({'some': 'data'})
        self.assertFalse(client.post_results.called)

    def test_post_results(self):
        """
        Test the post_results method, ensuring a requests call is made.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.logger.info = MagicMock()
        content = {'duration_seconds': 0,
                   'cpid': 'test-id',
                   'results': [{'name': 'tempest.passed.test', 'uid': None}]}
        expected_response = json.dumps({'test_id': 42})

        @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/')
        def refstack_api_mock(url, request):
            return expected_response

        with httmock.HTTMock(refstack_api_mock):
            client.post_results("http://127.0.0.1", content)
            client.logger.info.assert_called_with(
                'http://127.0.0.1/v1/results/ Response: '
                '%s' % expected_response)

    def test_post_results_with_sign(self):
        """
        Test the post_results method, ensuring a requests call is made.
        """
        argv = self.mock_argv(command='upload', priv_key='rsa_key')
        argv.append('fake.json')
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        client.logger.info = MagicMock()
        content = {'duration_seconds': 0,
                   'cpid': 'test-id',
                   'results': [{'name': 'tempest.passed.test'}]}
        expected_response = json.dumps({'test_id': 42})

        @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/')
        def refstack_api_mock(url, request):
            return expected_response

        with httmock.HTTMock(refstack_api_mock):
            rsapath = os.path.join(self.test_path, 'rsa_key')
            client.post_results("http://127.0.0.1", content, sign_with=rsapath)
            client.logger.info.assert_called_with(
                'http://127.0.0.1/v1/results/ Response: %s' %
                expected_response)

    def test_run_tempest(self):
        """
        Test that the test command will run the tempest script using the
        default configuration.
        """
        args = rc.parse_cli_args(
            self.mock_argv(verbose='-vv', test_cases='tempest.api.compute'))
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        mock_popen = self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        self.patch("os.path.isfile", return_value=True)
        self.mock_data()
        client.get_passed_tests = MagicMock(return_value=[{'name': 'test'}])
        client.logger.info = MagicMock()
        client._save_json_results = MagicMock()
        client.post_results = MagicMock()
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client.test()

        mock_popen.assert_called_with(
            ['tempest', 'run',
             '--serial', '--regex', 'tempest.api.compute'],
            stderr=None
        )

        self.assertFalse(client.post_results.called)

    def test_run_tempest_upload(self):
        """
        Test that the test command will run the tempest script and call
        post_results when the --upload argument is passed in.
        """
        argv = self.mock_argv(verbose='-vv',
                              test_cases='tempest.api.compute')
        argv.insert(2, '--upload')
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        mock_popen = self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        self.patch("os.path.isfile", return_value=True)
        self.mock_data()
        client.get_passed_tests = MagicMock(return_value=['test'])
        client.post_results = MagicMock()
        client._save_json_results = MagicMock()
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client._get_cpid_from_keystone = MagicMock()
        client.test()
        mock_popen.assert_called_with(
            ['tempest', 'run',
             '--serial', '--regex', 'tempest.api.compute'],
            stderr=None
        )

        self.assertTrue(client.post_results.called)

    def test_run_tempest_upload_with_sign(self):
        """
        Test that the test command will run the tempest script and call
        post_results when the --upload argument is passed in.
        """
        argv = self.mock_argv(verbose='-vv', priv_key='rsa_key',
                              test_cases='tempest.api.compute')
        argv.insert(2, '--upload')
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        mock_popen = self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0)
        )
        self.patch("os.path.isfile", return_value=True)
        self.mock_data()
        client.get_passed_tests = MagicMock(return_value=['test'])
        client.post_results = MagicMock()
        client._save_json_results = MagicMock()
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client._get_cpid_from_keystone = MagicMock(
            return_value='test-id')
        client.test()
        mock_popen.assert_called_with(
            ['tempest', 'run',
             '--serial', '--regex', 'tempest.api.compute'],
            stderr=None
        )

        self.assertTrue(client.post_results.called)
        client.post_results.assert_called_with(
            'http://127.0.0.1',
            {'duration_seconds': 0,
             'cpid': 'test-id',
             'results': ['test']},
            sign_with='rsa_key'
        )

    @mock.patch('refstack_client.list_parser.TestListParser.'
                'create_include_list')
    @mock.patch('refstack_client.list_parser.'
                'TestListParser.get_normalized_test_list')
    def test_run_tempest_with_test_list(self, mock_normalize,
                                        mock_include_list):
        """Test that the Tempest script runs with a test list file."""
        argv = self.mock_argv(verbose='-vv')
        argv.extend(['--test-list', 'test-list.txt'])
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        mock_popen = self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        self.patch("os.path.isfile", return_value=True)
        self.patch("os.path.getsize", return_value=4096)
        self.mock_data()
        client.get_passed_tests = MagicMock(return_value=[{'name': 'test'}])
        client._save_json_results = MagicMock()
        client.post_results = MagicMock()
        mock_normalize.return_value = '/tmp/some-list'
        mock_include_list.return_value = '/tmp/some-list'
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client.test()

        mock_include_list.assert_called_with('test-list.txt')
        # TODO(kopecmartin) rename the below argument when refstack-client
        # uses tempest which contains the following change in its code:
        # https://review.opendev.org/c/openstack/tempest/+/768583
        mock_popen.assert_called_with(
            ['tempest', 'run',
             '--serial', '--whitelist_file', '/tmp/some-list'],
            stderr=None
        )

    def test_run_tempest_no_conf_file(self):
        """
        Test when a nonexistent configuration file is passed in.
        """
        args = rc.parse_cli_args(self.mock_argv(conf_file_name='ptn-khl'))
        client = rc.RefstackClient(args)
        self.assertRaises(SystemExit, client.test)

    def test_forbidden_conf_file(self):
        """
        Test when the user passes in a file that the user does not have
        read access to.
        """
        file = tempfile.NamedTemporaryFile()
        # Remove read access
        os.chmod(file.name, 0o220)
        args = rc.parse_cli_args(self.mock_argv(conf_file_name=file.name))
        client = rc.RefstackClient(args)
        self.assertRaises(SystemExit, client.test)

    def test_run_tempest_nonexisting_directory(self):
        """
        Test when the Tempest directory does not exist.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = "/does/not/exist"
        self.assertRaises(SystemExit, client.test)

    def test_run_tempest_result_tag(self):
        """
        Check that the result JSON file is renamed with the result file tag
        when the --result-file-tag argument is passed in.
        """
        argv = self.mock_argv(verbose='-vv',
                              test_cases='tempest.api.compute')
        argv.insert(2, '--result-file-tag')
        argv.insert(3, 'my-test')
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        mock_popen = self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        self.patch("os.path.isfile", return_value=True)
        self.mock_data()
        client.get_passed_tests = MagicMock(return_value=['test'])
        client._save_json_results = MagicMock()
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client._get_cpid_from_keystone = MagicMock(
            return_value='test-id')
        client.test()

        mock_popen.assert_called_with(
            ['tempest', 'run',
             '--serial', '--regex', 'tempest.api.compute'],
            stderr=None
        )
        # Since '1' is in the next-stream file, we expect the JSON output file
        # to be 'my-test-1.json'.
        expected_file = os.path.join(self.test_path, '.testrepository',
                                     'my-test-1.json')
        client._save_json_results.assert_called_with(mock.ANY, expected_file)

    def test_failed_run(self):
        """
        Test when the Tempest script returns a non-zero exit code.
        """
        self.patch('refstack_client.refstack_client.subprocess.Popen',
                   return_value=MagicMock(returncode=1))
        args = rc.parse_cli_args(self.mock_argv(verbose='-vv'))
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        self.mock_data()
        client.logger.warning = MagicMock()
        client._get_keystone_config = MagicMock(
            return_value=self.v2_config)
        client._get_cpid_from_keystone = MagicMock()
        client.test()
        self.assertTrue(client.logger.warning.called)

    def test_upload(self):
        """
        Test that the upload command runs as expected.
        """
        upload_file_path = self.test_path + "/.testrepository/0.json"
        args = rc.parse_cli_args(
            self.mock_argv(command='upload', priv_key='rsa_key') +
            [upload_file_path])
        client = rc.RefstackClient(args)

        client.post_results = MagicMock()
        client.upload()
        expected_json = {
            'duration_seconds': 0,
            'cpid': 'test-id',
            'results': [
                {'name': 'tempest.passed.test'},
                {'name': 'tempest.tagged_passed.test',
                 'uuid': '0146f675-ffbd-4208-b3a4-60eb628dbc5e'}
            ]
        }
        client.post_results.assert_called_with('http://127.0.0.1',
                                               expected_json,
                                               sign_with='rsa_key')

    def test_subunit_upload(self):
        """
        Test that the subunit upload command runs as expected.
        """
        upload_file_path = self.test_path + "/.testrepository/0"
        args = rc.parse_cli_args(
            self.mock_argv(command='upload-subunit', priv_key='rsa_key') +
            ['--keystone-endpoint', 'http://0.0.0.0:5000/v2.0'] +
            [upload_file_path])
        client = rc.RefstackClient(args)
        client.post_results = MagicMock()
        client.upload_subunit()
        expected_json = {
            'duration_seconds': 0,
            'cpid': hashlib.md5('0.0.0.0'.encode('utf-8')).hexdigest(),
            'results': [
                {'name': 'tempest.passed.test'},
                {'name': 'tempest.tagged_passed.test',
                 'uuid': '0146f675-ffbd-4208-b3a4-60eb628dbc5e'}
            ]
        }
        client.post_results.assert_called_with('http://127.0.0.1',
                                               expected_json,
                                               sign_with='rsa_key')

    def test_upload_nonexisting_file(self):
        """
        Test when the file to be uploaded does not exist.
        """
        upload_file_path = self.test_path + "/.testrepository/foo.json"
        args = rc.parse_cli_args(['upload', upload_file_path,
                                  '--url', 'http://api.test.org'])
        client = rc.RefstackClient(args)
        self.assertRaises(SystemExit, client.upload)

    def test_yield_results(self):
        """
        Test the yield_results method, ensuring that results are retrieved.
        """
        args = rc.parse_cli_args(self.mock_argv(command='list'))
        client = rc.RefstackClient(args)
        expected_response = {
            "pagination": {
                "current_page": 1,
                "total_pages": 1
            },
            "results": [
                {
                    "cpid": "42",
                    "created_at": "2015-04-28 13:57:05",
                    "test_id": "1",
                    "url": "http://127.0.0.1:8000/output.html?test_id=1"
                },
                {
                    "cpid": "42",
                    "created_at": "2015-04-28 13:57:05",
                    "test_id": "2",
                    "url": "http://127.0.0.1:8000/output.html?test_id=2"
                }]}

        @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/')
        def refstack_api_mock(url, request):
            return json.dumps(expected_response)

        with httmock.HTTMock(refstack_api_mock):
            results = client.yield_results("http://127.0.0.1")
            self.assertEqual(expected_response['results'], next(results))
            # Since Python3.7 StopIteration exceptions are transformed into
            # RuntimeError (PEP 479):
            # https://docs.python.org/3/whatsnew/3.7.html
            self.assertRaises((StopIteration, RuntimeError), next, results)

    @mock.patch('six.moves.input', side_effect=KeyboardInterrupt)
    @mock.patch('sys.stdout', new_callable=MagicMock)
    def test_list(self, mock_stdout, mock_input):
        args = rc.parse_cli_args(self.mock_argv(command='list'))
        client = rc.RefstackClient(args)
        results = [[{"cpid": "42",
                     "created_at": "2015-04-28 13:57:05",
                     "test_id": "1",
                     "url": "http://127.0.0.1:8000/output.html?test_id=1"},
                    {"cpid": "42",
                     "created_at": "2015-04-28 13:57:05",
                     "test_id": "2",
                     "url": "http://127.0.0.1:8000/output.html?test_id=2"}]]
        mock_results = MagicMock()
        mock_results.__iter__.return_value = results
        client.yield_results = MagicMock(return_value=mock_results)
        client.list()
        self.assertTrue(mock_stdout.write.called)

    def test_sign_pubkey(self):
        """
        Test that the test command will run the tempest script and call
        post_results when the --upload argument is passed in.
        """
        args = rc.parse_cli_args(['sign',
                                  os.path.join(self.test_path, 'rsa_key')])
        client = rc.RefstackClient(args)
        pubkey, signature = client._sign_pubkey()
        self.assertTrue(pubkey.decode('utf8').startswith('ssh-rsa AAAA'))
        self.assertTrue(signature.decode('utf8').startswith('413cb954'))

    def test_set_env_params(self):
        """
        Test that the environment variables are correctly set.
        """
        args = rc.parse_cli_args(self.mock_argv())
        client = rc.RefstackClient(args)
        client.tempest_dir = self.test_path
        client._prep_test()
        conf_dir = os.path.abspath(os.path.dirname(self.conf_file_name))
        conf_file = os.path.basename(self.conf_file_name)
        self.assertEqual(os.environ.get('TEMPEST_CONFIG_DIR'), conf_dir)
        self.assertEqual(os.environ.get('TEMPEST_CONFIG'), conf_file)

    @mock.patch('refstack_client.list_parser.TestListParser.'
                'create_include_list')
    def test_run_tempest_with_empty_test_list(self, mock_include_list):
        """Test that refstack-client can handle an empty test list file."""
        argv = self.mock_argv(verbose='-vv')
        argv.extend(['--test-list', 'foo.txt'])
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        self.mock_data()
        self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        client._get_keystone_config = MagicMock(return_value=self.v2_config)
        client.tempest_dir = self.test_path
        self.patch("os.path.isfile", return_value=True)
        empty_file = tempfile.NamedTemporaryFile()
        mock_include_list.return_value = empty_file.name
        self.assertRaises(SystemExit, client.test)

    def test_run_tempest_with_non_exist_test_list_file(self):
        """Test that refstack-client runs with a nonexistent test list file."""
        argv = self.mock_argv(verbose='-vv')
        argv.extend(['--test-list', 'foo.txt'])
        args = rc.parse_cli_args(argv)
        client = rc.RefstackClient(args)
        self.mock_data()
        self.patch(
            'refstack_client.list_parser.TestListParser._get_tempest_test_ids',
            return_value={'foo': ''})
        self.patch(
            'refstack_client.refstack_client.subprocess.Popen',
            return_value=MagicMock(returncode=0))
        client._get_keystone_config = MagicMock(return_value=self.v2_config)
        client.tempest_dir = self.test_path
        self.assertRaises(IOError, client.test)