Skip to content

Commit

Permalink
[kbnArchiver] convert archive names to root-relative paths (#101839)
Browse files Browse the repository at this point in the history
* [kbnArchiver] convert archive names to root-relative paths

* ensure that newly multiline hooks are explicitly async

* missed a newly multiline hook

* fix exists check

* avoid extra lines by wrapping arrow body in {}

* one block more

* fix errant `name` variable

Co-authored-by: spalger <spalger@users.noreply.github.com>
  • Loading branch information
Spencer and spalger authored Jun 10, 2021
1 parent 3559d37 commit 7917e3c
Show file tree
Hide file tree
Showing 40 changed files with 234 additions and 102 deletions.
23 changes: 2 additions & 21 deletions packages/kbn-test/src/kbn_archiver_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ export function runKbnArchiverCli() {
new RunWithCommands({
description: 'Import/export saved objects from archives, for testing',
globalFlags: {
string: ['config', 'space', 'kibana-url', 'dir'],
string: ['config', 'space', 'kibana-url'],
help: `
--space space id to operate on, defaults to the default space
--config optional path to an FTR config file that will be parsed and used for defaults
--kibana-url set the url that kibana can be reached at, uses the "servers.kibana" setting from --config by default
--dir directory that contains exports to be imported, or where exports will be saved, uses the "kbnArchiver.directory"
setting from --config by default
`,
},
async extendContext({ log, flags }) {
Expand Down Expand Up @@ -79,23 +77,6 @@ export function runKbnArchiverCli() {
);
}

let importExportDir;
if (flags.dir) {
if (typeof flags.dir !== 'string') {
throw createFlagError('expected --dir to be a string');
}

importExportDir = flags.dir;
} else if (config) {
importExportDir = config.get('kbnArchiver.directory');
}

if (!importExportDir) {
throw createFlagError(
'--config does not include a kbnArchiver.directory, specify it or include --dir flag'
);
}

const space = flags.space;
if (!(space === undefined || typeof space === 'string')) {
throw createFlagError('--space must be a string');
Expand All @@ -106,7 +87,7 @@ export function runKbnArchiverCli() {
kbnClient: new KbnClient({
log,
url: kibanaUrl,
importExportDir,
importExportBaseDir: process.cwd(),
}),
};
},
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-test/src/kbn_client/kbn_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface KbnClientOptions {
certificateAuthorities?: Buffer[];
log: ToolingLog;
uiSettingDefaults?: UiSettingValues;
importExportDir?: string;
importExportBaseDir?: string;
}

export class KbnClient {
Expand Down Expand Up @@ -67,7 +67,7 @@ export class KbnClient {
this.log,
this.requester,
this.savedObjects,
options.importExportDir
options.importExportBaseDir
);
}

Expand Down
26 changes: 14 additions & 12 deletions packages/kbn-test/src/kbn_client/kbn_client_import_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import { inspect } from 'util';
import Fs from 'fs/promises';
import { existsSync } from 'fs';
import Path from 'path';

import FormData from 'form-data';
import { ToolingLog, isAxiosResponseError, createFailError } from '@kbn/dev-utils';
import { ToolingLog, isAxiosResponseError, createFailError, REPO_ROOT } from '@kbn/dev-utils';

import { KbnClientRequester, uriencode, ReqOptions } from './kbn_client_requester';
import { KbnClientSavedObjects } from './kbn_client_saved_objects';
Expand Down Expand Up @@ -39,26 +40,27 @@ export class KbnClientImportExport {
public readonly log: ToolingLog,
public readonly requester: KbnClientRequester,
public readonly savedObjects: KbnClientSavedObjects,
public readonly dir?: string
public readonly baseDir: string = REPO_ROOT
) {}

private resolvePath(path: string) {
if (!Path.extname(path)) {
path = `${path}.json`;
}

if (!this.dir && !Path.isAbsolute(path)) {
const absolutePath = Path.resolve(this.baseDir, path);
if (!existsSync(absolutePath)) {
throw new Error(
'unable to resolve relative path to import/export without a configured dir, either path absolute path or specify --dir'
`unable to resolve path [${path}] to import/export, resolved relative to [${this.baseDir}]`
);
}

return this.dir ? Path.resolve(this.dir, path) : path;
return absolutePath;
}

async load(name: string, options?: { space?: string }) {
const src = this.resolvePath(name);
this.log.debug('resolved import for', name, 'to', src);
async load(path: string, options?: { space?: string }) {
const src = this.resolvePath(path);
this.log.debug('resolved import for', path, 'to', src);

const objects = await parseArchive(src);
this.log.info('importing', objects.length, 'saved objects', { space: options?.space });
Expand Down Expand Up @@ -91,8 +93,8 @@ export class KbnClientImportExport {
}
}

async unload(name: string, options?: { space?: string }) {
const src = this.resolvePath(name);
async unload(path: string, options?: { space?: string }) {
const src = this.resolvePath(path);
this.log.debug('unloading docs from archive at', src);

const objects = await parseArchive(src);
Expand All @@ -110,8 +112,8 @@ export class KbnClientImportExport {
this.log.success(deleted, 'saved objects deleted');
}

async save(name: string, options: { types: string[]; space?: string }) {
const dest = this.resolvePath(name);
async save(path: string, options: { types: string[]; space?: string }) {
const dest = this.resolvePath(path);
this.log.debug('saving export to', dest);

const resp = await this.req(options.space, {
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/kql_telemetry/kql_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ export default function ({ getService }: FtrProviderContext) {
const es = getService('es');

describe('telemetry API', () => {
before(() => kibanaServer.importExport.load('saved_objects/basic'));
after(() => kibanaServer.importExport.unload('saved_objects/basic'));
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should increment the opt *in* counter in the .kibana/kql-telemetry document', async () => {
await supertest
Expand Down
5 changes: 4 additions & 1 deletion test/api_integration/apis/saved_objects/bulk_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export default function ({ getService }: FtrProviderContext) {
before(async () => {
KIBANA_VERSION = await getKibanaVersion(getService);
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID });
await kibanaServer.importExport.load('saved_objects/basic', { space: SPACE_ID });
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json',
{ space: SPACE_ID }
);
});

after(() => kibanaServer.spaces.delete(SPACE_ID));
Expand Down
10 changes: 8 additions & 2 deletions test/api_integration/apis/saved_objects/bulk_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ export default function ({ getService }: FtrProviderContext) {

before(async () => {
KIBANA_VERSION = await getKibanaVersion(getService);
await kibanaServer.importExport.load('saved_objects/basic');
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

after(() => kibanaServer.importExport.unload('saved_objects/basic'));
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 200 with individual responses', async () =>
await supertest
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/saved_objects/bulk_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ export default function ({ getService }: FtrProviderContext) {
const kibanaServer = getService('kibanaServer');

describe('bulkUpdate', () => {
before(() => kibanaServer.importExport.load('saved_objects/basic'));
after(() => kibanaServer.importExport.unload('saved_objects/basic'));
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 200', async () => {
const response = await supertest
Expand Down
10 changes: 8 additions & 2 deletions test/api_integration/apis/saved_objects/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ export default function ({ getService }: FtrProviderContext) {

before(async () => {
KIBANA_VERSION = await getKibanaVersion(getService);
await kibanaServer.importExport.load('saved_objects/basic');
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

after(() => kibanaServer.importExport.unload('saved_objects/basic'));
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 200', async () => {
await supertest
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/saved_objects/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ export default function ({ getService }: FtrProviderContext) {
const kibanaServer = getService('kibanaServer');

describe('delete', () => {
before(() => kibanaServer.importExport.load('saved_objects/basic'));
after(() => kibanaServer.importExport.unload('saved_objects/basic'));
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 200 when deleting a doc', async () =>
await supertest
Expand Down
5 changes: 4 additions & 1 deletion test/api_integration/apis/saved_objects/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default function ({ getService }: FtrProviderContext) {
before(async () => {
KIBANA_VERSION = await getKibanaVersion(getService);
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID });
await kibanaServer.importExport.load('saved_objects/basic', { space: SPACE_ID });
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json',
{ space: SPACE_ID }
);
});

after(() => kibanaServer.spaces.delete(SPACE_ID));
Expand Down
48 changes: 34 additions & 14 deletions test/api_integration/apis/saved_objects/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ export default function ({ getService }: FtrProviderContext) {
describe('find', () => {
before(async () => {
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID });
await kibanaServer.importExport.load('saved_objects/basic', { space: SPACE_ID });
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json',
{ space: SPACE_ID }
);

await kibanaServer.spaces.create({ id: `${SPACE_ID}-foo`, name: `${SPACE_ID}-foo` });
await kibanaServer.importExport.load('saved_objects/basic/foo-ns', {
space: `${SPACE_ID}-foo`,
});
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic/foo-ns.json',
{
space: `${SPACE_ID}-foo`,
}
);
});

after(async () => {
Expand Down Expand Up @@ -255,10 +261,18 @@ export default function ({ getService }: FtrProviderContext) {
});

describe('`has_reference` and `has_reference_operator` parameters', () => {
before(() => kibanaServer.importExport.load('saved_objects/references', { space: SPACE_ID }));
after(() =>
kibanaServer.importExport.unload('saved_objects/references', { space: SPACE_ID })
);
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/references.json',
{ space: SPACE_ID }
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/references.json',
{ space: SPACE_ID }
);
});

it('search for a reference', async () => {
await supertest
Expand Down Expand Up @@ -319,12 +333,18 @@ export default function ({ getService }: FtrProviderContext) {
});

describe('searching for special characters', () => {
before(() =>
kibanaServer.importExport.load('saved_objects/find_edgecases', { space: SPACE_ID })
);
after(() =>
kibanaServer.importExport.unload('saved_objects/find_edgecases', { space: SPACE_ID })
);
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/find_edgecases.json',
{ space: SPACE_ID }
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/find_edgecases.json',
{ space: SPACE_ID }
);
});

it('can search for objects with dashes', async () =>
await supertest
Expand Down
10 changes: 8 additions & 2 deletions test/api_integration/apis/saved_objects/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ export default function ({ getService }: FtrProviderContext) {

before(async () => {
KIBANA_VERSION = await getKibanaVersion(getService);
await kibanaServer.importExport.load('saved_objects/basic');
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(() => kibanaServer.importExport.unload('saved_objects/basic'));

it('should return 200', async () =>
await supertest
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/saved_objects/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ export default function ({ getService }: FtrProviderContext) {
};

describe('with basic data existing', () => {
before(() => kibanaServer.importExport.load('saved_objects/basic'));
after(() => kibanaServer.importExport.unload('saved_objects/basic'));
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 415 when no file passed in', async () => {
await supertest
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/saved_objects/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ export default function ({ getService }: FtrProviderContext) {
});

describe('with kibana index', () => {
before(() => kibanaServer.importExport.load('saved_objects/basic'));
after(() => kibanaServer.importExport.unload('saved_objects/basic'));
before(async () => {
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});
after(async () => {
await kibanaServer.importExport.unload(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json'
);
});

it('should return 200', async () =>
await supertest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export default function ({ getService }: FtrProviderContext) {
describe('with basic data existing', () => {
before(async () => {
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID });
await kibanaServer.importExport.load('saved_objects/basic', { space: SPACE_ID });
await kibanaServer.importExport.load(
'test/api_integration/fixtures/kbn_archiver/saved_objects/basic.json',
{ space: SPACE_ID }
);
});
after(() => kibanaServer.spaces.delete(SPACE_ID));

Expand Down
Loading

0 comments on commit 7917e3c

Please sign in to comment.