blob: 135716e3df3ab0fcf245243a3f2b275f69514d76 [file] [edit]
// Copyright 2026 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.
#include "process_tree.h"
#include "test.h"
TEST(ProcessTreeTest, ConstructionAndPrinting) {
std::vector<ProcessTree::ProcessInfo> processes = {
{ 100, 1, "init", {} },
{ 200, 100, "ninja", {} },
{ 201, 200, "python", { { "python", "script.py" }, true } },
{ 202, 201, "gcc", { { "gcc", "-c", "foo.c" }, true } },
{ 203, 200, "bash", {} },
{ 204, 203, "ls", { { "ls", "-l" }, true } },
};
ProcessTree::SystemSnapshot snapshot;
snapshot.processes = processes;
for (const auto& proc : snapshot.processes) {
snapshot.ppid_to_children[proc.ppid].push_back(proc.pid);
snapshot.pid_to_info[proc.pid] = &proc;
}
ProcessTree tree(200, snapshot);
std::unordered_map<ProcessTree::Pid, std::string> known;
known[200] = "ninja build";
std::string output;
tree.Print(1, known, &output);
EXPECT_NE(std::string::npos, output.find("- [PID 201]: python script.py"));
EXPECT_NE(std::string::npos, output.find(" - [PID 202]: gcc -c foo.c"));
EXPECT_NE(std::string::npos, output.find("- [PID 203]: [bash]"));
EXPECT_NE(std::string::npos, output.find(" - [PID 204]: ls -l"));
}
TEST(ProcessTreeTest, DisconnectedTree) {
std::vector<ProcessTree::ProcessInfo> processes = {
{ 100, 1, "init", {} },
{ 300, 400, "orphan", {} },
};
ProcessTree::SystemSnapshot snapshot;
snapshot.processes = processes;
for (const auto& proc : snapshot.processes) {
snapshot.ppid_to_children[proc.ppid].push_back(proc.pid);
snapshot.pid_to_info[proc.pid] = &proc;
}
// Root PID 200 is not in the list.
ProcessTree tree(200, snapshot);
std::string output;
tree.Print(1, {}, &output);
EXPECT_EQ("", output);
}