<commit_before>// Copyright 2020 Google LLC
//
// 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
//
//     https://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.

#include "google/cloud/storage/client.h"
#include "google/cloud/storage/testing/object_integration_test.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/status.h"
#include "google/cloud/status_or.h"
#include "google/cloud/testing_util/scoped_environment.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::ScopedEnvironment;
using ::google::cloud::testing_util::StatusIs;
using ::testing::HasSubstr;
using ::testing::Not;

using ErrorParsingIntegrationTest =
    ::google::cloud::storage::testing::ObjectIntegrationTest;

TEST_F(ErrorParsingIntegrationTest, FailureContainsErrorInfo) {
  // TODO(#9947) - enable with new implementation once fixed.
  ScopedEnvironment env("GOOGLE_CLOUD_CPP_STORAGE_USE_LEGACY_HTTP", "any");
  StatusOr<Client> client = MakeIntegrationTestClient();
  ASSERT_STATUS_OK(client);

  auto object_name = MakeRandomObjectName();

  // Start a resumable upload and finalize the upload.
  auto insert = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
                                     IfGenerationMatch(0));
  ASSERT_THAT(insert, StatusIs(StatusCode::kOk));
  ScheduleForDelete(*insert);

  // Overwrite the object.
  insert = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
                                IfGenerationMatch(0));
  ASSERT_THAT(insert, Not(StatusIs(StatusCode::kOk)));
  if (UsingEmulator()) return;
  EXPECT_THAT(insert.status().message(),
              HasSubstr("pre-conditions you specified did not hold"));
}

}  // anonymous namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
}  // namespace storage
}  // namespace cloud
}  // namespace google
<commit_msg>test(storage): check for useful `ErrorInfo` (#9974)<commit_after>// Copyright 2020 Google LLC
//
// 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
//
//     https://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.

#include "google/cloud/storage/client.h"
#include "google/cloud/storage/testing/object_integration_test.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/status.h"
#include "google/cloud/status_or.h"
#include "google/cloud/testing_util/scoped_environment.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::ScopedEnvironment;
using ::google::cloud::testing_util::StatusIs;
using ::testing::HasSubstr;
using ::testing::IsEmpty;
using ::testing::Not;

using ErrorParsingIntegrationTest =
    ::google::cloud::storage::testing::ObjectIntegrationTest;

TEST_F(ErrorParsingIntegrationTest, FailureContainsErrorInfo) {
  // TODO(#9947) - enable with new implementation once fixed.
  ScopedEnvironment env("GOOGLE_CLOUD_CPP_STORAGE_USE_LEGACY_HTTP", "any");
  StatusOr<Client> client = MakeIntegrationTestClient();
  ASSERT_STATUS_OK(client);

  auto object_name = MakeRandomObjectName();

  // Start a resumable upload and finalize the upload.
  auto insert = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
                                     IfGenerationMatch(0));
  ASSERT_THAT(insert, StatusIs(StatusCode::kOk));
  ScheduleForDelete(*insert);

  // Overwrite the object.
  insert = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
                                IfGenerationMatch(0));
  ASSERT_THAT(insert, Not(StatusIs(StatusCode::kOk)));
  if (UsingEmulator() || UsingGrpc()) return;
  EXPECT_THAT(insert.status().message(),
              HasSubstr("pre-conditions you specified did not hold"));
  auto const& error_info = insert.status().error_info();
  EXPECT_THAT(error_info.reason(), Not(IsEmpty()));
  EXPECT_THAT(error_info.domain(), Not(IsEmpty()));
  EXPECT_THAT(error_info.metadata(), Not(IsEmpty()));
}

}  // anonymous namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
}  // namespace storage
}  // namespace cloud
}  // namespace google

{'fraction_non_alphanumeric': 0.11569767441860465, 'fraction_numerical': 0.006201550387596899, 'mean_word_length': 4.646608315098468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319148', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1483, 'n_words': 384}