mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-04 17:24:56 -05:00
Added endpoint for fetching all notebook jobs (#834)
* Added endpoint for fetching all notebook jobs * Refractored NotebookJobInfo to AgentNotebookInfo to make it more consistent with the rest of the codebase * Added Notebook History endpoint in contracts. * Added Create, Update, Delete notebook endpoints. Also added separate fetch template, materialized notebook endpoints. This will make the Notebook Request and Notebook History responses lighter. * AgentNotebookInfo is now derived from AgentJobInfo * added fetch noteook history endpoint * Added fetching materialized notebook endpoint * Added code for cleaning up the directory * Added create notebook api * Added Update and delete notebook job * Fixed notebook history API * Added last run info to the script and template folder * Added execute database feature for notebook Jobs * SQL commands are now using sqlparameters to prevent any injection attacks * Changed rundate and runtime to string to preserve leading zeros * integration test for agentnotebooks api * Made some changes mentioned in PR * Refactored the code, removed enpoint logic from the notebook handler and wrote test cases * changes select statements, fixed a bug in the test job cleanup and fixed other stuff mentioned in the PR. * added notebook_error column in notebook history select statement * Added get template notebook endpoint
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Management;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public class AgentNotebookTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test case for fetch notebook jobs Request Handler
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleAgentNotebooksRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var fetchNotebooksContext = new Mock<RequestContext<AgentNotebooksResult>>();
|
||||
|
||||
fetchNotebooksContext.Setup(x => x.SendResult(It.IsAny<AgentNotebooksResult>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleAgentNotebooksRequest(new AgentNotebooksParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri
|
||||
}, fetchNotebooksContext.Object);
|
||||
|
||||
fetchNotebooksContext.Verify(x => x.SendResult(It.Is<AgentNotebooksResult>(p => p.Success == true)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the create job helper function
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestAgentNotebookCreateHelper()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
AgentNotebookInfo notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
Assert.Equal(false, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
notebook = AgentTestUtils.SetupNotebookJob(connectionResult).Result;
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
await AgentTestUtils.CleanupNotebookJob(connectionResult, notebook);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the create job request handler with an invalid file path
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestHandleCreateAgentNotebookRequestWithInvalidTemplatePath()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
AgentNotebookInfo notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
var createNotebookContext = new Mock<RequestContext<CreateAgentNotebookResult>>();
|
||||
createNotebookContext.Setup(x => x.SendResult(It.IsAny<CreateAgentNotebookResult>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleCreateAgentNotebookRequest(new CreateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = "garbargepath"
|
||||
}, createNotebookContext.Object);
|
||||
|
||||
createNotebookContext.Verify(x => x.SendResult(It.Is<CreateAgentNotebookResult>(p => p.Success == false)));
|
||||
Assert.Equal(false, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// creating a job with duplicate name
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestDuplicateJobCreation()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
AgentNotebookInfo notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
var createNotebookContext = new Mock<RequestContext<CreateAgentNotebookResult>>();
|
||||
createNotebookContext.Setup(x => x.SendResult(It.IsAny<CreateAgentNotebookResult>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleCreateAgentNotebookRequest(new CreateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = AgentTestUtils.CreateTemplateNotebookFile()
|
||||
}, createNotebookContext.Object);
|
||||
|
||||
createNotebookContext.Verify(x => x.SendResult(It.Is<CreateAgentNotebookResult>(p => p.Success == true)));
|
||||
await service.HandleCreateAgentNotebookRequest(new CreateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = AgentTestUtils.CreateTemplateNotebookFile()
|
||||
}, createNotebookContext.Object);
|
||||
createNotebookContext.Verify(x => x.SendResult(It.Is<CreateAgentNotebookResult>(p => p.Success == false)));
|
||||
await AgentTestUtils.CleanupNotebookJob(connectionResult, notebook);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the create notebook job handler
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestCreateAgentNotebookHandler()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
AgentNotebookInfo notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
var createNotebookContext = new Mock<RequestContext<CreateAgentNotebookResult>>();
|
||||
createNotebookContext.Setup(x => x.SendResult(It.IsAny<CreateAgentNotebookResult>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleCreateAgentNotebookRequest(new CreateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = AgentTestUtils.CreateTemplateNotebookFile()
|
||||
}, createNotebookContext.Object);
|
||||
createNotebookContext.Verify(x => x.SendResult(It.Is<CreateAgentNotebookResult>(p => p.Success == true)));
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
var createdNotebook = AgentTestUtils.GetNotebook(connectionResult, notebook.Name);
|
||||
await AgentTestUtils.CleanupNotebookJob(connectionResult, createdNotebook);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the delete notebook job handler
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestDeleteAgentNotebookHandler()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
//creating a notebook job
|
||||
AgentNotebookInfo notebook = AgentTestUtils.SetupNotebookJob(connectionResult).Result;
|
||||
//verifying it's getting created
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
//deleting the notebook job
|
||||
var deleteNotebookContext = new Mock<RequestContext<ResultStatus>>();
|
||||
deleteNotebookContext.Setup(x => x.SendResult(It.IsAny<ResultStatus>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleDeleteAgentNotebooksRequest(new DeleteAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook
|
||||
}, deleteNotebookContext.Object);
|
||||
deleteNotebookContext.Verify(x => x.SendResult(It.Is<ResultStatus>(p => p.Success == true)));
|
||||
//verifying if the job is deleted
|
||||
Assert.Equal(false, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// deleting a existing notebook job
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestDeleteNonExistentJob()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
//getting a test notebook object
|
||||
var notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
|
||||
var deleteNotebookContext = new Mock<RequestContext<ResultStatus>>();
|
||||
deleteNotebookContext.Setup(x => x.SendResult(It.IsAny<ResultStatus>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleDeleteAgentNotebooksRequest(new DeleteAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook
|
||||
}, deleteNotebookContext.Object);
|
||||
//endpoint should error out
|
||||
deleteNotebookContext.Verify(x => x.SendResult(It.Is<ResultStatus>(p => p.Success == false)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// updating a non existing notebook job
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestUpdateNonExistentJob()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
//getting a test notebook object
|
||||
AgentNotebookInfo notebook = AgentTestUtils.GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
|
||||
var updateNotebookContext = new Mock<RequestContext<UpdateAgentNotebookResult>>();
|
||||
updateNotebookContext.Setup(x => x.SendResult(It.IsAny<UpdateAgentNotebookResult>())).Returns(Task.FromResult(new object()));
|
||||
await service.HandleUpdateAgentNotebookRequest(new UpdateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = AgentTestUtils.CreateTemplateNotebookFile()
|
||||
}, updateNotebookContext.Object);
|
||||
// enpoint should error out
|
||||
updateNotebookContext.Verify(x => x.SendResult(It.Is<UpdateAgentNotebookResult>(p => p.Success == false)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// update notebook handler with garbage path
|
||||
/// </summary>
|
||||
[Fact]
|
||||
internal async Task TestUpdateWithGarbagePath()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
//seting up a temp notebook job
|
||||
var notebook = AgentTestUtils.SetupNotebookJob(connectionResult).Result;
|
||||
//verifying that the notebook is created
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
|
||||
var updateNotebookContext = new Mock<RequestContext<UpdateAgentNotebookResult>>();
|
||||
updateNotebookContext.Setup(x => x.SendResult(It.IsAny<UpdateAgentNotebookResult>())).Returns(Task.FromResult(new object()));
|
||||
//calling the endpoint with a garbage path
|
||||
await service.HandleUpdateAgentNotebookRequest(new UpdateAgentNotebookParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Notebook = notebook,
|
||||
TemplateFilePath = "garbargepath"
|
||||
}, updateNotebookContext.Object);
|
||||
//the enpoint should return false
|
||||
updateNotebookContext.Verify(x => x.SendResult(It.Is<UpdateAgentNotebookResult>(p => p.Success == false)));
|
||||
|
||||
//cleaning up the job
|
||||
await AgentTestUtils.CleanupNotebookJob(connectionResult, notebook);
|
||||
Assert.Equal(false, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
internal async Task TestDeletingUpdatedJob()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var service = new AgentService();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
//seting up a temp notebook job
|
||||
var notebook = AgentTestUtils.SetupNotebookJob(connectionResult).Result;
|
||||
//verifying that the notebook is created
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
|
||||
var originalName = notebook.Name;
|
||||
//Changing the notebookName
|
||||
notebook.Name = "myTestNotebookJob" + Guid.NewGuid().ToString();
|
||||
|
||||
Assert.Equal(false, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
|
||||
await AgentNotebookHelper.UpdateNotebook(
|
||||
service,
|
||||
connectionResult.ConnectionInfo.OwnerUri,
|
||||
originalName,
|
||||
notebook,
|
||||
null,
|
||||
ManagementUtils.asRunType(0)
|
||||
);
|
||||
|
||||
Assert.Equal(true, AgentTestUtils.VerifyNotebook(connectionResult, notebook));
|
||||
|
||||
//cleaning up the job
|
||||
await AgentTestUtils.CleanupNotebookJob(connectionResult, notebook);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,14 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Management;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Moq;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
@@ -22,7 +24,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
|
||||
internal static AgentJobStepInfo GetTestJobStepInfo(
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobInfo job,
|
||||
AgentJobInfo job,
|
||||
string stepName = "Test Job Step1")
|
||||
{
|
||||
return new AgentJobStepInfo()
|
||||
@@ -57,7 +59,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
CategoryType = 1,
|
||||
LastRun = "today",
|
||||
NextRun = "tomorrow",
|
||||
JobId = Guid.NewGuid().ToString()
|
||||
JobId = Guid.NewGuid().ToString(),
|
||||
Owner = "sa"
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,12 +77,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
internal static AgentProxyInfo GetTestProxyInfo()
|
||||
{
|
||||
return new AgentProxyInfo()
|
||||
{
|
||||
{
|
||||
AccountName = "Test Proxy",
|
||||
CredentialName = SecurityTestUtils.TestCredentialName,
|
||||
Description = "Test proxy description",
|
||||
IsEnabled = true
|
||||
};
|
||||
IsEnabled = true
|
||||
};
|
||||
}
|
||||
|
||||
internal static AgentScheduleInfo GetTestScheduleInfo()
|
||||
@@ -93,11 +96,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentJob(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobInfo job)
|
||||
{
|
||||
var context = new Mock<RequestContext<CreateAgentJobResult>>();
|
||||
var context = new Mock<RequestContext<CreateAgentJobResult>>();
|
||||
await service.HandleCreateAgentJobRequest(new CreateAgentJobParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -107,12 +110,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task UpdateAgentJob(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobInfo job)
|
||||
{
|
||||
job.Description = "Update job description";
|
||||
var context = new Mock<RequestContext<UpdateAgentJobResult>>();
|
||||
var context = new Mock<RequestContext<UpdateAgentJobResult>>();
|
||||
await service.HandleUpdateAgentJobRequest(new UpdateAgentJobParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -122,12 +125,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentJob(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobInfo job,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobInfo job,
|
||||
bool verify = true)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
await service.HandleDeleteAgentJobRequest(new DeleteAgentJobParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -141,11 +144,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentJobStep(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobStepInfo stepInfo)
|
||||
{
|
||||
var context = new Mock<RequestContext<CreateAgentJobStepResult>>();
|
||||
var context = new Mock<RequestContext<CreateAgentJobStepResult>>();
|
||||
await service.HandleCreateAgentJobStepRequest(new CreateAgentJobStepParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -153,13 +156,13 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
|
||||
internal static async Task UpdateAgentJobStep(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobStepInfo stepInfo)
|
||||
{
|
||||
var context = new Mock<RequestContext<UpdateAgentJobStepResult>>();
|
||||
var context = new Mock<RequestContext<UpdateAgentJobStepResult>>();
|
||||
await service.HandleUpdateAgentJobStepRequest(new UpdateAgentJobStepParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -169,11 +172,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentJobStep(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentJobStepInfo stepInfo)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
await service.HandleDeleteAgentJobStepRequest(new DeleteAgentJobStepParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
@@ -183,7 +186,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentOperator(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentOperatorInfo operatorInfo)
|
||||
{
|
||||
@@ -197,7 +200,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task UpdateAgentOperator(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentOperatorInfo operatorInfo)
|
||||
{
|
||||
@@ -211,7 +214,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentOperator(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentOperatorInfo operatorInfo)
|
||||
{
|
||||
@@ -225,7 +228,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentProxy(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentProxyInfo proxy)
|
||||
{
|
||||
@@ -239,7 +242,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task UpdateAgentProxy(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
string originalProxyName,
|
||||
AgentProxyInfo proxy)
|
||||
@@ -255,8 +258,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentProxy(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentProxyInfo proxy)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
@@ -269,7 +272,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentSchedule(
|
||||
AgentService service,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentScheduleInfo schedule)
|
||||
{
|
||||
@@ -277,16 +280,16 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
await service.HandleCreateAgentScheduleRequest(new CreateAgentScheduleParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Schedule = schedule
|
||||
Schedule = schedule
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task UpdateAgentSchedule(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
string originalScheduleName,
|
||||
AgentScheduleInfo schedule)
|
||||
internal static async Task UpdateAgentSchedule(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
string originalScheduleName,
|
||||
AgentScheduleInfo schedule)
|
||||
{
|
||||
var context = new Mock<RequestContext<AgentScheduleResult>>();
|
||||
await service.HandleUpdateAgentScheduleRequest(new UpdateAgentScheduleParams()
|
||||
@@ -299,8 +302,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentSchedule(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentScheduleInfo schedule)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
@@ -346,5 +349,116 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
var service = new AgentService();
|
||||
await DeleteAgentJob(service, connectionResult, job);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static async Task<AgentNotebookInfo> SetupNotebookJob(
|
||||
TestConnectionResult connectionResult,
|
||||
AgentNotebookInfo notebook = null)
|
||||
{
|
||||
var service = new AgentService();
|
||||
if (notebook == null)
|
||||
{
|
||||
notebook = GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
|
||||
}
|
||||
string tempNotebookPath = CreateTemplateNotebookFile();
|
||||
|
||||
await AgentNotebookHelper.CreateNotebook(
|
||||
service,
|
||||
connectionResult.ConnectionInfo.OwnerUri,
|
||||
notebook,
|
||||
tempNotebookPath,
|
||||
ManagementUtils.asRunType(0)
|
||||
);
|
||||
|
||||
var createdNotebook = GetNotebook(connectionResult, notebook.Name);
|
||||
File.Delete(tempNotebookPath);
|
||||
return createdNotebook;
|
||||
}
|
||||
|
||||
public static async Task CleanupNotebookJob(TestConnectionResult connectionResult, AgentNotebookInfo notebook)
|
||||
{
|
||||
var service = new AgentService();
|
||||
await AgentNotebookHelper.DeleteNotebook(
|
||||
service,
|
||||
connectionResult.ConnectionInfo.OwnerUri,
|
||||
notebook,
|
||||
ManagementUtils.asRunType(0)
|
||||
);
|
||||
}
|
||||
|
||||
public static AgentNotebookInfo GetNotebook(TestConnectionResult connectionResult, string name){
|
||||
var notebookList = AgentNotebookHelper.GetAgentNotebooks(connectionResult.ConnectionInfo).Result;
|
||||
foreach(AgentNotebookInfo n in notebookList)
|
||||
{
|
||||
if(n.Name == name)
|
||||
{
|
||||
return n;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static bool VerifyNotebook(TestConnectionResult connectionResult, AgentNotebookInfo notebook)
|
||||
{
|
||||
var notebookList = AgentNotebookHelper.GetAgentNotebooks(connectionResult.ConnectionInfo).Result;
|
||||
foreach (AgentNotebookInfo n in notebookList)
|
||||
{
|
||||
if (NotebookObjectEquals(notebook, n))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static bool NotebookObjectEquals(AgentNotebookInfo expectedNotebook, AgentNotebookInfo actualNotebook)
|
||||
{
|
||||
return (
|
||||
expectedNotebook.Name == actualNotebook.Name
|
||||
&&
|
||||
expectedNotebook.Description == actualNotebook.Description
|
||||
);
|
||||
}
|
||||
|
||||
internal static string CreateTemplateNotebookFile()
|
||||
{
|
||||
Assembly assembly = Assembly.GetAssembly(typeof(AgentNotebookTests));
|
||||
Stream scriptStream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".Agent.NotebookResources.TestNotebook.ipynb");
|
||||
StreamReader reader = new StreamReader(scriptStream);
|
||||
string testNotebookString = reader.ReadToEnd();
|
||||
string tempNotebookPath = System.IO.Path.GetTempFileName().Replace(".tmp", ".ipynb");
|
||||
File.WriteAllText(tempNotebookPath, testNotebookString);
|
||||
return tempNotebookPath;
|
||||
}
|
||||
|
||||
internal static AgentNotebookInfo GetTestNotebookInfo(string TestJobName, string TargetDatabase)
|
||||
{
|
||||
return new AgentNotebookInfo()
|
||||
{
|
||||
Name = TestJobName,
|
||||
Description = "Test job description",
|
||||
CurrentExecutionStatus = JobExecutionStatus.Executing,
|
||||
LastRunOutcome = CompletionResult.InProgress,
|
||||
CurrentExecutionStep = "Step 1",
|
||||
Enabled = false,
|
||||
HasTarget = false,
|
||||
HasSchedule = false,
|
||||
HasStep = false,
|
||||
Runnable = true,
|
||||
Category = "Cateory 1",
|
||||
CategoryId = 1,
|
||||
CategoryType = 1,
|
||||
LastRun = "today",
|
||||
NextRun = "tomorrow",
|
||||
JobId = new Guid().ToString(),
|
||||
TargetDatabase = TargetDatabase,
|
||||
Owner = "sa",
|
||||
ExecuteDatabase = TargetDatabase,
|
||||
JobSchedules = new AgentScheduleInfo[0],
|
||||
Alerts = new AgentAlertInfo[0]
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"name": "SQL",
|
||||
"display_name": "SQL",
|
||||
"language": "sql"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "sql",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat_minor": 2,
|
||||
"nbformat": 4,
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": "select * from sys.all_objects",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"execution_count": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -44,4 +44,10 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Integration' ">
|
||||
<DefineConstants>$(DefineConstants);WINDOWS_ONLY_BUILD</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Remove=".\Agent\NotebookResources\TestNotebook.ipynb" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include=".\Agent\NotebookResources\TestNotebook.ipynb" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user