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

[Frontend] unit tests for node server #2745

Merged
merged 11 commits into from
Jan 6, 2020

Conversation

eterna2
Copy link
Contributor

@eterna2 eterna2 commented Dec 17, 2019

WIP: Adding basic unit tests for frontend node server.

PS: I noted some errors inside because seems like my new deployment cannot access the pod logs from my S3 buckets. Writing some unit tests to narrow down where are the bugs (could be my deployment issue also - cuz it worked last time, stop working after I upgrade the version).

See #2407
cc: @Bobgy


This change is Reviewable

@k8s-ci-robot
Copy link
Contributor

Hi @eterna2. Thanks for your PR.

I'm waiting for a kubeflow member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link
Contributor

@Bobgy Bobgy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into details later, but your efforts are really appreciated.
Thanks a lot!

/ok-to-test

frontend/server/aws-helper-test.ts Outdated Show resolved Hide resolved
@eterna2
Copy link
Contributor Author

eterna2 commented Dec 20, 2019

I need break up the big server.ts file so that unit test can be done more easily.

@Bobgy
Copy link
Contributor

Bobgy commented Dec 21, 2019

@eterna2 Thanks, that sounds reasonable.

Note that there are some ongoing work on https://github.com/kubeflow/pipelines/pull/2690/files which also touches node server. Beware of that and I recommend don't do too much refactoring that may conflict with it.

@eterna2
Copy link
Contributor Author

eterna2 commented Dec 23, 2019

Summary of changes:

  • unit tests for aws-helper and minio-helper (I found my bug, aws iam credential should work even when the session token expires now)
  • broke up setup.ts into
    • app.ts (registering all the middleware),
    • handlers/*.ts (defining the individual middleware), and
    • configs.ts (functions to parse argv and env vars into a config singleton)
  • e2e unit test for the express app:
    • test for gcs, s3, minio, and http/https artifacts
    • test for healthz
    • test for index.html

Impacts on other PRs

TODOs (in future PR?)

  • refractor k8s-helper (current state makes it impossible to do any unit test or mocking)
    • BLOCKS:
      • unit test for workflow-helper
      • e2e unit test for express app: for k8s-related routes (can't test, cuz can't mock k8s api)
  • probably shld add a real e2e test for the proxies in the integration test

@eterna2
Copy link
Contributor Author

eterna2 commented Dec 23, 2019

I updated @types/node to fix bug introduced by the recent updates: microsoft/TypeScript#32333

And updated @kubernetes/node-client to maintain compatibility with the updated @types/node.

This shld fix the docker build errors.

@eterna2 eterna2 changed the title [WIP] [Frontend] unit tests for node server [Frontend] unit tests for node server Dec 23, 2019
Copy link
Contributor

@Bobgy Bobgy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.
Left some minor comments.

frontend/.gitignore Outdated Show resolved Hide resolved
frontend/server/handlers/healthz.ts Outdated Show resolved Hide resolved
frontend/server/app.ts Show resolved Hide resolved
frontend/server/app.ts Outdated Show resolved Hide resolved
app: Application;
httpServer?: Server;

constructor(public readonly options: IConfigs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with node common patterns, is using constructor with heavy initialization logic common?
I'd personally prefer using a pure function, e.g. initUIServer(options) to create an instance of UIServer. Extra benefit is that, the creator function can be async too (I guess it's arguable whether we need this or not).

This is just discussion. What do you think?

Copy link
Contributor Author

@eterna2 eterna2 Dec 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classes are just syntactic sugars in js. I think there are no right common patterns (as in it is fine either way).

The main reason I did this is so that I write less code in my unit test. It is easier for me to do the setup and teardown if I only need to track a single object. But I probably can do the same with httpServer thou.

But I think this is perfectly fine too.

const app = initUIServer(options);
const server = app.listen(options.port, ()=>console.info(`listening to port:${port}.`);
server.close();

@Bobgy
Copy link
Contributor

Bobgy commented Dec 24, 2019

/test kubeflow-pipeline-sample-test

@eterna2
Copy link
Contributor Author

eterna2 commented Dec 26, 2019

  • Renamed all the interface (removed I)
  • Rebased to include the recent PR with tensorboard versioning
  • Added comments and fixed some lint/format errors

@Bobgy
Copy link
Contributor

Bobgy commented Dec 27, 2019

kubeflow-pipeline-sample-test test is flaky, we can retry later

Copy link
Contributor

@Bobgy Bobgy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great contribution! Having these tests in place will raise confidence and productivity of future development of this node server. Thanks for filling in the missing piece!

I finished the thorough review and left some (mostly minor) comments. Feel free to argue back as you see reasonable.

frontend/server/utils.ts Outdated Show resolved Hide resolved
frontend/server/handlers/tslint.json Outdated Show resolved Hide resolved
frontend/server/handlers/tensorboard.ts Outdated Show resolved Hide resolved
frontend/server/handlers/index-html.ts Outdated Show resolved Hide resolved
frontend/server/handlers/index-html.ts Show resolved Hide resolved
frontend/server/handlers/pod-logs.ts Outdated Show resolved Hide resolved
frontend/server/handlers/artifacts.ts Show resolved Hide resolved
frontend/server/configs.ts Outdated Show resolved Hide resolved
frontend/server/app.ts Outdated Show resolved Hide resolved
frontend/server/app.test.ts Outdated Show resolved Hide resolved
@Bobgy
Copy link
Contributor

Bobgy commented Dec 27, 2019

@eterna2 Here's how to integrate these tests in our CI.

This is the entrypoint for kubeflow-pipeline-frontend-test:

"test:ci:prow": "npm set unsafe-perm true && npm ci && npm run test:ci && ./scripts/report-coveralls.sh",

You can add an entry for calling server unit tests. It's fine doing it without coverage report. I can add coverage data later.

@eterna2
Copy link
Contributor Author

eterna2 commented Jan 2, 2020

Resolved all ur comments. Rewrote pod logs handler to be more functional. And added corresponding unit tests.

Also updated test:prow.

@Bobgy
Copy link
Contributor

Bobgy commented Jan 3, 2020

Awesome, I will review today

Copy link
Contributor

@Bobgy Bobgy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution again!
/lgtm
/approve

(awsInstanceProfileCredentials.ok as jest.Mock).mockClear();
(MinioClient as any).mockReset();
(awsInstanceProfileCredentials.getCredentials as jest.Mock).mockReset();
(awsInstanceProfileCredentials.ok as jest.Mock).mockReset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok fixed all the tests.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Bobgy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Bobgy
Copy link
Contributor

Bobgy commented Jan 3, 2020

/hold
It seems e2e tests are failing. let me retry first
kubeflow-pipeline-e2e-test

@Bobgy
Copy link
Contributor

Bobgy commented Jan 3, 2020

/test kubeflow-pipeline-e2e-test

@k8s-ci-robot k8s-ci-robot removed the lgtm label Jan 5, 2020
@Bobgy
Copy link
Contributor

Bobgy commented Jan 6, 2020

/hold cancel
Thanks! Looks great

@Bobgy
Copy link
Contributor

Bobgy commented Jan 6, 2020

/lgtm

@k8s-ci-robot k8s-ci-robot merged commit 2ec2fd2 into kubeflow:master Jan 6, 2020
Jeffwan pushed a commit to Jeffwan/pipelines that referenced this pull request Dec 9, 2020
* Add unit tests for aws-helper and minio-helper.

* Break up server.ts into app.ts and handlers/*.ts so that unit test can be written more easily.

* update @types/node because of microsoft/TypeScript#32333

* upgrade kubernete-client version to be compatible with new @types/node version

* fix bug: AWSInstanceProfileCredentials.profile() returns a promise not a string

* remove I prefix from interfaces. minor fixes on lint errors.

* fix format errors

* Added more unit tests, and minor fixes.

* Fix comment: GKS should be GKE

* use jest.resetAllMocks instead of reset each mock individually

* fix format for helper test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants