Skip to content

Commit

Permalink
Add tests to verify --output reg options are handled correctly.
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Promislow <epromislow@suse.com>
  • Loading branch information
ericpromislow committed Jun 22, 2023
1 parent fb6f272 commit 17e6b3b
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 101 deletions.
186 changes: 186 additions & 0 deletions bats/tests/preferences/list-settings-output.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
load '../helpers/load'

RD_USE_IMAGE_ALLOW_LIST=true

@test 'factory reset' {
factory_reset
# bypass the defaults deployment file
mkdir -p "$(dirname "${PATH_CONFIG_FILE})")"
touch "$PATH_CONFIG_FILE"
}

@test 'start app' {
start_container_engine
wait_for_container_engine
}

@test 'report parameters for json' {
run rdctl list-settings '--output=json,fish'
assert_failure
assert_output --partial 'the json output format takes no sub-formats, got "json,fish"'
}

@test 'report unrecognized output-options' {
run rdctl list-settings '--output=bickley,ruff'
assert_failure
assert_output --partial $'expecting an output format of \'json\' or \'reg\', got "bickley,ruff"'
}

@test 'report unrecognized reg sub-options' {
run rdctl list-settings '--output=reg,hklm,ruff'
assert_failure
assert_output --partial 'expecting a reg output-format parameter, got "ruff" in "reg,hklm,ruff"'
}

@test 'report duplicate reg hives' {
for x in hklm hkcu; do
for y in hklm hkcu; do
option="reg,${x},locked,${y}"
run rdctl list-settings "--output=${option}"
assert_failure
assert_output --partial $"already specified registry hive \"${x}\" in \"${option}\", can't respecify"
done
done
}

@test 'report duplicate registry sections' {
for x in defaults locked; do
for y in defaults locked; do
option="reg,${x},hkcu,${y}"
run rdctl list-settings "--output=${option}"
assert_failure
assert_output --partial $"already specified registry section \"${x}\" in \"${option}\", can't respecify"
done
done
}

@test 'generates registry output for hklm/defaults' {
for option in reg reg,hklm reg,hklm,defaults reg,defaults; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
done
}

@test 'generates registry output for hklm/locked' {
for option in reg,hklm,locked reg,locked; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\locked\application]'
done
}

@test 'generates registry output for hkcu/defaults' {
for option in reg,hkcu,defaults reg,hkcu; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
done
}

@test 'generates registry output for hkcu/locked' {
run rdctl list-settings --output reg,hkcu,locked
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\locked\application]'
}

# The result of the `assert_output` for heredocuments looks suspicious (I see it always passing),
# but this serves to document the expected full reg output
@test 'generates registry output' {
run rdctl list-settings --output reg
assert_success
assert_output <<'EOF'
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies]
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop]
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults]
"version"=dword:8
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]
"adminAccess"=dword:0
"debug"=dword:0
"pathManagementStrategy"="rcfiles"
"autoStart"=dword:0
"startInBackground"=dword:0
"hideNotificationIcon"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application\extensions]
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application\extensions\allowed]
"enabled"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application\telemetry]
"enabled"=dword:1
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application\updater]
"enabled"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application\window]
"quitOnClose"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\containerEngine]
"name"="moby"
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\containerEngine\allowedImages]
"enabled"=dword:1
"patterns"=hex(7):66,00,69,00,73,00,68,00,00,00,73,00,68,00,65,00,65,00,70,00,00,00,63,00,6f,00,77,00,73,00,00,00,00,00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\virtualMachine]
"memoryInGB"=dword:4
"numberCPUs"=dword:2
"hostResolver"=dword:1
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\kubernetes]
"version"="1.25.9"
"port"=dword:192b
"enabled"=dword:1
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\kubernetes\options]
"traefik"=dword:1
"flannel"=dword:1
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\kubernetes\ingress]
"localhostOnly"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\experimental]
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\experimental\virtualMachine]
"socketVMNet"=dword:0
"networkingTunnel"=dword:0
"type"="qemu"
"useRosetta"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\experimental\virtualMachine\mount]
"type"="reverse-sshfs"
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\experimental\virtualMachine\mount\9p]
"securityModel"="none"
"protocolVersion"="9p2000.L"
"msizeInKib"=dword:80
"cacheMode"="mmap"
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\experimental\virtualMachine\proxy]
"enabled"=dword:0
"address"=""
"password"=""
"port"=dword:c38
"username"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\portForwarding]
"includeKubernetesServices"=dword:0
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\images]
"showAll"=dword:1
"namespace"="k8s.io"
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\diagnostics]
"showMuted"=dword:0
EOF
}

@test 'needs a shutdown' {
rdctl shutdown
}
80 changes: 0 additions & 80 deletions e2e/rdctl.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,86 +143,6 @@ test.describe('Command server', () => {

test.afterAll(() => teardown(electronApp, __filename));

test.describe('rdctl -pre', () => {
test.describe('list-settings output formats', () => {
function regToJSON(regFormatOutput: string): RecursivePartial<Settings> {
return {};
}
test.describe('detects option errors', () => {
test("it doesn't allow json sub-options", async() => {
const { stdout, stderr } = await rdctl(['list-settings', '--output=json,fish']);

expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`the json output format takes no sub-formats, got "json,fish"`),
stdout: '',
});
});
test("it doesn't allow unrecognized output-options", async() => {
const { stdout, stderr } = await rdctl(['list-settings', '--output=bickley,ruff']);

expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`expecting an output format of 'json' or 'reg', got "bickley,ruff"`),
stdout: '',
});
});
test("it doesn't allow unrecognized reg sub-options", async() => {
const { stdout, stderr } = await rdctl(['list-settings', '--output=reg,hklm,ruff']);

expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`expecting a reg output-format parameter, got "ruff" in "reg,hklm,ruff"`),
stdout: '',
});
});
test("it doesn't allow duplicate reg hives", async() => {
let { stdout, stderr } = await rdctl(['list-settings', '--output=reg,hklm,locked,hkcu']);

expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`already specified registry hive "hklm" in "reg,hklm,locked,hkcu", can't respecify`),
stdout: '',
});

({ stdout, stderr } = await rdctl(['list-settings', '--output=reg,hklm,locked,hklm']));
expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`already specified registry hive "hklm" in "reg,hklm,locked,hklm", can't respecify`),
stdout: '',
});

({ stdout, stderr } = await rdctl(['list-settings', '--output=reg,hkcu,locked,hkcu']));
expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`already specified registry hive "hkcu" in "reg,hkcu,locked,hkcu", can't respecify`),
stdout: '',
});

({ stdout, stderr } = await rdctl(['list-settings', '--output=reg,hkcu,locked,hklm']));
expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`already specified registry hive "hkcu" in "reg,hkcu,locked,hklm", can't respecify`),
stdout: '',
});
});
test("it doesn't allow duplicate reg sections", async() => {
const { stdout, stderr } = await rdctl(['list-settings', '--output=reg,hklm,locked,defaults']);

expect({ stdout, stderr }).toEqual({
stderr: expect.stringContaining(`already specified registry section "locked" in "reg,hklm,locked,defaults", can't respecify`),
stdout: '',
});
});
});
test('it converts settings to reg files', async() => {
await rdctl(['settings', '--method', 'PUT', '--body', JSON.stringify(defaultSettings)]);
const { stdout, stderr, error } = await rdctl(['list-settings', '--output=reg']);

expect({
stdout, stderr, error,
}).toEqual({
error: undefined,
stderr: '',
stdout: expect.stringContaining('[HKEY_LOCAL_MACHINE\\SOFTWARE\\Rancher Desktop\\Profile\\defaults]'),
});
expect(regToJSON(stdout)).toMatchObject(defaultSettings);
});
});
});
test('should load Kubernetes API', async() => {
const navPage = new NavPage(page);

Expand Down
1 change: 0 additions & 1 deletion scripts/generateCliCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ class Generator {
settingsTree: settingsTreeType): void {
if (preference.additionalProperties) {
settingsTree[lastName(propertyName)] = { type: 'hash' };
// console.log(`Skipping ${ propertyName }: not settable from the command-line.`);

return;
}
Expand Down
12 changes: 11 additions & 1 deletion src/go/rdctl/cmd/listSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ var outputSettingsFlags struct {
var listSettingsCmd = &cobra.Command{
Use: "list-settings",
Short: "Lists the current settings.",
Long: `Lists the current settings in JSON format.`,
Long: `Lists the current settings in JSON or Windows registry-file format.
The default output format is JSON.
To convert the current settings into a registry file, run the following command:
rdctl list-commands --output reg,X,Y
where X is either "hkcu" or "hklm", depending on whether you want to update HKEY_LOCAL_MACHINE
or HKEY_CURRENT_USER respectively,
and Y is either "defaults" or "locked", depending on which deployment profile you want to populate.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cobra.NoArgs(cmd, args); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions src/go/rdctl/pkg/reg/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ func JsonToReg(hiveType string, profileType string, settingsBodyAsJSON string) (
return nil, fmt.Errorf("error in json: %s\n", err)
}
headerLines := []string{"Windows Registry Editor Version 5.00"}
bodyLines, _, err := convertToRegFormat([]string{fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop", profileType}, reflect.ValueOf(settings), "")
bodyLines, _, err := convertToRegFormat([]string{fullHiveType, "SOFTWARE", "Policies", "Rancher Desktop", profileType}, reflect.ValueOf(settings), "")
if err != nil {
return nil, err
}
if len(bodyLines) > 0 {
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Profiles"))
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop"))
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Policies"))
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Policies", "Rancher Desktop"))
}
return append(headerLines, bodyLines...), nil
}
Loading

0 comments on commit 17e6b3b

Please sign in to comment.