Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions shell/detail/__tests__/autoscaling.horizontalpodautoscaler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,55 @@ describe('view: autoscaling.horizontalpodautoscaler', () => {
});
});
});

describe('with malformed metrics:', () => {
const valueWithMalformedMetrics = {
status: {
currentMetrics: [],
},
spec: {
metrics: [
{
type: 'Pods',
// Missing 'pods' property - causes metricValue to be undefined
},
{
type: 'External',
external: {
// Missing 'metric' property - metricValue.metric is undefined
target: { averageValue: 100, type: 'AverageValue' }
}
},
{
type: 'Resource',
// Missing 'resource' property
}
]
}
};

it('should not throw TypeError when metrics are malformed', () => {
expect(() => {
mount(HorizontalPodAutoScaler, {
props: { value: valueWithMalformedMetrics },
global: { mocks, stubs },
});
}).not.toThrow();
});

it('should gracefully handle malformed metrics with null values', () => {
const wrapper = mount(HorizontalPodAutoScaler, {
props: { value: valueWithMalformedMetrics },
global: { mocks, stubs },
});

expect(wrapper.exists()).toBe(true);
expect(wrapper.vm.mappedMetrics).toHaveLength(3);

// Metric names should be null for malformed metrics
expect(wrapper.vm.mappedMetrics[0].metricName).toBeNull();
expect(wrapper.vm.mappedMetrics[1].metricName).toBeNull();
expect(wrapper.vm.mappedMetrics[2].metricName).toBeNull();
});
});
});
4 changes: 2 additions & 2 deletions shell/detail/autoscaling.horizontalpodautoscaler/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export default {
// The format is different between 'Resource' metrics and others.
// See for examples: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#appendix-horizontal-pod-autoscaler-status-conditions
if (metricType !== 'resource') {
currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue.metric.name);
currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue?.metric?.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment only.

if the value metricValue.metric.name doesn't exist there's no reason to search for it. however given that i don't think currentMetrics scales very high and this is more legible i think we could keep as is

} else {
currentMatch = findBy(currentMetrics, 'resource.name', metric.resource.name);
currentMatch = findBy(currentMetrics, 'resource.name', metric.resource?.name);
}

const current = currentMatch ? get(currentMatch, `${ camelCase(metric.type) }.current`) : null;
Expand Down
Loading