static int TestConnectivity() { using namespace test; Porto::Connection api; std::vector<std::string> containers; ExpectApiSuccess(api.List(containers)); std::string name = "a"; ExpectApiSuccess(api.Create(name)); ExpectApiSuccess(api.Destroy(name)); return 0; }
void AsRoot(Porto::Connection &api) { api.Close(); ExpectEq(seteuid(0), 0); ExpectEq(setegid(0), 0); ExpectEq(setgroups(0, nullptr), 0); }
void ExpectApi(Porto::Connection &api, int ret, int exp, const char *where) { std::stringstream ss; if (ret == exp) return; int err; std::string msg; api.GetLastError(err, msg); TError error((rpc::EError)err, msg); ss << "Got error from libporto: " << error << " (" << ret << " != " << exp << ") at " << where; throw ss.str(); }
void WaitPortod(Porto::Connection &api, int times) { Say() << "Waiting for portod startup" << std::endl; std::vector<std::string> clist; do { if (times-- <= 0) break; usleep(1000000); } while (api.List(clist) != 0); if (times <= 0) throw std::string("Waited too long for portod startup"); }
void WaitState(Porto::Connection &api, const std::string &name, const std::string &state, int sec) { Say() << "Waiting for " << name << " to be in state " << state << std::endl; int times = sec * 10; std::string ret; do { if (times-- <= 0) break; usleep(100000); (void)api.GetData(name, "state", ret); } while (ret != state); if (times <= 0) throw std::string("Waited too long for task to change state"); }
void TestDaemon(Porto::Connection &api) { struct dirent **lst; int pid; AsRoot(api); api.Close(); sleep(1); Say() << "Make sure portod-slave doesn't have zombies" << std::endl; pid = ReadPid(config().slave_pid().path()); ExpectEq(ChildrenNum(pid), 0); Say() << "Make sure portod-slave doesn't have invalid FDs" << std::endl; std::string path = ("/proc/" + std::to_string(pid) + "/fd"); // when sssd is running getgrnam opens unix socket to read database int sssFd = 0; if (WordCount("/etc/nsswitch.conf", "sss")) sssFd = 3; /** * . * .. * 0 (stdin) * 1 (stdout) * 128 (event pipe) * 129 (ack pipe) * 130 (rpc socket) * 2 (stderr) * 3 (portod.log) * 4 (epoll) * 5 (host netlink) * 6 (signalfd) */ int nr = scandir(path.c_str(), &lst, NULL, alphasort); PrintFds(path, lst, nr); ExpectLessEq(nr, 12 + sssFd); ExpectLessEq(12, nr); Say() << "Make sure portod-master doesn't have zombies" << std::endl; pid = ReadPid(config().master_pid().path()); ExpectEq(ChildrenNum(pid), 1); Say() << "Make sure portod-master doesn't have invalid FDs" << std::endl; Say() << "Number of portod-master fds=" << nr << std::endl; path = ("/proc/" + std::to_string(pid) + "/fd"); /** * . * .. * 0 (stdin) * 1 (stdout) * 130 (rpc socket) * 2 (stderr) * 3 (portoloop.log) * 4 (epoll) * 6 (event pipe) * 7 (ack pipe) * 9 (signalfd) */ nr = scandir(path.c_str(), &lst, NULL, alphasort); PrintFds(path, lst, nr); ExpectLessEq(nr, 11 + sssFd); ExpectLessEq(11, nr); Say() << "Check portod-master queue size" << std::endl; std::string v; ExpectApiSuccess(api.GetData("/", "porto_stat[queued_statuses]", v)); Expect(v == std::to_string(0)); Say() << "Check portod-slave queue size" << std::endl; ExpectApiSuccess(api.GetData("/", "porto_stat[queued_events]", v)); Expect(v != std::to_string(0)); // RotateLogs // TODO: check rtnl classes }
void WaitContainer(Porto::Connection &api, const std::string &name, int sec) { std::string who; ExpectApiSuccess(api.WaitContainers({name}, who, sec)); ExpectEq(who, name); }