Skip to content

Commit

Permalink
fix issue with dataDirectories object being empty by initializing mis…
Browse files Browse the repository at this point in the history
…sing properties and adding optional chaining for increased code resilience + add unit test (rancher#11400)
  • Loading branch information
aalves08 authored Jul 10, 2024
1 parent a2993cc commit 89903d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ describe('component: DirectoryConfig', () => {
expect(k8sDistroInput.exists()).toBe(false);
});

it('should render the component with configuration being an empty object, without errors', () => {
const newMountOptions = clone(mountOptions);

newMountOptions.propsData.value = {};

wrapper = mount(
DirectoryConfig,
newMountOptions
);

const title = wrapper.find('h3');
const checkbox = wrapper.find('[data-testid="rke2-directory-config-individual-config-checkbox"]');
const commonInput = wrapper.find('[data-testid="rke2-directory-config-common-data-dir"]');
const systemAgentInput = wrapper.find('[data-testid="rke2-directory-config-systemAgent-data-dir"]');
const provisioningInput = wrapper.find('[data-testid="rke2-directory-config-provisioning-data-dir"]');
const k8sDistroInput = wrapper.find('[data-testid="rke2-directory-config-k8sDistro-data-dir"]');

expect(title.exists()).toBe(true);
expect(checkbox.exists()).toBe(true);
// for the default config, checkbox should be checked
expect(wrapper.vm.isSettingCommonConfig).toBe(true);
expect(commonInput.exists()).toBe(true);

// since we have all of the vars empty, then the individual inputs should not be there
expect(systemAgentInput.exists()).toBe(false);
expect(provisioningInput.exists()).toBe(false);
expect(k8sDistroInput.exists()).toBe(false);
});

it('updating common config path should set the correct values on each data dir variable', async() => {
wrapper = mount(
DirectoryConfig,
Expand Down
14 changes: 14 additions & 0 deletions shell/edit/provisioning.cattle.io.cluster/rke2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export default {
});
}
// default for dataDirectories configuration obj
if (!this.value.spec.rkeConfig.dataDirectories) {
set(this.value.spec.rkeConfig, 'dataDirectories', {
systemAgent: '',
Expand All @@ -179,6 +180,19 @@ export default {
});
}
// default for dataDirectories configuration systemAgent config
if (!this.value.spec.rkeConfig.dataDirectories.systemAgent) {
set(this.value.spec.rkeConfig.dataDirectories, 'systemAgent', '');
}
// default for dataDirectories configuration provisioning config
if (!this.value.spec.rkeConfig.dataDirectories.provisioning) {
set(this.value.spec.rkeConfig.dataDirectories, 'provisioning', '');
}
// default for dataDirectories configuration k8sDistro config
if (!this.value.spec.rkeConfig.dataDirectories.k8sDistro) {
set(this.value.spec.rkeConfig.dataDirectories, 'k8sDistro', '');
}
if (!this.value.spec.rkeConfig.machineGlobalConfig) {
set(this.value.spec, 'rkeConfig.machineGlobalConfig', {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ export default {
let atLeastOneDirWithAnIdentifier = false;
let allDirsWithSameIdentifier = false;
if (this.value.systemAgent.length || this.value.provisioning.length || this.value.k8sDistro.length) {
if (this.value?.systemAgent?.length || this.value?.provisioning?.length || this.value?.k8sDistro?.length) {
atLeastOneDirWithAnIdentifier = true;
if (this.value.systemAgent === this.value.provisioning && this.value.provisioning === this.value.k8sDistro &&
this.value.systemAgent === this.value.k8sDistro) {
if (this.value?.systemAgent === this.value?.provisioning && this.value?.provisioning === this.value?.k8sDistro &&
this.value?.systemAgent === this.value?.k8sDistro) {
allDirsWithSameIdentifier = true;
}
}
return {
isSettingCommonConfig: !(atLeastOneDirWithAnIdentifier && !allDirsWithSameIdentifier),
commonConfig: allDirsWithSameIdentifier ? this.value.systemAgent : '',
commonConfig: allDirsWithSameIdentifier ? this.value?.systemAgent : '',
};
},
watch: {
Expand Down

0 comments on commit 89903d5

Please sign in to comment.