Skip to content

Commit

Permalink
Fix bug in local-deploy, add live test for OCI (#14592)
Browse files Browse the repository at this point in the history
Fix bug in local-deploy, add live test for OCI
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/14592)
  • Loading branch information
anthony-c-martin authored Jul 19, 2024
1 parent cdb0fd7 commit 132ade5
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 80 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
submodules: true

- name: Setup Node.js
uses: actions/setup-node@v4
Expand Down Expand Up @@ -786,6 +789,6 @@ jobs:
- name: Run Bicep Live E2E Tests (${{ matrix.environment }})
run: npm ci && npm run test:live:${{ matrix.environment }}
env:
BICEP_CLI_DOTNET_RID: ${{ matrix.runtime.rid }}
BICEP_CLI_DOTNET_RID: linux-musl-x64
BICEP_CLI_EXECUTABLE: ../../../Bicep.Cli.E2eTests/src/temp/bicep-cli/bicep
working-directory: ./src/Bicep.Cli.E2eTests
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"experimentalFeaturesEnabled": {
"extensibility": true,
"extensionRegistry": true,
"localDeploy": true
},
"extensions": {
"mock": "$TARGET_REFERENCE"
},
"cloud": {
"currentProfile": "AzureUSGovernment",
"credentialPrecedence": [
// used by CI
"Environment",
"AzureCLI"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"extensibility": true,
"extensionRegistry": true,
"localDeploy": true
},
"extensions": {
"mock": "$TARGET_REFERENCE"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"experimentalFeaturesEnabled": {
"extensibility": true,
"extensionRegistry": true,
"localDeploy": true
},
"extensions": {
"mock": "$TARGET_REFERENCE"
},
"cloud": {
"currentProfile": "AzureCloud",
"credentialPrecedence": ["AzureCLI"]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extension '../../temp/local-deploy/provider.tgz'
extension mock

param payload string

Expand Down
68 changes: 68 additions & 0 deletions src/Bicep.Cli.E2eTests/src/localDeploy.test.live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Live tests for "bicep local-deploy".
*
* @group live
*/

import os from "os";
import { invokingBicepCommand } from "./utils/command";
import { copyToTempFile, pathToExampleFile, pathToTempFile } from "./utils/fs";
import {
platformSupportsLocalDeploy,
publishExtension,
} from "./utils/localdeploy";
import { getEnvironment } from "./utils/liveTestEnvironments";
import { BicepRegistryReferenceBuilder } from "./utils/br";
import { itif } from "./utils/testHelpers";

describe("bicep local-deploy", () => {
const environment = getEnvironment();
const testArea = `local-deploy-live${environment.suffix}`;
const builder = new BicepRegistryReferenceBuilder(
environment.registryUri,
testArea,
);

itif(platformSupportsLocalDeploy())(
"should publish and run an extension published to a registry",
() => {
const baseFolder = pathToExampleFile("local-deploy");
const target = builder.getBicepReference("mock", "0.0.1");

const files = {
bicep: copyToTempFile(baseFolder, "main.bicep", testArea),
bicepparam: copyToTempFile(baseFolder, "main.bicepparam", testArea),
bicepconfig: copyToTempFile(
baseFolder,
`bicepconfig${environment.suffix}.json`,
testArea,
{
relativePath: "bicepconfig.json",
values: {
$TARGET_REFERENCE: target,
},
},
),
};

const typesIndexPath = pathToTempFile(testArea, "types", "index.json");
publishExtension(typesIndexPath, target)
.shouldSucceed()
.withEmptyStdout();

invokingBicepCommand("local-deploy", files.bicepparam)
.shouldSucceed()
.withStdout(
[
'Output sayHiResult: "Hello, World!"',
"Resource sayHi (Create): Succeeded",
"Result: Succeeded",
"",
].join(os.EOL),
);
},
);
});
117 changes: 41 additions & 76 deletions src/Bicep.Cli.E2eTests/src/localDeploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,50 @@
* @group CI
*/

import spawn from "cross-spawn";
import os from "os";
import path from "path";
import { invokingBicepCommand } from "./utils/command";
import { copyToTempFile, pathToExampleFile, pathToTempFile } from "./utils/fs";
import {
ensureParentDirExists,
expectFileExists,
pathToExampleFile,
pathToTempFile,
} from "./utils/fs";

const itif = (condition: boolean) => condition ? it : it.skip;
const cliDotnetRid = process.env.BICEP_CLI_DOTNET_RID;
// We don't have an easy way of running this test for linux-musl-x64 RID, so skip for now.
const canRunLocalDeploy = () => !cliDotnetRid || architectures.map(x => x.dotnetRid).includes(cliDotnetRid);

const mockExtensionExeName = 'bicep-ext-mock';
const mockExtensionProjPath = path.resolve(
__dirname,
"../../Bicep.Local.Extension.Mock"
);

const architectures = [
{ dotnetRid: 'osx-arm64', bicepArgs: ['--bin-osx-arm64', `${mockExtensionProjPath}/bin/release/net8.0/osx-arm64/publish/${mockExtensionExeName}`] },
{ dotnetRid: 'osx-x64', bicepArgs: ['--bin-osx-x64', `${mockExtensionProjPath}/bin/release/net8.0/osx-x64/publish/${mockExtensionExeName}`] },
{ dotnetRid: 'linux-x64', bicepArgs: ['--bin-linux-x64', `${mockExtensionProjPath}/bin/release/net8.0/linux-x64/publish/${mockExtensionExeName}`] },
{ dotnetRid: 'win-x64', bicepArgs: ['--bin-win-x64', `${mockExtensionProjPath}/bin/release/net8.0/win-x64/publish/${mockExtensionExeName}.exe`] },
];
platformSupportsLocalDeploy,
publishExtension,
} from "./utils/localdeploy";
import { itif } from "./utils/testHelpers";

describe("bicep local-deploy", () => {
itif(canRunLocalDeploy())("should build and deploy a provider published to the local file system", () => {

for (const arch of architectures) {
execDotnet(['publish', '--verbosity', 'quiet', '--configuration', 'release', '--self-contained', 'true', '-r', arch.dotnetRid, mockExtensionProjPath]);
}

const typesIndexPath = pathToTempFile("local-deploy", "types", "index.json");
const typesDir = path.dirname(typesIndexPath);
ensureParentDirExists(typesIndexPath);

execDotnet(['run', '--verbosity', 'quiet', '--project', mockExtensionProjPath], {
MOCK_TYPES_OUTPUT_PATH: typesDir,
});

const targetPath = pathToTempFile("local-deploy", "provider.tgz");

invokingBicepCommand(
"publish-provider",
typesIndexPath,
"--target",
targetPath,
...(architectures.flatMap(arch => arch.bicepArgs))
)
.shouldSucceed()
.withEmptyStdout();

expectFileExists(targetPath);

const bicepparamFilePath = pathToExampleFile("local-deploy", "main.bicepparam");

invokingBicepCommand("local-deploy", bicepparamFilePath)
.shouldSucceed()
.withStdout([
'Output sayHiResult: "Hello, World!"',
'Resource sayHi (Create): Succeeded',
'Result: Succeeded',
''
].join(os.EOL));
});
const testArea = "local-deploy";

itif(platformSupportsLocalDeploy())(
"should publish and run an extension published to the local file system",
() => {
const baseFolder = pathToExampleFile("local-deploy");
const target = pathToTempFile(testArea, "mock.tgz");

const files = {
bicep: copyToTempFile(baseFolder, "main.bicep", testArea),
bicepparam: copyToTempFile(baseFolder, "main.bicepparam", testArea),
bicepconfig: copyToTempFile(baseFolder, `bicepconfig.json`, testArea, {
relativePath: "bicepconfig.json",
values: {
$TARGET_REFERENCE: "./mock.tgz",
},
}),
};

const typesIndexPath = pathToTempFile(testArea, "types", "index.json");
publishExtension(typesIndexPath, target)
.shouldSucceed()
.withEmptyStdout();

invokingBicepCommand("local-deploy", files.bicepparam)
.shouldSucceed()
.withStdout(
[
'Output sayHiResult: "Hello, World!"',
"Resource sayHi (Create): Succeeded",
"Result: Succeeded",
"",
].join(os.EOL),
);
},
);
});

function execDotnet(args: string[], envOverrides?: NodeJS.ProcessEnv) {
const result = spawn.sync('dotnet', args, {
encoding: 'utf-8',
stdio: 'inherit',
env: {
...process.env,
...(envOverrides ?? {})
}
});
expect(result.status).toBe(0);
}
21 changes: 20 additions & 1 deletion src/Bicep.Cli.E2eTests/src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,23 @@ export function writeTempFile(

export function ensureParentDirExists(filePath: string): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
}

export function copyToTempFile(
baseFolder: string,
relativePath: string,
testArea: string,
replace?: {
values: Record<string, string>;
relativePath: string
}
) {
const fileContents = readFileSync(path.join(baseFolder, relativePath));

const replacedContents = Object.entries(replace?.values ?? {}).reduce(
(contents, [from, to]) => contents.replace(from, to),
fileContents,
);

return writeTempFile(testArea, replace?.relativePath ?? relativePath, replacedContents);
}
Loading

0 comments on commit 132ade5

Please sign in to comment.