Skip to content

Commit

Permalink
Added odo catalog list components -o json to get list of available co…
Browse files Browse the repository at this point in the history
…mponent types and versions
  • Loading branch information
sudhirverma committed Oct 3, 2019
1 parent c984206 commit 7b41220
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
14 changes: 7 additions & 7 deletions src/odo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class Command {
return `odo list --app ${app} --project ${project} -o json`;
}
static listCatalogComponents() {
return `odo catalog list components`;
return `odo catalog list components -o json`;
}
static listCatalogServices () {
return `odo catalog list services`;
Expand Down Expand Up @@ -706,7 +706,9 @@ export class OdoImpl implements Odo {

public async getComponentTypes(): Promise<string[]> {
const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponents());
return result.stdout.trim().split('\n').slice(1).map((value) => value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|')[0]);
const items = JSON.parse(result.stdout).items;
const componentTypes: Array<string> = items.map((value: { metadata: { name: any; }; }) => value.metadata.name);
return componentTypes;
}

public async getComponentChildren(component: OpenShiftObject): Promise<OpenShiftObject[]> {
Expand Down Expand Up @@ -764,11 +766,9 @@ export class OdoImpl implements Odo {

public async getComponentTypeVersions(componentName: string) {
const result: cliInstance.CliExitData = await this.execute(Command.listCatalogComponents());
const versions = result.stdout.trim().split('\n').slice(1).filter((value) => {
const data = value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|');
return data[0] === componentName;
}).map((value) => value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|')[2]);
return versions && versions.length > 0 ? versions[0].split(',') : [];
const items = JSON.parse(result.stdout).items;
const versions = items.find((value: { metadata: { name: string; }; }) => value.metadata.name === componentName);
return versions.spec.allTags;
}

public async getServiceTemplates(): Promise<string[]> {
Expand Down
67 changes: 36 additions & 31 deletions test/unit/odo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,35 @@ suite("odo", () => {
});

suite("catalog integration", () => {
const http = 'httpd';
const nodejs = 'nodejs';
const python = 'python';

const odoCatalog: string = [
`NAME PROJECT TAGS`,
`${nodejs} openshift 1.0`,
`${python} openshift 1.0,2.0`,
`${http} openshift 2.2,2.3,latest`
].join('\n');
let result: string[];
const odoCatalog = JSON.stringify({
kind : "ComponentTypeList",
apiVersion : "odo.openshift.io/v1alpha1",
items: [
{
kind : "ComponentType",
apiVersion : "odo.openshift.io/v1alpha1",
metadata: {
name : "nodejs",
namespace : "openshift",
creationTimestamp : null
},
spec: {
allTags: [
"0.10",
"10",
"4",
"6",
"8",
"8-RHOAR",
"latest",
]
}
}
]
});

const catalogData: CliExitData = {
error: null,
stderr: '',
Expand All @@ -410,32 +428,19 @@ suite("odo", () => {

setup(async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').resolves(catalogData);
result = await odoCli.getComponentTypes();
});

test("getComponentTypes returns correct number of component types", () => {
assert.equal(result.length, 3);
test("getComponentTypes returns correct component type names", async () => {
const result = await odoCli.getComponentTypes();
expect(result.length).equals(1);
expect(result[0]).equals('nodejs');
});

test("getComponentTypes returns correct component type names", () => {
const resultArray = result.filter((element: string) => {
return element === http || element === nodejs || element === python;
});
assert.equal(resultArray.length, 3);
});

test("getComponentTypeVersions returns correct number of tags for component type", () => {
return Promise.all([
odoCli.getComponentTypeVersions(nodejs).then((result)=> {
assert.equal(result.length, 1);
}),
odoCli.getComponentTypeVersions(python).then((result)=> {
assert.equal(result.length, 2);
}),
odoCli.getComponentTypeVersions(http).then((result)=> {
assert.equal(result.length, 3);
})
]);
test("getComponentTypeVersions returns correct number of tags for component type", async () => {
const result = await odoCli.getComponentTypeVersions(nodejs);
expect(result.length).equals(7);
expect(result[0]).equals("0.10");
expect(result[6]).equals("latest");
});
});

Expand Down

0 comments on commit 7b41220

Please sign in to comment.