# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for the fetch_builds module."""

import errno
import unittest

# The third-party mock module is expected to be available in PYTHONPATH.
import mock

import fetch_build


# The tests below test private functions (W0212).
# Some methods don't reference self because they use the mock module (R0201).
# pylint: disable=R0201,W0212
class FetchBuildTest(unittest.TestCase):

  def setUp(self):
    # Mocks of the os and bisect_utils modules are used in the methods below.
    cloud_storage_patcher = mock.patch('fetch_build.cloud_storage')
    self.mock_cloud_storage = cloud_storage_patcher.start()
    self.addCleanup(cloud_storage_patcher.stop)

  @mock.patch('fetch_build.os.path.exists')
  def test_FetchFromCloudStorage_FileFound(self, mock_os_path_exists):
    self.mock_cloud_storage.Exists.return_value = True
    mock_os_path_exists.return_value = True
    local_path = fetch_build.FetchFromCloudStorage(
        'my_bucket', 'remote/foo.zip', 'local')
    self.assertEqual('local/foo.zip', local_path)
    self.mock_cloud_storage.Get.assert_called_with(
        'my_bucket', 'remote/foo.zip', 'local/foo.zip')

  def test_FetchFromCloudStorage_FileNotFound(self):
    self.mock_cloud_storage.Exists.return_value = False
    local_path = fetch_build.FetchFromCloudStorage(
        'my_bucket', 'remote/foo.zip', 'local')
    self.assertIsNone(local_path)
    self.assertFalse(self.mock_cloud_storage.Get.called)


class BuildArchiveTest(unittest.TestCase):

  def test_CreatePerfBuildArchive(self):
    archive = fetch_build.BuildArchive.Create(fetch_build.PERF_BUILDER)
    self.assertEqual('chrome-perf', archive.BucketName())
    self.assertTrue(isinstance(archive, fetch_build.PerfBuildArchive))

  def test_CreateFullBuildArchive(self):
    archive = fetch_build.BuildArchive.Create(fetch_build.FULL_BUILDER)
    archive._platform = 'linux'
    self.assertEqual('chromium-linux-archive', archive.BucketName())
    self.assertTrue(isinstance(archive, fetch_build.FullBuildArchive))

  def test_BuildArchive_NonExistentType(self):
    self.assertRaises(
        NotImplementedError, fetch_build.BuildArchive.Create, 'other')

  def test_FullBuildArchive_Linux(self):
    archive = fetch_build.FullBuildArchive()
    archive._platform = 'linux'
    self.assertEqual('chromium-linux-archive', archive.BucketName())
    self.assertEqual(
        'chromium.linux/Linux Builder/full-build-linux_1234567890abcdef.zip',
        archive.FilePath('1234567890abcdef'))

  def test_FullBuildArchive_Android(self):
    archive = fetch_build.FullBuildArchive()
    archive._platform = 'android'
    self.assertEqual('chromium-android', archive.BucketName())
    self.assertEqual(
        'android_main_rel/full-build-linux_1234567890abcdef.zip',
        archive.FilePath('1234567890abcdef'))

  def test_FullBuildArchive_Linux_BuilderName(self):
    archive = fetch_build.FullBuildArchive()
    archive._platform = 'linux'
    self.assertEqual('linux_full_bisect_builder', archive.GetBuilderName())

  def test_FullBuildArchive_Windows_BuildTime(self):
    archive = fetch_build.FullBuildArchive()
    archive._platform = 'win'
    self.assertEqual(14400, archive.GetBuilderBuildTime())

  def test_PerfBuildArchive_Linux(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'linux'
    self.assertEqual('chrome-perf', archive.BucketName())
    self.assertEqual(
        'Linux Builder/full-build-linux_1234567890abcdef.zip',
        archive.FilePath('1234567890abcdef'))

  def test_PerfBuildArchive_Android(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'android'
    self.assertEqual('chrome-perf', archive.BucketName())
    self.assertEqual(
        'android_perf_rel/full-build-linux_123456.zip',
        archive.FilePath('123456'))

  def test_PerfBuildArchive_AndroidArm64(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'android_arm64'
    self.assertEqual('chrome-perf', archive.BucketName())
    self.assertEqual(
        'android_perf_rel_arm64/full-build-linux_123456.zip',
        archive.FilePath('123456'))

  def test_PerfBuildArchive_64BitWindows(self):
    archive = fetch_build.PerfBuildArchive(target_arch='x64')
    archive._platform = 'win64'
    self.assertEqual('chrome-perf', archive.BucketName())
    self.assertEqual(
        'Win x64 Builder/full-build-win32_123456.zip',
        archive.FilePath('123456'))

  def test_PerfBuildArchive_WithDepsPatchSha(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'linux'
    self.assertEqual(
        'Linux Builder/full-build-linux_123456'
        '_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.zip',
        archive.FilePath(123456, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))

  def test_PerfBuildArchive_64BitWindows_BuilderName(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'win64'
    self.assertEqual('win_x64_perf_bisect_builder', archive.GetBuilderName())

  def test_PerfBuildArchive_64BitWindows_BuildTime(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'win64'
    self.assertEqual(14400, archive.GetBuilderBuildTime())

  def test_PerfBuildArchive_Windows_BuilderName(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'win'
    self.assertEqual('win_perf_bisect_builder', archive.GetBuilderName())

  def test_PerfBuildArchive_Windows_BuildTime(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'win'
    self.assertEqual(14400, archive.GetBuilderBuildTime())

  def test_PerfBuildArchive_Linux_BuilderName(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'linux'
    self.assertEqual('linux_perf_bisect_builder', archive.GetBuilderName())

  def test_PerfBuildArchive_Linux_BuildTime(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'linux'
    self.assertEqual(14400, archive.GetBuilderBuildTime())

  def test_PerfBuildArchive_Android_BuilderName(self):
    archive = fetch_build.PerfBuildArchive()
    archive._platform = 'android'
    self.assertEqual('android_perf_bisect_builder', archive.GetBuilderName())

  def test_PerfBuildArchive_Android_BuildTime(self):
    archive = fetch_build.PerfBuildArchive()