-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: test InstructLab in a container
Signed-off-by: Jeff MAURY <[email protected]>
- Loading branch information
Showing
16 changed files
with
777 additions
and
19 deletions.
There are no files selected for viewing
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,3 @@ | ||
{ | ||
"default": "docker.io/redhat/instructlab@sha256:c6b2ecb4547b1f43b5539ee99bdbf5c9ae40599fabe1c740622295d9721b91c4" | ||
} |
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
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
156 changes: 156 additions & 0 deletions
156
packages/backend/src/managers/instructlab/instructlabManager.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,156 @@ | ||
/********************************************************************** | ||
* 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 { TaskRegistry } from '../../registries/TaskRegistry'; | ||
import { beforeAll, beforeEach, expect, test, vi } from 'vitest'; | ||
import type { ContainerCreateResult, ContainerInfo, ImageInfo, Webview } from '@podman-desktop/api'; | ||
import { containerEngine, EventEmitter } from '@podman-desktop/api'; | ||
import type { PodmanConnection } from '../podmanConnection'; | ||
import { INSTRUCTLAB_CONTAINER_LABEL, InstructlabManager } from './instructlabManager'; | ||
import { ContainerRegistry } from '../../registries/ContainerRegistry'; | ||
import { TestEventEmitter } from '../../tests/utils'; | ||
import { VMType } from '@shared/src/models/IPodman'; | ||
import type { Task } from '@shared/src/models/ITask'; | ||
import instructlab_images from '../../assets/instructlab-images.json'; | ||
|
||
vi.mock('@podman-desktop/api', () => { | ||
return { | ||
EventEmitter: vi.fn(), | ||
containerEngine: { | ||
listContainers: vi.fn(), | ||
listImages: vi.fn(), | ||
createContainer: vi.fn(), | ||
onEvent: vi.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
const taskRegistry = new TaskRegistry({ postMessage: vi.fn().mockResolvedValue(undefined) } as unknown as Webview); | ||
|
||
const podmanConnection: PodmanConnection = { | ||
onPodmanConnectionEvent: vi.fn(), | ||
findRunningContainerProviderConnection: vi.fn(), | ||
} as unknown as PodmanConnection; | ||
|
||
let instructlabManager: InstructlabManager; | ||
|
||
beforeAll(() => { | ||
vi.mocked(EventEmitter).mockImplementation(() => new TestEventEmitter() as unknown as EventEmitter<unknown>); | ||
}); | ||
|
||
beforeEach(() => { | ||
const containerRegistry = new ContainerRegistry(); | ||
containerRegistry.init(); | ||
instructlabManager = new InstructlabManager('', taskRegistry, podmanConnection, containerRegistry); | ||
}); | ||
|
||
test('getInstructLabContainer should return undefined if no containers', async () => { | ||
vi.mocked(containerEngine.listContainers).mockResolvedValue([]); | ||
const containerId = await instructlabManager.getInstructLabContainer(); | ||
expect(containerId).toBeUndefined(); | ||
}); | ||
|
||
test('getInstructLabContainer should return undefined if no instructlab container', async () => { | ||
vi.mocked(containerEngine.listContainers).mockResolvedValue([{ Id: 'dummyId' } as unknown as ContainerInfo]); | ||
const containerId = await instructlabManager.getInstructLabContainer(); | ||
expect(containerId).toBeUndefined(); | ||
}); | ||
|
||
test('getInstructLabContainer should return id if instructlab container', async () => { | ||
vi.mocked(containerEngine.listContainers).mockResolvedValue([ | ||
{ | ||
Id: 'dummyId', | ||
State: 'running', | ||
Labels: { [`${INSTRUCTLAB_CONTAINER_LABEL}`]: 'dummyLabel' }, | ||
} as unknown as ContainerInfo, | ||
]); | ||
const containerId = await instructlabManager.getInstructLabContainer(); | ||
expect(containerId).toBe('dummyId'); | ||
}); | ||
|
||
test('requestCreateInstructlabContainer throws error if no podman connection', async () => { | ||
const containerIdPromise = instructlabManager.requestCreateInstructlabContainer({}); | ||
await expect(containerIdPromise).rejects.toBeInstanceOf(Error); | ||
}); | ||
|
||
async function waitTasks(containerId: string, nb: number): Promise<Task[]> { | ||
return vi.waitFor(() => { | ||
const tasks = taskRegistry.getTasksByLabels({ trackingId: containerId }); | ||
if (tasks.length !== nb) { | ||
throw new Error('not completed'); | ||
} | ||
return tasks; | ||
}); | ||
} | ||
|
||
test('requestCreateInstructlabContainer returns id and error if listImage returns error', async () => { | ||
vi.mocked(podmanConnection.findRunningContainerProviderConnection).mockReturnValue({ | ||
name: 'Podman Machine', | ||
vmType: VMType.UNKNOWN, | ||
type: 'podman', | ||
status: () => 'started', | ||
endpoint: { | ||
socketPath: 'socket.sock', | ||
}, | ||
}); | ||
vi.mocked(containerEngine.listImages).mockRejectedValue(new Error()); | ||
const containerId = await instructlabManager.requestCreateInstructlabContainer({}); | ||
expect(containerId).toBeDefined(); | ||
const tasks = await waitTasks(containerId, 2); | ||
expect(tasks.some(task => task.state === 'error')).toBeTruthy(); | ||
}); | ||
|
||
test('requestCreateInstructlabContainer returns id and error if listImage returns image', async () => { | ||
vi.mocked(podmanConnection.findRunningContainerProviderConnection).mockReturnValue({ | ||
name: 'Podman Machine', | ||
vmType: VMType.UNKNOWN, | ||
type: 'podman', | ||
status: () => 'started', | ||
endpoint: { | ||
socketPath: 'socket.sock', | ||
}, | ||
}); | ||
vi.mocked(containerEngine.listImages).mockResolvedValue([ | ||
{ RepoTags: [instructlab_images.default] } as unknown as ImageInfo, | ||
]); | ||
const containerId = await instructlabManager.requestCreateInstructlabContainer({}); | ||
expect(containerId).toBeDefined(); | ||
const tasks = await waitTasks(containerId, 3); | ||
expect(tasks.some(task => task.state === 'error')).toBeTruthy(); | ||
}); | ||
|
||
test('requestCreateInstructlabContainer returns id and no error if createContainer returns id', async () => { | ||
vi.mocked(podmanConnection.findRunningContainerProviderConnection).mockReturnValue({ | ||
name: 'Podman Machine', | ||
vmType: VMType.UNKNOWN, | ||
type: 'podman', | ||
status: () => 'started', | ||
endpoint: { | ||
socketPath: 'socket.sock', | ||
}, | ||
}); | ||
vi.mocked(containerEngine.listImages).mockResolvedValue([ | ||
{ RepoTags: [instructlab_images.default] } as unknown as ImageInfo, | ||
]); | ||
vi.mocked(containerEngine.createContainer).mockResolvedValue({ | ||
id: 'containerId', | ||
} as unknown as ContainerCreateResult); | ||
const containerId = await instructlabManager.requestCreateInstructlabContainer({}); | ||
expect(containerId).toBeDefined(); | ||
const tasks = await waitTasks(containerId, 3); | ||
expect(tasks.some(task => task.state === 'error')).toBeFalsy(); | ||
}); |
Oops, something went wrong.