Skip to content
Merged
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
140 changes: 140 additions & 0 deletions shell/utils/__tests__/aws.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { isIpv4Network, isIpv6Network, getVpcDisplayName, getSubnetDisplayName } from '@shell/utils/aws';

describe('aws utils', () => {
describe('isIpv4Network', () => {
it.each([
{
desc: 'returns true when CidrBlock is set',
network: { CidrBlock: '10.0.0.0/16' },
expected: true,
},
{
desc: 'returns false when CidrBlock is absent',
network: {},
expected: false,
},
{
desc: 'returns false when CidrBlock is an empty string',
network: { CidrBlock: '' },
expected: false,
},
{
desc: 'returns true when CidrBlock is a non-empty string',
network: { CidrBlock: '0.0.0.0/0' },
expected: true,
},
])('$desc', ({ network, expected }) => {
expect(isIpv4Network(network as any)).toStrictEqual(expected);
});
});

describe('isIpv6Network', () => {
it.each([
{
desc: 'returns true when Ipv6CidrBlockAssociationSet has entries',
network: { Ipv6CidrBlockAssociationSet: [{ Ipv6CidrBlock: '::/0' }] },
expected: true,
},
{
desc: 'returns false when Ipv6CidrBlockAssociationSet is an empty array',
network: { Ipv6CidrBlockAssociationSet: [] },
expected: false,
},
{
desc: 'returns false when Ipv6CidrBlockAssociationSet is absent',
network: {},
expected: false,
},
{
desc: 'returns false when Ipv6CidrBlockAssociationSet is undefined',
network: { Ipv6CidrBlockAssociationSet: undefined },
expected: false,
},
])('$desc', ({ network, expected }) => {
expect(isIpv6Network(network as any)).toStrictEqual(expected);
});
});

describe('getVpcDisplayName', () => {
it.each([
{
desc: 'returns "Name (VpcId)" when Name tag is present',
vpc: {
VpcId: 'vpc-abc123',
Tags: [{ Key: 'Name', Value: 'my-vpc' }],
},
expected: 'my-vpc (vpc-abc123)',
},
{
desc: 'returns VpcId alone when Tags array is empty',
vpc: {
VpcId: 'vpc-abc123',
Tags: [],
},
expected: 'vpc-abc123',
},
{
desc: 'returns VpcId alone when Tags is absent',
vpc: { VpcId: 'vpc-abc123' },
expected: 'vpc-abc123',
},
{
desc: 'returns VpcId alone when no tag has Key "Name"',
vpc: {
VpcId: 'vpc-abc123',
Tags: [{ Key: 'Env', Value: 'prod' }],
},
expected: 'vpc-abc123',
},
{
desc: 'uses the first Name tag when multiple tags exist',
vpc: {
VpcId: 'vpc-abc123',
Tags: [
{ Key: 'Env', Value: 'prod' },
{ Key: 'Name', Value: 'primary-vpc' },
],
},
expected: 'primary-vpc (vpc-abc123)',
},
])('$desc', ({ vpc, expected }) => {
expect(getVpcDisplayName(vpc as any)).toStrictEqual(expected);
});
});

describe('getSubnetDisplayName', () => {
it.each([
{
desc: 'returns "Name (SubnetId)" when Name tag is present',
subnet: {
SubnetId: 'subnet-xyz789',
Tags: [{ Key: 'Name', Value: 'my-subnet' }],
},
expected: 'my-subnet (subnet-xyz789)',
},
{
desc: 'returns SubnetId alone when Tags array is empty',
subnet: {
SubnetId: 'subnet-xyz789',
Tags: [],
},
expected: 'subnet-xyz789',
},
{
desc: 'returns SubnetId alone when Tags is absent',
subnet: { SubnetId: 'subnet-xyz789' },
expected: 'subnet-xyz789',
},
{
desc: 'returns SubnetId alone when no tag has Key "Name"',
subnet: {
SubnetId: 'subnet-xyz789',
Tags: [{ Key: 'Zone', Value: 'us-east-1a' }],
},
expected: 'subnet-xyz789',
},
])('$desc', ({ subnet, expected }) => {
expect(getSubnetDisplayName(subnet as any)).toStrictEqual(expected);
});
});
});
68 changes: 68 additions & 0 deletions shell/utils/__tests__/kube.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { normalizeName } from '@shell/utils/kube';

describe('normalizeName', () => {
it.each([
{
desc: 'trims leading and trailing whitespace',
input: ' hello ',
expected: 'hello',
},
{
desc: 'converts uppercase to lowercase',
input: 'Hello World',
expected: 'hello-world',
},
{
desc: 'replaces spaces with hyphens',
input: 'my resource name',
expected: 'my-resource-name',
},
{
desc: 'collapses multiple spaces into a single hyphen',
input: 'foo bar',
expected: 'foo-bar',
},
{
desc: 'collapses consecutive hyphens into one',
input: 'foo--bar',
expected: 'foo-bar',
},
{
desc: 'strips leading hyphens',
input: '---foo',
expected: 'foo',
},
{
desc: 'strips trailing hyphens',
input: 'foo---',
expected: 'foo',
},
{
desc: 'handles empty string',
input: '',
expected: '',
},
{
desc: 'handles null by treating it as empty string',
input: null as unknown as string,
expected: '',
},
{
desc: 'handles undefined by treating it as empty string',
input: undefined as unknown as string,
expected: '',
},
{
desc: 'leaves already-normalized names unchanged',
input: 'my-resource',
expected: 'my-resource',
},
{
desc: 'handles mixed-case with spaces and leading/trailing hyphens',
input: ' -My Resource- ',
expected: 'my-resource',
},
])('$desc', ({ input, expected }) => {
expect(normalizeName(input)).toStrictEqual(expected);
});
});
Loading