Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Synthetics] chore - synthetics unskip test in mki #204273

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import moment from 'moment';
import semver from 'semver';
import { v4 as uuidv4 } from 'uuid';
import { RoleCredentials } from '@kbn/ftr-common-functional-services';
import { formatKibanaNamespace } from '@kbn/synthetics-plugin/common/formatters';
import {
ConfigKey,
HTTPFields,
Expand All @@ -22,21 +23,17 @@ import rawExpect from 'expect';
import { DeploymentAgnosticFtrProviderContext } from '../../../ftr_provider_context';
import { getFixtureJson } from './helpers/get_fixture_json';
import { comparePolicies, getTestSyntheticsPolicy } from './sample_data/test_policy';
import {
INSTALLED_VERSION,
PrivateLocationTestService,
} from '../../../services/synthetics_private_location';
import { PrivateLocationTestService } from '../../../services/synthetics_private_location';
import { addMonitorAPIHelper, keyToOmitList, omitMonitorKeys } from './create_monitor';
import { SyntheticsMonitorTestService } from '../../../services/synthetics_monitor';

export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
describe('PrivateLocationAddMonitor', function () {
// see details: https://github.com/elastic/kibana/issues/204204
this.tags(['failsOnMKI']);
const kibanaServer = getService('kibanaServer');
const supertestAPI = getService('supertestWithoutAuth');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const supertestWithAuth = getService('supertest');
const samlAuth = getService('samlAuth');
const retry = getService('retry');

let testFleetPolicyID: string;
let editorUser: RoleCredentials;
Expand All @@ -49,7 +46,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
const testPrivateLocations = new PrivateLocationTestService(getService);

const addMonitorAPI = async (monitor: any, statusCode = 200) => {
return addMonitorAPIHelper(supertestAPI, monitor, statusCode, editorUser, samlAuth);
return addMonitorAPIHelper(supertestWithoutAuth, monitor, statusCode, editorUser, samlAuth);
};

const deleteMonitor = async (
Expand Down Expand Up @@ -83,7 +80,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
it('add a test private location', async () => {
privateLocations = await testPrivateLocations.setTestLocations([testFleetPolicyID]);

const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.get(SYNTHETICS_API_URLS.SERVICE_LOCATIONS)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -122,7 +119,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {

newMonitor.name = invalidName;

const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand All @@ -135,7 +132,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
message: `Invalid locations specified. Private Location(s) 'invalidLocation' not found. Available private locations are '${privateLocations[0].label}'`,
});

const apiGetResponse = await supertestAPI
const apiGetResponse = await supertestWithoutAuth
.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + `?query="${invalidName}"`)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -203,7 +200,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
},
});

const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.put(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + newMonitorId)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -267,7 +264,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
({ id }) => id !== testFleetPolicyID2
);

await supertestAPI
await supertestWithoutAuth
.put(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + newMonitorId + '?internal=true')
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -317,100 +314,70 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
expect(packagePolicy).eql(undefined);
});

// it('handles spaces', async () => {
// const username = 'admin';
// const password = `${username}-password`;
// const roleName = 'uptime-role';
// const SPACE_ID = `test-space-${uuidv4()}`;
// const SPACE_NAME = `test-space-name ${uuidv4()}`;
// let monitorId = '';
// const monitor = {
// ...httpMonitorJson,
// name: `Test monitor ${uuidv4()}`,
// [ConfigKey.NAMESPACE]: 'default',
// locations: [
// {
// id: testFleetPolicyID,
// agentPolicyId: testFleetPolicyID,
// label: 'Test private location 0',
// isServiceManaged: false,
// geo: {
// lat: 0,
// lon: 0,
// },
// },
// ],
// };

// try {
// await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_NAME });
// await security.role.create(roleName, {
// kibana: [
// {
// feature: {
// uptime: ['all'],
// actions: ['all'],
// },
// spaces: ['*'],
// },
// ],
// });
// await security.user.create(username, {
// password,
// roles: [roleName],
// full_name: 'a kibana user',
// });
// const apiResponse = await supertestWithoutAuth
// .post(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
// .auth(username, password)
// .set('kbn-xsrf', 'true')
// .send(monitor)
// .expect(200);

// const { created_at: createdAt, updated_at: updatedAt } = apiResponse.body;
// expect([createdAt, updatedAt].map((d) => moment(d).isValid())).eql([true, true]);

// expect(omit(apiResponse.body, keyToOmitList)).eql(
// omitMonitorKeys({
// ...monitor,
// [ConfigKey.NAMESPACE]: formatKibanaNamespace(SPACE_ID),
// url: apiResponse.body.url,
// })
// );
// monitorId = apiResponse.body.id;

// const policyResponse = await supertestWithAuth.get(
// '/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'
// );

// const packagePolicy = policyResponse.body.items.find(
// (pkgPolicy: PackagePolicy) =>
// pkgPolicy.id === monitorId + '-' + testFleetPolicyID + `-${SPACE_ID}`
// );

// expect(packagePolicy.policy_id).eql(testFleetPolicyID);
// expect(packagePolicy.name).eql(`${monitor.name}-Test private location 0-${SPACE_ID}`);
// comparePolicies(
// packagePolicy,
// getTestSyntheticsPolicy({
// name: monitor.name,
// id: monitorId,
// location: { id: testFleetPolicyID },
// namespace: formatKibanaNamespace(SPACE_ID),
// spaceId: SPACE_ID,
// })
// );
// await supertestWithoutAuth
// .delete(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
// .auth(username, password)
// .set('kbn-xsrf', 'true')
// .send({ ids: [monitorId] })
// .expect(200);
// } finally {
// await security.user.delete(username);
// await security.role.delete(roleName);
// }
// });
it('handles spaces', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was accidentally commented out. I restored it.

const SPACE_ID = `test-space-${uuidv4()}`;
const SPACE_NAME = `test-space-name ${uuidv4()}`;
const spaceScopedPrivateLocation = await testPrivateLocations.addTestPrivateLocation(
SPACE_ID
);
let monitorId = '';
const monitor = {
...httpMonitorJson,
name: `Test monitor ${uuidv4()}`,
[ConfigKey.NAMESPACE]: 'default',
locations: [spaceScopedPrivateLocation],
};

await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_NAME });
const apiResponse = await supertestWithoutAuth
.post(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
.set('kbn-xsrf', 'true')
.send(monitor);

const { created_at: createdAt, updated_at: updatedAt } = apiResponse.body;
expect([createdAt, updatedAt].map((d) => moment(d).isValid())).eql([true, true]);

expect(omit(apiResponse.body, keyToOmitList)).eql(
omitMonitorKeys({
...monitor,
[ConfigKey.NAMESPACE]: formatKibanaNamespace(SPACE_ID),
url: apiResponse.body.url,
})
);
monitorId = apiResponse.body.id;

const policyResponse = await supertestWithAuth.get(
'/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'
);

const packagePolicy = policyResponse.body.items.find(
(pkgPolicy: PackagePolicy) =>
pkgPolicy.id === monitorId + '-' + spaceScopedPrivateLocation.id + `-${SPACE_ID}`
);

expect(packagePolicy.policy_id).eql(spaceScopedPrivateLocation.id);
expect(packagePolicy.name).eql(
`${monitor.name}-${spaceScopedPrivateLocation.label}-${SPACE_ID}`
);
comparePolicies(
packagePolicy,
getTestSyntheticsPolicy({
name: monitor.name,
id: monitorId,
location: { id: spaceScopedPrivateLocation.id, name: spaceScopedPrivateLocation.label },
namespace: formatKibanaNamespace(SPACE_ID),
spaceId: SPACE_ID,
})
);
await supertestWithoutAuth
.delete(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
.send({ ids: [monitorId] })
.expect(200);
});

it('handles is_tls_enabled true', async () => {
let monitorId = '';
Expand All @@ -430,7 +397,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
};

try {
const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -479,7 +446,7 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
};

try {
const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand Down Expand Up @@ -510,23 +477,21 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {
});

it('handles auto upgrading policies', async () => {
// force a lower version
const lowerVersion = '1.1.1';
await testPrivateLocations.installSyntheticsPackage({ version: lowerVersion });
let monitorId = '';
const privateLocation = await testPrivateLocations.addTestPrivateLocation();

const monitor = {
...httpMonitorJson,
name: `Test monitor ${uuidv4()}`,
[ConfigKey.NAMESPACE]: 'default',
locations: [
{
id: testFleetPolicyID,
label: privateLocations[0].label,
isServiceManaged: false,
},
],
locations: [privateLocation],
};

try {
const apiResponse = await supertestAPI
const apiResponse = await supertestWithoutAuth
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.set(editorUser.apiKeyHeader)
.set(samlAuth.getInternalRequestHeader())
Expand All @@ -540,20 +505,21 @@ export default function ({ getService }: DeploymentAgnosticFtrProviderContext) {

const packagePolicy = policyResponse.body.items.find(
(pkgPolicy: PackagePolicy) =>
pkgPolicy.id === monitorId + '-' + testFleetPolicyID + `-default`
pkgPolicy.id === monitorId + '-' + privateLocation.id + `-default`
);

expect(packagePolicy.package.version).eql(INSTALLED_VERSION);

expect(packagePolicy.package.version).eql(lowerVersion);
await supertestWithAuth.post('/api/fleet/setup').set('kbn-xsrf', 'true').send().expect(200);
const policyResponseAfterUpgrade = await supertestWithAuth.get(
'/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'
);
const packagePolicyAfterUpgrade = policyResponseAfterUpgrade.body.items.find(
(pkgPolicy: PackagePolicy) =>
pkgPolicy.id === monitorId + '-' + testFleetPolicyID + `-default`
);
expect(semver.gte(packagePolicyAfterUpgrade.package.version, INSTALLED_VERSION)).eql(true);
await retry.tryForTime(60 * 1000, async () => {
const policyResponseAfterUpgrade = await supertestWithAuth.get(
'/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'
);
const packagePolicyAfterUpgrade = policyResponseAfterUpgrade.body.items.find(
(pkgPolicy: PackagePolicy) =>
pkgPolicy.id === monitorId + '-' + privateLocation.id + `-default`
);
expect(semver.gt(packagePolicyAfterUpgrade.package.version, lowerVersion)).eql(true);
});
} finally {
await deleteMonitor(monitorId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SyntheticsPrivateLocations } from '@kbn/synthetics-plugin/common/runtim
import { KibanaSupertestProvider } from '@kbn/ftr-common-functional-services';
import { DeploymentAgnosticFtrProviderContext } from '../ftr_provider_context';

export const INSTALLED_VERSION = '1.1.1';
export const INSTALLED_VERSION = '1.2.4';

export class PrivateLocationTestService {
private supertestWithAuth: ReturnType<typeof KibanaSupertestProvider>;
Expand All @@ -23,23 +23,23 @@ export class PrivateLocationTestService {
this.retry = getService('retry');
}

async installSyntheticsPackage() {
async installSyntheticsPackage(
{ version }: { version: string } = { version: INSTALLED_VERSION }
) {
await this.supertestWithAuth
.post('/api/fleet/setup')
.set('kbn-xsrf', 'true')
.send()
.expect(200);
const response = await this.supertestWithAuth
.get(`/api/fleet/epm/packages/synthetics/${INSTALLED_VERSION}`)
// attempt to delete any existing package so we can install specific version
await this.supertestWithAuth
.delete(`/api/fleet/epm/packages/synthetics`)
.set('kbn-xsrf', 'true');
await this.supertestWithAuth
.post(`/api/fleet/epm/packages/synthetics/${version}`)
.set('kbn-xsrf', 'true')
.send({ force: true })
.expect(200);
if (response.body.item.status !== 'installed') {
await this.supertestWithAuth
.post(`/api/fleet/epm/packages/synthetics/${INSTALLED_VERSION}`)
.set('kbn-xsrf', 'true')
.send({ force: true })
.expect(200);
}
}

async addTestPrivateLocation(spaceId?: string) {
Expand Down
Loading