Rspec is giving file is not defined error

90 views Asked by At

My code is here

require 'spec_helper'
describe file('/home/ravendra/backup.sh') do
  its(:content) { should match /^ip=[$]1/ }
  its(:content) { should match /^ssh_port=[$]2/ }
  its(:content) { should match /^user=[$]3/ }
  its(:content) { should match /^TIMESTAMP=`date \S%y-%m-%d\w%H-%M-%S`/ }  
  its(:content) { should match /^sh \/root\/backup_scripts\/openstack_backup.sh/ }
  its(:content) { should match /^sh \/root\/backup_scripts\/dashboard_backup.sh/ }
  its(:content) { should match /^tar -czvf musashi_backup-[$]TIMESTAMP.tar.gz \/musashi_backup/ }
  its(:content) { should match /^backup_remote_server \(\) {/ }
  its(:content) { should match /^rsync -rvaz --progress -e "ssh -p [$]ssh_port" musashi_backup-[$]TIMESTAMP.tar.gz [$]user@(.*?).(.*?).(.*?):(~)\// }
  its(:content) { should match /^rsync -rvaz --progress -e "ssh -p [$]ssh_port" \/root\/backup_scripts\/restore.sh [$]user@(.*?).(.*?).(.*?):(~)\// }
  its(:content) { should match /^}/ }
  its(:content) { should match /^if \[ "[$]#" -eq 3 \]; then/ }
  its(:content) { should match /^backup_remote_server/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^rsync -rvaz --progress musashi_backup-[$]TIMESTAMP.tar.gz \/musashi_backup\// }
  its(:content) { should match /^rsync -rvaz --progress \/root\/backup_scripts\/restore.sh \/musashi_backup\// }
end

describe file('/home/ravendra/backup_server.sh') do
  its(:content) { should match /^ip=[$]1/ }
  its(:content) { should match /^ssh_port=[$]2/ }
  its(:content) { should match /^user=[$]3/ }
  its(:content) { should match /^TIMESTAMP=`date \S%y-%m-%d\w%H-%M-%S`/ }
  its(:content) { should match /^is_IP \(\) {/ }
  its(:content) { should match /^local stat=1/ }
  its(:content) { should match /^if \[\[ [$]ip =~ ^\[0-9\]{1,3}\\.\[0-9\]{1,3}\\.\[0-9\]{1,3}\\.\[0-9\]{1,3}[$] \]\]; then/ }
  its(:content) { should match /^OIFS=[$]IFS/ }
  its(:content) { should match /^IFS='.'/ }
  its(:content) { should match /^ip=\([$]ip\)/ }
  its(:content) { should match /^IFS=[$]OIFS/ }
  its(:content) { should match /^\[\[ [$]{ip\[0\]} -le 255 && [$]{ip\[1\]} -le 255 \\/ }
  its(:content) { should match /^&& [$]{ip\[2\]} -le 255 && [$]{ip\[3\]} -le 255 \]\]/ }
  its(:content) { should match /^stat=[$]?/ }
  its(:content) { should match /^echo [$]stat/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^return [$]stat/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^ip_ping \(\) {/ }
  its(:content) { should match /^if \[\[ [$]\(ping -q -c3 [$]ip\) == @\(*100% packet loss*\) \]\]; then/ }
  its(:content) { should match /^echo "Unable to Ping the given Host"/ }
  its(:content) { should match /^echo "Note : Please check the value of 'kernel parameter net.ipv4.icmp_echo_ignore_all' at the location '\/proc\/sys\/net\/ipv4\/'.\\/ }
  its(:content) { should match /^If it's value is 1 then it will prevent other host from successfully pinging your server.In such case change it's \\/ }
  its(:content) { should match /^value to 0 for successful pinging. Please try again."/ }
  its(:content) { should match /^else/ }
  its(:content) { should match /^ssh_validation/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^is_ssh_port \(\) {/ }
  its(:content) { should match /^nc -z -w 3 [$]ip [$]ssh_port/ }
  its(:content) { should match /^if \[ "[$]?" -ne 0 \]; then/ }
  its(:content) { should match /^echo "Connection to" [$]ip "on port" [$]ssh_port "failed. Please try again."/ }
  its(:content) { should match /^else/ }
  its(:content) { should match /^echo "Connection to" [$]ip "on port" [$]ssh_port "succeeded"/ }
  its(:content) { should match /^backup_server/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^backup_server \(\) {/ }
  its(:content) { should match /^tar -czvf musashi_backup-[$]TIMESTAMP.tar.gz \/musashi_backup/ }
  its(:content) { should match /^rsync -rvaz --progress -e "ssh -p [$]ssh.port" musashi_backup-[$]TIMESTAMP.tar.gz [$]user@(.*?).(.*?).(.*?):(~)\// }
  its(:content) { should match /^rsync -rvaz --progress -e "ssh -p [$]ssh.port" \/root\/backup_scripts\/restore.sh [$]user@(.*?).(.*?).(.*?):(~)\// }
  its(:content) { should match /^create_cron/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^ip_validation \(\) {/ }
  its(:content) { should match /^return_val=[$]\(is_IP\)/ }
  its(:content) { should match /^if \[ "[$]return_val" == 0 \]; then/ }
  its(:content) { should match /^ip_ping/ }
  its(:content) { should match /^elif \[ "[$]return_val" == 1 \]; then/ }
  its(:content) { should match /^echo "IP address should be in range of 0 to 255"/ }
  its(:content) { should match /^else/ }
  its(:content) { should match /^echo "Please put a valid IP addres"/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^is_ssh \(\) {/ }
  its(:content) { should match /^local stat=1/ }
  its(:content) { should match /^if \[\[ [$]ssh_port =~ ^\(\[0-9\]{1,4}\|\[1-5\]\[0-9\]{4}\|6\[0-4\]\[0-9\]{3}\|65\[0-4\]\[0-9\]{2}\|655\[0-2\]\[0-9\]\|6553\[0-5\]\)[$] \]\]; then/ }
  its(:content) { should match /^stat=[$]?/ }
  its(:content) { should match /^echo [$]stat/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^return [$]stat/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^ssh_validation \(\) {/ }
  its(:content) { should match /^return_val=[$]\(is_ssh\)/ }
  its(:content) { should match /^if \[ "[$]return_val" == 0 \]; then/ } 
  its(:content) { should match /^is_ssh_port/ }
  its(:content) { should match /^else/ } 
  its(:content) { should match /^echo "Please put a valid SSH port number. It should be in range of 0 to 65535."/ }
  its(:content) { should match /^fi/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^ create_cron \(\) {/ }
  its(:content) { should match /^\( crontab -l \| grep -v \/root\/backup_scripts\/backup.sh \) \| crontab -/ }
  its(:content) { should match /^crontab -l \| { cat; echo "0 1 * * * \/root\/backup_scripts\/backup.sh [$]ip [$]ssh_port [$]user"; } \| crontab -/ }
  its(:content) { should match /^}/ }
  its(:content) { should match /^if \[ "[$]#" -eq 3 \]; then/ }
  its(:content) { should match /^ip_validation/ }
  its(:content) { should match /^else/ }
  its(:content) { should match /^echo "Please provide valid number of arguments."/ }
  its(:content) { should match /^fi/ }
end

but when i run with rspec spec/rootfiles_spec.rb it gives us

/home/ravendra/Documents/rails-project/pif_form/spec/rootfiles_spec.rb:2:in `<top (required)>': undefined method `file' for main:Object (NoMethodError)
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
from /home/ravendra/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
from /home/ravendra/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
from /home/ravendra/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
from /home/ravendra/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from /home/ravendra/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'

Some one help me

1

There are 1 answers

0
Jörg W Mittag On

The error message is telling you that there is no file method in RSpec. And, if you look at the documentation of RSpec, you will see, that there is indeed no file method in RSpec.

So, you have to either write one or install some library which has one.