-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from alanenriqueo/releases/v1
Release v1.2.0
- Loading branch information
Showing
1,657 changed files
with
331,299 additions
and
256,380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import * as core from '@actions/core'; | ||
import { IAuthorizer } from 'azure-actions-webclient/Authorizer/IAuthorizer'; | ||
import { WebRequest, WebResponse } from 'azure-actions-webclient/WebClient'; | ||
import { ServiceClient as AzureRestClient, ToError, AzureError } from 'azure-actions-webclient/AzureRestClient'; | ||
|
||
import AzureMySqlResourceManager, { FirewallRule } from '../src/AzureMySqlResourceManager'; | ||
import FirewallManager from '../src/FirewallManager'; | ||
|
||
jest.mock('azure-actions-webclient/AzureRestClient'); | ||
|
||
describe('AzureMySqlResourceManager tests', () => { | ||
it('initializes resource manager correctly for single server', async () => { | ||
let getRequestUrlSpy = jest.spyOn(AzureRestClient.prototype, 'getRequestUri').mockReturnValue('https://randomUrl/'); | ||
let beginRequestSpy = jest.spyOn(AzureRestClient.prototype, 'beginRequest').mockResolvedValue({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer' | ||
}, | ||
{ | ||
name: 'testServer2', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer2' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}); | ||
|
||
let resourceManager = await AzureMySqlResourceManager.getResourceManager('testServer', {} as IAuthorizer); | ||
let server = resourceManager.getMySqlServer(); | ||
|
||
expect(server!.name).toEqual('testServer'); | ||
expect(server!.id).toEqual('/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer'); | ||
expect(getRequestUrlSpy).toHaveBeenCalledTimes(1); | ||
expect(beginRequestSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('initializes resource manager correctly for flexible server', async () => { | ||
let getRequestUrlSpy = jest.spyOn(AzureRestClient.prototype, 'getRequestUri').mockReturnValue('https://randomUrl/'); | ||
let beginRequestSpy = jest.spyOn(AzureRestClient.prototype, 'beginRequest').mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer' | ||
}, | ||
{ | ||
name: 'testServer2', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer2' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}).mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer3', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer3' | ||
}, | ||
{ | ||
name: 'testServer4', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer4' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}); | ||
|
||
let resourceManager = await AzureMySqlResourceManager.getResourceManager('testServer3', {} as IAuthorizer); | ||
let server = resourceManager.getMySqlServer(); | ||
|
||
expect(server!.name).toEqual('testServer3'); | ||
expect(server!.id).toEqual('/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer3'); | ||
expect(getRequestUrlSpy).toHaveBeenCalledTimes(2); | ||
expect(beginRequestSpy).toHaveBeenCalledTimes(2); | ||
}) | ||
|
||
it('throws if resource manager fails to initialize', async () => { | ||
let getRequestUrlSpy = jest.spyOn(AzureRestClient.prototype, 'getRequestUri').mockReturnValue('https://randomUrl/'); | ||
let beginRequestSpy = jest.spyOn(AzureRestClient.prototype, 'beginRequest').mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer1', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer1' | ||
}, | ||
{ | ||
name: 'testServer2', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer2' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}).mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer3', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/flexibleServers/testServer3' | ||
}, | ||
{ | ||
name: 'testServer4', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/flexibleServers/testServer4' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}); | ||
|
||
let expectedError = `Unable to get details of MySQL server testServer. MySQL server 'testServer' was not found in the subscription.`; | ||
try { | ||
await AzureMySqlResourceManager.getResourceManager('testServer', {} as IAuthorizer); | ||
} catch(error) { | ||
expect(error.message).toEqual(expectedError); | ||
} | ||
expect(getRequestUrlSpy).toHaveBeenCalledTimes(2); | ||
expect(beginRequestSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('adds firewall rule successfully', async () => { | ||
let getRequestUrlSpy = jest.spyOn(AzureRestClient.prototype, 'getRequestUri').mockReturnValue('https://randomUrl/'); | ||
let beginRequestSpy = jest.spyOn(AzureRestClient.prototype, 'beginRequest').mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer' | ||
}, | ||
{ | ||
name: 'testServer2', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer2' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}).mockResolvedValueOnce({ | ||
statusCode: 202, | ||
body: {}, | ||
statusMessage: 'OK', | ||
headers: { | ||
'azure-asyncoperation': 'http://asyncRedirectionURI' | ||
} | ||
}).mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
'status': 'Succeeded' | ||
}, | ||
statusMessage: 'OK', | ||
headers: {} | ||
});; | ||
|
||
let getFirewallRuleSpy = jest.spyOn(AzureMySqlResourceManager.prototype, 'getFirewallRule').mockResolvedValue({ | ||
name: 'FirewallRule' | ||
} as FirewallRule); | ||
|
||
let resourceManager = await AzureMySqlResourceManager.getResourceManager('testServer', {} as IAuthorizer); | ||
await resourceManager.addFirewallRule('0.0.0.0', '1.1.1.1'); | ||
|
||
expect(getRequestUrlSpy).toHaveBeenCalledTimes(2); | ||
expect(beginRequestSpy).toHaveBeenCalledTimes(3); | ||
expect(getFirewallRuleSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('removes firewall rule successfully', async () => { | ||
let getRequestUrlSpy = jest.spyOn(AzureRestClient.prototype, 'getRequestUri').mockReturnValue('https://randomUrl/'); | ||
let beginRequestSpy = jest.spyOn(AzureRestClient.prototype, 'beginRequest').mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
value: [ | ||
{ | ||
name: 'testServer', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer' | ||
}, | ||
{ | ||
name: 'testServer2', | ||
id: '/subscriptions/SubscriptionId/resourceGroups/testrg/providers/Microsoft.DBforMySQL/servers/testServer2' | ||
} | ||
] | ||
}, | ||
statusMessage: 'OK', | ||
headers: [] | ||
}).mockResolvedValueOnce({ | ||
statusCode: 202, | ||
body: {}, | ||
statusMessage: 'OK', | ||
headers: { | ||
'azure-asyncoperation': 'http://asyncRedirectionURI' | ||
} | ||
}).mockResolvedValueOnce({ | ||
statusCode: 200, | ||
body: { | ||
'status': 'Succeeded' | ||
}, | ||
statusMessage: 'OK', | ||
headers: {} | ||
}); | ||
|
||
let resourceManager = await AzureMySqlResourceManager.getResourceManager('testServer', {} as IAuthorizer); | ||
await resourceManager.removeFirewallRule({ name: 'FirewallRule' } as FirewallRule); | ||
|
||
expect(getRequestUrlSpy).toHaveBeenCalledTimes(2); | ||
expect(beginRequestSpy).toHaveBeenCalledTimes(3); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as exec from '@actions/exec'; | ||
import MySqlUtils from "../src/MySqlUtils"; | ||
import AzureMySqlActionHelper from "../src/AzureMySqlActionHelper"; | ||
import { HttpClient } from '@actions/http-client'; | ||
|
||
jest.mock('@actions/http-client'); | ||
|
||
describe('MySqlUtils tests', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('detectIPAddress should return ip address', async () => { | ||
let getMySqlClientPathSpy = jest.spyOn(AzureMySqlActionHelper, 'getMySqlClientPath').mockResolvedValue('mysql.exe'); | ||
|
||
let execSpy = jest.spyOn(exec, 'exec').mockImplementation((_commandLine, _args, options) => { | ||
let mysqlClientError = `Client with IP address '1.2.3.4' is not allowed to access the server.`; | ||
options!.listeners!.stderr!(Buffer.from(mysqlClientError)); | ||
return Promise.reject(1); | ||
}); | ||
|
||
let httpSpy = jest.spyOn(HttpClient.prototype, 'getJson').mockResolvedValue({ | ||
statusCode: 200, | ||
result: { | ||
ip: '1.2.3.4', | ||
}, | ||
headers: {}, | ||
}); | ||
|
||
let ipAddress = await MySqlUtils.detectIPAddress('serverName', 'connString'); | ||
|
||
expect(getMySqlClientPathSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(httpSpy).toHaveBeenCalledTimes(1); | ||
expect(ipAddress).toBe('1.2.3.4'); | ||
}); | ||
|
||
it('detectIPAddress should return empty string', async () => { | ||
let getMySqlClientPathSpy = jest.spyOn(AzureMySqlActionHelper, 'getMySqlClientPath').mockResolvedValue('mysql.exe'); | ||
|
||
let execSpy = jest.spyOn(exec, 'exec').mockResolvedValue(0); | ||
|
||
let ipAddress = await MySqlUtils.detectIPAddress('serverName', 'connString'); | ||
|
||
expect(getMySqlClientPathSpy).toHaveBeenCalledTimes(1); | ||
expect(execSpy).toHaveBeenCalledTimes(1); | ||
expect(ipAddress).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.