Skip to content

Commit

Permalink
fix aks pool count validator and add some unit tests (#11908)
Browse files Browse the repository at this point in the history
  • Loading branch information
mantis-toboggan-md authored Sep 13, 2024
1 parent e02174d commit c88be67
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 40 deletions.
9 changes: 6 additions & 3 deletions pkg/aks/components/AksNodePool.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ export default defineComponent({
},
poolCountValidator() {
return (val: number) => this.validationRules?.count?.[0](val, this.pool.enableAutoScaling);
const canBeZero: boolean = this.pool.mode === 'User';
return (val: number) => this.validationRules?.count?.[0](val, canBeZero);
}
},
});
Expand Down Expand Up @@ -305,9 +307,10 @@ export default defineComponent({
type="number"
:mode="mode"
label-key="aks.nodePools.count.label"
:rules="[poolCountValidator]"
:min="pool.enableAutoScaling ? 0 : 1"
:rules="[poolCountValidator()]"
:min="pool.mode === 'User' ? 0 : 1"
:max="1000"
data-testid="aks-pool-count-input"
/>
</div>
<div class="col span-3">
Expand Down
38 changes: 3 additions & 35 deletions pkg/aks/components/CruAks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ import {
outboundTypeUserDefined,
privateDnsZone,
nodePoolNames,
nodePoolNamesUnique
nodePoolNamesUnique,
nodePoolCount
} from '../util/validators';
export const defaultNodePool = {
Expand Down Expand Up @@ -342,6 +343,7 @@ export default defineComponent({
privateDnsZone: privateDnsZone(this, 'aks.privateDnsZone.label', 'aksConfig.privateDnsZone'),
poolNames: nodePoolNames(this),
poolNamesUnique: nodePoolNamesUnique(this),
poolCount: nodePoolCount(this),
vmSizeAvailable: () => {
if (this.touchedVmSize) {
Expand Down Expand Up @@ -433,40 +435,6 @@ export default defineComponent({
return this.canUseAvailabilityZones || !isUsingAvailabilityZones ? undefined : this.t('aks.errors.availabilityZones');
},
poolCount: (count?: number, autoscale = false) => {
let min = 1;
let errMsg = this.t('aks.errors.poolCount');
if (autoscale) {
min = 0;
errMsg = this.t('aks.errors.poolAutoscaleCount');
}
if (count || count === 0) {
return count >= min ? undefined : errMsg;
} else {
let allValid = true;
this.nodePools.forEach((pool: AKSNodePool) => {
const { count = 0, enableAutoScaling } = pool;
if (enableAutoScaling) {
min = 0;
} else {
min = 1;
}
if (count < min) {
this.$set(pool._validation, '_validCount', false);
allValid = false;
} else {
this.$set(pool._validation, '_validCount', true);
}
});
return allValid ? undefined : this.t('aks.errors.poolCount');
}
},
poolMin: (min?:number) => {
if (min || min === 0) {
return min < 0 || min > 1000 ? this.t('aks.errors.poolMin') : undefined;
Expand Down
35 changes: 35 additions & 0 deletions pkg/aks/components/__tests__/AksNodePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,39 @@ describe('aks node pool component', () => {

expect(wrapper.props().pool.nodeLabels).toStrictEqual(newLabels);
});

it('should validate pool count using the provided count validator function', async() => {
const countValidator = jest.fn();

mount(AksNodePool, {
propsData: {
pool: { ...defaultPool, count: -1 },
validationRules: { count: [countValidator] }
},
...requiredSetup()

});

expect(countValidator).toHaveBeenCalledWith(-1, false);
});

it.each([
['System', false],
['User', true],
])('should validate node pool count differently if the pool mode is User', async(mode, allowZeroCount) => {
const countValidator = jest.fn();

mount(AksNodePool, {
propsData: {
pool: {
...defaultPool, count: -1, mode
},
validationRules: { count: [countValidator] }
},
...requiredSetup()

});

expect(countValidator).toHaveBeenCalledWith(-1, allowZeroCount);
});
});
4 changes: 2 additions & 2 deletions pkg/aks/l10n/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ aks:
availabilityZones: Availability zones are not available in the selected region.
privateDnsZone: Private DNS Zone Resource ID must be in the format /subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCEGROUP_NAME/providers/Microsoft.Network/privateDnsZones/PRIVATE_DNS_ZONE_NAME. The Private DNS Zone Resource Name must be in the format privatelink.REGION.azmk8s.io, SUBZONE.privatelink.REGION.azmk8s.io, private.REGION.azmk8s.io, or SUBZONE.private.REGION.azmk8s.io
poolName: Node pool names must be 1-12 characters long, consist only of lowercase letters and numbers, and start with a letter.
poolCount: Node count must be at least one when autoscaling is disabled.
poolAutoscaleCount: Node count cannot be less than zero.
poolCount: Node count must be at least one in System pools.
poolUserCount: Node count cannot be less than zero.
poolMinMax: The minimum number of nodes must be less than or equal to the maximum number of nodes, and the node count must be between or equal to the minimum and maximum.
poolMin: The minimum number of nodes must be greater than 0 and at most 1000.
poolMax: The maximum number of nodes must be greater than 0 and at most 1000.
Expand Down
45 changes: 45 additions & 0 deletions pkg/aks/util/__tests__/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,48 @@ describe('fx: nodePoolNames', () => {
expect(validator(name)).toStrictEqual(expected);
});
});

describe('fx: nodePoolCount', () => {
// AksNodePool unit tests check that the second arg is passed in as expected
it('validates that count is at least 1 when second arg is false', () => {
const validator = validators.nodePoolCount(mockCtx);

expect(validator(1, false)).toBeUndefined();
expect(validator(0, false)).toStrictEqual(MOCK_TRANSLATION);
});

it('validates that count is at least 0 when second arg is true', () => {
const validator = validators.nodePoolCount(mockCtx);

expect(validator(1, true)).toBeUndefined();
expect(validator(0, true)).toBeUndefined();
expect(validator(-1, true)).toStrictEqual(MOCK_TRANSLATION);
});

it('validates each node pool in the provided context when not passed a count value', () => {
const ctx = {
...mockCtx,
nodePools: [
{
name: 'abc', _validation: {}, mode: 'System', count: 0
},
{
name: 'def', _validation: {}, mode: 'System', count: 1
},
{
name: 'hij', _validation: {}, mode: 'User', count: 0
},
{
name: 'klm', _validation: {}, mode: 'User', count: -1
}
] as unknown as AKSNodePool[]
};
const validator = validators.nodePoolCount(ctx);

validator();
expect(ctx.nodePools[0]?._validation?._validCount).toStrictEqual(false);
expect(ctx.nodePools[1]?._validation?._validCount).toStrictEqual(true);
expect(ctx.nodePools[2]?._validation?._validCount).toStrictEqual(true);
expect(ctx.nodePools[3]?._validation?._validCount).toStrictEqual(false);
});
});
36 changes: 36 additions & 0 deletions pkg/aks/util/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,39 @@ export const nodePoolNamesUnique = (ctx: any) => {
}
};
};

export const nodePoolCount = (ctx:any) => {
return (count?: number, canBeZero = false) => {
let min = 1;
let errMsg = ctx.t('aks.errors.poolCount');

if (canBeZero) {
min = 0;
errMsg = ctx.t('aks.errors.poolUserCount');
}
if (count || count === 0) {
return count >= min ? undefined : errMsg;
} else {
let allValid = true;

ctx.nodePools.forEach((pool: AKSNodePool) => {
const { count = 0, mode } = pool;

if (mode === 'User') {
min = 0;
} else {
min = 1;
}

if (count < min) {
ctx.$set(pool._validation, '_validCount', false);
allValid = false;
} else {
ctx.$set(pool._validation, '_validCount', true);
}
});

return allValid ? undefined : ctx.t('aks.errors.poolCount');
}
};
};

0 comments on commit c88be67

Please sign in to comment.