Skip to content

Commit

Permalink
Add tests and update pipeline (#5)
Browse files Browse the repository at this point in the history
* Add recommender console app

* Use general default azure credential

* Add dev branch trigger

* Read pred results from storage

* test pipeline

* Update azure-pipelines.yml for Azure Pipelines

* update pipeline

* update projects path

* update package path in deployment

* Add unit test

* ignore bin and obj

* Test pipeline

* update pipeline

* update pipeline
  • Loading branch information
jason-dou authored Feb 27, 2023
1 parent 1cfbfb6 commit 7c9c21a
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.DS_Store
*__.json
*__.json
**/bin/*
**/obj/*
63 changes: 60 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@ trigger:
pool:
vmImage: ubuntu-latest

steps:
- script: bash test/check-prediction-exist.sh
displayName: "Check prediction result exist"
stages:
- stage: Main
displayName: "Main branch"
condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
jobs:
- job: MainJob
displayName: "Main branch job"
steps:
- task: UseDotNet@2
inputs:
version: "6.0.x"
packageType: sdk
- task: DotNetCoreCLI@2
inputs:
command: publish
arguments: "--configuration Release --output publish_output"
projects: "**/az-function.csproj"
publishWebProjects: false
modifyOutputPath: false
zipAfterPublish: false
- task: ArchiveFiles@2
displayName: "Archive files"
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output"
includeRootFolder: false
archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
artifactName: "drop"
- task: AzureFunctionApp@1 # Add this at the end of your file
inputs:
azureSubscription: "MyServiceConnection"
appType: functionAppLinux # default is functionApp
appName: "fnapp-y3ac2abjvi3wq"
package: $(System.DefaultWorkingDirectory)/**/*.zip
runtimeStack: "DOTNET|6.0"
- stage: Development
displayName: "Development branch"
condition: ne(variables['Build.SourceBranch'], 'refs/heads/main')
jobs:
- job: DevJob
displayName: "Development branch job"
steps:
- task: DotNetCoreCLI@2
inputs:
command: "restore"
projects: "**/az-function.csproj"

- task: DotNetCoreCLI@2
inputs:
command: "build"
projects: "**/az-function.csproj"
arguments: "--configuration Release"

- task: DotNetCoreCLI@2
inputs:
command: "test"
projects: "**/az-function-tests.csproj"
arguments: "--configuration Release"
3 changes: 0 additions & 3 deletions src/az-function/Recommendation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public static async Task<IActionResult> Run(
var isAuthorized = await IsAuthorized(req, log);
if (isAuthorized)
{
var path = System.IO.Path.Combine(context.FunctionAppDirectory, "movies.pred.tsv");

// Read the TSV file into a list of dictionaries
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
using (StreamReader reader = new StreamReader(predBlob))
Expand All @@ -50,7 +48,6 @@ public static async Task<IActionResult> Run(
var field = headers[i] == Constants.Columns.GENRES ? fields[i].Replace("\"", "") : fields[i];
row.Add(headers[i], field);
}

}
data.Add(row);
}
Expand Down
3 changes: 0 additions & 3 deletions src/az-function/az-function.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.26.1" />
</ItemGroup>
<ItemGroup>
<None Update="movies.pred.tsv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
34 changes: 34 additions & 0 deletions test/az-function-tests/MockBlobStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text;

namespace My.Function;
public class MockBlobStream : MemoryStream
{
public override void Flush()
{
// Do nothing
}

public override long Seek(long offset, SeekOrigin origin)
{
// Do nothing
return 0;
}

public override void SetLength(long value)
{
// Do nothing
}

public override int Read(byte[] buffer, int offset, int count)
{
// Return some test data
byte[] testData = Encoding.UTF8.GetBytes("Hello, world!");
Buffer.BlockCopy(testData, 0, buffer, offset, testData.Length);
return testData.Length;
}

public override void Write(byte[] buffer, int offset, int count)
{
// Do nothing
}
}
25 changes: 25 additions & 0 deletions test/az-function-tests/RecommendationUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Azure.Storage.Blobs;
using Moq;
using My.Function;
using Xunit;

namespace My.Function;

public class RecommendationUnitTest
{
[Fact]
public async Task NoToken_ShouldBeUnauthorized()
{
var mockStream = new MockBlobStream();
var req = new Mock<HttpRequest>();
var log = new Mock<ILogger>();
var executionContext = new Microsoft.Azure.WebJobs.ExecutionContext();

var result = await Recommendation.Run(req.Object, mockStream, log.Object, executionContext) as StatusCodeResult;

Assert.Equal(401, result.StatusCode);
}
}
1 change: 1 addition & 0 deletions test/az-function-tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
30 changes: 30 additions & 0 deletions test/az-function-tests/az-function-tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>az_function_tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\az-function\az-function.csproj" />
</ItemGroup>

</Project>
7 changes: 0 additions & 7 deletions test/check-prediction-exist.sh

This file was deleted.

0 comments on commit 7c9c21a

Please sign in to comment.