Skip to content

Commit

Permalink
Rebase current deployment processing into the error processor.
Browse files Browse the repository at this point in the history
Also fixed a bug that on mac and linux deployment profiles were
being created but not deleted.

Signed-off-by: Eric Promislow <[email protected]>
  • Loading branch information
ericpromislow committed May 16, 2023
1 parent 3adbf2a commit 1300c15
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 85 deletions.
5 changes: 1 addition & 4 deletions e2e/lockedFields.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ test.describe('Locked fields', () => {
}

async function restoreUserProfile() {
// `deploymentProfile` if `saveUserProfile` throws.
if (deploymentProfile) {
await createUserProfile(deploymentProfile.defaults, deploymentProfile.locked);
}
await createUserProfile(deploymentProfile?.defaults ?? null, deploymentProfile?.locked ?? null);
}

test.describe.configure({ mode: 'serial' });
Expand Down
2 changes: 1 addition & 1 deletion e2e/utils/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function createDefaultSettings(overrides: RecursivePartial<Settings> = {}
if (!fs.existsSync(settingsFullPath)) {
fs.mkdirSync(paths.config, { recursive: true });
fs.writeFileSync(path.join(paths.config, fileSettingsName), settingsJson);
console.log('Default settings file successfully created on: ', `${ paths.config }/${ fileSettingsName }`, settingsData);
console.log(`Default settings file successfully created at ${ paths.config }/${ fileSettingsName }`);
} else {
try {
const contents = fs.readFileSync(settingsFullPath, { encoding: 'utf-8' });
Expand Down
135 changes: 65 additions & 70 deletions pkg/rancher-desktop/main/__tests__/deploymentProfiles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import os from 'os';
import path from 'path';

import * as settings from '@pkg/config/settings';
import { readDeploymentProfiles } from '@pkg/main/deploymentProfiles';
import {
readDeploymentProfiles,
lockableDefaultSettings, validateDeploymentProfile,
} from '@pkg/main/deploymentProfiles';
import { spawnFile } from '@pkg/utils/childProcess';
import Logging from '@pkg/utils/logging';
import { RecursivePartial } from '@pkg/utils/typeUtils';
Expand Down Expand Up @@ -204,68 +207,68 @@ describe('deployment profiles', () => {
/* Mock console.error() to capture error messages. */
let consoleMock: jest.SpyInstance<void, [message?: any, ...optionalArgs: any[]]>;

beforeEach(async() => {
nativeReg.deleteTree(nativeReg.HKCU, path.join(...(REGISTRY_PATH_PROFILE)));
testDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'regtest-'));
regFilePath = path.join(testDir, 'import.reg');
consoleMock = jest.spyOn(console, 'error');
});
afterEach(async() => {
await fs.promises.rm(testDir, { force: true, recursive: true });
consoleMock.mockReset();
});
// TODO: Add an `afterAll(clearRegistry)` when we're finished developing.

describe('profile', () => {
describe('defaults', () => {
describe('happy paths', () => {
const defaultUserProfile: RecursivePartial<settings.Settings> = {
application: {
debug: true,
adminAccess: false,
telemetry: { enabled: true },
},
containerEngine: {
allowedImages: {
enabled: false,
patterns: ['edmonton', 'calgary', 'red deer', 'bassano'],
beforeEach(async() => {
nativeReg.deleteTree(nativeReg.HKCU, path.join(...(REGISTRY_PATH_PROFILE)));
testDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'regtest-'));
regFilePath = path.join(testDir, 'import.reg');
consoleMock = jest.spyOn(console, 'error');
});
afterEach(async() => {
await fs.promises.rm(testDir, { force: true, recursive: true });
consoleMock.mockReset();
});
// TODO: Add an `afterAll(clearRegistry)` when we're finished developing.

describe('profile', () => {
describe('defaults', () => {
describe('happy paths', () => {
const defaultUserProfile: RecursivePartial<settings.Settings> = {
application: {
debug: true,
adminAccess: false,
telemetry: { enabled: true },
},
containerEngine: {
allowedImages: {
enabled: false,
patterns: ['edmonton', 'calgary', 'red deer', 'bassano'],
},
name: settings.ContainerEngine.MOBY,
},
WSL: {
integrations: {
kingston: false,
napanee: false,
yarker: true,
weed: true,
},
},
kubernetes: {
version: '867-5309',
},
diagnostics: {
showMuted: true,
mutedChecks: {
montreal: true,
'riviere du loup': false,
magog: false,
},
},
name: settings.ContainerEngine.MOBY,
},
WSL: {
integrations: {
kingston: false,
napanee: false,
yarker: true,
weed: true
}
},
kubernetes: {
version: '867-5309',
},
diagnostics: {
showMuted: true,
mutedChecks: {
montreal: true,
'riviere du loup': false,
magog: false
extensions: {
bellingham: 'WA',
portland: 'OR',
shasta: 'CA',
elko: 'NV',
},
},
extensions: {
bellingham: 'WA',
portland: 'OR',
shasta: 'CA',
elko: 'NV'
},
};
const lockedUserProfile = {
containerEngine: {
allowedImages: {
enabled: false,
patterns: ['busybox', 'nginx'],
};
const lockedUserProfile = {
containerEngine: {
allowedImages: {
enabled: false,
patterns: ['busybox', 'nginx'],
},
},
},
};
};

describe('no system profiles, no user profiles', () => {
it('loads nothing', async() => {
Expand Down Expand Up @@ -299,19 +302,10 @@ describe('deployment profiles', () => {
});
});
describe('error paths', () => {
const limitedUserProfile = {
application: {
telemetry: { enabled: true },
},
diagnostics: {
showMuted: true,
},
};

it('loads a bad profile, complains about all the errors, and keeps only valid entries', async() => {
await clearRegistry();
await installInRegistry(incorrectDefaultsUserRegFile);
await expect(readDeploymentProfiles(true))
await expect(readDeploymentProfiles())
.rejects
.toThrow(`Error in registry settings:
Error for registry entry 'application.debug': expecting value of type boolean, got '"should be a number"'
Expand All @@ -325,6 +319,7 @@ Error for registry entry 'wsl.integrations': expecting value of type object, got
Error for registry entry 'kubernetes.version': expecting value of type string, got '{}'
Error for registry entry 'kubernetes.enabled': expecting value of type boolean, got '-7'
Error for registry entry 'diagnostics.mutedChecks': expecting value of type object, got '66'`);
});
});
});
});
Expand Down
15 changes: 5 additions & 10 deletions pkg/rancher-desktop/main/deploymentProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import { RecursivePartial } from '@pkg/utils/typeUtils';

const console = Logging.deploymentProfile;

const REGISTRY_PATH_PROFILE = ['SOFTWARE', 'Rancher Desktop', 'Profile'];

export const testingDefaultsHiveName = 'DefaultsTest';
export const testingLockedHiveName = 'LockedTest';

export class DeploymentProfileError extends Error {
}

Expand Down Expand Up @@ -472,18 +467,18 @@ function validateDeploymentProfileWithErrors(profile: any, errors: string[], sch
const schemaVal = schema[key];
const profileVal = profile[key];

if (typeof profileVal !== 'object') {
if (typeof profileVal !== typeof schemaVal) {
errors.push(`Error for field '${ fullPath(key) }': expecting value of type ${ typeof schemaVal }, got '${ JSON.stringify(profileVal) }'`);
}
} else if (Array.isArray(profileVal) || Array.isArray(schemaVal)) {
if (Array.isArray(profileVal) || Array.isArray(schemaVal)) {
if (Array.isArray(profileVal) !== Array.isArray(schemaVal)) {
if (Array.isArray(schemaVal)) {
errors.push(`Error for field '${ fullPath(key) }': expecting an array, got '${ JSON.stringify(profileVal) }'`);
} else {
errors.push(`Error for field '${ fullPath(key) }': expecting value of type ${ typeof schemaVal }, got an array ${ JSON.stringify(profileVal) }`);
}
}
} else if (typeof profileVal !== 'object') {
if (typeof profileVal !== typeof schemaVal) {
errors.push(`Error for field '${ fullPath(key) }': expecting value of type ${ typeof schemaVal }, got '${ JSON.stringify(profileVal) }'`);
}
} else if (haveUserDefinedObject(parentPathParts.concat(key))) {
// Keep this part of the profile
} else if (typeof profileVal !== typeof schemaVal) {
Expand Down

0 comments on commit 1300c15

Please sign in to comment.