summaryrefslogtreecommitdiffstats
path: root/tests/src/maintenance/controller_test.cc
blob: d2ce19f1fa5654250e53c5044ddbbbabc5a9d07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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 "common/debug.h"
#include "maintenance/controller.h"

#include "db/file_models.h"
#include "db/models.h"

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>

#include <android-base/file.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using ::testing::Return;
using ::testing::_;
using ::testing::SaveArg;
using ::testing::SaveArgPointee;

namespace iorap::maintenance {

static std::string GetTestDataPath(const std::string& fn) {
  static std::string exec_dir = android::base::GetExecutableDirectory();
  return exec_dir + "/tests/src/maintenance/testdata/" + fn;
}

class MockExec : public IExec {
 public:
  MOCK_METHOD(int,
              Execve,
              (const std::string& pathname, std::vector<std::string>& argv_vec, char *const envp[]),
              (override));

  MOCK_METHOD(pid_t, Fork, (), (override));
};

class ControllerTest: public ::testing::Test {
 protected:
  void SetUp() override {
    // The db is a fake db with the following tables:
    //
    // packages:
    // id, name, version
    // 1, com.android.settings, 1
    // 2, com.yawanng, 1
    //
    // activities:
    // id, name
    // 1, Setting
    // 2, yawanng
    //
    // app_launch_histories:
    // id, activity_id, temperature, trace_enabled, readahead_enabled, intent_start_ns, total_time_ns, report_fully_drawn_ns
    // 1, 1, 1, 1, 1, 1, 2, NULL
    // 2, 1, 1, 1, 1, NULL, 4, 5
    // 3, 1, 1, 1, 1, 3, NULL, NULL
    // 4, 1, 1, 1, 1, 3, 7, 8
    // 5, 1, 1, 0, 1, 4, 9, 10
    // 6, 1, 2, 1, 1, 5, 11, 12
    // 7, 2, 1, 1, 1, 6, 21, 22
    // 8, 2, 1, 1, 1, 7, 22, 23
    //
    // raw_traces:
    // id, history_id, file_path
    // 1, 1, 1.txt
    // 2, 3, 3.txt
    // 3, 4, 4.txt
    // 4, 5, 5.txt
    // 5, 6, 6.txt
    // 6, 7, 7.txt
    // 7, 8, 8.txt
    db_path = GetTestDataPath("test_sqlite.db");
  }

  std::string db_path;
  TemporaryDir root_path;
};

MATCHER_P(AreArgsExpected, compiled_trace_path, "") {
  std::vector<std::string> expect =
    { "1.txt",
      "3.txt",
      "4.txt",
      "--timestamp_limit_ns", "2",
      "--timestamp_limit_ns", "18446744073709551615",
      "--timestamp_limit_ns", "8",
      "--output-text",
      "--output-proto", compiled_trace_path,
      "--verbose" };
  return arg == expect;
}

TEST_F(ControllerTest, CompilationController) {
  auto mock_exec = std::make_shared<MockExec>();
  iorap::db::SchemaModel db_schema = db::SchemaModel::GetOrCreate(db_path);
  db::DbHandle db{db_schema.db()};

  setenv("IORAPD_ROOT_DIR", root_path.path ,1);
  std::string compiled_trace_path = std::string(root_path.path) +
      "/com.android.settings/1/Setting/compiled_traces/compiled_trace.pb";

  // No recompile
  ControllerParameters params{
    /*output_text=*/true,
    /*inode_textcache=*/std::nullopt,
    /*verbose=*/true,
    /*recompile=*/false,
    /*min_traces=*/3,
    mock_exec};

  ON_CALL(*mock_exec, Fork())
      .WillByDefault(Return(-2));

  EXPECT_CALL(*mock_exec,
              Execve("/system/bin/iorap.cmd.compiler",
                     AreArgsExpected(compiled_trace_path),
                     nullptr))
      .Times(2);

  CompileAppsOnDevice(db, params);

  // Recompile
  ControllerParameters params2{
    /*output_text=*/true,
    /*inode_textcache=*/std::nullopt,
    /*verbose=*/true,
    /*recompile=*/true,
    /*min_traces=*/3,
    mock_exec};

  // Create a fake compiled trace file to test recompile.
  std::ofstream tmp_file;
  tmp_file.open(compiled_trace_path);
  tmp_file.close();

  CompileAppsOnDevice(db, params2);
}

}  // namespace iorap::maintenance