-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresults.js
227 lines (209 loc) · 5.29 KB
/
results.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*!
* Module dependencies
*/
// Third party modules
const mongoose = require('mongoose');
const QueryPlugin = require('mongoose-query');
const logger = require('../tools/logger');
// Local components
const FileSchema = require('./extends/file');
const tools = require('../tools');
// Model variables
const {Schema} = mongoose;
const {Types} = Schema;
const {ObjectId, Mixed} = Types;
const {filedb} = tools;
const fileProvider = filedb.provider;
// @Todo justify why file schema is extended here instead of adding to root model
FileSchema.add({
ref: {type: ObjectId, ref: 'Resource'},
from: {type: String, enum: ['dut', 'framework', 'env', 'other']}
});
// Device(s) Under Test
const DutSchema = new Schema({
type: {type: String, enum: ['hw', 'simulator', 'process']},
ref: {type: ObjectId, ref: 'Resource'},
platform: {type: String},
vendor: {type: String},
model: {type: String},
ver: {type: String},
sn: {type: String},
provider: {
name: {type: String},
id: {type: String},
ver: {type: String}
}
});
/**
* User schema
*/
const ResultSchema = new Schema({
tcid: {type: String, required: true, index: true},
tcRef: {type: ObjectId, ref: 'Testcase'},
job: {
id: {type: String, default: ''}
},
campaign: {type: String, default: '', index: true},
campaignRef: {type: ObjectId, ref: 'Campaign'},
cre: {
time: {type: Date, default: Date.now, index: true},
user: {type: String},
userRef: {type: ObjectId, ref: 'User'}
},
exec: {
verdict: {
type: String,
required: true,
enum: ['pass', 'fail', 'inconclusive', 'blocked', 'error', 'skip'],
index: true
},
note: {type: String, default: ''},
duration: {type: Number}, // seconds
profiling: {type: Mixed},
metadata: {type: Mixed},
metrics: {type: Mixed},
env: { // environment information
ref: {type: ObjectId, ref: 'Resource'},
rackId: {type: String},
framework: {
name: {type: String, default: ''},
ver: {type: String, default: ''}
}
},
sut: { // software under test
ref: {type: ObjectId, ref: 'Build'},
gitUrl: {type: String},
buildName: {type: String},
buildDate: {type: Date},
buildUrl: {type: String},
buildSha1: {type: String},
branch: {type: String},
commitId: {type: String},
tag: [{type: String}],
href: {type: String},
cut: [{type: String}], // Component Under Test
fut: [{type: String}] // Feature Under Test
},
duts: [DutSchema],
logs: [FileSchema]
}
}, {
toJSON: {virtuals: true, getters: true},
toObject: {virtuals: true, getters: true}
});
/**
* Query plugin
*/
ResultSchema.plugin(QueryPlugin); // install QueryPlugin
/**
* Add your
* - pre-save hooks
* - validations
* - virtuals
*/
/**
* Methods
*/
ResultSchema.methods.getBuildRef = function () { // eslint-disable-line func-names
logger.debug('lookup build..');
return this.get('exec.sut.ref');
};
/**
* Mappers
*/
async function linkRelatedBuild(result) {
const buildChecksum = result.get('exec.sut.buildSha1');
if (!buildChecksum) {
return;
}
if (result.get('exec.sut.ref')) {
// already given
return;
}
logger.debug(`Processing result build sha1: ${buildChecksum}`);
const build = await mongoose.model('Build')
.findOne({'files.sha1': buildChecksum})
.select('_id')
.exec();
if (build) {
logger.debug(`Build found, linking Result: ${result._id} with Build: ${build._id}`);
result.set('exec.sut.ref', build._id); // eslint-disable-line no-param-reassign
}
}
async function linkTestcase(result) {
const {tcid, tcRef} = result;
if (!tcid) {
throw new Error('tcid is missing!');
}
if (tcRef) {
return;
}
logger.debug(`Processing result tcid: ${tcid}`);
const test = await mongoose.model('Testcase')
.findOne({tcid})
.select('_id')
.exec();
if (test) {
logger.debug(`Test found, linking Result: ${result._id} with Test: ${test._id}`);
result.tcRef = test._id; // eslint-disable-line no-param-reassign
}
}
async function storeFile(file, i) {
file.prepareDataForStorage(i);
// Decide what to do with file
if (fileProvider === 'mongodb') {
file.keepInMongo(i);
return Promise.resolve();
}
if (fileProvider) {
return file.storeInFiledb(filedb, i);
}
file.dumpData(i);
return Promise.resolve();
}
async function preSave(next) {
try {
// Link related objects
await linkTestcase(this);
await linkRelatedBuild(this);
const logs = this.get('exec.logs');
await Promise.all(logs.map(storeFile));
next();
} catch (error) {
next(error);
}
}
ResultSchema.pre('save', preSave);
/**
* Virtuals
*/
ResultSchema
.virtual('exec.dut')
.get(function dutGet() {
const duts = this.get('exec.duts');
const obj = duts.length === 1 ? duts[0].toJSON() : {};
obj.count = duts.length;
return obj;
})
.set(function dutSet(obj) {
if (!this.exec.duts) {
this.exec.duts = [];
}
if (obj.count === this.exec.duts.length) {
// expect that duts array holds same data than what dut object contains
return;
}
this.exec.duts.push(obj);
});
/**
* Statics
*/
/*
ResultSchema.static({
});
*/
/**
* Register
*/
const Result = mongoose.model('Result', ResultSchema);
module.exports = {Model: Result, Collection: 'Result'};