1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6
7from pyfakefs import fake_filesystem_unittest
8
9from dependency_manager import uploader
10
11
12class CloudStorageUploaderTest(fake_filesystem_unittest.TestCase):
13  def setUp(self):
14    self.setUpPyfakefs()
15    self.bucket = 'cloud_storage_bucket'
16    self.local_path = os.path.abspath(os.path.join('path', 'to', 'dependency'))
17    self.fs.CreateFile(self.local_path)
18    self.remote_path = 'config_folder/remote_path'
19
20  def testCloudStorageUploaderMissingData(self):
21    self.assertRaises(ValueError, uploader.CloudStorageUploader,
22                      None, self.remote_path, self.local_path)
23    self.assertRaises(ValueError, uploader.CloudStorageUploader,
24                      self.bucket, None, self.local_path)
25    self.assertRaises(ValueError, uploader.CloudStorageUploader,
26                      self.bucket, self.remote_path, None)
27
28  def testCloudStorageUploaderLocalFileMissing(self):
29    self.fs.RemoveObject(self.local_path)
30    self.assertRaises(ValueError, uploader.CloudStorageUploader,
31                      self.bucket, self.remote_path, self.local_path)
32
33  def testCloudStorageUploaderCreation(self):
34    upload_data = uploader.CloudStorageUploader(
35        self.bucket, self.remote_path, self.local_path)
36    expected_bucket = self.bucket
37    expected_remote_path = self.remote_path
38    expected_cs_backup_path = '%s.old' % expected_remote_path
39    expected_local_path = self.local_path
40    self.assertEqual(expected_bucket, upload_data._cs_bucket)
41    self.assertEqual(expected_remote_path, upload_data._cs_remote_path)
42    self.assertEqual(expected_local_path, upload_data._local_path)
43    self.assertEqual(expected_cs_backup_path, upload_data._cs_backup_path)
44
45  def testCloudStorageUploaderEquality(self):
46    upload_data = uploader.CloudStorageUploader(
47        self.bucket, self.remote_path, self.local_path)
48    upload_data_exact = uploader.CloudStorageUploader(
49        self.bucket, self.remote_path, self.local_path)
50    upload_data_equal = uploader.CloudStorageUploader(
51        'cloud_storage_bucket',
52        'config_folder/remote_path',
53        os.path.abspath(os.path.join('path', 'to', 'dependency')))
54    self.assertEqual(upload_data, upload_data)
55    self.assertEqual(upload_data, upload_data_exact)
56    self.assertEqual(upload_data_exact, upload_data)
57    self.assertEqual(upload_data, upload_data_equal)
58    self.assertEqual(upload_data_equal, upload_data)
59
60
61  def testCloudStorageUploaderInequality(self):
62    new_local_path = os.path.abspath(os.path.join('new', 'local', 'path'))
63    self.fs.CreateFile(new_local_path)
64    new_bucket = 'new_bucket'
65    new_remote_path = 'new_remote/path'
66
67    upload_data = uploader.CloudStorageUploader(
68        self.bucket, self.remote_path, self.local_path)
69    upload_data_all_different = uploader.CloudStorageUploader(
70        new_bucket, new_remote_path, new_local_path)
71    upload_data_different_bucket = uploader.CloudStorageUploader(
72        new_bucket, self.remote_path, self.local_path)
73    upload_data_different_remote_path = uploader.CloudStorageUploader(
74        self.bucket, new_remote_path, self.local_path)
75    upload_data_different_local_path = uploader.CloudStorageUploader(
76        self.bucket, self.remote_path, new_local_path)
77
78    self.assertNotEqual(upload_data, 'a string!')
79    self.assertNotEqual(upload_data, 0)
80    self.assertNotEqual(upload_data, 2354)
81    self.assertNotEqual(upload_data, None)
82    self.assertNotEqual(upload_data, upload_data_all_different)
83    self.assertNotEqual(upload_data_all_different, upload_data)
84    self.assertNotEqual(upload_data, upload_data_different_bucket)
85    self.assertNotEqual(upload_data_different_bucket, upload_data)
86    self.assertNotEqual(upload_data, upload_data_different_remote_path)
87    self.assertNotEqual(upload_data_different_remote_path, upload_data)
88    self.assertNotEqual(upload_data, upload_data_different_local_path)
89    self.assertNotEqual(upload_data_different_local_path, upload_data)
90
91  #TODO: write unittests for upload and rollback
92