Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proxy support #480

Merged
merged 19 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"@adobe/aio-cli-lib-app-config": "^0.2.1",
"@adobe/aio-lib-core-config": "^2.0.0",
"@adobe/aio-lib-core-logging": "^1.1.0",
"@adobe/aio-lib-core-networking": "github:adobe/aio-lib-core-networking#master",
"@adobe/aio-lib-env": "^1.1.0",
"@adobe/aio-lib-ims": "^4.1.0",
"@adobe/aio-lib-runtime": "^3.0.1",
"@adobe/aio-lib-web": "^5.0.0",
"@adobe/generator-aio-app": "^3.0.0",
"@adobe/generator-aio-console": "^2.2.1",
"@adobe/aio-lib-runtime": "github:adobe/aio-lib-runtime#master",
"@adobe/aio-lib-web": "github:adobe/aio-lib-web#master",
"@adobe/generator-aio-app": "github:adobe/generator-aio-app#master",
"@adobe/aio-cli-lib-console": "github:adobe/generator-aio-console#master",
"@oclif/command": "^1.5.11",
"@oclif/config": "^1.12.9",
"@oclif/plugin-help": "^2.2.3",
Expand Down Expand Up @@ -42,6 +43,7 @@
"yeoman-environment": "^3.2.0"
},
"devDependencies": {
"@adobe/aio-lib-test-proxy": "^1.0.0",
"@adobe/eslint-config-aio-lib-config": "^1.3.0",
"@oclif/dev-cli": "^1.21.3",
"@types/jest": "^26.0.10",
Expand Down
2 changes: 1 addition & 1 deletion src/BaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const loadConfig = require('@adobe/aio-cli-lib-app-config')
const inquirer = require('inquirer')
const { CONSOLE_API_KEYS, APPLICATION_CONFIG_KEY, EXTENSIONS_CONFIG_KEY } = require('./lib/defaults')
const { getCliInfo } = require('./lib/app-helper')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app', { provider: 'debug' })

const {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/app-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const which = require('which')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lib-app-helper', { provider: 'debug' })
const { getToken, context } = require('@adobe/aio-lib-ims')
const { CLI } = require('@adobe/aio-lib-ims/src/context')
const fetch = require('node-fetch')
const { createFetch } = require('@adobe/aio-lib-core-networking')
const chalk = require('chalk')
const aioConfig = require('@adobe/aio-lib-core-config')
const { AIO_CONFIG_WORKSPACE_SERVICES, AIO_CONFIG_ORG_SERVICES } = require('./defaults')
Expand Down Expand Up @@ -255,6 +255,7 @@ async function downloadOWJar (url, outFile) {
aioLogger.debug(`downloadOWJar - url: ${url} outFile: ${outFile}`)
let response
try {
const fetch = createFetch()
response = await fetch(url)
} catch (e) {
aioLogger.debug(`connection error while downloading '${url}'`, e)
Expand Down Expand Up @@ -284,6 +285,7 @@ async function waitForOpenWhiskReadiness (host, endTime, period, timeout, waitFu
let ok

try {
const fetch = createFetch()
const response = await fetch(host + '/api/v1')
ok = response.ok
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions test/BaseCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jest.mock('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-ims')
const { getToken } = require('@adobe/aio-lib-ims')

jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
LibConsoleCLI.init.mockResolvedValue({})

const TheCommand = require('../src/BaseCommand')
Expand Down
6 changes: 3 additions & 3 deletions test/commands/app/add/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks')
const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks')

jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {
getWorkspaces: jest.fn(),
promptForSelectWorkspace: jest.fn(),
Expand Down
6 changes: 3 additions & 3 deletions test/commands/app/delete/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks')
const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks')

jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {
getEnabledServicesForOrg: jest.fn(),
promptForRemoveServiceSubscriptions: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ yeoman.createEnv.mockReturnValue({
jest.mock('@adobe/aio-lib-ims')

// mock console calls
jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {
getEnabledServicesForOrg: jest.fn(),
promptForSelectOrganization: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions test/commands/app/list/extension-points.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const BaseCommand = require('../../../../src/BaseCommand')
const yaml = require('js-yaml')
const dataMocks = require('../../../data-mocks/config-loader')

jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {}
LibConsoleCLI.init.mockResolvedValue(mockConsoleCLIInstance)

Expand Down
4 changes: 2 additions & 2 deletions test/commands/app/list/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const BaseCommand = require('../../../../src/BaseCommand')
const yaml = require('js-yaml')
const dataMocks = require('../../../data-mocks/config-loader')

jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {}
LibConsoleCLI.init.mockResolvedValue(mockConsoleCLIInstance)

Expand Down
6 changes: 3 additions & 3 deletions test/commands/app/use.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const mockPrompt = jest.fn()
inquirer.createPromptModule.mockReturnValue(mockPrompt)

// mock LibConsoleCLI
const consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks')
jest.mock('@adobe/generator-aio-console/lib/console-cli.js')
const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js')
const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
const mockConsoleCLIInstance = {
getWorkspaces: jest.fn(),
promptForSelectWorkspace: jest.fn(),
Expand Down
31 changes: 17 additions & 14 deletions test/commands/lib/app-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ governing permissions and limitations under the License.
// unmock to test proper returned urls from getActionUrls
jest.unmock('@adobe/aio-lib-runtime')

const mockFetch = jest.fn()
jest.mock('@adobe/aio-lib-core-networking', () => ({
createFetch: jest.fn(() => mockFetch)
}))

jest.mock('@adobe/aio-lib-core-config')
jest.mock('node-fetch')
jest.mock('execa')
jest.mock('process')
jest.mock('fs-extra') // do not touch the real fs
Expand All @@ -25,7 +29,6 @@ const which = require('which')
const fs = require('fs-extra')
const execa = require('execa')
const appHelper = require('../../../src/lib/app-helper')
const fetch = require('node-fetch')
const aioConfig = require('@adobe/aio-lib-core-config')
const libEnv = require('@adobe/aio-lib-env')
const libIms = require('@adobe/aio-lib-ims')
Expand All @@ -34,11 +37,11 @@ beforeEach(() => {
Object.defineProperty(process, 'platform', { value: 'linux' })
execa.mockReset()
execa.command.mockReset()
fetch.mockReset()
aioConfig.get.mockReset()
aioConfig.set.mockReset()
libEnv.getCliEnv.mockReset()
libIms.getToken.mockReset()
mockFetch.mockReset()
})

const getMockConfig = require('../../data-mocks/config-loader')
Expand Down Expand Up @@ -389,7 +392,7 @@ test('downloadOWJar failed (server has response, but not ok)', async () => {
ok: false,
statusText: 'some error'
}
fetch.mockReturnValueOnce(response)
mockFetch.mockResolvedValueOnce(response)

const url = 'https://some.url'
const result = appHelper.downloadOWJar(url, 'foo/bar')
Expand All @@ -398,7 +401,7 @@ test('downloadOWJar failed (server has response, but not ok)', async () => {

test('downloadOWJar failed (no server response, fetch exception)', async () => {
const err = new Error('some fetch error')
fetch.mockRejectedValueOnce(err)
mockFetch.mockRejectedValueOnce(err)

const url = 'https://some.url'
const result = appHelper.downloadOWJar(url, 'foo/bar')
Expand Down Expand Up @@ -428,7 +431,7 @@ test('downloadOWJar ok', async () => {
})
}
})
fetch.mockReturnValueOnce(response)
mockFetch.mockResolvedValueOnce(response)

const result = appHelper.downloadOWJar(url, fileToWrite)
await expect(result).resolves.toBeUndefined()
Expand All @@ -451,27 +454,27 @@ test('downloadOWJar (server connected ok, streaming error)', async () => {
const url = 'https://some.url'
const fileToWrite = 'foo/bar'

fetch.mockReturnValueOnce(response)
mockFetch.mockResolvedValueOnce(response)

const result = appHelper.downloadOWJar(url, fileToWrite)
await expect(result).rejects.toEqual(streamError)
})

test('runOpenWhiskJar ok', async () => {
fetch.mockReturnValue({ ok: true })
mockFetch.mockResolvedValue({ ok: true })
execa.mockReturnValue({ stdout: jest.fn() })

const result = appHelper.runOpenWhiskJar('jar', 'conf')

await expect(result).resolves.toEqual({
proc: expect.any(Object)
})
expect(fetch).toHaveBeenCalledTimes(1)
expect(mockFetch).toHaveBeenCalledTimes(1)
expect(execa).toHaveBeenCalledWith('java', expect.arrayContaining(['jar', 'conf']), {})
})

test('runOpenWhiskJar with AIO_OW_JVM_ARGS env var is passed to execa', async () => {
fetch.mockReturnValue({ ok: true })
mockFetch.mockResolvedValue({ ok: true })
execa.mockReturnValue({ stdout: jest.fn() })

aioConfig.get.mockReturnValueOnce('arg1 arg2')
Expand All @@ -481,7 +484,7 @@ test('runOpenWhiskJar with AIO_OW_JVM_ARGS env var is passed to execa', async ()
await expect(result).resolves.toEqual({
proc: expect.any(Object)
})
expect(fetch).toHaveBeenCalledTimes(1)
expect(mockFetch).toHaveBeenCalledTimes(1)
expect(execa).toHaveBeenCalledWith('java', expect.arrayContaining(['arg1', 'arg2', 'jar', 'conf']), {})
})

Expand All @@ -497,7 +500,7 @@ test('waitForOpenWhiskReadiness timeout', async () => {
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc)

await expect(result).rejects.toEqual(new Error(`local openwhisk stack startup timed out: ${timeout}ms`))
expect(fetch).toHaveBeenCalledTimes(0)
expect(mockFetch).toHaveBeenCalledTimes(0)
expect(waitFunc).toHaveBeenCalledTimes(0)
})

Expand All @@ -510,14 +513,14 @@ test('waitForOpenWhiskReadiness (fail, retry, then success)', async () => {
const waitFunc = jest.fn((_period) => {
expect(_period).toEqual(period)
})
fetch
mockFetch
.mockRejectedValueOnce(new Error('some error')) // first fail (fetch exception)
.mockRejectedValueOnce({ ok: false }) // second fail (response not ok)
.mockResolvedValue({ ok: true }) // finally success
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc)

await expect(result).resolves.not.toBeDefined()
expect(fetch).toHaveBeenCalledTimes(3)
expect(mockFetch).toHaveBeenCalledTimes(3)
expect(waitFunc).toHaveBeenCalledTimes(2)
})

Expand Down