From 05cb7a30fd5cc748827242d1956a29fe81a60a8d Mon Sep 17 00:00:00 2001 From: Tre' Seymour Date: Mon, 20 Jul 2020 09:45:23 -0600 Subject: [PATCH] Add opts that dev-utils expects, and add unit tests. --- .ci/Jenkinsfile_coverage | 4 +- .../__tests__/mocks/team_assign_mock.json | 3 ++ .../__tests__/team_assignment.test.js | 45 +++++++++++++++++++ .../ingest_coverage/constants.js | 2 +- .../team_assignment/get_data.js | 14 +++--- .../ingest_coverage/team_assignment/index.js | 32 ++++++++----- .../team_assignment/update_ingest_pipeline.js | 4 +- .../shell_scripts/assign_teams.sh | 2 +- 8 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 src/dev/code_coverage/ingest_coverage/__tests__/mocks/team_assign_mock.json create mode 100644 src/dev/code_coverage/ingest_coverage/__tests__/team_assignment.test.js diff --git a/.ci/Jenkinsfile_coverage b/.ci/Jenkinsfile_coverage index ebb9c3dc86dd2..faccdbd6578b9 100644 --- a/.ci/Jenkinsfile_coverage +++ b/.ci/Jenkinsfile_coverage @@ -12,9 +12,9 @@ kibanaPipeline(timeoutMinutes: 240) { ]) { workers.base(name: 'coverage-worker', size: 'l', ramDisk: false, bootstrapped: false) { catchError { - kibanaCoverage.runTests() +// kibanaCoverage.runTests() kibanaTeamAssign.load('team_assignment', "### Upload Team Assignment JSON") - handleIngestion(TIME_STAMP) +// handleIngestion(TIME_STAMP) } handleFail() } diff --git a/src/dev/code_coverage/ingest_coverage/__tests__/mocks/team_assign_mock.json b/src/dev/code_coverage/ingest_coverage/__tests__/mocks/team_assign_mock.json new file mode 100644 index 0000000000000..355c484a84fa3 --- /dev/null +++ b/src/dev/code_coverage/ingest_coverage/__tests__/mocks/team_assign_mock.json @@ -0,0 +1,3 @@ +{ + "abc": "123" +} diff --git a/src/dev/code_coverage/ingest_coverage/__tests__/team_assignment.test.js b/src/dev/code_coverage/ingest_coverage/__tests__/team_assignment.test.js new file mode 100644 index 0000000000000..e597ffb5d2f4b --- /dev/null +++ b/src/dev/code_coverage/ingest_coverage/__tests__/team_assignment.test.js @@ -0,0 +1,45 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you 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. + */ + +import expect from '@kbn/expect'; +import { fetch } from '../team_assignment/get_data'; +import { noop } from '../utils'; + +describe(`Team Assignment`, () => { + const mockPath = 'src/dev/code_coverage/ingest_coverage/__tests__/mocks/team_assign_mock.json'; + describe(`fetch fn`, () => { + it(`should be a fn`, () => { + expect(typeof fetch).to.be('function'); + }); + describe(`applied to a path that exists`, () => { + it(`should return the contents of the path`, () => { + const sut = fetch(mockPath); + expect(sut.chain(JSON.parse)).to.have.property('abc'); + }); + }); + describe(`applied to an non-existing path`, () => { + it(`should return a Left with the error message within`, () => { + const expectLeft = (err) => + expect(err.message).to.contain('ENOENT: no such file or directory'); + + fetch('fake_path.json').fold(expectLeft, noop); + }); + }); + }); +}); diff --git a/src/dev/code_coverage/ingest_coverage/constants.js b/src/dev/code_coverage/ingest_coverage/constants.js index ae3079afd911d..f2f467e461ae5 100644 --- a/src/dev/code_coverage/ingest_coverage/constants.js +++ b/src/dev/code_coverage/ingest_coverage/constants.js @@ -32,4 +32,4 @@ export const TEAM_ASSIGNMENT_PIPELINE_NAME = process.env.PIPELINE_NAME || 'team_ export const CODE_COVERAGE_CI_JOB_NAME = 'elastic+kibana+code-coverage'; export const RESEARCH_CI_JOB_NAME = 'elastic+kibana+qa-research'; export const CI_JOB_NAME = process.env.COVERAGE_JOB_NAME || RESEARCH_CI_JOB_NAME; -export const RESEARCH_CLUSTER_ES_HOST = process.env.ES_HOST || 'http://localhost:9200'; +export const ES_HOST = process.env.ES_HOST || 'http://localhost:9200'; diff --git a/src/dev/code_coverage/ingest_coverage/team_assignment/get_data.js b/src/dev/code_coverage/ingest_coverage/team_assignment/get_data.js index d9fbf5690d8a4..34526a2f79302 100644 --- a/src/dev/code_coverage/ingest_coverage/team_assignment/get_data.js +++ b/src/dev/code_coverage/ingest_coverage/team_assignment/get_data.js @@ -19,13 +19,15 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; -import { fromNullable } from '../either'; +import { tryCatch as tc } from '../either'; const ROOT = resolve(__dirname, '../../../../..'); + const resolveFromRoot = resolve.bind(null, ROOT); -const path = ` -src/dev/code_coverage/ingest_coverage/team_assignment/ingestion_pipeline_painless.json`; -const resolved = resolveFromRoot(path.trimStart()); -const getContents = (scriptPath) => readFileSync(scriptPath, 'utf8'); -export const fetch = () => fromNullable(resolved).map(getContents); +const resolved = (path) => () => resolveFromRoot(path); + +const getContents = (path) => tc(() => readFileSync(path, 'utf8')); + +// fetch :: String -> Left | Right +export const fetch = (path) => tc(resolved(path)).chain(getContents); diff --git a/src/dev/code_coverage/ingest_coverage/team_assignment/index.js b/src/dev/code_coverage/ingest_coverage/team_assignment/index.js index 301f7fb2dee2f..11f9748708283 100644 --- a/src/dev/code_coverage/ingest_coverage/team_assignment/index.js +++ b/src/dev/code_coverage/ingest_coverage/team_assignment/index.js @@ -20,29 +20,39 @@ import { run } from '@kbn/dev-utils'; import { TEAM_ASSIGNMENT_PIPELINE_NAME } from '../constants'; import { fetch } from './get_data'; -import { noop } from '../utils'; import { update } from './update_ingest_pipeline'; -export const uploadTeamAssignmentJson = () => run(execute, { description }); - const updatePipeline = update(TEAM_ASSIGNMENT_PIPELINE_NAME); -function execute({ flags, log }) { +const execute = ({ flags, log }) => { if (flags.verbose) log.verbose(`### Verbose logging enabled`); - fetch().fold(noop, updatePipeline(log)); + const logLeft = handleErr(log); + const updateAndLog = updatePipeline(log); + + const { path } = flags; + + fetch(path).fold(logLeft, updateAndLog); +}; + +function handleErr(log) { + return (msg) => log.error(msg); } -function description() { - return ` +const description = ` Upload the latest team assignment pipeline def from src, to the cluster. + `; -Examples: +const flags = { + string: ['path', 'verbose'], + help: ` +--path Required, path to painless definition for team assignment. + `, +}; -node scripts/load_team_assignment.js --verbose +const usage = 'node scripts/load_team_assignment.js --verbose --path PATH_TO_PAINLESS_SCRIPT.json'; - `; -} +export const uploadTeamAssignmentJson = () => run(execute, { description, flags, usage }); diff --git a/src/dev/code_coverage/ingest_coverage/team_assignment/update_ingest_pipeline.js b/src/dev/code_coverage/ingest_coverage/team_assignment/update_ingest_pipeline.js index 03844b2a5dd32..22a9d0a461ebf 100644 --- a/src/dev/code_coverage/ingest_coverage/team_assignment/update_ingest_pipeline.js +++ b/src/dev/code_coverage/ingest_coverage/team_assignment/update_ingest_pipeline.js @@ -18,12 +18,12 @@ */ import { createFailError } from '@kbn/dev-utils'; -import { RESEARCH_CLUSTER_ES_HOST } from '../constants'; +import { ES_HOST } from '../constants'; import { pretty, green } from '../utils'; const { Client } = require('@elastic/elasticsearch'); -const node = RESEARCH_CLUSTER_ES_HOST; +const node = ES_HOST; const client = new Client({ node }); export const update = (id) => (log) => async (body) => { diff --git a/src/dev/code_coverage/shell_scripts/assign_teams.sh b/src/dev/code_coverage/shell_scripts/assign_teams.sh index 186cbecb436e9..aaa14655a9a26 100644 --- a/src/dev/code_coverage/shell_scripts/assign_teams.sh +++ b/src/dev/code_coverage/shell_scripts/assign_teams.sh @@ -9,7 +9,7 @@ export PIPELINE_NAME ES_HOST="https://${USER_FROM_VAULT}:${PASS_FROM_VAULT}@${HOST_FROM_VAULT}" export ES_HOST -node scripts/load_team_assignment.js --verbose +node scripts/load_team_assignment.js --verbose --path src/dev/code_coverage/ingest_coverage/team_assignment/ingestion_pipeline_painless.json echo "### Code Coverage Team Assignment - Complete" echo ""