Testing works fine when run locally but fails on Circleci

config.yml

    # PHP CircleCI 2.0 configuration file
    #
    # Check https://circleci.com/docs/2.0/language-php/ for more details
    #
        version: 2
        jobs:
          build:
            docker:
              # Specify the version you desire here
              - image: circleci/php:7.3.3
              - image: circleci/python:3.7.3

        steps:
          # Install pip
          - run: sudo apt install python-pip
          # Install aws-cli
          - run:
              name: Install aws-cli
              command: sudo pip install awscli

          # Install sam-cli
          - run:
              name: Install sam-cli
              command: sudo pip install aws-sam-cli

          - checkout

          - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
          - run: sudo apt-get update
          - run: sudo apt-get install -y libjpeg62-turbo-dev libpng-dev libfreetype6-dev
          - run: sudo docker-php-ext-install zip pdo mysqli pdo_mysql mbstring tokenizer ctype json bcmath gd
          - run: sudo docker-php-ext-enable pdo_mysql

          # Download and cache dependencies
          - restore_cache:
              keys:
                # "composer.lock" can be used if it is committed to the repo
                - v1-dependencies-{{ checksum "composer.json" }}
                # fallback to using the latest cache if no exact match is found
                - v1-dependencies-

          - run: composer install -n --prefer-dist

          - save_cache:
              key: composer-v1-{{ checksum "composer.json" }}
              paths:
                - ./vendor

          # prepare the database
          - run: touch /tmp/testing.sqlite
          - run: php artisan migrate --database=sqlite --force

          # run tests with phpunit or codecept
          - run: ./vendor/bin/phpunit

          # delete test database
          - run: sudo rm /tmp/testing.sqlite

          # set environment variables to .env
          - run: ....
          - run: ....
          - run: ....
          - run: ....
          - run: .... 
          - run: ....
          - run: ....
          - run: ....
          
          # commit to package
          - run: composer install --optimize-autoloader --no-dev
          - run: sudo php artisan cache:clear
          - run: sudo php artisan view:clear
          - run: sudo php artisan config:clear
          - run: sudo php artisan route:clear
          - run: sam package --output-template-file .stack.yaml --s3-bucket ticketpass-api
          - run: sam deploy --template-file .stack.yaml --capabilities CAPABILITY_IAM --stack-name ticketpass-api

Circleci output error:

Below I have pasted the type of error I get on the circleci terminal, and this is timed by the number of api endpoint that I am currently testing (80 circa). It sounds more like a code error in my PHP project, however, I do not have any of these whenever I run it on my machine.

1) Tests\Feature\getHostDetailsTest::testGetHostGroups
ErrorException: Trying to get property 'id' of non-object

/home/circleci/project/tests/Feature/HostTest.php:51

In my experience, errors of this kind are down to a differing environment between your local tests and your CI tests.

I would suggest tracing this error using the SSH-after-build function, and seeing why this object does not exist. Maybe it is expecting a database record, but the remote/CI database does not have a row? Or perhaps a recordset is null because a table does not exist, etc?

2 Likes

Hey @halfer true, I actually had an issue with the API when generating the images, and they were not sent properly, however, now that I have fixed this, I constantly get 500 error in return when runs on CircleCI only, despite running just fine locally.

I will post the test function along with the updated config file.

class TestDummys extends TestCase
{
        private static $hostId;
        private static $access_token = '';
        private static $user;
        private static $charityId;

    public function testDummy()
    {
            self::$access_token = auth()->login(User::first());
            self::$hostId = HostGroup::first()->id;

        $path = storage_path('testimage.png');
        $name = 'testimage.png';
        $file = new UploadedFile($path, $name, 'image/png', null, null, true);

        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . self::$access_token,
          ])->json('POST', '/host/' . self::$hostId .'/charity/external', [
              'name' => 'Charity',
              'contact' => 'foo@gmail.com',
              'registration_number' => '12345',
              'account_number' => '12345',
              'sort_code' => '12345',
              'country_code' => 'GB',
              'iban' => '124535',
              'image' => $file
          ]);        

              $response->assertStatus(200);
    
        }
}

PHP CircleCI 2.0 configuration file

#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
    version: 2
    jobs:
      build:
        docker:
          # Specify the version you desire here
          - image: circleci/php:7.3.3
          - image: circleci/python:3.7.3

    steps:
      # Install pip
      - run: sudo apt install python-pip
      # Install aws-cli
      - run:
          name: Install aws-cli
          command: sudo pip install awscli

      # Install sam-cli
      - run:
          name: Install sam-cli
          command: sudo pip install aws-sam-cli

      - checkout

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo apt-get update
      - run: sudo apt-get install -y libjpeg62-turbo-dev libpng-dev libfreetype6-dev
      - run: sudo docker-php-ext-install zip pdo mysqli pdo_mysql mbstring tokenizer ctype json bcmath gd
      - run: sudo docker-php-ext-enable pdo_mysql

      # Download and cache dependencies
      - restore_cache:
          keys:
            # "composer.lock" can be used if it is committed to the repo
            - v1-dependencies-{{ checksum "composer.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: composer install -n --prefer-dist

      - save_cache:
          key: composer-v1-{{ checksum "composer.json" }}
          paths:
            - ./vendor

      # prepare the database
      - run: touch /tmp/testing.sqlite
      - run: php artisan migrate --database=sqlite --force
      
      # get image and store it
  - run: curl https://d3qyaps1yzzqpv.cloudfront.net/images/eb_1554715247_2158207.png -o /tmp/testimage.png




      # run tests with phpunit or codecept
      - run: ./vendor/bin/phpunit

      # delete test database
      - run: sudo rm /tmp/testing.sqlite

      # set environment variables to .env
      - run: ....
      - run: ....
      - run: ....
      - run: ....
      - run: .... 
      - run: ....
      - run: ....
      - run: ....
      
      # commit to package
      - run: composer install --optimize-autoloader --no-dev
      - run: sudo php artisan cache:clear
      - run: sudo php artisan view:clear
      - run: sudo php artisan config:clear
      - run: sudo php artisan route:clear
      - run: sam package --output-template-file .stack.yaml --s3-bucket ticketpass-api
      - run: sam deploy --template-file .stack.yaml --capabilities CAPABILITY_IAM --stack-name ticketpass-api

A 500 error generally means that there was a fatal error when processing an HTTP request. Assuming the HTTP system under test is your own, this will likely have generated logs, possibly from Apache or PHP. Are you perhaps missing a PHP extension?

Agree with the fact that it sounds to be an HTTP request issue, however, upon checks, I can confirm that the error I get is pretty much the same as the one I used to get when I first posted this issue. (Few 400’s, 403’s, 500’s which are basically all chained since, if one fails, consequently the others are failing too).

Since this is always running successfully locally, I believe that in Circleci this fails, due to not finding the right file path through my PHP code when this gets created:

in order to do the job with the HTTP request, and obviously never fails locally as I am getting the image straight from my storage.
I am not a 100% sure about this…but this is what actually seems to happen…

Well, you can find out:

  • Have a look at your logs - is the request handled by Apache? Maybe there are some PHP errors;
  • Get an SSH session to the CircleCI build server and use some file_put_contents() calls, inserted using nano, to debug where it is failing.
1 Like

I have got the SSH session to the CircleCI build server, I was able to see that the folder got created successfully along with the image… Also debugged the code and still getting this random Server Error, which I highly doubt it!.. I am actually confused about this…

Did you happen to see if this is in the php/apache/server log? Maybe there’s a silly redirect breaking somewhere.

Hey @drazisil, no there is no redirect or anything similar actually.

@heyabredda - I wonder, in relation to the things I have asked you to check, would you mind looking again, via SSH? I appreciate you’re confused, that’s understandable - but do bear in mind that there really is nothing readers can help you with, in the absence of concrete things for us to look at.

Did you try writing some custom log lines to see where your code exits? You say that a folder got created, okay - I assume that is part of your web app. What else did you discover? You need to keep adding log write code to your PHP app until you narrow down to the point where the error/exit/crash happens. Would you make that the next thing you do?

1 Like

Hey @halfer upon checks, I found out that initially, the problem that I was facing was that the Redis server was not running by default, therefore I had to run the command to run it through my config file, once I have solved this, the 500 error code persisted, and I found out that it had something to do with uploading images to S3 since the bucket region was not the same as the request region (sounds odd, true), but changing bucket, this finally worked fine. Thanks for your support.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.