<commit_before># -*- coding: utf-8 -*-
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration tests for setmeta command."""

from __future__ import absolute_import

from gslib.cs_api_map import ApiSelector
import gslib.tests.testcase as testcase
from gslib.tests.testcase.integration_testcase import SkipForS3
from gslib.tests.util import ObjectToURI as suri
from gslib.util import Retry
from gslib.util import UTF8


class TestSetMeta(testcase.GsUtilIntegrationTestCase):
  """Integration tests for setmeta command."""

  def test_initial_metadata(self):
    """Tests copying file to an object with metadata."""
    objuri = suri(self.CreateObject(contents='foo'))
    inpath = self.CreateTempFile()
    ct = 'image/gif'
    self.RunGsUtil(['-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
                    '-h', 'Content-Type:%s' % ct, 'cp', inpath, objuri])
    # Use @Retry as hedge against bucket listing eventual consistency.
    @Retry(AssertionError, tries=3, timeout_secs=1)
    def _Check1():
      stdout = self.RunGsUtil(['ls', '-L', objuri], return_stdout=True)
      self.assertRegexpMatches(stdout, r'Content-Type:\s+%s' % ct)
      self.assertRegexpMatches(stdout, r'xyz:\s+abc')
    _Check1()

  def test_overwrite_existing(self):
    """Tests overwriting an object's metadata."""
    objuri = suri(self.CreateObject(contents='foo'))
    inpath = self.CreateTempFile()
    self.RunGsUtil(['-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
                    '-h', 'Content-Type:image/gif', 'cp', inpath, objuri])
    self.RunGsUtil(['setmeta', '-h', 'Content-Type:text/html', '-h',
                    'x-%s-meta-xyz' % self.provider_custom_meta, objuri])
    # Use @Retry as hedge against bucket listing eventual consistency.
    @Retry(AssertionError, tries=3, timeout_secs=1)
    def _Check1():
      stdout = self.RunGsUtil(['ls', '-L', objuri], return_stdout=True)
      self.assertRegexpMatches(stdout, r'Content-Type:\s+text/html')
      self.assertNotIn('xyz', stdout)
    _Check1()

  @SkipForS3('Preconditions not supported for s3 objects')
  def test_generation_precondition(self):
    """Tests setting metadata with a generation precondition."""
    object_uri = self.CreateObject(contents='foo')
    generation = object_uri.generation
    ct = 'image/gif'
    stderr = self.RunGsUtil(
        ['-h', 'x-goog-if-generation-match:%d' % (long(generation) + 1),
         'setmeta', '-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
         '-h', 'Content-Type:%s' % ct, suri(object_uri)], expected_status=1,
        return_stderr=True)
    if self.test_api == ApiSelector.XML:
      # XML API returns a 400 if the generation does not match some valid one.
      self.assertIn('BadRequestException', stderr)
    else:
      self.assertIn('Precondition', stderr)
<commit_msg>Fix test_setmeta for XML API for different service behavior<commit_after># -*- coding: utf-8 -*-
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration tests for setmeta command."""

from __future__ import absolute_import

import gslib.tests.testcase as testcase
from gslib.tests.testcase.integration_testcase import SkipForS3
from gslib.tests.util import ObjectToURI as suri
from gslib.util import Retry
from gslib.util import UTF8


class TestSetMeta(testcase.GsUtilIntegrationTestCase):
  """Integration tests for setmeta command."""

  def test_initial_metadata(self):
    """Tests copying file to an object with metadata."""
    objuri = suri(self.CreateObject(contents='foo'))
    inpath = self.CreateTempFile()
    ct = 'image/gif'
    self.RunGsUtil(['-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
                    '-h', 'Content-Type:%s' % ct, 'cp', inpath, objuri])
    # Use @Retry as hedge against bucket listing eventual consistency.
    @Retry(AssertionError, tries=3, timeout_secs=1)
    def _Check1():
      stdout = self.RunGsUtil(['ls', '-L', objuri], return_stdout=True)
      self.assertRegexpMatches(stdout, r'Content-Type:\s+%s' % ct)
      self.assertRegexpMatches(stdout, r'xyz:\s+abc')
    _Check1()

  def test_overwrite_existing(self):
    """Tests overwriting an object's metadata."""
    objuri = suri(self.CreateObject(contents='foo'))
    inpath = self.CreateTempFile()
    self.RunGsUtil(['-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
                    '-h', 'Content-Type:image/gif', 'cp', inpath, objuri])
    self.RunGsUtil(['setmeta', '-h', 'Content-Type:text/html', '-h',
                    'x-%s-meta-xyz' % self.provider_custom_meta, objuri])
    # Use @Retry as hedge against bucket listing eventual consistency.
    @Retry(AssertionError, tries=3, timeout_secs=1)
    def _Check1():
      stdout = self.RunGsUtil(['ls', '-L', objuri], return_stdout=True)
      self.assertRegexpMatches(stdout, r'Content-Type:\s+text/html')
      self.assertNotIn('xyz', stdout)
    _Check1()

  @SkipForS3('Preconditions not supported for s3 objects')
  def test_generation_precondition(self):
    """Tests setting metadata with a generation precondition."""
    object_uri = self.CreateObject(contents='foo')
    generation = object_uri.generation
    ct = 'image/gif'
    stderr = self.RunGsUtil(
        ['-h', 'x-goog-if-generation-match:%d' % (long(generation) + 1),
         'setmeta', '-h', 'x-%s-meta-xyz:abc' % self.provider_custom_meta,
         '-h', 'Content-Type:%s' % ct, suri(object_uri)], expected_status=1,
        return_stderr=True)
    self.assertIn('Precondition', stderr)

{'fraction_non_alphanumeric': 0.1294493459081229, 'fraction_numerical': 0.0074536051110435046, 'mean_word_length': 3.8488200589970503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3072614', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2005, 'n_words': 562}