Beispiel #1
0
 def testOpenStream(self):
     """Tests OpenStream."""
     artifact = base.BaseArtifact('artifact')
     with self.assertRaises(NotImplementedError) as err:
         artifact.OpenStream()
     expected_err_message = '_GetStream() is not implemented in BaseArtifact'
     self.assertEqual(str(err.exception), expected_err_message)
Beispiel #2
0
    def testReadableSize(self):
        """Tests ReadableSize() method."""
        artifact_name = 'artifact'
        artifact = base.BaseArtifact(artifact_name)
        self.assertEqual(artifact.readable_size, 'Unknown size')

        #pylint: disable=protected-access
        artifact._size = 12345
        self.assertEqual(artifact.readable_size, '12.1KiB')

        artifact._size = 1234567
        self.assertEqual(artifact.readable_size, '1.2MiB')

        artifact._size = 123456789
        self.assertEqual(artifact.readable_size, '117.7MiB')

        artifact._size = 12345678901
        self.assertEqual(artifact.readable_size, '11.5GiB')

        artifact._size = 1023 * 1024 * 1024 * 1024
        self.assertEqual(artifact.readable_size, '1,023.0GiB')

        artifact._size = 1234567890123
        self.assertEqual(artifact.readable_size, '1.1TiB')

        artifact._size = 12345678901234567890
        self.assertEqual(artifact.readable_size, '10,965.2PiB')
Beispiel #3
0
  def testReadableSize(self):
    artifact_name = 'artifact'
    artifact = base.BaseArtifact(artifact_name)
    self.assertEqual(artifact.readable_size, 'Unknown size')

    artifact._size = 12345
    self.assertEqual(artifact.readable_size, '12.1KiB')

    artifact._size = 1234567
    self.assertEqual(artifact.readable_size, '1.2MiB')

    artifact._size = 123456789
    self.assertEqual(artifact.readable_size, '117.7MiB')

    artifact._size = 12345678901
    self.assertEqual(artifact.readable_size, '11.5GiB')

    artifact._size = 1023 * 1024 * 1024 * 1024
    self.assertEqual(artifact.readable_size, '1,023.0GiB')

    artifact._size = 1234567890123
    self.assertEqual(artifact.readable_size, '1.1TiB')

    artifact._size = 12345678901234567890
    self.assertEqual(artifact.readable_size, '10,965.2PiB')
Beispiel #4
0
  def testInstantiate(self):
    artifact_name = 'artifact'
    artifact = base.BaseArtifact(artifact_name)

    self.assertEqual(artifact.size, 0)
    self.assertEqual(artifact.name, artifact_name)
    expected_remote_path = 'Base/artifact'
    self.assertEqual(artifact.remote_path, expected_remote_path)
Beispiel #5
0
  def testFailUploadNoRetry(self, patched_storage, patched_getstream):
    patched_getstream.return_value = BytesIO(b'fake_content')
    patched_storage.side_effect = errors.ForensicateError('random_error')

    test_artifact = base.BaseArtifact('test_artifact')

    uploader_object = uploader.GCSUploader(
        'gs://fake_bucket/', 'no_keyfile', 'client_id', FakeStampManager())
    uploader_object._boto_configured = True

    with self.assertRaises(errors.ForensicateError):
      uploader_object._UploadStream(
          test_artifact.OpenStream(), 'gs://fake_bucket/remote/path')
Beispiel #6
0
    def testFailUploadRetryWorthy(self, patched_storage, patched_getstream):
        patched_getstream.return_value = StringIO('fake_content')
        patched_storage.side_effect = boto.exception.GSDataError('boom')

        test_artifact = base.BaseArtifact('test_artifact')

        uploader_object = uploader.GCSUploader('gs://fake_bucket/',
                                               'no_keyfile', 'client_id',
                                               FakeStampManager())
        uploader_object._boto_configured = True

        with self.assertRaises(errors.RetryableError):
            uploader_object._UploadStream(test_artifact.OpenStream(),
                                          'gs://fake_bucket/remote/path')
Beispiel #7
0
  def testUploadArtifact(self, patched_getstream):
    test_artifact = base.BaseArtifact('test_artifact')
    patched_getstream.return_value = BytesIO(b'fake_content')

    uploader_object = FakeGCSUploader(self.gcs_url)

    expected_resultpath = (
        'bucket_name/some/where/20171012-135619/fake_uuid/Base/'
        'test_artifact')
    expected_uploaded_streams = {
        ('bucket_name/some/where/20171012-135619/fake_uuid/'
         'Base/test_artifact'): 'fake_content',
        ('bucket_name/some/where/20171012-135619/fake_uuid/'
         'stamp.json'): json.dumps(FAKE_STAMP._asdict())
    }

    result_path = uploader_object.UploadArtifact(test_artifact)
    self.assertEqual(result_path, expected_resultpath)
    self.assertEqual(
        uploader_object._uploaded_streams, expected_uploaded_streams)
Beispiel #8
0
  def testUploadArtifact(self, patched_getstream):
    test_artifact = base.BaseArtifact('test_artifact')
    patched_getstream.return_value = BytesIO(b'fake_content')

    uploader_object = uploader.LocalCopier(
        self.temp_dir, FakeStampManager(), stamp=FAKE_STAMP)

    expected_artifact_path = (
        self.temp_dir+'/20171012-135619/fake_uuid/Base/test_artifact')
    expected_artifact_content = 'fake_content'

    expected_stamp_path = (
        self.temp_dir+'/20171012-135619/fake_uuid/stamp.json')
    expected_stamp_content = json.dumps(FAKE_STAMP._asdict())

    result_path = uploader_object.UploadArtifact(test_artifact)

    self.assertEqual(expected_artifact_path, result_path)
    with open(result_path, 'r') as artifact_file:
      self.assertEqual(expected_artifact_content, artifact_file.read())

    with open(expected_stamp_path, 'r') as stamp_file:
      self.assertEqual(expected_stamp_content, stamp_file.read())