Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxuan-ms authored and dcm committed Sep 2, 2020
1 parent 462d32a commit b22ee2d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,7 @@ language: python
env:
- BLACK_VERSION: "19.10b0"
global:
- REACT_APP_API_BASE_URL: "/api"
- REACT_APP_API_BASE_URL: "/fake/api/endpoint"

python:
- "2.7"
Expand Down
6 changes: 3 additions & 3 deletions testplan/testing/multitest/driver/kafka.py
Expand Up @@ -34,11 +34,11 @@ class KafkaStandalone(app.App):
"""
Driver for starting a Kafka instance in standalone mode.
:param cfg_template: Zookeeper config file template.
:param cfg_template: Kafka config file template.
:type cfg_template: ``str``
:param binary: zkServer.sh file path.
:param binary: kafka-server-start.sh file path.
:type binary: ``str``
:param port: Zookeeper listen port. Zookeeper doesn't support random port
:param port: Kafka listen port.
:type port: ``int``
:param env: Environmental variables to be made available to Zookeeper process.
:type env: ``dict``
Expand Down
60 changes: 47 additions & 13 deletions testplan/web_ui/testing/.env
@@ -1,16 +1,50 @@
# when debugging locally, create a file ".env.local" next to this file
# and set this variable to the full base URL of your API endpoint.
# You can set this variable to just the API path base if your API uses
# he same hostname and scheme as the webserver serving this app (which is
# likely "http://localhost:3000" when debugging locally). The app will set
# basic CORS headers when NODE_ENV == "development" or "test".
# When debugging locally, create a file ".env.local" next to this file
# containing:

REACT_APP_API_BASE_URL=/path/to/api/endpoint

# ... if your API is served from the same origin as this webapp. For example,
# if this web app is available at "https://www.myapp.mil/" and the API is
# available at "https://www.myapp.mil/api" then you'd set:

REACT_APP_API_BASE_URL=/api

# If your API is served from a different origin (e.g. if you're debugging
# locally at http://localhost:3000) than your API (say it's still
# "https://www.myapp.mil/api") then set:

REACT_APP_API_BASE_URL=https://www.myapp.mil/api

# This will also work if the web app and API are served from the same origin.
# The app will set basic CORS headers when NODE_ENV == "development" or "test".
# More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
#
#=============================================================================#
# Note that NOT setting REACT_APP_API_BASE_URL - either in the environment or #
# in another .env.* file - will result in a build error. #
#=============================================================================#
#
# More info:
# - https://create-react-app.dev/docs/advanced-configuration/
# - https://create-react-app.dev/docs/adding-custom-environment-variables/
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
# The .env.* files override one another like so (highest > lowest priority):
# > when NODE_ENV === "development":
# - (variables from environment)
# - .env.development.local
# - .env.development
# - .env.local
# - .env
# > when NODE_ENV === "production":
# - (variables from environment)
# - .env.production.local
# - .env.production
# - .env.local
# - .env
# > when NODE_ENV === "test": (omits .env.local)
# - (variables from environment)
# - .env.test.local
# - .env.test
# - .env
#
# The following configuration simply picks up the base url from an environment
# variable.
# More info:
# - https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used
# - https://create-react-app.dev/docs/advanced-configuration
#
REACT_APP_API_BASE_URL=${REACT_APP_API_BASE_URL}
REACT_APP_API_BASE_URL=OverrideMeOrThereWillBeABuildError
Expand Up @@ -13,6 +13,37 @@ import { PropagateIndices } from '../../../reportUtils';
import { toPlainObjectIn, flattened } from '../../../../Common/utils';

const __DEV__ = process.env.NODE_ENV !== 'production';
const API_BASE_URL = process.env.REACT_APP_API_BASE_URL;
const UNSET_API_BASE_URL_VAL = 'OverrideMeOrThereWillBeABuildError';
/** @type {URL} */
const API_BASE_URL_OBJ = (() => {
// these conditionals + try-catches are top-level so errors will be caught
// during the build.
// eslint-disable-next-line max-len
if(!_.isString(API_BASE_URL) || API_BASE_URL === UNSET_API_BASE_URL_VAL) {
throw new Error(
"The environment variable REACT_APP_API_BASE_URL must be set to your " +
"API's base URL. See this project's .env file for more information."
);
}
let apiBaseUrlObj;
try {
// this will not error when API_BASE_URL is a full URI
apiBaseUrlObj = new URL(API_BASE_URL);
} catch(err1) {
try {
// this will not error when API_BASE_URL only a path
apiBaseUrlObj = new URL(API_BASE_URL, window.location.origin);
} catch(err2) {
throw new Error(
`The environment variable REACT_APP_API_BASE_URL is not set to a ` +
`valid URL or a URL path - received ` +
`REACT_APP_API_BASE_URL="${API_BASE_URL}".`
);
}
}
return apiBaseUrlObj;
})();

let TEST_REPORTS = {}; // eslint-disable-line no-unused-vars
if(__DEV__) {
Expand All @@ -22,49 +53,14 @@ if(__DEV__) {
};
}

/** @returns {URL} */
function apiBaseUrlFromEnv() {
const prodErrMsg = 'API error: Please notify support with your logs.';
const baseURL = process.env.REACT_APP_API_BASE_URL;

if(!_.isString(baseURL)) {
let err;
if(__DEV__) {
err = new TypeError(
"The environment variable REACT_APP_API_BASE_URL must be set to your " +
"API's base URL. See this project's .env file for more information."
);
} else {
err = new Error(prodErrMsg);
}
throw err;
}

let baseURLObj;
try {
baseURLObj = new URL(baseURL);
} catch(originalError) {
let underlyingError;
if(__DEV__) throw {
originalError,
underlyingError: 'Environment variable REACT_APP_API_BASE_URL is not ' +
`a valid URL. Got "${REACT_APP_API_BASE_URL}".`,
}
throw new Error(prodErrMsg);
}

return baseURLObj;
}

const axiosDefaultConfig = (() => {
const baseUrlObj = apiBaseUrlFromEnv();
const baseURLOrigin = baseUrlObj.origin;
const baseURL = baseUrlObj.href;
const apiBaseURLOrigin = API_BASE_URL_OBJ.origin;
const apiBaseURL = API_BASE_URL_OBJ.href;
const headers = { ...defaultHeaders.common, Accept: 'application/json' };
if(__DEV__ && window.location.origin !== baseURLOrigin) {
headers['Access-Control-Allow-Origin'] = baseURLOrigin;
if(__DEV__ && window.location.origin !== apiBaseURLOrigin) {
headers['Access-Control-Allow-Origin'] = apiBaseURLOrigin;
}
return { baseURL, headers, timeout: 60_000 };
return { baseURL: apiBaseURL, headers, timeout: 60_000 };
})();

const fetchFakeReport = async ({ testReport }, { dispatch, requestId }) => {
Expand Down

0 comments on commit b22ee2d

Please sign in to comment.