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

test: add test for app usecase #48

Merged
merged 1 commit into from
Jan 13, 2023
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
79 changes: 79 additions & 0 deletions __tests__/app/usecases/cleanup-legacy-test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {Inputs} from '@app/inputs'
import {cleanupLegacyStacks} from '@app/usecases/cleanup-legacy-stacks'
import {Logger} from '@entities/lib'
import {LocalWorkspace} from '@pulumi/pulumi/automation'
import {subHours} from 'date-fns'
import {mock} from 'jest-mock-extended'
import {assertInfos} from '../../fixtures/assertions'

const testWorkspace = mock<LocalWorkspace>()
const testLogger = mock<Logger>()

jest.mock('@pulumi/pulumi/automation', () => {
return {
LocalWorkspace: {
async create() {
return testWorkspace
}
}
}
})

jest.mock('@actions/core', () => {
return {
info(message: string) {
testLogger.info(message)
}
}
})

describe('cleanupLegacyStacks', () => {
const now = new Date('2021-01-01 12:00:00')
const lastUpdate = subHours(now, 3).toISOString()

beforeEach(() => {
jest.useFakeTimers()
jest.setSystemTime(now)
testWorkspace.listStacks.mockResolvedValue([
{
name: 'dev-1',
current: false,
updateInProgress: false,
lastUpdate,
resourceCount: 1
}
])
})

it('cleans up legacy stacks defined by the inputs', async () => {
const config = `
policies:
clean-dev:
match:
name: 'dev*'
ttl:
hours: 2
`
const inputs: Inputs = {
preview: true,
verbose: true,
config,
workDir: '.'
}

await cleanupLegacyStacks(inputs)

assertInfos(testLogger, [
'checking stack dev-1',
' checking policy clean-dev',
' [fail] checked [updateInProgress=false]',
' [fail] checked stack age [2021-01-01T09:00:00.000Z] against ttl [2 hours]',
' [fail] checked name [dev-1] against pattern [dev*]',
' [result] legacy stack - marking for cleanup',
'[PREVIEW] Found 1 legacy dev stack(s) out of 1 total',
'[PREVIEW] Destroyed legacy stack: dev-1',
'[PREVIEW] Removed legacy stack: dev-1',
'[PREVIEW] Removed 1 legacy stacks, 0 remaining'
])
})
})