Skip to content

Commit

Permalink
Break L1s into multiple classes (#2822)
Browse files Browse the repository at this point in the history
* Break L1s into multiple classes

* pr fixes

* oops

* Enable readonly variable test

* Attempt to fix stuck functional tests

* test

* test

* test

* Read task zip directly from externals

* test

* test

* test

* Don't run in parallel

* New L1 changes

* save

* L1 copyright

Co-authored-by: Tommy Petty <[email protected]>
Co-authored-by: Danny McCormick <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2020
1 parent 045f329 commit 011380e
Show file tree
Hide file tree
Showing 19 changed files with 763 additions and 659 deletions.
4 changes: 3 additions & 1 deletion src/Test/L1/Mock/FakeAgentPluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using Agent.Sdk;
using Microsoft.VisualStudio.Services.Agent.Worker;

namespace Microsoft.VisualStudio.Services.Agent.Tests.L1.Worker
Expand Down
4 changes: 3 additions & 1 deletion src/Test/L1/Mock/FakeBuildServer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using Microsoft.VisualStudio.Services.WebApi;
using System.Linq;
using Microsoft.VisualStudio.Services.Agent.Worker.Build;
using Build2 = Microsoft.TeamFoundation.Build.WebApi;

Expand Down
3 changes: 3 additions & 0 deletions src/Test/L1/Mock/FakeConfigurationStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down
3 changes: 3 additions & 0 deletions src/Test/L1/Mock/FakeCustomerIntelligenceServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.VisualStudio.Services.Agent.Worker.Telemetry;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
Expand Down
3 changes: 3 additions & 0 deletions src/Test/L1/Mock/FakeJobServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down
3 changes: 3 additions & 0 deletions src/Test/L1/Mock/FakeReleaseServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
Expand Down
8 changes: 3 additions & 5 deletions src/Test/L1/Mock/FakeTaskManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using System.Linq;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.VisualStudio.Services.Agent.Worker;
using Pipelines = Microsoft.TeamFoundation.DistributedTask.Pipelines;

Expand Down
4 changes: 3 additions & 1 deletion src/Test/L1/Mock/FakeTaskServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
Expand Down
6 changes: 3 additions & 3 deletions src/Test/L1/Plugins/FakeCheckoutTask.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using Agent.Plugins.Repository;
using Agent.Sdk;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Microsoft.VisualStudio.Services.Agent.Tests.L1.Worker
{
Expand Down
6 changes: 4 additions & 2 deletions src/Test/L1/Plugins/FakeGitCliManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Agent.Plugins.Repository;
using Agent.Sdk;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.Common;
using System.Threading.Tasks;
using System.Threading;
Expand All @@ -13,7 +15,7 @@ public class FakeGitCliManager : GitCliManager
{
public FakeGitCliManager(Dictionary<string, string> envs = null) : base(envs)
{ }

public override async Task LoadGitExecutionInfo(AgentTaskPluginExecutionContext context, bool useBuiltInGit)
{
// There is no built-in git for OSX/Linux
Expand Down
52 changes: 52 additions & 0 deletions src/Test/L1/Worker/CheckoutL1Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.TeamFoundation.DistributedTask.Pipelines;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.VisualStudio.Services.Agent.Tests.L1.Worker
{
[Collection("Worker L1 Tests")]
public class CheckoutL1Tests : L1TestBase
{
[Fact]
[Trait("Level", "L1")]
[Trait("Category", "Worker")]
public async Task NoCheckout()
{
try
{
// Arrange
SetupL1();
var message = LoadTemplateMessage();
// Remove checkout
for (var i = message.Steps.Count - 1; i >= 0; i--)
{
var step = message.Steps[i];
if (step is TaskStep && ((TaskStep)step).Reference.Name == "Checkout")
{
message.Steps.RemoveAt(i);
}
}

// Act
var results = await RunWorker(message);

// Assert
AssertJobCompleted();
Assert.Equal(TaskResult.Succeeded, results.Result);

var steps = GetSteps();
Assert.Equal(3, steps.Count()); // Init, CmdLine, Finalize
Assert.Equal(0, steps.Where(x => x.Name == "Checkout").Count());
}
finally
{
TearDown();
}
}
}
}
50 changes: 50 additions & 0 deletions src/Test/L1/Worker/ConditionsL1Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.TeamFoundation.DistributedTask.WebApi;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.VisualStudio.Services.Agent.Tests.L1.Worker
{
[Collection("Worker L1 Tests")]
public class ConditionsL1Tests : L1TestBase
{
[Fact]
[Trait("Level", "L1")]
[Trait("Category", "Worker")]
public async Task Conditions_Failed()
{
try
{
// Arrange
SetupL1();
var message = LoadTemplateMessage();
// Remove all tasks
message.Steps.Clear();
// Add a normal step and one that only runs on failure
message.Steps.Add(CreateScriptTask("echo This will run"));
var failStep = CreateScriptTask("echo This shouldn't...");
failStep.Condition = "failed()";
message.Steps.Add(failStep);

// Act
var results = await RunWorker(message);

// Assert
AssertJobCompleted();
Assert.Equal(TaskResult.Succeeded, results.Result);

var steps = GetSteps();
Assert.Equal(4, steps.Count()); // Init, CmdLine, CmdLine, Finalize
var faiLStep = steps[2];
Assert.Equal(TaskResult.Skipped, faiLStep.Result);
}
finally
{
TearDown();
}
}
}
}
140 changes: 140 additions & 0 deletions src/Test/L1/Worker/ConfigL1Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.TeamFoundation.DistributedTask.WebApi;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.VisualStudio.Services.Agent.Tests.L1.Worker
{
[Collection("Worker L1 Tests")]
public class ConfigL1Tests : L1TestBase
{
[Fact]
[Trait("Level", "L1")]
[Trait("Category", "Worker")]
public async Task TrackingConfigsShouldBeConsistentAcrossRuns()
{
try
{
// Arrange
SetupL1();
FakeConfigurationStore fakeConfigurationStore = GetMockedService<FakeConfigurationStore>();
var message1 = LoadTemplateMessage();
// second message is the same definition but a different job with a different repository checked out
var message2 = LoadTemplateMessage(jobId: "642e8db6-0794-4b7b-8fd9-33ee9202a795", jobName: "__default2", jobDisplayName: "Job2", checkoutRepoAlias: "repo2");

// Act
var results1 = await RunWorker(message1);
var trackingConfig1 = GetTrackingConfig(message1);
AssertJobCompleted(1);
Assert.Equal(TaskResult.Succeeded, results1.Result);

// Act2
var results2 = await RunWorker(message2);
var trackingConfig2 = GetTrackingConfig(message2);
AssertJobCompleted(2);
Assert.Equal(TaskResult.Succeeded, results2.Result);

// Assert
Assert.Equal(trackingConfig1.BuildDirectory, trackingConfig2.BuildDirectory);
Assert.Equal(trackingConfig1.HashKey, trackingConfig2.HashKey);
}
finally
{
TearDown();
}
}

[Fact]
[Trait("Level", "L1")]
[Trait("Category", "Worker")]
public async Task TrackingConfigsShouldBeConsistentAcrossMulticheckoutRuns()
{
try
{
// Arrange
SetupL1();
FakeConfigurationStore fakeConfigurationStore = GetMockedService<FakeConfigurationStore>();
var message1 = LoadTemplateMessage(additionalRepos: 2);
message1.Steps.Add(CreateCheckoutTask("Repo2"));
message1.Steps.Add(CreateCheckoutTask("Repo2"));
// second message is the same definition but a different job with a different order of the repos being checked out in a different order
var message2 = LoadTemplateMessage(jobId: "642e8db6-0794-4b7b-8fd9-33ee9202a795", jobName: "__default2", jobDisplayName: "Job2", checkoutRepoAlias: "Repo3", additionalRepos: 2);
message2.Steps.Add(CreateCheckoutTask("Repo2"));
message2.Steps.Add(CreateCheckoutTask("self"));

// Act
var results1 = await RunWorker(message1);
var trackingConfig1 = GetTrackingConfig(message1);
AssertJobCompleted(1);
Assert.Equal(TaskResult.Succeeded, results1.Result);

// Act2
var results2 = await RunWorker(message2);
var trackingConfig2 = GetTrackingConfig(message2);
AssertJobCompleted(2);
Assert.Equal(TaskResult.Succeeded, results2.Result);

// Assert
Assert.Equal(trackingConfig1.BuildDirectory, trackingConfig2.BuildDirectory);
Assert.Equal(trackingConfig1.HashKey, trackingConfig2.HashKey);
}
finally
{
TearDown();
}
}

[Fact]
[Trait("Level", "L1")]
[Trait("Category", "Worker")]
public async Task TrackingConfigsShouldBeConsistentAcrossRunsWithDifferentCheckouts()
{
try
{
// Arrange
SetupL1();
FakeConfigurationStore fakeConfigurationStore = GetMockedService<FakeConfigurationStore>();
var message1 = LoadTemplateMessage(additionalRepos: 2);
message1.Variables.Add("agent.useWorkspaceIds", new VariableValue(Boolean.TrueString, false, true));

// second message is the same definition but a different job with a different order of the repos being checked out in a different order
var message2 = LoadTemplateMessage(jobId: "642e8db6-0794-4b7b-8fd9-33ee9202a795", jobName: "__default2", jobDisplayName: "Job2", checkoutRepoAlias: "Repo2", additionalRepos: 1);
message2.Variables.Add("agent.useWorkspaceIds", new VariableValue(Boolean.TrueString, false, true));

// third message uses the same repos as the first
var message3 = LoadTemplateMessage(additionalRepos: 2);
message3.Variables.Add("agent.useWorkspaceIds", new VariableValue(Boolean.TrueString, false, true));

// Act
var results1 = await RunWorker(message1);
var trackingConfig1 = GetTrackingConfig(message1);
AssertJobCompleted(1);
Assert.Equal(TaskResult.Succeeded, results1.Result);

// Act2
var results2 = await RunWorker(message2);
var trackingConfig2 = GetTrackingConfig(message2);
AssertJobCompleted(2);
Assert.Equal(TaskResult.Succeeded, results2.Result);

// Act3
var results3 = await RunWorker(message3);
var trackingConfig3 = GetTrackingConfig(message3);
AssertJobCompleted(3);
Assert.Equal(TaskResult.Succeeded, results3.Result);

// Assert - the first and third runs should be consistent
Assert.NotEqual(trackingConfig1.BuildDirectory, trackingConfig2.BuildDirectory);
Assert.Equal(trackingConfig1.BuildDirectory, trackingConfig3.BuildDirectory);
Assert.Equal(trackingConfig1.HashKey, trackingConfig3.HashKey);
}
finally
{
TearDown();
}
}
}
}
Loading

0 comments on commit 011380e

Please sign in to comment.