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

get module name from sys.modules #17779

Merged
merged 11 commits into from
Oct 10, 2022
Merged

get module name from sys.modules #17779

merged 11 commits into from
Oct 10, 2022

Conversation

girarda
Copy link
Contributor

@girarda girarda commented Oct 10, 2022

What

This is a fix for #17772.
Acceptance tests don't run from the same main file as running the discovery command.
Instead of inferring the connector's module from the main file, we can get it from the list of modules that are loaded in the environment at runtime

How

  • Iterate over the modules loaded at runtime and select the one that starts with "source_"

Recommended reading order

  1. airbyte-cdk/python/airbyte_cdk/sources/declarative/schema/json_schema.py

Testing

Testing cdk changes is tedious. I used the following script with the following docker image to run the SATs using this branch

CURRENT_DIR="$PWD"
DIR=$(basename "$PWD")
echo $DIR

# Build SAT
cd ../../bases/source-acceptance-test
docker build . -t airbyte/source-acceptance-test:dev

# Build connector
cd ../../../
docker build . -t airbyte/$DIR:dev -f airbyte-integrations/connectors/$DIR/Dockerfile
cd $CURRENT_DIR
#python3 -m pytest integration_tests -p integration_tests.acceptance

# Run
docker run --rm -it \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /tmp:/tmp \
    -v $(pwd):/test_input \
    --env CASSETTE_MODE \
    --env CASSETTE_PATH \
    airbyte/source-acceptance-test:dev \
    --acceptance-test-config /test_input

dockerfile:

FROM python:3.9-slim

# Bash is installed for more convenient debugging.
RUN apt-get update && apt-get install -y bash automake build-essential git libtool libtool-bin autoconf && rm -rf /var/lib/apt/lists/*

WORKDIR /airbyte/integration_code
COPY ./airbyte-integrations/connectors/source-exchange-rates-tutorial-2/source_exchange_rates_tutorial_2 ./source_exchange_rates_tutorial_2
RUN echo "$ls"

COPY ./airbyte-cdk/python ./cdk

COPY ./airbyte-integrations/connectors/source-exchange-rates-tutorial-2/main.py ./
COPY ./airbyte-integrations/connectors/source-exchange-rates-tutorial-2/setup.py ./

#COPY setup.py ./
RUN pip install -e ./cdk
RUN pip install .

ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.3.1
LABEL io.airbyte.name=airbyte/source-exchange-rates-tutorial-2

🚨 User Impact 🚨

Are there any breaking changes? What is the end result perceived by the user? If yes, please merge this PR with the 🚨🚨 emoji so changelog authors can further highlight this if needed.

Pre-merge Checklist

Expand the relevant checklist and delete the others.

New Connector

Community member or Airbyter

  • Community member? Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
    • docs/integrations/README.md
    • airbyte-integrations/builds.md
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub by running the /publish command described here
  • After the connector is published, connector added to connector index as described here
  • Seed specs have been re-generated by building the platform and committing the changes to the seed spec files, as described here
Updating a connector

Community member or Airbyter

  • Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • Changelog updated in docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub and connector version bumped by running the /publish command described here
Connector Generator
  • Issue acceptance criteria met
  • PR name follows PR naming conventions
  • If adding a new generator, add it to the list of scaffold modules being tested
  • The generator test modules (all connectors with -scaffold in their name) have been updated with the latest scaffold by running ./gradlew :airbyte-integrations:connector-templates:generator:testScaffoldTemplates then checking in your changes
  • Documentation which references the generator is updated as needed

Tests

Unit

Put your unit tests output here.

Integration

Put your integration tests output here.

Acceptance

Put your acceptance tests output here.

@github-actions github-actions bot added the CDK Connector Development Kit label Oct 10, 2022
@girarda girarda marked this pull request as ready for review October 10, 2022 12:09
@girarda girarda requested a review from a team as a code owner October 10, 2022 12:09
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

a few small things, but don't need to block on approval

# the connector's module name can be inferred by looking at the modules loaded and look for the one starting with source_
source_modules = [
k for k, v in sys.modules.items() if "source_" in k
] # example: ['source_exchange_rates', 'source_exchange_rates_tutorial.source']
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 a little confused by your comment example here. Are you saying that the result of one sys.modules call will result in these two items in the map? Or that these are two possible permutations of how the sys.modules call will be returned (one called from the source_exchange_rates source and one called from the source_exchange_rates_tutorial source)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed the comment. thanks!

source_modules = [
k for k, v in sys.modules.items() if "source_" in k
] # example: ['source_exchange_rates', 'source_exchange_rates_tutorial.source']
module = source_modules[0].split(".")[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

If a developer went out of their way to name their modules incorrectly w/o source_, this would crash. More of a user error to deviate from the template, but we should check here and throw an error instead of crashing. I'm sure other things would fail if they did that as well, but just in case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@girarda
Copy link
Contributor Author

girarda commented Oct 10, 2022

/publish-cdk dry-run=false

https://github.com/airbytehq/airbyte/actions/runs/3220453525

@girarda
Copy link
Contributor Author

girarda commented Oct 10, 2022

/publish-cdk dry-run=false

🕑 https://github.com/airbytehq/airbyte/actions/runs/3222250101
https://github.com/airbytehq/airbyte/actions/runs/3222250101

@girarda girarda merged commit 62500af into master Oct 10, 2022
@girarda girarda deleted the alex/infer_module_name branch October 10, 2022 20:54
jhammarstedt pushed a commit to jhammarstedt/airbyte that referenced this pull request Oct 31, 2022
* get module name from sys.modules

* bump

* fix comment

* throw exception

* fix unittests

* Add missing files

* remove debug prints

* indent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDK Connector Development Kit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants