TDD in Ruby Course

Saturday, July 25, 2015

Docker Q&A

1. How to monitor the processes?

Process monitoring builtin.

2. Logging and log files.

Stdin and Stdout. Can be forwarded. Syslog and journald driver.

3. Storing and manipulating files.

Create volume.
docker run -v /foo

docker run -v /var/foo:foo

4. Do we need to rebuild the image if the code is changed?

See Docker Rails Dev Demo

5. Why set :bind, '0.0.0.0' ? It does not work without this line.

6. How to run tests when the image is built?

  Dockerfile is not used to run unit tests.

Friday, July 24, 2015

Notes from How to Debug Anything Presentation by James Golick

A blind debugging session.
The website using PHP is down.
What we have to work with

The source code. (No)
Knowledge of the system. (No)
Familiarity with the programming language. (No)
SSH Access. (Yes)

Logging in the real world is often useless.
Find a pid. ps aux | grep apache

sudo strace -ff -s 2048 -p pid

How to read strace output

write(l, "hi\n", 3) = 3

write -> function name
arguments are l, "hi\n" and 3
return value is 3

To learn more about system calls: man 2 write

strace gives lot of output. Work backwards and find the failure. Look for the error message in the strace output. Find the cause by reading the strace output. Find the offender and write down your hypothesis. Prove your hypothesis. Find the offender. Fix the bug.

0. Forget everything you think you know.
1. Get a third party opinion.
2. Refer the 'Linux Performance Tools' diagram for a list of third-party tools.

You can also use strace to start a process.

sudo strace -ff apt-get update

Work backwards, find failure. Find the cause. Confirm your hypothesis. Locate a hook. Stare at the code. Confirm your hypothesis.

2. Locate the correct source code.
3. Identify a hard-coded string to grep for.
4. Stare at the code until it makes sense.
5. Fix whatever is broken.


0. Forget everything you think you know.
1. Get a third party opinion.
2. Locate the correct source code.
3. Identify a hard-coded string to grep for.
4. Stare at the code until it makes sense.
5. Fix whatever is broken.

puts Process.ppid

t = Thread.new do
   sleep 10
end

# Grabbing the pid.
pid = Process.pid

puts pid

# Get the child pids.
pipe = IO.popen("ps -ef | grep #{pid}")

child_pids = pipe.readlines.map do |line|
  puts line
  parts = line.split(/\s+/)
  # puts parts
  # parts[2] if parts[3] == pid.to_s and parts[2] != pipe.pid.to_s
end.compact

# Show the child processes.
# puts child_pids
#
#
# q = Queue.new
# # q.pop
#
t.join

Thursday, July 02, 2015

Colt Gem Released

Colt is a micro gem used to subscribe to a given plan using the Stripe API. The plans must already exist in your account. The current version can check Stripe credentials and Stripe API version.

 Source Code. You can find the different versions at Rubygems.org

Tuesday, June 30, 2015

Check Amazon S3 Credentials

1. gem install aws-s3
2. In IRB console : require 'aws/s3'
3.
AWS::S3::Base.establish_connection!(access_key_id: 'abcd', secret_access_key: 'secret')
AWS::S3::Service.buckets
AWS::S3::Bucket.find('your-existing-bucket-name')

It would be nice to steal code from aws-s3 gem to test if the credentials is valid or not.

ERROR: "https://rubygems.org" is not allowed by the gemspec, which only allows "TODO: Set to 'http://mygemserver.com'"

Problem: 

Not able to publish gem to rubygems.org

Resolution:

Delete the following lines from your-gem.gemspec file.

  if spec.respond_to?(:metadata)
    spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
  else
    raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
  end

how to use active support outside of rails

Error : NoMethodError: undefined method `hours' for 1:Fixnum

Resolution:

1. bundle open activesupport
2. Search for 'def hours', it is found in core_ext/numeric/time.rb
3. Add require 'active_support/core_ext/numberic/time.rb'

This is better than doing : require 'active_support/all'

Saturday, June 27, 2015

How to use Simplecov in gem development

1. Add the simplecov gem and start before anything else in test_helper.rb

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'simplecov'

SimpleCov.start

require 'rasam'
require 'minitest/autorun'

2. You can mark private methods as:
    # :nocov:
  to exclude from test coverage.
 
3. Run : rake test

4. Open coverage/index.html