import _ from 'lodash' ; import { TransportResult } from '@elastic/elasticsearch' ; import { elasticsearchServiceMock } from 'src/core/server/mocks' ; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { getESUpgradeStatus } from './es_deprecations_status' ; import fakeDeprecations from './__fixtures__/fake_deprecations.json' ; const fakeIndexNames = Object . keys ( fakeDeprecations . index_settings ) ; const asApiResponse = < T > ( body ) : TransportResult < T > => ( { body , } as TransportResult < T > ) ; describe ( 'getESUpgradeStatus' , ( ) => { const resolvedIndices = { indices : fakeIndexNames . map ( ( indexName ) => { if ( indexName === 'closed_index' ) { return { name : indexName , attributes : [ 'closed' ] } ; } return { name : indexName , attributes : [ 'open' ] } ; } ) , } ; const deprecationsResponse = _ . cloneDeep ( fakeDeprecations ) ; const esClient = elasticsearchServiceMock . createScopedClusterClient ( ) ; esClient . asCurrentUser . migration . deprecations . mockResolvedValue ( asApiResponse ( deprecationsResponse ) ) ; esClient . asCurrentUser . transport . request . mockResolvedValue ( asApiResponse ( { features : [ { feature_name : 'machine_learning' , minimum_index_version : '7.1.1' , migration_status : 'MIGRATION_NEEDED' , indices : [ { index : '.ml-config' , version : '7.1.1' , } , ] , } , ] , migration_status : 'MIGRATION_NEEDED' , } ) ) ; esClient . asCurrentUser . indices . resolveIndex . mockResolvedValue ( asApiResponse ( resolvedIndices ) ) ; it ( 'calls /_migration/deprecations' , async ( ) => { await getESUpgradeStatus ( esClient ) ; expect ( esClient . asCurrentUser . migration . deprecations ) . toHaveBeenCalled ( ) ; } ) ; it ( 'returns the correct shape of data' , async ( ) => { const resp = await getESUpgradeStatus ( esClient ) ; expect ( resp ) . toMatchSnapshot ( ) ; } ) ; it ( 'returns totalCriticalDeprecations > 0 when critical issues found' , async ( ) => { esClient . asCurrentUser . migration . deprecations . mockResolvedValue ( asApiResponse ( { cluster_settings : [ { level : 'critical' , message : 'Do count me' , url : 'https://...' } ] , node_settings : [ ] , ml_settings : [ ] , index_settings : { } , } ) ) ; await expect ( getESUpgradeStatus ( esClient ) ) . resolves . toHaveProperty ( 'totalCriticalDeprecations' , 1 ) ; } ) ; it ( 'returns totalCriticalDeprecations === 0 when no critical issues found' , async ( ) => { esClient . asCurrentUser . migration . deprecations . mockResolvedValue ( asApiResponse ( { cluster_settings : [ { level : 'warning' , message : 'Do not count me' , url : 'https://...' } ] , node_settings : [ ] , ml_settings : [ ] , index_settings : { } , } ) ) ; await expect ( getESUpgradeStatus ( esClient ) ) . resolves . toHaveProperty ( 'totalCriticalDeprecations' , 0 ) ; } ) ; it ( 'filters out system indices returned by upgrade system indices API' , async ( ) => { esClient . asCurrentUser . migration . deprecations . mockResolvedValue ( asApiResponse ( { cluster_settings : [ ] , node_settings : [ ] , ml_settings : [ ] , index_settings : { '.ml-config' : [ { level : 'critical' , message : 'Index created before 7.0' , url : 'https://' , details : '...' , resolve_during_rolling_upgrade : false , } , ] , } , } ) ) ; const upgradeStatus = await getESUpgradeStatus ( esClient ) ; expect ( upgradeStatus . deprecations ) . toHaveLength ( 0 ) ; expect ( upgradeStatus . totalCriticalDeprecations ) . toBe ( 0 ) ; } ) ; } ) ;