Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 76 additions & 20 deletions src/project/utils/__tests__/getProjectServices.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { faker } from '@faker-js/faker/locale/af_ZA'
import { getAwsDbConfigMock } from '@tests/mocks/aws'
import { getAwsAppConfigMock, getAwsDbConfigMock } from '@tests/mocks/aws'
import { PROVIDER, SERVICE_TYPE } from '@src/constants'
import type { ProjectConfiguration } from '@src/project/types'
import { getProjectServices } from '../getProjectServices'
import { getProjectMock } from '@tests/mocks/project'
import { getProjectServices } from '@src/project/utils/getProjectServices'
import { faker } from '@faker-js/faker'
import { ENVIRONMENT } from '@src/project/constants'
import type { ServiceConfiguration } from '@src/services/registry'

describe('getProjectServices', () => {
it('registers the aws provider and networking', () => {
const config: ProjectConfiguration = {
state: {
bucket: faker.internet.domainWord(),
lockTable: faker.internet.domainWord(),
statePath: `${faker.internet.domainWord()}.tfstate`,
},
environments: {
production: {
mysql: getAwsDbConfigMock(),
},
},
}
const environment = ENVIRONMENT.PRODUCTION

const services = getProjectServices(config, 'production')
it('registers the aws provider and networking', () => {
const dbConfig = getAwsDbConfigMock() as ServiceConfiguration
const config = getProjectMock([dbConfig])
const services = getProjectServices(config, environment)

// Service count: state + mysql + provider + networking = 4
expect(services).toHaveLength(4)
Expand All @@ -32,7 +25,70 @@ describe('getProjectServices', () => {
]),
)
expect(services.every((srv) => srv.provider === PROVIDER.AWS)).toBe(true)
expect(config.environments.production?.mysql.region).not.toBeUndefined()
expect(services.every((srv) => srv.region === config.environments.production?.mysql.region))
expect(services.every((srv) => srv.region === dbConfig.region))
})

it('allows explicit declarations of provider services', () => {
const config = getProjectMock([
{
type: SERVICE_TYPE.PROVIDER,
region: 'eu-central-1',
name: 'aws-provider',
},
getAwsDbConfigMock() as ServiceConfiguration,
])

const services = getProjectServices(config, environment)
expect(services.filter((srv) => srv.type === SERVICE_TYPE.PROVIDER)).toHaveLength(1)
})

it('allows explicit declarations of networking services', () => {
const config = getProjectMock([
{
type: SERVICE_TYPE.NETWORKING,
region: 'eu-central-1',
name: 'aws-networking',
},
getAwsDbConfigMock() as ServiceConfiguration,
])

const services = getProjectServices(config, environment)
expect(services.filter((srv) => srv.type === SERVICE_TYPE.NETWORKING)).toHaveLength(1)
})

it('allows explicit declarations of cluster services', () => {
const config = getProjectMock([
{
type: SERVICE_TYPE.CLUSTER,
region: 'eu-central-1',
name: 'aws-cluster',
clusterName: faker.internet.domainWord(),
},
getAwsAppConfigMock() as ServiceConfiguration,
getAwsDbConfigMock() as ServiceConfiguration,
])

const services = getProjectServices(config, environment)
expect(services.filter((srv) => srv.type === SERVICE_TYPE.CLUSTER)).toHaveLength(1)
})

it('registers requirements automatically', () => {
const config = getProjectMock([
getAwsAppConfigMock() as ServiceConfiguration,
getAwsDbConfigMock() as ServiceConfiguration,
])

const services = getProjectServices(config, environment)
expect(services.map((srv) => srv.type)).toEqual(
expect.arrayContaining([
SERVICE_TYPE.STATE,
SERVICE_TYPE.PROVIDER,
SERVICE_TYPE.NETWORKING,
SERVICE_TYPE.CLUSTER,
SERVICE_TYPE.DNS,
SERVICE_TYPE.APP,
SERVICE_TYPE.MYSQL,
]),
)
})
})