diff --git a/.github/actions/setup-server/action.yml b/.github/actions/setup-server/action.yml new file mode 100644 index 0000000000..fe16033ca7 --- /dev/null +++ b/.github/actions/setup-server/action.yml @@ -0,0 +1,42 @@ +name: "Setup test env" +description: "Setup test environment for actions" + +inputs: + node-version: + description: "Node version" + required: false + default: "14" + cache-extension: + description: "Extension for fetching cached dependencies" + required: true + +runs: + using: "composite" + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} + + - name: Setup Node.js environment + uses: actions/setup-node@v2.4.1 + with: + node-version: ${{ inputs.node-version }} + cache: "yarn" + + - name: Bootstrap packages + uses: ./.github/actions/cache-bootstrap + with: + extension: ${{ inputs.cache-extension }} + + - name: Build Packages + shell: "bash" + run: yarn build + + - name: Install dev cli + shell: "bash" + run: sudo npm i -g medusa-dev-cli + + - name: Set path to medusa repo + shell: "bash" + run: medusa-dev --set-path-to-repo $(pwd) diff --git a/.github/actions/test-server/action.yml b/.github/actions/test-server/action.yml new file mode 100644 index 0000000000..5bdf141b24 --- /dev/null +++ b/.github/actions/test-server/action.yml @@ -0,0 +1,33 @@ +name: "Test server" +description: "Test the currently running medusa server to see if a user has been created and that the server is seeded" + +inputs: + email: + description: "email of user to log in" + required: false + default: "test@test.com" + password: + description: "password of user to log in" + required: false + default: "password" + pathToSeedData: + description: "path to seed data" + required: false + default: "../cli-test/data/seed.json" + +runs: + using: "composite" + steps: + - name: Wait for live server response + shell: "bash" + run: ./integration-tests/scripts/cli/wait-for-server-live.sh + - name: Log in with user + shell: "bash" + run: ./integration-tests/scripts/cli/login.sh ${{ inputs.email }} ${{ inputs.password }} + - name: GetProducts + shell: "bash" + run: ./integration-tests/scripts/cli/get-products.sh ${{ inputs.pathToSeedData }} + + - name: Kill server + shell: "bash" + run: kill -9 $(lsof -t -i :9000) diff --git a/.github/workflows/test-cli-with-database.yml b/.github/workflows/test-cli-with-database.yml new file mode 100644 index 0000000000..00ce8a5e8f --- /dev/null +++ b/.github/workflows/test-cli-with-database.yml @@ -0,0 +1,100 @@ +name: CLI Pipeline +on: [pull_request] + +jobs: + test-cli-with-database: + env: + REDIS_URL: redis://localhost:6379 + DATABASE_URL: "postgres://postgres:postgres@localhost/cli-test" + + strategy: + matrix: + db: [sqlite, postgres] + + services: + redis: + image: redis + # Set health checks to wait until redis has started + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 6379:6379 + + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + POSTGRES_DB: cli-test + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2.3.5 + with: + fetch-depth: 0 + + - name: Setup development server + uses: ./.github/actions/setup-server + with: + cache-extension: "cli-test" + + - name: Install Medusa cli + run: yarn global add @medusajs/medusa-cli + + - name: Create Medusa project + run: | + medusa new cli-test + working-directory: .. + + - name: Install postgres config + if: matrix.db == 'postgres' + run: | + curl \ + https://raw.githubusercontent.com/medusajs/medusa-starter-default/feat/postgres-config/medusa-config.js \ + --output medusa-config.js + working-directory: ../cli-test + + - name: run medusa dev + run: medusa-dev --force-install + working-directory: ../cli-test + + - name: Run migrations + run: medusa migrations run + working-directory: ../cli-test + + - name: Seed db + run: yarn seed + working-directory: ../cli-test + + - name: Create admin user + run: medusa user -e test@test.com -p password -i admin_123 + working-directory: ../cli-test + + ########################## Test medusa develop ############################### + + - name: Run development server + run: medusa develop > /dev/null 2>&1 & + working-directory: ../cli-test + + - name: Testing development server + uses: ./.github/actions/test-server + + ########################### Test medusa start ################################ + + - name: Starting medusa + run: medusa start > /dev/null 2>&1 & + working-directory: ../cli-test + + - name: Testing server + uses: ./.github/actions/test-server diff --git a/integration-tests/scripts/cli/get-products.sh b/integration-tests/scripts/cli/get-products.sh new file mode 100755 index 0000000000..60da1f0471 --- /dev/null +++ b/integration-tests/scripts/cli/get-products.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +seedJson=$(pwd)/$1 + +res=$(curl -s 'http://localhost:9000/store/products' | \ +python3 -c " + +import sys, json; + +loadedProducts = json.load(sys.stdin)['products']; + +seededProducts = json.load(open(\""$seedJson"\"))['products']; + +result = set([product['title'] for product in loadedProducts]) == set([product['title'] for product in seededProducts]) + +print(result) +") + +if [[ "$res" != "True" ]] ; then + echo "Seed failed, products are not equal" + exit 1 +else + exit 0 +fi \ No newline at end of file diff --git a/integration-tests/scripts/cli/login.sh b/integration-tests/scripts/cli/login.sh new file mode 100755 index 0000000000..bf9ab24e2f --- /dev/null +++ b/integration-tests/scripts/cli/login.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +status_code=$(curl \ + -X POST\ + -H "Content-Type: application/json"\ + -d '{"email":"'$1'", "password":"'$2'"}'\ + --write-out %{http_code}\ + http://localhost:9000/admin/auth) + +if [[ "$status_code" -ne 200 ]] ; then + echo "Site status changed to $status_code" + exit 1 +else + exit 0 +fi \ No newline at end of file diff --git a/integration-tests/scripts/cli/wait-for-server-live.sh b/integration-tests/scripts/cli/wait-for-server-live.sh new file mode 100755 index 0000000000..639de3d9d7 --- /dev/null +++ b/integration-tests/scripts/cli/wait-for-server-live.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +for i in {1..6} +do + echo $i + status_code=$(curl \ + -X GET \ + --write-out %{http_code} \ + --silent\ + --output /dev/null\ + http://localhost:9000/store/products) + +echo $status_code + if [[ "$status_code" -ne 000 ]] ; then + echo "exiting" + exit 0 + else + sleep 5 + fi +done + +echo $status_code + +if [[ "$status_code" = 000 ]] ; then + echo "Site status changed to $status_code" + exit 1 +else + exit 0 +fi