Skip to content

Commit

Permalink
Cobbler-API: Enable tests for "get_distro" and "get_distros"
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoolGuy committed Jul 20, 2024
1 parent 311ae4a commit 81f1ad7
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 24 deletions.
101 changes: 92 additions & 9 deletions projects/cobbler-api/src/lib/cobbler-api.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {TestBed} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {BackgroundBuildisoOptions, BackgroundImportOptions, BackgroundReplicateOptions} from 'cobbler-api';
import {Distro} from './custom-types/items';
import {Event, ExtendedVersion, InstallationStatus} from './custom-types/misc';
import {COBBLER_URL} from './lib.config';
import {AngularXmlrpcService} from 'typescript-xmlrpc';
Expand Down Expand Up @@ -338,9 +339,46 @@ describe('CobblerApiService', () => {
expect(service).toBeFalsy();
});

xit('should execute the get_distro action on the Cobbler Server', () => {
service.get_distro('');
expect(service).toBeFalsy();
it('should execute the get_distro action on the Cobbler Server', () => {
// eslint-disable-next-line max-len
const methodResponse = `<?xml version='1.0'?><methodResponse><params><param><value><struct><member><name>parent</name><value><string></string></value></member><member><name>depth</name><value><int>0</int></value></member><member><name>ctime</name><value><double>1721480439.039089</double></value></member><member><name>mtime</name><value><double>1721480439.039089</double></value></member><member><name>uid</name><value><string>12f034d6781946d1af0783e20684cbd4</string></value></member><member><name>name</name><value><string>test</string></value></member><member><name>comment</name><value><string></string></value></member><member><name>kernel_options</name><value><struct></struct></value></member><member><name>kernel_options_post</name><value><struct></struct></value></member><member><name>autoinstall_meta</name><value><struct></struct></value></member><member><name>fetchable_files</name><value><struct></struct></value></member><member><name>boot_files</name><value><struct></struct></value></member><member><name>template_files</name><value><struct></struct></value></member><member><name>owners</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>mgmt_classes</name><value><array><data></data></array></value></member><member><name>mgmt_parameters</name><value><struct></struct></value></member><member><name>is_subobject</name><value><boolean>0</boolean></value></member><member><name>tree_build_time</name><value><double>0.0</double></value></member><member><name>arch</name><value><string>x86_64</string></value></member><member><name>boot_loaders</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>breed</name><value><string></string></value></member><member><name>initrd</name><value><string>/root/initrd</string></value></member><member><name>kernel</name><value><string>/root/kernel</string></value></member><member><name>os_version</name><value><string></string></value></member><member><name>redhat_management_key</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>source_repos</name><value><array><data></data></array></value></member><member><name>remote_boot_kernel</name><value><string></string></value></member><member><name>remote_grub_kernel</name><value><string></string></value></member><member><name>remote_boot_initrd</name><value><string></string></value></member><member><name>remote_grub_initrd</name><value><string></string></value></member><member><name>ks_meta</name><value><struct></struct></value></member></struct></value></param></params></methodResponse>`
const result: Distro = {
ctime: 1721480439.039089,
depth: 0,
mtime: 1721480439.039089,
uid: "12f034d6781946d1af0783e20684cbd4",
source_repos: [],
tree_build_time: 0,
arch: "x86_64",
autoinstall_meta: {},
boot_files: {},
boot_loaders: "<<inherit>>",
is_subobject: false,
parent: "",
breed: "",
comment: "",
fetchable_files: {},
initrd: "/root/initrd",
kernel: "/root/kernel",
remote_boot_initrd: "",
remote_boot_kernel: "",
remote_grub_initrd: "",
remote_grub_kernel: "",
kernel_options: {},
kernel_options_post: {},
mgmt_classes: [],
mgmt_parameters: {},
name: "test",
os_version: "",
owners: "<<inherit>>",
redhat_management_key: "<<inherit>>",
template_files: {},
}
service.get_distro('', false, false, "").subscribe(value => {
expect(value).toEqual(result)
});
const mockRequest = httpTestingController.expectOne('http://localhost/cobbler_api');
mockRequest.flush(methodResponse);
});

xit('should execute the get_profile action on the Cobbler Server', () => {
Expand Down Expand Up @@ -383,14 +421,59 @@ describe('CobblerApiService', () => {
expect(service).toBeFalsy();
});

xit('should execute the get_item_names action on the Cobbler Server', () => {
service.get_item_names('');
expect(service).toBeFalsy();
it('should execute the get_item_names action on the Cobbler Server', () => {
// eslint-disable-next-line max-len
const methodResponse = `<?xml version='1.0'?><methodResponse><params><param><value><array><data><value><string>testdistro</string></value></data></array></value></param></params></methodResponse>`
const result = ["testdistro"]
service.get_item_names('distro').subscribe(value => {
expect(value).toEqual(result)
});
const mockRequest = httpTestingController.expectOne('http://localhost/cobbler_api');
mockRequest.flush(methodResponse);
});

xit('should execute the get_distros action on the Cobbler Server', () => {
service.get_distros();
expect(service).toBeFalsy();
it('should execute the get_distros action on the Cobbler Server', () => {
// eslint-disable-next-line max-len
const methodResponse = `<?xml version='1.0'?><methodResponse><params><param><value><array><data><value><struct><member><name>parent</name><value><string></string></value></member><member><name>depth</name><value><int>0</int></value></member><member><name>ctime</name><value><double>1721480439.039089</double></value></member><member><name>mtime</name><value><double>1721480439.039089</double></value></member><member><name>uid</name><value><string>12f034d6781946d1af0783e20684cbd4</string></value></member><member><name>name</name><value><string>test</string></value></member><member><name>comment</name><value><string></string></value></member><member><name>kernel_options</name><value><struct></struct></value></member><member><name>kernel_options_post</name><value><struct></struct></value></member><member><name>autoinstall_meta</name><value><struct></struct></value></member><member><name>fetchable_files</name><value><struct></struct></value></member><member><name>boot_files</name><value><struct></struct></value></member><member><name>template_files</name><value><struct></struct></value></member><member><name>owners</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>mgmt_classes</name><value><array><data></data></array></value></member><member><name>mgmt_parameters</name><value><struct></struct></value></member><member><name>is_subobject</name><value><boolean>0</boolean></value></member><member><name>tree_build_time</name><value><double>0.0</double></value></member><member><name>arch</name><value><string>x86_64</string></value></member><member><name>boot_loaders</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>breed</name><value><string></string></value></member><member><name>initrd</name><value><string>/root/initrd</string></value></member><member><name>kernel</name><value><string>/root/kernel</string></value></member><member><name>os_version</name><value><string></string></value></member><member><name>redhat_management_key</name><value><string>&lt;&lt;inherit&gt;&gt;</string></value></member><member><name>source_repos</name><value><array><data></data></array></value></member><member><name>remote_boot_kernel</name><value><string></string></value></member><member><name>remote_grub_kernel</name><value><string></string></value></member><member><name>remote_boot_initrd</name><value><string></string></value></member><member><name>remote_grub_initrd</name><value><string></string></value></member><member><name>ks_meta</name><value><struct></struct></value></member></struct></value></data></array></value></param></params></methodResponse>`
const result: Array<Distro> = [
{
ctime: 1721480439.039089,
depth: 0,
mtime: 1721480439.039089,
uid: "12f034d6781946d1af0783e20684cbd4",
source_repos: [],
tree_build_time: 0,
arch: "x86_64",
autoinstall_meta: {},
boot_files: {},
boot_loaders: "<<inherit>>",
is_subobject: false,
parent: "",
breed: "",
comment: "",
fetchable_files: {},
initrd: "/root/initrd",
kernel: "/root/kernel",
remote_boot_initrd: "",
remote_boot_kernel: "",
remote_grub_initrd: "",
remote_grub_kernel: "",
kernel_options: {},
kernel_options_post: {},
mgmt_classes: [],
mgmt_parameters: {},
name: "test",
os_version: "",
owners: "<<inherit>>",
redhat_management_key: "<<inherit>>",
template_files: {}
}
]
service.get_distros().subscribe(value => {
expect(value).toEqual(result)
});
const mockRequest = httpTestingController.expectOne('http://localhost/cobbler_api');
mockRequest.flush(methodResponse);
});

xit('should execute the get_profiles action on the Cobbler Server', () => {
Expand Down
51 changes: 43 additions & 8 deletions projects/cobbler-api/src/lib/cobbler-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ export class CobblerApiService {
);
}

private rebuildItem(xmlrpcStruct: XmlRpcStruct): object {
const result = {}
xmlrpcStruct.members.forEach(value => {
if (value.name === "ks_meta") {
// Skip legacy keys
return;
}
if (AngularXmlrpcService.instanceOfXmlRpcArray(value.value)) {
result[value.name] = this.convertXmlRpcArrayToTypeScriptArray(value.value)
} else if (AngularXmlrpcService.instanceOfXmlRpcStruct(value.value)) {
result[value.name] = this.convertXmlRpcStructToTypeScriptObject(value.value)
} else if (value.value === "&lt;&lt;inherit&gt;&gt;") {
// FIXME: Maybe we need to XML encode this as other strings potentially also could need encoding
result[value.name] = "<<inherit>>"
} else {
result[value.name] = value.value
}
})
return result
}

// TODO: Create casting magic to output the right item type
get_item(what: string, name: string, flatten: boolean = false): Observable<object> {
return this.client
Expand All @@ -470,14 +491,17 @@ export class CobblerApiService {
);
}

get_distro(name: string, flatten: boolean = false): Observable<Distro> {
get_distro(name: string, flatten: boolean = false, resolved: boolean = false, token: string): Observable<Distro> {
return this.client
.methodCall('get_distro', [name, flatten])
.methodCall('get_distro', [name, flatten, resolved, token])
.pipe(
map<MethodResponse | MethodFault, Distro>((data: MethodResponse | MethodFault) => {
if (AngularXmlrpcService.instanceOfMethodResponse(data)) {
// FIXME: Make the cast without the unknown possible
return data.value as unknown as Distro;
if (!AngularXmlrpcService.instanceOfXmlRpcStruct(data.value)) {
throw new Error("Expected XML-RPC Struct not something else!")
}
const result = this.rebuildItem(data.value)
return result as Distro;
} else if (AngularXmlrpcService.instanceOfMethodFault(data)) {
throw new Error('Getting the requested distro failed with code "' + data.faultCode + '" and error message "'
+ data.faultString + '"');
Expand Down Expand Up @@ -623,8 +647,10 @@ export class CobblerApiService {
.pipe(
map<MethodResponse | MethodFault, Array<string>>((data: MethodResponse | MethodFault) => {
if (AngularXmlrpcService.instanceOfMethodResponse(data)) {
// FIXME: Make the cast without the unknown possible
return data.value as unknown as Array<string>;
if (!AngularXmlrpcService.instanceOfXmlRpcArray(data.value)) {
throw new Error("Expected XML-RPC Array but got something else!")
}
return data.value.data as Array<string>;
} else if (AngularXmlrpcService.instanceOfMethodFault(data)) {
throw new Error('Getting the item names failed with code "' + data.faultCode + '" and error message "'
+ data.faultString + '"');
Expand All @@ -639,8 +665,17 @@ export class CobblerApiService {
.pipe(
map<MethodResponse | MethodFault, Array<Distro>>((data: MethodResponse | MethodFault) => {
if (AngularXmlrpcService.instanceOfMethodResponse(data)) {
// FIXME: Make the cast without the unknown possible
return data.value as unknown as Array<Distro>;
if (!AngularXmlrpcService.instanceOfXmlRpcArray(data.value)) {
throw new Error("Expected XML-RPC Array!")
}
const result = []
data.value.data.forEach(value => {
if (!AngularXmlrpcService.instanceOfXmlRpcStruct(value)) {
throw new Error("Expected XML-RPC Struct!")
}
result.push(this.rebuildItem(value))
})
return result as Array<Distro>;
} else if (AngularXmlrpcService.instanceOfMethodFault(data)) {
throw new Error('Getting all distros failed with code "' + data.faultCode + '" and error message "'
+ data.faultString + '"');
Expand Down
19 changes: 12 additions & 7 deletions projects/cobbler-api/src/lib/custom-types/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ export interface Item {
}

export interface Distro extends Item {
source_repos: Array<any>;
tree_build_time: string;
is_subobject: boolean;
source_repos: Array<string>;
tree_build_time: number;
arch: string;
autoinstall_meta: object;
boot_files: Array<any>;
boot_loader: string;
boot_files: object;
boot_loaders: string | Array<string>;
breed: string;
comment: string;
fetchable_files: Array<any>;
parent: string;
fetchable_files: object;
initrd: string;
kernel: string;
remote_boot_initrd: string;
remote_boot_kernel: string;
remote_grub_initrd: string;
remote_grub_kernel: string;
kernel_options: object;
kernel_options_post: object;
mgmt_classes: Array<any>;
mgmt_classes: Array<string>;
mgmt_parameters: object;
name: string;
os_version: string;
owners: Array<any>;
owners: string | Array<string>;
redhat_management_key: string;
template_files: object;
}
Expand Down

0 comments on commit 81f1ad7

Please sign in to comment.