Debugging Ruby Comments Service

Setup RubyMine and Comment Service

Some of these instructions may work outside of RubyMine, but they were originally done using RubyMine.  RubyMine is a product of JetBrains, so setup and licensing are like PyCharm.

Debug local version of Comment Service with RubyMine

This method involves deploying the Comment Service Ruby to your local host machine, and using Mongo and the rest of the application from Devstack.

  • Install rbenv on your host machine.  The following assumes Mac + Homebrew:

    brew install rbenv
  • Install Ruby 1.9

    rbenv install 1.9.3-p551
    rbenv global '1.9.3-p551'
    # verify the following gives 1.9.3-p551
    ruby --version
  • Add the following to your local ~/.bash_profile:

    eval "$(rbenv init -)"
  • Update your local Gems:

    # on your host machine
    cd ~/edx/cs_comments_service  # update path as required for your installation
    bundle install

    After running "bundle install", you may need to delete the cs_comments_service/.bundle directory if you see the following error:

    There was an error while trying to write to `/edx/app/forum/.gem/ruby/1.9.1`.

  • Forward Mongo port from devstack to your local environment.

    # Add the following to your devstack Vagrantfile:
    
        config.vm.network :forwarded_port, guest: 27017, host: 27017  # Mongo
  • Add local Ruby SDK to RubyMine 
    • RubyMine => Preferences => Languages and Frameworks => Ruby SDK and Gems
    • Use '+' to add an SDK
      • Choose "New local...".
      • Choose the newly installed Ruby SDK: ~/.rbenv/versions/1.9.3-p551/bin/ruby
      • Choose OK.
  •  Add Run Configuration
    • Run => Edit Configurations => + (Add New Configuration)
      • Choose "Rack" for the type of configuration.
      • Name: Comment Service
      • Server: WEBrick
      • Port: 28080 (or some unused port)
      • Environment variables: API_KEY=password
      • Use other SDK: ruby-1.9.3-p551 (~/.rbenv/versions/1.9.3-p551...)
      • Switch from "Configuration" tab to "Bundler" tab and check box.
      • Screens should look as follows:

This Run Configuration can be used to Run => "Debug...", and will run the comment service under http://localhost:28080

If you wish to run this same comment service from the command-line, rather than from RubyMine, you would use the following command:

API_KEY=password bundle exec rackup -o 0.0.0.0 -p 28080

This stopped working for me (Robert Raposa) as of April 3, 2017, with the following error:

NoMethodError: undefined method `each' for nil:NilClass
  /Users/robertraposa/.rbenv/versions/1.9.3-p550/lib/ruby/gems/1.9.1/gems/mongo-2.1.1/lib/mongo/cluster.rb:114:in `initialize'
  /Users/robertraposa/.rbenv/versions/1.9.3-p550/lib/ruby/gems/1.9.1/gems/mongo-2.1.1/lib/mongo/client.rb:300:in `new'
  /Users/robertraposa/.rbenv/versions/1.9.3-p550/lib/ruby/gems/1.9.1/gems/mongo-2.1.1/lib/mongo/client.rb:300:in `create_from_addresses'

I will try to update if I ever resolve this. For now, I need to abandon using the debugger.  


The Devstack LMS/Studio will not yet be able to see this local version of devstack.  In order to do that, you'll need to do remote port forwarding either by:

  • SSH to Vagrant using the following before starting LMS or Studio:

    vagrant ssh -- -R 18080:localhost:28080

    OR

  • Set up an ssh config by doing the following:
    • Run the following command:

      vagrant ssh-config
    • Copy the output to cat ~/.ssh/config with the following changes:

      # Use Host vagrant instead of Host default
      Host vagrant
        # PASTE properties output above
        # Add the following lines:
        # For running cs_comment_service locally
        RemoteForward 18080 127.0.0.1:28080
    • SSH to Vagrant using the new config as follows:

      ssh vagrant

Running and Debugging RSpec Tests

Running RSpec Tests locally using Docker

Using Docker, it is possible to run the RSpec tests as they are run in Travis.

The Mac and Travis load tests in a different order due to differences in the file system. If a failure occurs in one and not the other, output the test descriptions and determine if there is an order dependent order.

Also, note that the cs_comment_service directory is shared with your local git clone, which could also have changes that didn't come from docker.


First set up Docker on your machine using install front the internet.  Then, from your local cs_comments_service directory, run the following:

# start docker containers
docker-compose -f .travis/docker-compose-travis.yml up

# run the tests
docker exec forum_testing /edx/app/forum/cs_comments_service/.travis/run_tests.sh

Alternatively, after running docker-compose up, you can go into the forums docker image and run tests:

# ssh into the container
docker exec -ti forum_testing /bin/bash
cd /edx/app/forum/cs_comments_service

# once inside the Docker image, you could run the travis tests as follows:
.travis/run_tests.sh


# or, you could run the contents of the above run_tests.sh script from the command-line:
. /edx/app/forum/forum_env
export MONGOHQ_URL="mongodb://mongo.edx:27017/cs_comments_service_test"
bundle install
bin/rake search:initialize
# Then you could repeatedly run whatever tests make sense:
bin/rspec
bin/rspec -fd spec/api/search_spec.rb

If you wish to start with a clean container again, you can run the following:

docker-compose -f .travis/docker-compose-travis.yml down


If you have problems, try the Docker related Troubleshooting documentation.

Debugging RSpec Tests locally with RubyMine

Debugging the RSpec tests locally is simplest outside of Docker, directly on your local install.

Do not use the debugger gem when debugging from RubyMine.

Follow the instructions above for setting up RubyMine and rbenv locally.

If you have already set this up, and recently updated code, be sure to re-run bundle install:

cd cs_comments_service
bundle install

You also need to expose Mongo and ES to your local environment.  Inside cs_comments_service/.travis/docker-compose-travis.yml, add the port forwarding lines below:

# NOTE: You can use different ports if these are in conflict with your local environment.
services:
  elasticsearch:
	...
    ports:
      - "9200:9200"
  mongo:
	...
    ports:
      - "27017:27017"

Initialize the ES cluster if it hasn't been done already (as done in run_tests.sh):

export SEARCH_SERVER="http://localhost:9200"
bin/rake search:initialize

You should then be able to set up an RSpec run configuration, or right-mouse click on an RSpec file to run it locally.  Under Edit Configurations, you can copy the configuration to make it more permanent.

If the test involves Mongo or ES, you'll also need to Edit Configuration and add the following environment variables to the configuration:

SEARCH_SERVER=http://localhost:9200
MONGOHQ_URL=mongodb://localhost:27017/cs_comments_service_test

Running RSpec Tests locally using command line debugger

These are old instructions that haven't been tested with recent changes. Please edit/remove this comment or section as appropriate if you try this out.


  1. vagrant ssh
  2. sudo su forum
  3. rspec command will run all tests
  4. To run just a specific test, add ":focus => true" where the test is declared. Example:
    1. describe "app" do
          describe "notifications", :focus => true do

  5. Add a "debugger" command in the code where you want to stop in the debugger
  6. Now specify -d to the rspec command (rspec -d)
  7. If that fails with a message that starts with "cannot load such file -- ruby-debug", do this:
    1. edit Gemfile and uncomment the line with "debugger"
    2. bundle install
    3. Run rspec -d again

Running RSpec Tests on Devstack (not currently supported)

The RSpec tests currently rely on a Mongo 3 feature. At this time, Vagrant devstack still has an older version of Mongo, so you can not yet use the Vagrant devstack for running the RSpec tests.


Running RSpec Tests on Sandbox

On a sandbox, you can execute the following:

source /edx/app/forum/forum_env
cd /edx/app/forum/cs_comments_service
sudo mkdir coverage
# replace USERNAME with your username
sudo chown -R USERNAME:USERNAME coverage
bundle exec rspec

The RSpec tests will fail if you don't have Mongo 3 set up. See below for instructions.

Setting up Mongo 3.0.12 on a sandbox

Sandbox do not yet have Mongo 3 installed.  To verify, you ultimately want to see:

mongod --version
db version v3.0.12

If it is already set up on your sandbox, this section can probably be removed.  Otherwise, you can run the following:

# backup conf file
cp /etc/mongod.conf ~
# edit apt-get repo as follows
sudo vi /etc/apt/sources.list
  # replace mongo repo with the following line
  deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.0 multiverse
  # if there is a deb-src line for mongo, comment it out


# update apt-get, ignore warnings for rabbitmg
sudo apt-get update


# install mongo 3.0.12
sudo apt-get install mongodb-org=3.0.12
sudo apt-get install mongodb-org-server=3.0.12
sudo apt-get install mongodb-org-shell=3.0.12
sudo apt-get install mongodb-org-mongos=3.0.12
sudo apt-get install mongodb-org-tools=3.0.12


# stop service, restore conf file, and start service
sudo service mongod stop
sudo cp ~/mongod.conf /etc/mongod.conf
sudo service mongod start


Troubleshooting Errors

If you see the following error, it means you are probably trying to run the rspec tests against an older version of mongo.  The version of mongo on devstack has not yet been upgraded, and the tests will fail as follows:

/edx/app/forum/.gem/ruby/1.9.1/gems/mongo-2.1.1/lib/mongo/operation/result.rb:226:in `validate!': no such cmd: listCollections (59) (Mongo::Error::OperationFailure)