-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: axel7083 <[email protected]>
- Loading branch information
Showing
2 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/backend/src/registries/ContainerRegistry.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import { expect, test, vi } from 'vitest'; | ||
import { ContainerRegistry } from './ContainerRegistry'; | ||
import { ContainerJSONEvent } from '@podman-desktop/api'; | ||
Check failure on line 20 in packages/backend/src/registries/ContainerRegistry.spec.ts GitHub Actions / linter, formatters and unit tests / windows-2022
Check failure on line 20 in packages/backend/src/registries/ContainerRegistry.spec.ts GitHub Actions / linter, formatters and unit tests / ubuntu-22.04
|
||
|
||
const mocks = vi.hoisted(() => ({ | ||
onEventMock: vi.fn(), | ||
DisposableCreateMock: vi.fn(), | ||
})); | ||
|
||
vi.mock('@podman-desktop/api', async () => { | ||
return { | ||
Disposable: { | ||
create: mocks.DisposableCreateMock, | ||
}, | ||
containerEngine: { | ||
onEvent: mocks.onEventMock, | ||
}, | ||
}; | ||
}); | ||
|
||
test('ContainerRegistry init', () => { | ||
const registry = new ContainerRegistry(); | ||
registry.init(); | ||
|
||
expect(mocks.onEventMock).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('ContainerRegistry subscribe', () => { | ||
// Get the callback created by the ContainerRegistry | ||
let callback: (event: ContainerJSONEvent) => void; | ||
mocks.onEventMock.mockImplementation((method: (event: ContainerJSONEvent) => void) => { | ||
callback = method; | ||
}); | ||
|
||
// Create the ContainerRegistry and init | ||
const registry = new ContainerRegistry(); | ||
registry.init(); | ||
|
||
// Let's create a dummy subscriber | ||
let subscribedStatus: undefined | string = undefined; | ||
registry.subscribe('random', (status: string) => { | ||
subscribedStatus = status; | ||
}); | ||
|
||
// Generate a fake event | ||
callback({ | ||
status: 'die', | ||
id: 'random', | ||
type: 'container', | ||
}); | ||
|
||
expect(subscribedStatus).toBe('die'); | ||
expect(mocks.DisposableCreateMock).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('ContainerRegistry unsubscribe all if container remove', () => { | ||
// Get the callback created by the ContainerRegistry | ||
let callback: (event: ContainerJSONEvent) => void; | ||
mocks.onEventMock.mockImplementation((method: (event: ContainerJSONEvent) => void) => { | ||
callback = method; | ||
}); | ||
|
||
// Create the ContainerRegistry and init | ||
const registry = new ContainerRegistry(); | ||
registry.init(); | ||
|
||
// Let's create a dummy subscriber | ||
const subscribeMock = vi.fn(); | ||
registry.subscribe('random', subscribeMock); | ||
|
||
// Generate a remove event | ||
callback({ status: 'remove', id: 'random', type: 'container' }); | ||
|
||
// Call it a second time | ||
callback({ status: 'remove', id: 'random', type: 'container' }); | ||
|
||
// Our subscriber should only have been called once, the first, after it should have been removed. | ||
expect(subscribeMock).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('ContainerRegistry subscriber disposed should not be called', () => { | ||
// Get the callback created by the ContainerRegistry | ||
let callback: (event: ContainerJSONEvent) => void; | ||
mocks.onEventMock.mockImplementation((method: (event: ContainerJSONEvent) => void) => { | ||
callback = method; | ||
}); | ||
|
||
mocks.DisposableCreateMock.mockImplementation(callback => ({ | ||
dispose: () => callback(), | ||
})); | ||
|
||
// Create the ContainerRegistry and init | ||
const registry = new ContainerRegistry(); | ||
registry.init(); | ||
|
||
// Let's create a dummy subscriber | ||
const subscribeMock = vi.fn(); | ||
const disposable = registry.subscribe('random', subscribeMock); | ||
disposable.dispose(); | ||
|
||
// Generate a random event | ||
callback({ status: 'die', id: 'random', type: 'container' }); | ||
|
||
// never should have been called | ||
expect(subscribeMock).toHaveBeenCalledTimes(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters