test_runner.cc 7.11 KB
Newer Older
1
// ============================================================================
Diego Molteni's avatar
Diego Molteni committed
2
// Copyright 2017-2021, Schlumberger
Sid Stenersen's avatar
Sid Stenersen committed
3
//
4
5
6
// 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
Sid Stenersen's avatar
Sid Stenersen committed
7
//
8
//      http://www.apache.org/licenses/LICENSE-2.0
Sid Stenersen's avatar
Sid Stenersen committed
9
//
10
11
12
13
14
15
16
17
// 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 "test_runner.h"
Diego Molteni's avatar
Diego Molteni committed
18
#include "shared/utils.h"
19
20
21
22
23
24
25
26
27
28

#include "SDException.h"
#include "SDUtils.h"

#include <iostream>

using namespace seismicdrive;

std::string TestRunner::exeName;

Diego Molteni's avatar
Diego Molteni committed
29
TestRunner::TestRunner(const Test::Params &params, bool logging)
Sid Stenersen's avatar
Sid Stenersen committed
30
31
    : params(params),
      logging(logging)
32
33
34
{
}

Sid Stenersen's avatar
Sid Stenersen committed
35
36
37
38
TestRunner::~TestRunner()
{
    try
    {
Diego Molteni's avatar
Diego Molteni committed
39
40
        SDManager sdManager(params.sdUrl, params.sdApiKey);
        sdManager.setAuthProviderFromString(params.idToken);
41
42
        SDUtils sdutils(&sdManager);
        // std::cout << "Removing temporary datasets" << std::endl;
Sid Stenersen's avatar
Sid Stenersen committed
43
44
45
46
        for (const auto &dsn : datasetsToDelete)
        {
            try
            {
47
48
                sdutils.deleteDataset(dsn);
            }
Sid Stenersen's avatar
Sid Stenersen committed
49
50
            catch (const SDException &)
            {
51
52
53
            }
        }
    }
Sid Stenersen's avatar
Sid Stenersen committed
54
55
    catch (const std::exception &)
    {
56
57
58
    }
}

Sid Stenersen's avatar
Sid Stenersen committed
59
60
void TestRunner::addToDatasetsToDelete(const std::string datasetName)
{
61
62
63
    datasetsToDelete.insert(datasetName);
}

Sid Stenersen's avatar
Sid Stenersen committed
64
65
void TestRunner::throwOne(std::string what)
{
66
67
    throw std::runtime_error(what);
}
Sid Stenersen's avatar
Sid Stenersen committed
68
69
void TestRunner::throwOne(std::string label, const std::exception &ex)
{
70
71
72
    throwOne(label + ' ' + ex.what());
}

Sid Stenersen's avatar
Sid Stenersen committed
73
74
void TestRunner::throwUsage(const Test::Params &params)
{
75
76
77
78
79
    auto s = "Usage: " + exeName + " --tenant=\"name\""
                                   " --subproject=\"name\""
                                   " --idtoken=\"token\""
                                   " --service-url=\"url\""
                                   " --service-key=\"key\"";
Sid Stenersen's avatar
Sid Stenersen committed
80
81
    if (params.testType == params.Regression)
    {
82
83
84
85
86
        s += " --legal-tag=\"legalTag\"";
    }
    throwOne(s);
}

Sid Stenersen's avatar
Sid Stenersen committed
87
88
TestRunner *TestRunner::Create(int argc, char *argv[])
{
89
90
91
92
93
    exeName = getFilename(argv[0]);
    std::unordered_map<std::string, std::string> args;
    for (int i = 1; i < argc; i++)
    {
        std::string variable,
Sid Stenersen's avatar
Sid Stenersen committed
94
95
96
97
            value,
            arg = argv[i];
        if (!arg.rfind("--", 0))
        {
98
99
            auto j = arg.find('=');
            variable = arg.substr(2, j - 2);
Sid Stenersen's avatar
Sid Stenersen committed
100
101
            if (j != arg.npos)
            {
102
103
104
105
106
107
                value = arg.substr(j + 1);
                if (value.size() > 1 && value[0] == '"' && value.back() == '"')
                {
                    value = value.substr(1, value.size() - 2);
                }
            }
Sid Stenersen's avatar
Sid Stenersen committed
108
109
            if (variable.size())
            {
110
111
112
113
114
                args[variable] = value;
            }
        }
    }
    Test::Params params;
Sid Stenersen's avatar
Sid Stenersen committed
115
    std::string tenant = args["tenant"],
116
                subproject = args["subproject"],
Sid Stenersen's avatar
Sid Stenersen committed
117
                idToken = args["idtoken"],
118
119
                serviceUrl = args["service-url"],
                serviceKey = args["service-key"],
Sid Stenersen's avatar
Sid Stenersen committed
120
121
                logging = args["logging"],
                legalTag = args["legal-tag"],
122

Sid Stenersen's avatar
Sid Stenersen committed
123
124
125
126
127
128
129
                dsn = args["dataset-name"],
                oc = args["object-count"],
                os = args["object-size"],
                minos = args["min-object-size"],
                maxos = args["max-object-size"],
                maxth = args["max-threads"],
                rc = args["run-count"],
Diego Molteni's avatar
Diego Molteni committed
130
                sizeUnit = args["size-unit"],
Sid Stenersen's avatar
Sid Stenersen committed
131
                tt = args["test-type"];
132
    params.idToken = idToken;
Diego Molteni's avatar
Diego Molteni committed
133
134
    params.sdUrl = serviceUrl;
    params.sdApiKey = serviceKey;
135
    params.outputFilename = args["output-filename"];
Sid Stenersen's avatar
Sid Stenersen committed
136
137
    params.testType = exeName == "regression" ? params.Regression : tt == "sequential-read" ? params.SequentialRead
                                                                                            : params.SequentialWriteRandomRead;
138
    bool usage = false;
Sid Stenersen's avatar
Sid Stenersen committed
139
140
    try
    {
141
        uint64_t multiplier = MB;
Sid Stenersen's avatar
Sid Stenersen committed
142
143
        if (sizeUnit.size())
        {
144
            char c = static_cast<char>(toupper(sizeUnit[0]));
Sid Stenersen's avatar
Sid Stenersen committed
145
146
            multiplier = c == 'K' ? KB : c == 'G' ? GB
                                                  : MB;
147
        }
Sid Stenersen's avatar
Sid Stenersen committed
148
149
        if (os.size())
        {
150
151
            params.objectSize = std::stoi(os) * multiplier;
        }
Sid Stenersen's avatar
Sid Stenersen committed
152
153
        if (minos.size())
        {
154
155
            params.minObjectSize = std::stoi(minos) * multiplier;
        }
Sid Stenersen's avatar
Sid Stenersen committed
156
157
        if (maxos.size())
        {
158
159
            params.maxObjectSize = std::stoi(maxos) * multiplier;
        }
Sid Stenersen's avatar
Sid Stenersen committed
160
161
        if (maxth.size())
        {
Diego Molteni's avatar
Diego Molteni committed
162
            params.maxThreads.clear();
Sid Stenersen's avatar
Sid Stenersen committed
163
            for (auto s : sdutils::split(maxth, ',', false))
Sid Stenersen's avatar
Sid Stenersen committed
164
            {
Diego Molteni's avatar
Diego Molteni committed
165
166
                params.maxThreads.push_back(std::stoi(s));
            }
167
        }
Sid Stenersen's avatar
Sid Stenersen committed
168
169
        if (oc.size())
        {
170
171
            params.objectCount = std::stoi(oc);
        }
Sid Stenersen's avatar
Sid Stenersen committed
172
173
        if (rc.size())
        {
174
175
176
            params.runCount = std::stoi(rc);
        }
    }
Sid Stenersen's avatar
Sid Stenersen committed
177
178
    catch (const std::exception &)
    {
179
180
        usage = true;
    }
Diego Molteni's avatar
Diego Molteni committed
181
    if ((dsn.empty() && (tenant.empty() || subproject.empty())) || usage || idToken.empty() || serviceUrl.empty() || serviceKey.empty() || (legalTag.empty() && params.testType == params.Regression))
182
    {
Diego Molteni's avatar
Diego Molteni committed
183
        throwUsage(params);
184
    }
Sid Stenersen's avatar
Sid Stenersen committed
185
186
    if (dsn.size())
    {
187
188
189
        params.addToDsn = 0;
        params.sdDatasetName = dsn;
    }
Sid Stenersen's avatar
Sid Stenersen committed
190
191
    else
    {
192
193
194
195
        params.addToDsn = 15;
        params.sdDatasetName = "sd://" + tenant + '/' + subproject + '/' + exeName;
    }
    params.legalTag = legalTag;
Diego Molteni's avatar
Diego Molteni committed
196
    return new TestRunner(params, !strcasecmp(&logging[0], "ON"));
197
198
}

Diego Molteni's avatar
Diego Molteni committed
199
unsigned TestRunner::run()
200
201
{
    std::string label, what;
Sid Stenersen's avatar
Sid Stenersen committed
202
203
    try
    {
Diego Molteni's avatar
Diego Molteni committed
204
        unsigned testsPassed = 0;
Sid Stenersen's avatar
Sid Stenersen committed
205
206
        for (auto &test : tests)
        {
Diego Molteni's avatar
Diego Molteni committed
207
208
209
210
211
212
            std::cout << frame(test->getName(), '=');

            SDManager sdManager(params.sdUrl, params.sdApiKey, logging);
            sdManager.setAuthProviderFromString(params.idToken);
            addToDatasetsToDelete(test->getDatasetName());
            testsPassed += test->run(&sdManager);
213
            test.reset(nullptr);
Diego Molteni's avatar
Diego Molteni committed
214
            std::cout << "\n\n";
215
        }
Diego Molteni's avatar
Diego Molteni committed
216
        return testsPassed;
217
    }
Sid Stenersen's avatar
Sid Stenersen committed
218
    catch (const error::format::Error &ex)
Sid Stenersen's avatar
Sid Stenersen committed
219
    {
220
221
        throwOne("[Value Error]", ex);
    }
Sid Stenersen's avatar
Sid Stenersen committed
222
    catch (const error::authprovider::Error &ex)
Sid Stenersen's avatar
Sid Stenersen committed
223
    {
224
225
        throwOne("[Auth Provider Error]", ex);
    }
Sid Stenersen's avatar
Sid Stenersen committed
226
    catch (const error::seismicstore::ActionFailed &ex)
Sid Stenersen's avatar
Sid Stenersen committed
227
    {
Sid Stenersen's avatar
Sid Stenersen committed
228
        throwOne("[Seismic Store Accessor Error]", ex);
229
    }
Sid Stenersen's avatar
Sid Stenersen committed
230
    catch (const error::storage::AccessError &ex)
Sid Stenersen's avatar
Sid Stenersen committed
231
    {
232
233
        throwOne("[GCS Accessor Error]", ex);
    }
Sid Stenersen's avatar
Sid Stenersen committed
234
    catch (const error::dataset::Error &ex)
Sid Stenersen's avatar
Sid Stenersen committed
235
    {
236
237
        throwOne("[Dataset Error]", ex);
    }
Sid Stenersen's avatar
Sid Stenersen committed
238
239
    catch (const SDException &ex)
    {
240
241
        throwOne("[Seismic Store Error]", ex);
    }
Sid Stenersen's avatar
Sid Stenersen committed
242
243
    catch (const std::exception &ex)
    {
244
245
        throwOne("[ERROR]", ex);
    }
Diego Molteni's avatar
Diego Molteni committed
246
    return 0;
247
}