codeql-action/lib/tracer-config.test.js

278 строки
11 KiB
JavaScript
Исходник Обычный вид История

2020-08-24 14:53:09 +03:00
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2020-08-24 14:53:09 +03:00
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const util = __importStar(require("./util"));
const ava_1 = __importDefault(require("ava"));
const tracer_config_1 = require("./tracer-config");
2020-08-24 14:53:09 +03:00
const languages_1 = require("./languages");
const codeql_1 = require("./codeql");
2020-08-24 14:53:09 +03:00
const testing_utils_1 = require("./testing-utils");
testing_utils_1.setupTests(ava_1.default);
function getTestConfig(tmpDir) {
return {
languages: [languages_1.Language.java],
queries: {},
pathsIgnore: [],
paths: [],
originalUserInput: {},
tempDir: tmpDir,
toolCacheDir: tmpDir,
2020-09-14 12:44:43 +03:00
codeQLCmd: "",
2020-08-24 14:53:09 +03:00
};
}
// A very minimal setup
2020-09-14 12:44:43 +03:00
ava_1.default("getTracerConfigForLanguage - minimal setup", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
const codeQL = codeql_1.setCodeQL({
2020-09-14 12:44:43 +03:00
async getTracerEnv() {
2020-08-24 14:53:09 +03:00
return {
2020-09-14 12:44:43 +03:00
ODASA_TRACER_CONFIGURATION: "abc",
foo: "bar",
2020-08-24 14:53:09 +03:00
};
},
});
2020-08-25 13:09:07 +03:00
const result = await tracer_config_1.getTracerConfigForLanguage(codeQL, config, languages_1.Language.javascript);
2020-09-14 12:44:43 +03:00
t.deepEqual(result, { spec: "abc", env: { foo: "bar" } });
2020-08-24 14:53:09 +03:00
});
});
// Existing vars should not be overwritten, unless they are critical or prefixed with CODEQL_
2020-09-14 12:44:43 +03:00
ava_1.default("getTracerConfigForLanguage - existing / critical vars", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-08-25 13:09:07 +03:00
// Set up some variables in the environment
2020-09-14 12:44:43 +03:00
process.env["foo"] = "abc";
process.env["SEMMLE_PRELOAD_libtrace"] = "abc";
process.env["SEMMLE_RUNNER"] = "abc";
process.env["SEMMLE_COPY_EXECUTABLES_ROOT"] = "abc";
process.env["SEMMLE_DEPTRACE_SOCKET"] = "abc";
process.env["SEMMLE_JAVA_TOOL_OPTIONS"] = "abc";
process.env["SEMMLE_DEPTRACE_SOCKET"] = "abc";
process.env["CODEQL_VAR"] = "abc";
2020-08-25 13:09:07 +03:00
// Now CodeQL returns all these variables, and one more, with different values
2020-08-24 14:53:09 +03:00
const codeQL = codeql_1.setCodeQL({
2020-09-14 12:44:43 +03:00
async getTracerEnv() {
2020-08-24 14:53:09 +03:00
return {
2020-09-14 12:44:43 +03:00
ODASA_TRACER_CONFIGURATION: "abc",
foo: "bar",
baz: "qux",
SEMMLE_PRELOAD_libtrace: "SEMMLE_PRELOAD_libtrace",
SEMMLE_RUNNER: "SEMMLE_RUNNER",
SEMMLE_COPY_EXECUTABLES_ROOT: "SEMMLE_COPY_EXECUTABLES_ROOT",
SEMMLE_DEPTRACE_SOCKET: "SEMMLE_DEPTRACE_SOCKET",
SEMMLE_JAVA_TOOL_OPTIONS: "SEMMLE_JAVA_TOOL_OPTIONS",
CODEQL_VAR: "CODEQL_VAR",
2020-08-24 14:53:09 +03:00
};
},
});
2020-08-25 13:09:07 +03:00
const result = await tracer_config_1.getTracerConfigForLanguage(codeQL, config, languages_1.Language.javascript);
2020-08-24 14:53:09 +03:00
t.deepEqual(result, {
2020-09-14 12:44:43 +03:00
spec: "abc",
2020-08-24 14:53:09 +03:00
env: {
2020-08-25 13:09:07 +03:00
// Should contain all variables except 'foo', because that already existed in the
// environment with a different value, and is not deemed a "critical" variable.
2020-09-14 12:44:43 +03:00
baz: "qux",
SEMMLE_PRELOAD_libtrace: "SEMMLE_PRELOAD_libtrace",
SEMMLE_RUNNER: "SEMMLE_RUNNER",
SEMMLE_COPY_EXECUTABLES_ROOT: "SEMMLE_COPY_EXECUTABLES_ROOT",
SEMMLE_DEPTRACE_SOCKET: "SEMMLE_DEPTRACE_SOCKET",
SEMMLE_JAVA_TOOL_OPTIONS: "SEMMLE_JAVA_TOOL_OPTIONS",
CODEQL_VAR: "CODEQL_VAR",
},
2020-08-24 14:53:09 +03:00
});
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("concatTracerConfigs - minimal configs correctly combined", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec1 = path.join(tmpDir, "spec1");
fs.writeFileSync(spec1, "foo.log\n2\nabc\ndef");
2020-08-24 14:53:09 +03:00
const tc1 = {
spec: spec1,
env: {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const spec2 = path.join(tmpDir, "spec2");
fs.writeFileSync(spec2, "foo.log\n1\nghi");
2020-08-24 14:53:09 +03:00
const tc2 = {
spec: spec2,
env: {
2020-09-14 12:44:43 +03:00
c: "c",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const result = tracer_config_1.concatTracerConfigs({ javascript: tc1, python: tc2 }, config);
2020-08-24 14:53:09 +03:00
t.deepEqual(result, {
2020-09-14 12:44:43 +03:00
spec: path.join(tmpDir, "compound-spec"),
2020-08-24 14:53:09 +03:00
env: {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
c: "c",
},
2020-08-24 14:53:09 +03:00
});
t.true(fs.existsSync(result.spec));
2020-09-14 12:44:43 +03:00
t.deepEqual(fs.readFileSync(result.spec, "utf8"), `${path.join(tmpDir, "compound-build-tracer.log")}\n3\nabc\ndef\nghi`);
2020-08-24 14:53:09 +03:00
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("concatTracerConfigs - conflicting env vars", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec = path.join(tmpDir, "spec");
fs.writeFileSync(spec, "foo.log\n0");
2020-08-24 14:53:09 +03:00
// Ok if env vars have the same name and the same value
t.deepEqual(tracer_config_1.concatTracerConfigs({
2020-09-14 12:44:43 +03:00
javascript: { spec, env: { a: "a", b: "b" } },
python: { spec, env: { b: "b", c: "c" } },
2020-08-24 14:53:09 +03:00
}, config).env, {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
c: "c",
2020-08-24 14:53:09 +03:00
});
// Throws if env vars have same name but different values
const e = t.throws(() => tracer_config_1.concatTracerConfigs({
2020-09-14 12:44:43 +03:00
javascript: { spec, env: { a: "a", b: "b" } },
python: { spec, env: { b: "c" } },
2020-08-24 14:53:09 +03:00
}, config));
2020-09-14 12:44:43 +03:00
t.deepEqual(e.message, "Incompatible values in environment parameter b: b and c");
2020-08-24 14:53:09 +03:00
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("concatTracerConfigs - cpp spec lines come last if present", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec1 = path.join(tmpDir, "spec1");
fs.writeFileSync(spec1, "foo.log\n2\nabc\ndef");
2020-08-24 14:53:09 +03:00
const tc1 = {
spec: spec1,
env: {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const spec2 = path.join(tmpDir, "spec2");
fs.writeFileSync(spec2, "foo.log\n1\nghi");
2020-08-24 14:53:09 +03:00
const tc2 = {
spec: spec2,
env: {
2020-09-14 12:44:43 +03:00
c: "c",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const result = tracer_config_1.concatTracerConfigs({ cpp: tc1, python: tc2 }, config);
2020-08-24 14:53:09 +03:00
t.deepEqual(result, {
2020-09-14 12:44:43 +03:00
spec: path.join(tmpDir, "compound-spec"),
2020-08-24 14:53:09 +03:00
env: {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
c: "c",
},
2020-08-24 14:53:09 +03:00
});
t.true(fs.existsSync(result.spec));
2020-09-14 12:44:43 +03:00
t.deepEqual(fs.readFileSync(result.spec, "utf8"), `${path.join(tmpDir, "compound-build-tracer.log")}\n3\nghi\nabc\ndef`);
2020-08-24 14:53:09 +03:00
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("concatTracerConfigs - SEMMLE_COPY_EXECUTABLES_ROOT is updated to point to compound spec", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec = path.join(tmpDir, "spec");
fs.writeFileSync(spec, "foo.log\n0");
2020-08-24 14:53:09 +03:00
const result = tracer_config_1.concatTracerConfigs({
2020-09-14 12:44:43 +03:00
javascript: { spec, env: { a: "a", b: "b" } },
python: { spec, env: { SEMMLE_COPY_EXECUTABLES_ROOT: "foo" } },
2020-08-24 14:53:09 +03:00
}, config);
t.deepEqual(result.env, {
2020-09-14 12:44:43 +03:00
a: "a",
b: "b",
SEMMLE_COPY_EXECUTABLES_ROOT: path.join(tmpDir, "compound-temp"),
2020-08-24 14:53:09 +03:00
});
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("concatTracerConfigs - compound environment file is created correctly", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec1 = path.join(tmpDir, "spec1");
fs.writeFileSync(spec1, "foo.log\n2\nabc\ndef");
2020-08-24 14:53:09 +03:00
const tc1 = {
spec: spec1,
env: {
2020-09-14 12:44:43 +03:00
a: "a",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const spec2 = path.join(tmpDir, "spec2");
fs.writeFileSync(spec2, "foo.log\n1\nghi");
2020-08-24 14:53:09 +03:00
const tc2 = {
spec: spec2,
env: {
2020-09-14 12:44:43 +03:00
foo: "bar_baz",
},
2020-08-24 14:53:09 +03:00
};
2020-09-14 12:44:43 +03:00
const result = tracer_config_1.concatTracerConfigs({ javascript: tc1, python: tc2 }, config);
const envPath = `${result.spec}.environment`;
2020-08-24 14:53:09 +03:00
t.true(fs.existsSync(envPath));
const buffer = fs.readFileSync(envPath);
2020-08-25 13:09:07 +03:00
// Contents is binary data
t.deepEqual(buffer.length, 28);
t.deepEqual(buffer.readInt32LE(0), 2); // number of env vars
t.deepEqual(buffer.readInt32LE(4), 4); // length of env var definition
2020-09-14 12:44:43 +03:00
t.deepEqual(buffer.toString("utf8", 8, 12), "a=a\0"); // [key]=[value]\0
2020-08-25 13:09:07 +03:00
t.deepEqual(buffer.readInt32LE(12), 12); // length of env var definition
2020-09-14 12:44:43 +03:00
t.deepEqual(buffer.toString("utf8", 16, 28), "foo=bar_baz\0"); // [key]=[value]\0
2020-08-24 14:53:09 +03:00
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("getCombinedTracerConfig - return undefined when no languages are traced languages", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
// No traced languages
config.languages = [languages_1.Language.javascript, languages_1.Language.python];
const codeQL = codeql_1.setCodeQL({
2020-09-14 12:44:43 +03:00
async getTracerEnv() {
2020-08-24 14:53:09 +03:00
return {
2020-09-14 12:44:43 +03:00
ODASA_TRACER_CONFIGURATION: "abc",
foo: "bar",
2020-08-24 14:53:09 +03:00
};
},
});
2020-08-25 13:09:07 +03:00
t.deepEqual(await tracer_config_1.getCombinedTracerConfig(config, codeQL), undefined);
2020-08-24 14:53:09 +03:00
});
});
2020-09-14 12:44:43 +03:00
ava_1.default("getCombinedTracerConfig - valid spec file", async (t) => {
2020-08-24 14:53:09 +03:00
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
2020-09-14 12:44:43 +03:00
const spec = path.join(tmpDir, "spec");
fs.writeFileSync(spec, "foo.log\n2\nabc\ndef");
2020-08-24 14:53:09 +03:00
const codeQL = codeql_1.setCodeQL({
2020-09-14 12:44:43 +03:00
async getTracerEnv() {
2020-08-24 14:53:09 +03:00
return {
2020-09-14 12:44:43 +03:00
ODASA_TRACER_CONFIGURATION: spec,
foo: "bar",
2020-08-24 14:53:09 +03:00
};
},
});
2020-08-25 13:09:07 +03:00
const result = await tracer_config_1.getCombinedTracerConfig(config, codeQL);
2020-08-28 16:46:23 +03:00
const expectedEnv = {
2020-09-14 12:44:43 +03:00
foo: "bar",
ODASA_TRACER_CONFIGURATION: result.spec,
2020-08-28 16:46:23 +03:00
};
2020-09-14 12:44:43 +03:00
if (process.platform === "darwin") {
expectedEnv["DYLD_INSERT_LIBRARIES"] = path.join(path.dirname(codeQL.getPath()), "tools", "osx64", "libtrace.dylib");
2020-08-28 16:46:23 +03:00
}
2020-09-14 12:44:43 +03:00
else if (process.platform !== "win32") {
expectedEnv["LD_PRELOAD"] = path.join(path.dirname(codeQL.getPath()), "tools", "linux64", "${LIB}trace.so");
2020-08-28 16:46:23 +03:00
}
2020-08-24 14:53:09 +03:00
t.deepEqual(result, {
2020-09-14 12:44:43 +03:00
spec: path.join(tmpDir, "compound-spec"),
2020-08-28 16:46:23 +03:00
env: expectedEnv,
2020-08-24 14:53:09 +03:00
});
});
});
//# sourceMappingURL=tracer-config.test.js.map