-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check Tekton EventListeners CRD version to use matching api
- Loading branch information
Showing
8 changed files
with
177 additions
and
9 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
103 changes: 103 additions & 0 deletions
103
frontend/packages/dev-console/src/components/pipelines/__tests__/pipeline-operator.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,103 @@ | ||
import { k8sList } from '@console/internal/module/k8s'; | ||
import { ClusterServiceVersionKind } from '@console/operator-lifecycle-manager'; | ||
import { getPipelineOperatorVersion } from '../utils/pipeline-operator'; | ||
|
||
jest.mock('@console/internal/module/k8s', () => ({ | ||
k8sList: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const k8sListMock = k8sList as jest.Mock; | ||
|
||
describe('getPipelineOperatorVersion', () => { | ||
it('should fetch the ClusterServiceVersion from the api', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('1.0.1'); | ||
expect(version.major).toBe(1); | ||
expect(version.minor).toBe(0); | ||
expect(version.patch).toBe(1); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return the active ClusterServiceVersion if multiple returns', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
status: { phase: 'Pending' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('1.0.1'); | ||
expect(version.major).toBe(1); | ||
expect(version.minor).toBe(0); | ||
expect(version.patch).toBe(1); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should fetch the latest (highest) ClusterServiceVersion from the api', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v10.11.12' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('10.11.12'); | ||
expect(version.major).toBe(10); | ||
expect(version.minor).toBe(11); | ||
expect(version.patch).toBe(12); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return null if there is no ClusterServiceVersion available', async () => { | ||
k8sListMock.mockReturnValueOnce(Promise.resolve([])); | ||
await expect(getPipelineOperatorVersion('unit-test')).resolves.toBe(null); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return null if there is no matching ClusterServiceVersion available', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'another-operator.v1.1.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
status: { phase: 'Installing' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
await expect(getPipelineOperatorVersion('unit-test')).resolves.toBe(null); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
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
27 changes: 27 additions & 0 deletions
27
frontend/packages/dev-console/src/components/pipelines/utils/pipeline-operator.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,27 @@ | ||
import { compare, parse, SemVer } from 'semver'; | ||
import { | ||
ClusterServiceVersionKind, | ||
ClusterServiceVersionModel, | ||
ClusterServiceVersionPhase, | ||
} from '@console/operator-lifecycle-manager'; | ||
import { k8sList } from '@console/internal/module/k8s'; | ||
|
||
export const getPipelineOperatorVersion = async (namespace: string): Promise<SemVer | null> => { | ||
const csvs: ClusterServiceVersionKind[] = await k8sList(ClusterServiceVersionModel, { | ||
ns: namespace, | ||
}); | ||
const matchingCSVs = csvs.filter( | ||
(csv) => | ||
csv.metadata?.name?.startsWith('openshift-pipelines-operator') && | ||
csv.status?.phase === ClusterServiceVersionPhase.CSVPhaseSucceeded, | ||
); | ||
const versions = matchingCSVs | ||
.map((csv) => parse(csv.metadata?.name?.replace(/^openshift-pipelines-operator.v?/, ''))) | ||
.filter(Boolean); | ||
// Orders from small (oldest) to highest (newest) versions | ||
versions.sort(compare); | ||
if (versions.length > 0) { | ||
return versions[versions.length - 1]; | ||
} | ||
return null; | ||
}; |
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