Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terraform): update lockfile only for provder and required_provider #11510

Merged
merged 2 commits into from
Sep 1, 2021
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
71 changes: 69 additions & 2 deletions lib/manager/terraform/lockfile/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Array [
]
`;

exports[`manager/terraform/lockfile/index update single dependency with exact constraint 1`] = `
exports[`manager/terraform/lockfile/index update single dependency with exact constraint and and depType required_provider 1`] = `
Object {
"contents": "# This file is maintained automatically by \\"terraform init\\".
# Manual edits may be lost in future updates.
Expand Down Expand Up @@ -259,7 +259,74 @@ provider \\"registry.terraform.io/hashicorp/random\\" {
}
`;

exports[`manager/terraform/lockfile/index update single dependency with exact constraint 2`] = `
exports[`manager/terraform/lockfile/index update single dependency with exact constraint and and depType required_provider 2`] = `
Array [
Array [
"https://registry.terraform.io",
"hashicorp/aws",
"3.36.0",
],
]
`;

exports[`manager/terraform/lockfile/index update single dependency with exact constraint and depType provider 1`] = `
Object {
"contents": "# This file is maintained automatically by \\"terraform init\\".
# Manual edits may be lost in future updates.

provider \\"registry.terraform.io/hashicorp/aws\\" {
version = \\"3.36.0\\"
constraints = \\"3.36.0\\"
hashes = [
\\"h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=\\",
\\"h1:6zB2hX7YIOW26OrKsLJn0uLMnjqbPNxcz9RhlWEuuSY=\\",
]
}

provider \\"registry.terraform.io/hashicorp/azurerm\\" {
version = \\"2.50.0\\"
constraints = \\"~> 2.50\\"
hashes = [
\\"h1:Vr6WUm88s9hXGkyVjHtHsP2Jmc2ypQXn6ww7dXtvk1M=\\",
\\"zh:0c0688d5a743248f8646d39eb3645a4ac19fd7523ba1b47072fa3fb03b92b1b0\\",
\\"zh:2beb3a55ee970f87a9292ae96d57134be8a03d0566117e7be0fe0d9c1267e4ea\\",
\\"zh:38091b463fbafe5756420ce34c87845c2a391fec0cded27bdcbbca28febad382\\",
\\"zh:4ba455da3b37ba8f8b03ff2781121d9c54d0bd8afd76dfe67593011c475dd73f\\",
\\"zh:5d32b9ed871b3c3b774dc69f1fe14cdf7c1fd63d12bb5f21aad4bfbf75e5ee3d\\",
\\"zh:6c80cf90a3fc1e17d9caf67cc558c2ff91f8b25e29fdf00942f67711895be5c0\\",
\\"zh:c0a53e3165407999d10de7aaa983485d42797433c60b5775791ae299121279ed\\",
\\"zh:dab51d6d76041505aeebf20111febe8616ec465ca31dfb7901f5f5c23a5af095\\",
\\"zh:e1ad6399f6a6d799002206ee4cb7b794dbb2533b8c3c14502a4419955ec96bff\\",
\\"zh:e98f1d178d1e111b3f3449e27d305ce263071226fad3d86272e1bd161c26fd43\\",
\\"zh:eb76ec000c9c49a0bf730370c8880f671597bc01f7b7401ab301df7124c049ec\\",
]
}

provider \\"registry.terraform.io/hashicorp/random\\" {
version = \\"2.2.1\\"
constraints = \\"~> 2.2\\"
hashes = [
\\"h1:Zg1Bpi6vr7b0H6no8kVDfEucn5pvNALivdrVKVHarGs=\\",
\\"zh:072ce92b0138ee65df2e4e2e6e5f6632fa12a7e6453b91399bad89291855d426\\",
\\"zh:5731987fe61051515f449033e456ee55207caf17ef41096eb82247810585f53b\\",
\\"zh:6f18b10175708bb5839e1f2082dcc02651b876786cd54ec415a091f3821807c3\\",
\\"zh:7fa7737661380d18cba3cdc71c4ec6f2fd281b9d61112f6b48d06ca8bbf97771\\",
\\"zh:8466cb8fbb4de887b23039082a6e3dc85aeabce86dd808e2a7a65e4e1c51dbae\\",
\\"zh:888c63417701c13bbe785ab11dc690d4803e6a2156318cf188970b7b6400b99e\\",
\\"zh:a231df55d36fbad1a6705f5d3be4f7459a73ec76117d13f22aa83c10fc610278\\",
\\"zh:b62d9a4cd64a2d229070260f4abfef476ebbd7c5511b43e9cdccf23ce938f630\\",
\\"zh:b6bd1a325f909bb93f7c9bef00eb306bef1e406cbdf557901d755a3e7a4a5448\\",
\\"zh:b9f59afc23cc5567075f76313214baa1e5ce909325229e23c9a4666f7b26e7f7\\",
\\"zh:d040220c09b8d9d6bd937572bd5b14bc069af2b883185a873460530d8a1de6e6\\",
\\"zh:f254c1f943eb016ae07ebe91b23f813dc79f2064616c65f98c8f64ce23be90c4\\",
]
}
",
"name": ".terraform.lock.hcl",
}
`;

exports[`manager/terraform/lockfile/index update single dependency with exact constraint and depType provider 2`] = `
Array [
Array [
"https://registry.terraform.io",
Expand Down
87 changes: 83 additions & 4 deletions lib/manager/terraform/lockfile/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('manager/terraform/lockfile/index', () => {
).toBeNull();
});

it('update single dependency with exact constraint', async () => {
it('update single dependency with exact constraint and depType provider', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile as any);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');

Expand All @@ -79,7 +79,50 @@ describe('manager/terraform/lockfile/index', () => {

const result = await updateArtifacts({
packageFileName: 'main.tf',
updatedDeps: [{ depName: 'hashicorp/aws', lookupName: 'hashicorp/aws' }],
updatedDeps: [
{
depName: 'hashicorp/aws',
lookupName: 'hashicorp/aws',
depType: 'provider',
},
],
newPackageFileContent: '',
config: localConfig,
});
expect(result).not.toBeNull();
expect(result).toBeArrayOfSize(1);
expect(result[0].file).not.toBeNull();
expect(result[0].file).toMatchSnapshot();

expect(mockHash.mock.calls).toBeArrayOfSize(1);
expect(mockHash.mock.calls).toMatchSnapshot();
});

it('update single dependency with exact constraint and and depType required_provider', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile as any);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
'h1:6zB2hX7YIOW26OrKsLJn0uLMnjqbPNxcz9RhlWEuuSY=',
]);

const localConfig: UpdateArtifactsConfig = {
updateType: 'minor',
newVersion: '3.36.0',
newValue: '3.36.0',
...config,
};

const result = await updateArtifacts({
packageFileName: 'main.tf',
updatedDeps: [
{
depName: 'hashicorp/aws',
lookupName: 'hashicorp/aws',
depType: 'required_provider',
},
],
newPackageFileContent: '',
config: localConfig,
});
Expand All @@ -92,6 +135,29 @@ describe('manager/terraform/lockfile/index', () => {
expect(mockHash.mock.calls).toMatchSnapshot();
});

it('do not update dependency with depType module', async () => {
const localConfig: UpdateArtifactsConfig = {
updateType: 'minor',
newVersion: '3.36.0',
newValue: '3.36.0',
...config,
};

const result = await updateArtifacts({
packageFileName: 'main.tf',
updatedDeps: [
{
depName: 'terraform-aws-modules/vpc/aws',
lookupName: 'terraform-aws-modules/vpc/aws',
depType: 'module',
},
],
newPackageFileContent: '',
config: localConfig,
});
expect(result).toBeNull();
});

it('update single dependency with range constraint and minor update from private registry', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile as any);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
Expand All @@ -113,6 +179,7 @@ describe('manager/terraform/lockfile/index', () => {
updatedDeps: [
{
depName: 'azurerm',
depType: 'provider',
lookupName: 'azurerm',
registryUrls: ['https://registry.example.com'],
},
Expand Down Expand Up @@ -147,7 +214,13 @@ describe('manager/terraform/lockfile/index', () => {

const result = await updateArtifacts({
packageFileName: 'main.tf',
updatedDeps: [{ depName: 'random', lookupName: 'hashicorp/random' }],
updatedDeps: [
{
depName: 'random',
lookupName: 'hashicorp/random',
depType: 'provider',
},
],
newPackageFileContent: '',
config: localConfig,
});
Expand Down Expand Up @@ -178,7 +251,13 @@ describe('manager/terraform/lockfile/index', () => {

const result = await updateArtifacts({
packageFileName: 'test/main.tf',
updatedDeps: [{ depName: 'random', lookupName: 'hashicorp/random' }],
updatedDeps: [
{
depName: 'random',
lookupName: 'hashicorp/random',
depType: 'provider',
},
],
newPackageFileContent: '',
config: localConfig,
});
Expand Down
4 changes: 3 additions & 1 deletion lib/manager/terraform/lockfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export async function updateArtifacts({
// update all locks in the file during maintenance --> only update version in constraints
const maintenanceUpdates = await updateAllLocks(locks);
updates.push(...maintenanceUpdates);
} else {
} else if (
['provider', 'required_provider'].includes(updatedDeps[0].depType)
) {
// update only specific locks but with constrain updates
const dep = updatedDeps[0];

Expand Down