1# Copyright (C) 2024 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from unittest.mock import patch
16from atest import constants
17from atest.logstorage import logstorage_utils
18from pyfakefs import fake_filesystem_unittest
19
20
21class LogstorageUtilsTest(fake_filesystem_unittest.TestCase):
22
23  def setUp(self):
24    super().setUp()
25    self.setUpPyfakefs()
26
27  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
28  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
29  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
30  def test_is_upload_enabled_request_upload_returns_True(self):
31    res = logstorage_utils.is_upload_enabled(self._get_request_upload_args())
32
33    self.assertTrue(res)
34
35  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
36  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
37  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
38  def test_is_upload_enabled_disable_upload_returns_False(self):
39    res = logstorage_utils.is_upload_enabled(self._get_disable_upload_args())
40
41    self.assertFalse(res)
42
43  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
44  @patch('atest.constants.CREDENTIAL_FILE_NAME', None)
45  @patch('atest.constants.TOKEN_FILE_PATH', None)
46  def test_is_upload_enabled_missing_credentials_returns_False(self):
47    res = logstorage_utils.is_upload_enabled(self._get_request_upload_args())
48
49    self.assertFalse(res)
50
51  @patch('atest.constants.GTF_TARGETS', {})
52  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
53  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
54  def test_is_upload_enabled_missing_google_tradefed_returns_False(self):
55    res = logstorage_utils.is_upload_enabled(self._get_request_upload_args())
56
57    self.assertFalse(res)
58
59  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
60  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
61  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
62  def test_is_upload_enabled_previously_requested_returns_True(self):
63    logstorage_utils.is_upload_enabled(self._get_request_upload_args())
64
65    res = logstorage_utils.is_upload_enabled(
66        self._get_unspecified_upload_args()
67    )
68
69    self.assertTrue(res)
70
71  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
72  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
73  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
74  def test_is_upload_enabled_previously_disabled_returns_False(self):
75    logstorage_utils.is_upload_enabled(self._get_request_upload_args())
76    logstorage_utils.is_upload_enabled(self._get_disable_upload_args())
77
78    res = logstorage_utils.is_upload_enabled(
79        self._get_unspecified_upload_args()
80    )
81
82    self.assertFalse(res)
83
84  @patch('atest.constants.GTF_TARGETS', {'google-tradefed'})
85  @patch('atest.constants.CREDENTIAL_FILE_NAME', 'creds.txt')
86  @patch('atest.constants.TOKEN_FILE_PATH', 'token.txt')
87  def test_is_upload_enabled_never_requested_returns_False(self):
88    res = logstorage_utils.is_upload_enabled(
89        self._get_unspecified_upload_args()
90    )
91
92    self.assertFalse(res)
93
94  def _get_unspecified_upload_args(self):
95    """Returns arg dict with no upload flag."""
96    return {}
97
98  def _get_disable_upload_args(self):
99    """Returns arg dict with disable upload flag."""
100    return {constants.DISABLE_UPLOAD_RESULT: True}
101
102  def _get_request_upload_args(self):
103    """Returns arg dict with request upload flag."""
104    return {constants.REQUEST_UPLOAD_RESULT: True}
105