Skip to content

Commit

Permalink
[8.x] [Synthetics] chore - synthetics unskip test in mki (#204273) (#…
Browse files Browse the repository at this point in the history
…205516)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Synthetics] chore - synthetics unskip test in mki
(#204273)](#204273)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Dominique
Clarke","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-03T16:06:00Z","message":"[Synthetics]
chore - synthetics unskip test in mki (#204273)\n\n##
Summary\r\n\r\nUnskip test that was previously failing in
MKI.\r\n\r\nThese tests have been run against MKI.\r\n\r\nBackground on
the test\r\n--\r\nThe test confirms that synthetics integration policies
auto update when\r\nthe package version is updated. It attempts to
install an older version\r\nof the package and then run fleet setup to
install a newer version of\r\nthe package and trigger auto
upgrade\r\n\r\nWhy it failed\r\n--\r\nThe test expected a lower version
of the package to be installed, but a\r\nmore recent version was already
installed. Therefore, when we went to\r\ncreate synthetics integration
policies they were installed with the most\r\nrecent version.\r\n\r\nHow
we fixed it\r\n--\r\nBefore this test, we now uninstall the Synthetics
integration to ensure\r\nwe can force install an earlier
version.","sha":"b851db30137aa51c3390dee903870685feb754ae","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["chore","release_note:skip","v9.0.0","backport:prev-minor","Team:obs-ux-management"],"title":"[Synthetics]
chore - synthetics unskip test in
mki","number":204273,"url":"https://github.com/elastic/kibana/pull/204273","mergeCommit":{"message":"[Synthetics]
chore - synthetics unskip test in mki (#204273)\n\n##
Summary\r\n\r\nUnskip test that was previously failing in
MKI.\r\n\r\nThese tests have been run against MKI.\r\n\r\nBackground on
the test\r\n--\r\nThe test confirms that synthetics integration policies
auto update when\r\nthe package version is updated. It attempts to
install an older version\r\nof the package and then run fleet setup to
install a newer version of\r\nthe package and trigger auto
upgrade\r\n\r\nWhy it failed\r\n--\r\nThe test expected a lower version
of the package to be installed, but a\r\nmore recent version was already
installed. Therefore, when we went to\r\ncreate synthetics integration
policies they were installed with the most\r\nrecent version.\r\n\r\nHow
we fixed it\r\n--\r\nBefore this test, we now uninstall the Synthetics
integration to ensure\r\nwe can force install an earlier
version.","sha":"b851db30137aa51c3390dee903870685feb754ae"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204273","number":204273,"mergeCommit":{"message":"[Synthetics]
chore - synthetics unskip test in mki (#204273)\n\n##
Summary\r\n\r\nUnskip test that was previously failing in
MKI.\r\n\r\nThese tests have been run against MKI.\r\n\r\nBackground on
the test\r\n--\r\nThe test confirms that synthetics integration policies
auto update when\r\nthe package version is updated. It attempts to
install an older version\r\nof the package and then run fleet setup to
install a newer version of\r\nthe package and trigger auto
upgrade\r\n\r\nWhy it failed\r\n--\r\nThe test expected a lower version
of the package to be installed, but a\r\nmore recent version was already
installed. Therefore, when we went to\r\ncreate synthetics integration
policies they were installed with the most\r\nrecent version.\r\n\r\nHow
we fixed it\r\n--\r\nBefore this test, we now uninstall the Synthetics
integration to ensure\r\nwe can force install an earlier
version.","sha":"b851db30137aa51c3390dee903870685feb754ae"}}]}]
BACKPORT-->

Co-authored-by: Dominique Clarke <[email protected]>
  • Loading branch information
kibanamachine and dominiqueclarke authored Jan 3, 2025
1 parent 1ab4481 commit 2eb0784
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 137 deletions.
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,19 +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 () {
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 @@ -47,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 @@ -81,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 @@ -120,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 @@ -133,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 @@ -201,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 @@ -265,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 @@ -315,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 () => {
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 @@ -428,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 @@ -477,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 @@ -508,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 @@ -538,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

0 comments on commit 2eb0784

Please sign in to comment.