Skip to content

Commit

Permalink
add ci testing for new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg committed Jun 12, 2023
1 parent 534d82d commit 1fe6382
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 28 deletions.
42 changes: 28 additions & 14 deletions google/cloud/pubsub/samples/pubsub_samples_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "google/cloud/pubsub/samples/pubsub_samples_common.h"
#include "google/cloud/pubsub/testing/random_names.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/project.h"
#include <fstream>
#include <sstream>

Expand Down Expand Up @@ -167,25 +168,38 @@ std::string ReadFile(std::string const& path) {
return std::string{std::istreambuf_iterator<char>{ifs.rdbuf()}, {}};
}

std::string CommitSchemaRevisionsForRollbackSchemaTesting(
google::cloud::pubsub::SchemaServiceClient& schema_admin,
std::pair<std::string, std::string> CommitSchemaWithRevisionsForTesting(
google::cloud::pubsub::SchemaServiceClient& client,
std::string const& project_id, std::string const& schema_id,
std::string const& file) {
std::string const definition = ReadFile(file);
std::string const& schema_file, std::string const& revised_schema_file,
std::string const& type) {
std::string const initial_definition = ReadFile(schema_file);
std::string const revised_definition = ReadFile(revised_schema_file);
auto const schema_type = type == "AVRO"
? google::pubsub::v1::Schema::AVRO
: google::pubsub::v1::Schema::PROTOCOL_BUFFER;

google::pubsub::v1::CreateSchemaRequest create_request;
create_request.set_parent(google::cloud::Project(project_id).FullName());
create_request.set_schema_id(schema_id);
create_request.mutable_schema()->set_type(schema_type);
create_request.mutable_schema()->set_definition(initial_definition);
auto schema = client.CreateSchema(create_request);
if (!schema) throw std::move(schema).status();
std::string const first_revision_id = schema.value().revision_id();

google::pubsub::v1::CommitSchemaRequest request;
google::pubsub::v1::CommitSchemaRequest commit_request;
std::string const name =
google::cloud::pubsub::Schema(project_id, schema_id).FullName();
request.set_name(name);
request.mutable_schema()->set_name(name);
request.mutable_schema()->set_type(google::pubsub::v1::Schema::AVRO);
request.mutable_schema()->set_definition(definition);
auto schema = schema_admin.CommitSchema(request);
if (!schema) throw std::move(schema).status();
std::string first_revision_id = schema.value().revision_id();
schema = schema_admin.CommitSchema(request);
commit_request.set_name(name);
commit_request.mutable_schema()->set_name(name);
commit_request.mutable_schema()->set_type(schema_type);
commit_request.mutable_schema()->set_definition(revised_definition);
schema = client.CommitSchema(commit_request);
if (!schema) throw std::move(schema).status();
return first_revision_id;
std::string const last_revision_id = schema.value().revision_id();

return {first_revision_id, last_revision_id};
}

} // namespace examples
Expand Down
9 changes: 5 additions & 4 deletions google/cloud/pubsub/samples/pubsub_samples_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ std::string RandomSchemaId(google::cloud::internal::DefaultPRNG& generator);

std::string ReadFile(std::string const& path);

// Commit two new schema revision and return the first revision id.
std::string CommitSchemaRevisionsForRollbackSchemaTesting(
google::cloud::pubsub::SchemaServiceClient& schema_admin,
// Commit a schema with a revision and return the first and last revision id.
std::pair<std::string, std::string> CommitSchemaWithRevisionsForTesting(
google::cloud::pubsub::SchemaServiceClient& client,
std::string const& project_id, std::string const& schema_id,
std::string const& file);
std::string const& schema_file, std::string const& revised_schema_file,
std::string const& type);

} // namespace examples
} // namespace pubsub
Expand Down
75 changes: 65 additions & 10 deletions google/cloud/pubsub/samples/samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

namespace {

using ::google::cloud::pubsub::examples::
CommitSchemaRevisionsForRollbackSchemaTesting;
using ::google::cloud::pubsub::examples::CommitSchemaWithRevisionsForTesting;
using ::google::cloud::pubsub::examples::RandomSchemaId;
using ::google::cloud::pubsub::examples::RandomSnapshotId;
using ::google::cloud::pubsub::examples::RandomSubscriptionId;
Expand Down Expand Up @@ -1198,6 +1197,11 @@ void CreateTopicWithSchemaRevisions(
if (topic.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The topic already exists\n";
return;
} else if (topic.status().code() ==
google::cloud::StatusCode::kInvalidArgument) {
std::cout
<< "The first revision id must be older than the last revision id.\n";
return;
}
if (!topic) throw std::move(topic).status();

Expand All @@ -1222,6 +1226,12 @@ void UpdateTopicSchema(google::cloud::pubsub::TopicAdminClient client,
.set_first_revision_id(first_revision_id)
.set_last_revision_id(last_revision_id));

if (topic.status().code() == google::cloud::StatusCode::kInvalidArgument) {
std::cout
<< "The first revision id must be older than the last revision id.\n";
return;
}

if (!topic) throw std::move(topic).status();

std::cout << "The topic was successfully updated: " << topic->DebugString()
Expand Down Expand Up @@ -2121,19 +2131,40 @@ void AutoRunAvro(
std::cout << "\nRunning GetSchema sample" << std::endl;
GetSchema(schema_admin, {project_id, avro_schema_id});

// For testing RollbackSchema, create 2 new schema revisions and rollback to
// the first one. The DeleteSchema call will remove all revisions of the
// schema.
std::string revision_id = CommitSchemaRevisionsForRollbackSchemaTesting(
schema_admin, project_id, avro_schema_id, avro_schema_id);
// For testing commands that require a revision id.
auto avro_revision_schema_id = RandomSchemaId(generator);
auto const avro_revision_topic_id = RandomTopicId(generator);
auto const revision_ids = CommitSchemaWithRevisionsForTesting(
schema_admin, project_id, avro_revision_schema_id,
avro_schema_definition_file, avro_revised_schema_definition_file, "AVRO");
auto const first_revision_id = revision_ids.first;
auto const last_revision_id = revision_ids.second;
std::cout << "\nRunning CreateTopicWithSchemaRevisions sample [avro]"
<< std::endl;
CreateTopicWithSchemaRevisions(
topic_admin_client,
{project_id, avro_revision_topic_id, avro_revision_schema_id, "JSON",
first_revision_id, last_revision_id});
UpdateTopicSchema(topic_admin_client, {project_id, avro_revision_topic_id,
first_revision_id, first_revision_id});

std::cout << "\nRunning GetSchemaRevision sample" << std::endl;
GetSchemaRevision(schema_admin, {project_id, avro_schema_id, revision_id});
GetSchemaRevision(schema_admin,
{project_id, avro_revision_schema_id, first_revision_id});

std::cout << "\nRunning RollbackSchema sample" << std::endl;
RollbackSchema(schema_admin, {project_id, avro_schema_id, revision_id});
RollbackSchema(schema_admin,
{project_id, avro_revision_schema_id, first_revision_id});

std::cout << "\nRunning DeleteSchemaRevision sample" << std::endl;
DeleteSchemaRevision(schema_admin, {project_id, avro_schema_id, revision_id});
DeleteSchemaRevision(
schema_admin, {project_id, avro_revision_schema_id, first_revision_id});

std::cout
<< "\nCleaning up the topic and schema created for testing revisions"
<< std::endl;
DeleteTopic(topic_admin_client, {project_id, avro_revision_topic_id});
DeleteSchema(schema_admin, {project_id, avro_revision_schema_id});

std::cout << "\nRunning ListSchemas() sample" << std::endl;
ListSchemas(schema_admin, {project_id});
Expand Down Expand Up @@ -2215,6 +2246,30 @@ void AutoRunProtobuf(
ValidateMessageNamedSchema(schema_admin,
{project_id, proto_schema_id, proto_message_file});

// For testing commands that require a revision id.
auto proto_revision_schema_id = RandomSchemaId(generator);
auto const proto_revision_topic_id = RandomTopicId(generator);
auto const revision_ids = CommitSchemaWithRevisionsForTesting(
schema_admin, project_id, proto_revision_schema_id,
proto_schema_definition_file, proto_revised_schema_definition_file,
"PROTO");
auto const first_revision_id = revision_ids.first;
auto const last_revision_id = revision_ids.second;
std::cout << "\nRunning CreateTopicWithSchemaRevisions sample [proto]"
<< std::endl;
CreateTopicWithSchemaRevisions(
topic_admin_client,
{project_id, proto_revision_topic_id, proto_revision_schema_id, "BINARY",
first_revision_id, last_revision_id});
UpdateTopicSchema(topic_admin_client, {project_id, proto_revision_topic_id,
first_revision_id, first_revision_id});

std::cout
<< "\nCleaning up the topic and schema created for testing revisions"
<< std::endl;
DeleteTopic(topic_admin_client, {project_id, proto_revision_topic_id});
DeleteSchema(schema_admin, {project_id, proto_revision_schema_id});

std::cout << "\nRunning CreateTopicWithSchema() sample [proto]" << std::endl;
auto const proto_topic_id = RandomTopicId(generator);
CreateTopicWithSchema(topic_admin_client, {project_id, proto_topic_id,
Expand Down

0 comments on commit 1fe6382

Please sign in to comment.