1#!/usr/bin/env python3
2#
3# Copyright 2021, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for atest_gcp_utils."""
18
19import os
20from pathlib import Path
21import tempfile
22import unittest
23from unittest import mock
24
25from atest import constants
26from atest.logstorage import atest_gcp_utils
27
28
29class AtestGcpUtilsUnittests(unittest.TestCase):
30  """Unit tests for atest_gcp_utils.py"""
31
32  @mock.patch.object(atest_gcp_utils, '_prepare_data')
33  @mock.patch.object(atest_gcp_utils, 'fetch_credential')
34  def test_do_upload_flow(self, mock_request, mock_prepare):
35    """test do_upload_flow method."""
36    fake_extra_args = {}
37    fake_creds = mock.Mock()
38    fake_creds.token_response = {'access_token': 'fake_token'}
39    mock_request.return_value = fake_creds
40    fake_inv = {'invocationId': 'inv_id'}
41    fake_workunit = {'id': 'workunit_id'}
42    fake_local_build_id = 'L1234567'
43    fake_build_target = 'build_target'
44    mock_prepare.return_value = (
45        fake_inv,
46        fake_workunit,
47        fake_local_build_id,
48        fake_build_target,
49    )
50    fake_build_client_creator = lambda cred: None
51    constants.TOKEN_FILE_PATH = tempfile.NamedTemporaryFile().name
52    creds, inv = atest_gcp_utils.do_upload_flow(
53        fake_extra_args, fake_build_client_creator
54    )
55    self.assertEqual(fake_creds, creds)
56    self.assertEqual(fake_inv, inv)
57    self.assertEqual(
58        fake_extra_args[constants.INVOCATION_ID], fake_inv['invocationId']
59    )
60    self.assertEqual(
61        fake_extra_args[constants.WORKUNIT_ID], fake_workunit['id']
62    )
63    self.assertEqual(
64        fake_extra_args[constants.LOCAL_BUILD_ID], fake_local_build_id
65    )
66    self.assertEqual(fake_extra_args[constants.BUILD_TARGET], fake_build_target)
67
68    mock_request.return_value = None
69    creds, inv = atest_gcp_utils.do_upload_flow(
70        fake_extra_args, fake_build_client_creator
71    )
72    self.assertEqual(None, creds)
73    self.assertEqual(None, inv)
74