1#!/usr/bin/env python
2#
3# Copyright (C) 2018 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
17import mock
18import unittest
19
20from vts.utils.python.gcs import gcs_utils
21
22
23def simple_GetGcloudPath():
24    """mock function created for _GetGcloudPath"""
25    return "gcloud"
26
27
28def simple_ExecuteOneShellCommand(input_string):
29    """mock function created for ExecuteOneShellCommand"""
30    std_out = "this is standard output"
31    std_err = "this is standard error"
32    return_code = 0
33    return std_out, std_err, return_code
34
35
36class GcsUtilsTest(unittest.TestCase):
37    """Unit tests for gcs_utils module"""
38
39    def SetUp(self):
40        """Setup tasks"""
41        self.category = "category_default"
42        self.name = "name_default"
43
44    def testInitialization(self):
45        """Tests the initilization of a GcsUtils object"""
46        user_params = {"service_key_json_path": "key.json"}
47        _gcs_utils = gcs_utils.GcsUtils(user_params)
48        self.assertEqual(_gcs_utils.service_key_json_path, "key.json")
49
50    @mock.patch(
51        'vts.utils.python.gcs.gcs_utils.GcsUtils.GetGcloudPath',
52        side_effect=simple_GetGcloudPath)
53    @mock.patch(
54        'vts.utils.python.common.cmd_utils.ExecuteOneShellCommand',
55        side_effect=simple_ExecuteOneShellCommand)
56    def testGetGcloudAuth(self, simple_ExecuteOneShellCommand,
57                          simeple_GetGCloudPath):
58        """Tests the GetGcloudAuth function"""
59        user_params = {"service_key_json_path": "key.json"}
60        _gcs_utils = gcs_utils.GcsUtils(user_params)
61        _gcs_utils.GetGcloudAuth()
62        simple_ExecuteOneShellCommand.assert_called_with(
63            "gcloud auth activate-service-account --key-file key.json")
64
65    @mock.patch(
66        'vts.utils.python.common.cmd_utils.ExecuteOneShellCommand',
67        side_effect=simple_ExecuteOneShellCommand)
68    def testGetGcloudPath(self, simple_ExecuteOneShellCommand):
69        """Tests the GetGcloudPath static function"""
70        result = gcs_utils.GcsUtils.GetGcloudPath()
71        simple_ExecuteOneShellCommand.assert_called_with("which gcloud")
72        self.assertEqual(result, "this is standard output")
73
74    @mock.patch(
75        'vts.utils.python.common.cmd_utils.ExecuteOneShellCommand',
76        side_effect=simple_ExecuteOneShellCommand)
77    def testGetGsutilPath(self, simple_ExecuteOneShellCommand):
78        """Tests the GetGsutilPath static function"""
79        result = gcs_utils.GcsUtils.GetGsutilPath()
80        simple_ExecuteOneShellCommand.assert_called_with("which gsutil")
81        self.assertEqual(result, "this is standard output")
82
83
84if __name__ == "__main__":
85    unittest.main()
86