Skip to content

Commit

Permalink
Associate TestRun to Classic Release (#73)
Browse files Browse the repository at this point in the history
* add support for publishing release uri + environment uri in wrapper

* obtain release uri + environment uri from pipeline run

* publish release info with pipeline

* updated local test file

* remove test environment variables if not provided
  • Loading branch information
bryanbcook authored Jan 7, 2025
1 parent a9406c7 commit 750f7cc
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
18 changes: 15 additions & 3 deletions PublishTestPlanResultsV1/TaskParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ export function getProcessorParameters() : TestResultProcessorParameters {

export function getPublisherParameters() : TestRunPublisherParameters {
const accessToken = tl.getInput("accessToken", false) ?? tl.getVariable("SYSTEM_ACCESSTOKEN");
const buildId = tl.getVariable("BUILD_BUILDID"); // available in build and release pipelines
const collectionUri = tl.getInput("collectionUri", false) ?? tl.getVariable("SYSTEM_COLLECTIONURI");
const buildId = tl.getVariable("BUILD_BUILDID")!; // available in build and release pipelines
const releaseUri = tl.getVariable("RELEASE_RELEASEURI"); // only in release pipelines
const releaseEnvironmentUri = tl.getVariable("RELEASE_ENVIRONMENTURI"); // only in release pipelines
const collectionUri = tl.getInput("collectionUri", false) ?? tl.getVariable("SYSTEM_COLLECTIONURI")!;
const dryRun = tl.getBoolInput("dryRun", false);
const testRunTitle = tl.getInput("testRunTitle", false) ?? "PublishTestPlanResult";
const testFiles = getTestFiles().filter(file => file.indexOf('**') == -1);
return new TestRunPublisherParameters(collectionUri as string, accessToken as string, dryRun, testRunTitle, buildId as string, testFiles);
let result = new TestRunPublisherParameters(
collectionUri,
accessToken as string,
dryRun,
testRunTitle,
buildId,
testFiles
);
result.releaseUri = releaseUri;
result.releaseEnvironmentUri = releaseEnvironmentUri;
return result;
}

function getTestFiles() : string[] {
Expand Down
18 changes: 18 additions & 0 deletions PublishTestPlanResultsV1/Test-ExtensionLocally.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ param(
[AllowEmptyString()]
[string]$BuildId,

[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$ReleaseId,

[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$ReleaseEnvironmentId,

[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$DryRun,
Expand All @@ -88,6 +96,16 @@ $PSBoundParameters.GetEnumerator() | ForEach-Object {
# BuildId isn't an input parameter.
if ($BuildId) {
[System.Environment]::SetEnvironmentVariable("BUILD_BUILDID", $BuildId)
} else {
[System.Environment]::SetEnvironmentVariable("BUILD_BUILDID", "")
}

if ($ReleaseId -and $ReleaseEnvironmentId) {
[System.Environment]::SetEnvironmentVariable("RELEASE_RELEASEURI", "vstfs:///ReleaseManagement/Release/$ReleaseId")
[System.Environment]::SetEnvironmentVariable("RELEASE_ENVIRONMENTURI", "vstfs:///ReleaseManagement/Environment/$ReleaseEnvironmentId")
} else {
[System.Environment]::SetEnvironmentVariable("RELEASE_RELEASEURI", "")
[System.Environment]::SetEnvironmentVariable("RELEASE_ENVIRONMENTURI", "")
}

if (!$env:SYSTEM_DEFAULTWORKINGDIRECTORY) {
Expand Down
6 changes: 5 additions & 1 deletion PublishTestPlanResultsV1/publishing/TestRunPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class TestRunPublisher {
var publisher = new TestRunPublisher(ado, logger);
publisher.buildId = parameters.buildId;
publisher.dryRun = parameters.dryRun;
publisher.releaseUri = parameters.releaseUri;
publisher.releaseEnvironmentUri = parameters.releaseEnvironmentUri;
publisher.testRunTitle = parameters.testRunTitle;
publisher.testFiles = parameters.testFiles;

Expand All @@ -24,6 +26,8 @@ export class TestRunPublisher {
private logger : ILogger;
public buildId : string;
public dryRun : boolean;
public releaseUri? : string;
public releaseEnvironmentUri? : string;
public testRunTitle : string;
public testFiles : string[];

Expand Down Expand Up @@ -54,7 +58,7 @@ export class TestRunPublisher {

// create a test run for the project + testPlan using testPoint ids
const points = Array.from(results.matches.keys());
const testRun = await this.ado.createTestRun(projectId, testPlanId, points, this.buildId);
const testRun = await this.ado.createTestRun(projectId, testPlanId, points, this.buildId, this.releaseUri, this.releaseEnvironmentUri);

// obtain the testcaseresult definitions
var testCaseResults = await this.ado.getTestResults(projectId, testRun.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export class TestRunPublisherParameters {
public buildId : string;
public collectionUri: string;
public dryRun : boolean;
public releaseUri?: string;
public releaseEnvironmentUri?: string;
public testRunTitle : string;
public testFiles : string[];

Expand Down
6 changes: 5 additions & 1 deletion PublishTestPlanResultsV1/services/AdoWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ export class AdoWrapper {
* @param testPlanId test plan id
* @param testPoints array of testpoint ids
* @param buildId build identifier
* @param releaseUri url to the release
* @param releaseEnvironmentUri url to the release environment
* @returns returns the resulting TestRun from the operation
*/
async createTestRun(projectId : string, testPlanId : number, testPoints : number[], buildId : string) : Promise<Contracts.TestRun> {
async createTestRun(projectId : string, testPlanId : number, testPoints : number[], buildId : string, releaseUri? : string, releaseEnvironmentUri? : string) : Promise<Contracts.TestRun> {
this.logger.debug(`createTestRun projectId:${projectId} testPlanId:${testPlanId} testPoints: (${testPoints.length} items) - buildId:${buildId}`);

let testRun : Contracts.RunCreateModel = {
Expand All @@ -140,6 +142,8 @@ export class AdoWrapper {
build: <Contracts.ShallowReference>{
id: buildId,
},
releaseUri: releaseUri,
releaseEnvironmentUri: releaseEnvironmentUri,
pointIds: testPoints,
/* this property is required because it's not optional in the typescript def */
configurationIds: []
Expand Down
33 changes: 33 additions & 0 deletions PublishTestPlanResultsV1/test/TaskParameters.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,39 @@ describe('TaskParameters', () => {
// assert
expect(parameters.dryRun).to.be.true;
})

it("Should resolve empty values for release variables if not present", () => {
// arrange
util.setInput("testResultFiles", path.join(__dirname, "data", "xunit", "xunit-1.xml"));
util.loadData();

// act
require(tp);
var parameters = TaskParameters.getPublisherParameters();

// assert
expect(parameters.releaseUri).to.be.undefined;
expect(parameters.releaseEnvironmentUri).to.be.undefined;
})

context("For Release Pipeline", () => {

it("Should resolve release uri and environment uri from release pipeline", () => {
// arrange
util.setInput("testResultFiles", path.join(__dirname, "data", "xunit", "xunit-1.xml"));
util.setSystemVariable("RELEASE_RELEASEURI", "vstfs://ReleaseManagement/Release/1234");
util.setSystemVariable("RELEASE_ENVIRONMENTURI", "vstfs://ReleaseManagement/Environment/5678");
util.loadData();

// act
require(tp);
var parameters = TaskParameters.getPublisherParameters();

// assert
expect(parameters.releaseUri).to.satisfy( (x: string) => x.startsWith("vstfs://ReleaseManagement"));
expect(parameters.releaseEnvironmentUri).to.satisfy( (x: string) => x.startsWith("vstfs://ReleaseManagement"));
});
})
});

});
Expand Down
34 changes: 30 additions & 4 deletions PublishTestPlanResultsV1/test/TestRunPublisher.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ context("TestRunPublisher", () => {
testData = new TestResultProcessorResult("project1", <Contracts.TestPlan>{ id: 1});

subject = new TestRunPublisher(ado, logger);
subject.buildId = "123";
})

afterEach(() => {
Expand Down Expand Up @@ -59,9 +60,13 @@ context("TestRunPublisher", () => {
const serverUrl = process.env.SYSTEM_COLLECTIONURI as string;
const accessToken = (process.env.SYSTEM_ACCESSTOKEN ?? process.env.ENDPOINT_AUTH_PARAMETER_SYSTEMVSSCONNECTION_ACCESSTOKEN) as string;
const buildId = "123";
const rUri = "vstfs://ReleaseManagement/Release/1";
const eUri = "vstfs://ReleaseManagement/Environment/1";
const testFiles = ["file1", "file2"];
console.log(serverUrl);
var parameters = new TestRunPublisherParameters(serverUrl, accessToken, false, "Dummy", buildId, testFiles);
parameters.releaseUri = rUri;
parameters.releaseEnvironmentUri = eUri;

// act
var subject = await TestRunPublisher.create(parameters);
Expand All @@ -72,6 +77,8 @@ context("TestRunPublisher", () => {
expect(subject.testRunTitle).eq("Dummy");
expect(subject.buildId).eq("123");
expect(subject.testFiles.length).eq(2);
expect(subject.releaseUri).eq(rUri);
expect(subject.releaseEnvironmentUri).eq(eUri);
})

})
Expand Down Expand Up @@ -108,7 +115,7 @@ context("TestRunPublisher", () => {
var result = await subject.publishTestRun(testData);

// assert
expect(ado.createTestRun.calledWith( "project1", 1, [1,2])).eq(true);
expect(ado.createTestRun.calledWith( "project1", 1, [1,2], "123")).eq(true);
expect(ado.updateTestResults.calledOnce).eq(true);
})

Expand Down Expand Up @@ -167,7 +174,7 @@ context("TestRunPublisher", () => {
subject.testRunTitle = "MyTestRun";

// act
var assert = await subject.publishTestRun(testData);
var result = await subject.publishTestRun(testData);

// assert
expect(ado.updateTestRun.calledWithMatch(
Expand All @@ -177,7 +184,26 @@ context("TestRunPublisher", () => {
})
)).eq(true);
})
})

context("With Classic Release Information available", () => {

it("Should include release information in the test run", async () => {
// arrange
let buildId = "123";
let rUri = "vstfs://ReleaseManagement/Release/1";
let eUri = "vstfs://ReleaseManagement/Environment/1";
subject.buildId = buildId;
subject.releaseUri = rUri;
subject.releaseEnvironmentUri = eUri;

// act
var result = await subject.publishTestRun(testData);

// assert
expect(ado.createTestRun.calledWith( "project1", 1, [1,2], buildId, rUri, eUri )).eq(true);
})
})
})

it("Should attach testrun files to the testRun", async () => {
// arrange
Expand Down Expand Up @@ -209,7 +235,7 @@ context("TestRunPublisher", () => {
ado.createTestRun.callsFake( (prjId, plnId, points) => {

let testRun = <Contracts.TestRun>{
id: 400
id: runId
};

return Promise.resolve(testRun);
Expand Down
3 changes: 2 additions & 1 deletion PublishTestPlanResultsV1/test/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function clearData() {
.filter(key => (key.startsWith('INPUT_') ||
key.startsWith("SECRET_") ||
key.startsWith("VSTS_TASKVARIABLE_") ||
key.startsWith("BUILD_")
key.startsWith("BUILD_") ||
key.startsWith("RELEASE_")
)
// caution: System_* variables should not be deleted
).forEach(key => delete process.env[key]);
Expand Down

0 comments on commit 750f7cc

Please sign in to comment.