diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/synthetics/create_monitor_private_location.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/synthetics/create_monitor_private_location.ts index a940a5aec756b..c140ba7319e25 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/synthetics/create_monitor_private_location.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/synthetics/create_monitor_private_location.ts @@ -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, @@ -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; @@ -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 ( @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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 () => { + 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 = ''; @@ -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()) @@ -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()) @@ -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()) @@ -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); } diff --git a/x-pack/test/api_integration/deployment_agnostic/services/synthetics_private_location.ts b/x-pack/test/api_integration/deployment_agnostic/services/synthetics_private_location.ts index 8b3c95e0b9489..515c65db05277 100644 --- a/x-pack/test/api_integration/deployment_agnostic/services/synthetics_private_location.ts +++ b/x-pack/test/api_integration/deployment_agnostic/services/synthetics_private_location.ts @@ -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; @@ -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) {