Add tests and gating.yaml

Resolves: rhbz#1915766

Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
Adrian Reber 2021-05-07 09:41:11 +02:00
parent 59cd98456b
commit 4d2fd43eab
No known key found for this signature in database
GPG Key ID: 82C9378ED3C4906A
7 changed files with 186 additions and 0 deletions

6
gating.yaml Normal file
View File

@ -0,0 +1,6 @@
--- !Policy
product_versions:
- rhel-9
decision_context: osci_compose_gate
rules:
- !PassingTestCaseRule {test_case_name: osci.brew-build.tier0.functional}

12
tests/Makefile Normal file
View File

@ -0,0 +1,12 @@
all: add_person_cpp
protoc_middleman: addressbook.proto
protoc --cpp_out=. --python_out=. addressbook.proto
add_person_cpp: add_person.cc protoc_middleman
pkg-config --cflags protobuf # fails if protobuf is not installed
c++ -std=c++11 add_person.cc addressbook.pb.cc -o add_person_cpp `pkg-config --cflags --libs protobuf`
clean:
rm -f addressbook_pb2.py addressbook.pb.cc addressbook.pb.h add_person_cpp data
rm -rf __pycache__

55
tests/add_person.cc Normal file
View File

@ -0,0 +1,55 @@
// Based on the examples from the protobuf release tarball
#include <ctime>
#include <fstream>
#include <google/protobuf/util/time_util.h>
#include <iostream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
using google::protobuf::util::TimeUtil;
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 6) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE ID NAME EMAIL PHONE" << endl;
return -1;
}
tutorial::AddressBook address_book;
fstream input(argv[1], ios::in | ios::binary);
tutorial::Person* person = address_book.add_people();
person->set_id(atoi(argv[2]));
person->set_name(argv[3]);
person->set_email(argv[4]);
tutorial::Person::PhoneNumber* phone_number = person->add_phones();
phone_number->set_number(argv[5]);
phone_number->set_type(tutorial::Person::HOME);
*person->mutable_last_updated() = TimeUtil::SecondsToTimestamp(42);
{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}

51
tests/addressbook.proto Normal file
View File

@ -0,0 +1,51 @@
// See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials. They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials
// [START declaration]
syntax = "proto3";
package tutorial;
import "google/protobuf/timestamp.proto";
// [END declaration]
// [START java_declaration]
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
// [END java_declaration]
// [START csharp_declaration]
option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
// [END csharp_declaration]
// [START messages]
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
google.protobuf.Timestamp last_updated = 5;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
// [END messages]

27
tests/list_people.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
# Based on the examples from the protobuf release tarball
import addressbook_pb2
import sys
# Iterates though all people in the AddressBook and prints info about them.
def ListPeople(address_book):
for person in address_book.people:
print(f'{person.id},{person.name},{person.email},{person.phones[0].number}')
# Main procedure: Reads the entire address book from a file and prints all
# the information inside.
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
with open(sys.argv[1], "rb") as f:
address_book.ParseFromString(f.read())
ListPeople(address_book)

17
tests/run-simple-test.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
set -ex
uname -a
make clean all
./add_person_cpp data 13 JustName emailAddress 012344444
OUT=$(./list_people.py data)
if [ "$OUT" != "13,JustName,emailAddress,012344444" ]; then
echo "FAIL"
exit 1
fi
make clean
exit 0

18
tests/tests.yml Normal file
View File

@ -0,0 +1,18 @@
---
- hosts: localhost
roles:
- role: standard-test-basic
tags:
- classic
required_packages:
- make
- gcc-c++
- python3
- protobuf-devel
- protobuf-compiler
- python3-protobuf
- pkgconf-pkg-config
tests:
- simple:
dir: .
run: ./run-simple-test.sh