<commit_before>require 'spec_helper'
require 'time'

describe Server do
  before(:each) do
  
  end

  it "should create a new instance given valid attributes" do
    s = Factory.build(:server)
    s.valid?.should be true
  end
  
  it "should not be valid when no hostname is given" do
    s = Factory.build(:server)
    s.hostname = nil
    s.valid?.should be false
  end
  
  it "should not be valid when no interval is given" do
    s = Factory.build(:server)
    s.interval_hours = nil
    s.valid?.should be false
  end
  
  it "should not be valid when no keep_snapshots is given" do
    s = Factory.build(:server)
    s.keep_snapshots = nil
    s.valid?.should be false
  end
  
  it "should not accept impossible hours" do
    s = Factory.build(:server)
    s.window_start = 25
    s.valid?.should be false
    s.window_start = 1
    s.window_stop = 25
    s.valid?.should be false
    s.window_stop = 2
    s.valid?.should be true
  end
  
  it "should be valid when the other attributes are not given" do
    s = Factory.build(:server)
    s.connect_to = nil
    s.ssh_port = nil
    s.enabled = nil
    s.backup_server_id = nil
    s.last_backup = nil
    s.last_started = nil
    s.valid?.should be true
  end
  
  it "should provide a instance method to determine valid backup servers" do
    s1 = Factory.build(:server, :connect_to => '127.0.0.1')
    s2 = Factory.build(:server)
    s2.connect_to = nil
    BackupServer.should_receive(:available_for).with(s1.connect_to)
    BackupServer.should_receive(:available_for).with(s2.hostname)
    s1.possible_backup_servers
    s2.possible_backup_servers
  end
  
  it "should provide a to_s method" do
    s = Factory.build(:server)
    s.to_s.should == s.hostname
  end
  
  it "should not backup when backups are disabled" do
    server = Factory.build(:server, :enabled => false, 
                                    :last_started => (Time.new - ( 4 * 3600)),
                                    :last_backup => (Time.new - (3* 3600)),
                                    :interval_hours => 1)
    server.should_backup?.should be false
  end
  
  it "should not backup when no backup server is selected" do
    server = Factory.build(:server, :enabled => true, :backup_server => nil, 
                                    :last_started => (Time.new - ( 4 * 3600)),
                                    :last_backup => (Time.new - (3* 3600)),
                                    :interval_hours => 1)
    server.should_backup?.should be false
  end
  
  it "should know if a backup is running" do
    s1 = Factory.build(:server)
    s1.last_backup = Time.new
    s1.last_started = Time.new - 3600
    s1.backup_running?.should be false
    s2 = Factory.build(:server)
    s2.last_backup = Time.new - 3600
    s2.last_started = Time.new
    s2.backup_running?.should be true
  end
  
  it "should not backup when a backup is already running" do
    s = Factory.build(:server)
    s.last_backup = Time.new - 3600
    s.last_started = Time.new
    s.backup_running?.should be true
    s.should_backup?.should be false
  end
  
  it "should always be in the backup window when no start or end is given" do
    server = Factory.build(:server)
    server.in_backup_window?.should be true
    server.window_start = 0
    server.in_backup_window?.should be true
    server.window_start = nil
    server.window_stop = 0
    server.in_backup_window?.should be true
  end
  
  it "should know when it's not in the window" do
    server_one_hour_window = Factory.build(:server)
    server_one_hour_window.window_start = 1
    server_one_hour_window.window_stop = 2
    Time.should_receive(:new).and_return(Time.parse("03:00"))
    server_one_hour_window.in_backup_window?.should be false
    
    server_23_hour_window = Factory.build(:server)
    server_23_hour_window.window_start = 0
    server_23_hour_window.window_stop = 23
    Time.should_receive(:new).and_return(Time.parse("23:30"))
    server_23_hour_window.in_backup_window?.should be false
  end

  it "should know when it's in the window" do
    server = Factory.build(:server)
    server.window_start = 1
    server.window_stop = 2
    Time.should_receive(:new).and_return(Time.parse("01:30"))
    server.in_backup_window?.should be true
    
    server.window_start = 0
    server.window_stop = 1
    Time.should_receive(:new).and_return(Time.parse("00:30"))
    server.in_backup_window?.should be true
    
    server.window_start = 23
    server.window_stop = 0
    Time.should_receive(:new).and_return(Time.parse("23:30"))
    server.in_backup_window?.should be true
  end
  
  it "should know when its past the interval" do
    s = Factory.build(:server, :last_started => (Time.new - (2 * 3600)), :interval_hours => 1)
    s.interval_passed?.should be true
    
    s.interval_hours = 3
    s.interval_passed?.should be false
  end
  
  it "should know when to backup" do
    s = Factory.build(:server)
    s.last_backup = Time.new - (2 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 1
    s.should_backup?.should be true
    
    # already running
    s.last_backup = Time.new - (24 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be false
    
    # already did one in the window
    s.last_backup = Time.new - (3 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be false
    
    # The most common case
    s.window_start = nil
    s.window_stop = nil
    s.last_backup = Time.new - (23 * 3600)
    s.last_started = Time.new - ( 24 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be true
  end
  
  it "should know the excludes inherited trough the profiles" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.excludes << Factory.build(:exclude, :path => '/') # exclude one
    p2.excludes << Factory.build(:exclude, :path => '/var/log') # second exclude
    s.profiles << p1
    s.profiles << p2
    s.excludes.size.should == 2
  end
  
  it "should know the includes inherited trough the profiles" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.includes << Factory.build(:include, :path => '/') # include one
    p2.includes << Factory.build(:include, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2
    s.includes.size.should == 2
  end
  
  it "should compile the list of excludes to valid rsync args" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.excludes << Factory.build(:exclude, :path => '/') # include one
    p2.excludes << Factory.build(:exclude, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2
    
    s.rsync_excludes.should == '--exclude=/ --exclude=/var/log'
  end
  
  it "should compile the list of includes to valid rsync args" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.includes << Factory.build(:include, :path => '/') # include one
    p2.includes << Factory.build(:include, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2

    s.rsync_includes.should == '--include=/ --include=/var/log'
  end
  
end
<commit_msg>Test to ensure ssh_port is required.<commit_after>require 'spec_helper'
require 'time'

describe Server do
  before(:each) do
  
  end

  it "should create a new instance given valid attributes" do
    s = Factory.build(:server)
    s.valid?.should be true
  end
  
  it "should not be valid when no hostname is given" do
    s = Factory.build(:server)
    s.hostname = nil
    s.valid?.should be false
  end
  
  it "should not be valid when no interval is given" do
    s = Factory.build(:server)
    s.interval_hours = nil
    s.valid?.should be false
  end
  
  it "should not be valid when no ssh_port is given" do
    s = Factory.build(:server)
    s.ssh_port = nil
    s.valid?.should be false
  end
  
  it "should not be valid when no keep_snapshots is given" do
    s = Factory.build(:server)
    s.keep_snapshots = nil
    s.valid?.should be false
  end
  
  it "should not accept impossible hours" do
    s = Factory.build(:server)
    s.window_start = 25
    s.valid?.should be false
    s.window_start = 1
    s.window_stop = 25
    s.valid?.should be false
    s.window_stop = 2
    s.valid?.should be true
  end
  
  it "should be valid when the other attributes are not given" do
    s = Factory.build(:server)
    s.connect_to = nil
    s.enabled = nil
    s.backup_server_id = nil
    s.last_backup = nil
    s.last_started = nil
    s.valid?.should be true
  end
  
  it "should provide a instance method to determine valid backup servers" do
    s1 = Factory.build(:server, :connect_to => '127.0.0.1')
    s2 = Factory.build(:server)
    s2.connect_to = nil
    BackupServer.should_receive(:available_for).with(s1.connect_to)
    BackupServer.should_receive(:available_for).with(s2.hostname)
    s1.possible_backup_servers
    s2.possible_backup_servers
  end
  
  it "should provide a to_s method" do
    s = Factory.build(:server)
    s.to_s.should == s.hostname
  end
  
  it "should not backup when backups are disabled" do
    server = Factory.build(:server, :enabled => false, 
                                    :last_started => (Time.new - ( 4 * 3600)),
                                    :last_backup => (Time.new - (3* 3600)),
                                    :interval_hours => 1)
    server.should_backup?.should be false
  end
  
  it "should not backup when no backup server is selected" do
    server = Factory.build(:server, :enabled => true, :backup_server => nil, 
                                    :last_started => (Time.new - ( 4 * 3600)),
                                    :last_backup => (Time.new - (3* 3600)),
                                    :interval_hours => 1)
    server.should_backup?.should be false
  end
  
  it "should know if a backup is running" do
    s1 = Factory.build(:server)
    s1.last_backup = Time.new
    s1.last_started = Time.new - 3600
    s1.backup_running?.should be false
    s2 = Factory.build(:server)
    s2.last_backup = Time.new - 3600
    s2.last_started = Time.new
    s2.backup_running?.should be true
  end
  
  it "should not backup when a backup is already running" do
    s = Factory.build(:server)
    s.last_backup = Time.new - 3600
    s.last_started = Time.new
    s.backup_running?.should be true
    s.should_backup?.should be false
  end
  
  it "should always be in the backup window when no start or end is given" do
    server = Factory.build(:server)
    server.in_backup_window?.should be true
    server.window_start = 0
    server.in_backup_window?.should be true
    server.window_start = nil
    server.window_stop = 0
    server.in_backup_window?.should be true
  end
  
  it "should know when it's not in the window" do
    server_one_hour_window = Factory.build(:server)
    server_one_hour_window.window_start = 1
    server_one_hour_window.window_stop = 2
    Time.should_receive(:new).and_return(Time.parse("03:00"))
    server_one_hour_window.in_backup_window?.should be false
    
    server_23_hour_window = Factory.build(:server)
    server_23_hour_window.window_start = 0
    server_23_hour_window.window_stop = 23
    Time.should_receive(:new).and_return(Time.parse("23:30"))
    server_23_hour_window.in_backup_window?.should be false
  end

  it "should know when it's in the window" do
    server = Factory.build(:server)
    server.window_start = 1
    server.window_stop = 2
    Time.should_receive(:new).and_return(Time.parse("01:30"))
    server.in_backup_window?.should be true
    
    server.window_start = 0
    server.window_stop = 1
    Time.should_receive(:new).and_return(Time.parse("00:30"))
    server.in_backup_window?.should be true
    
    server.window_start = 23
    server.window_stop = 0
    Time.should_receive(:new).and_return(Time.parse("23:30"))
    server.in_backup_window?.should be true
  end
  
  it "should know when its past the interval" do
    s = Factory.build(:server, :last_started => (Time.new - (2 * 3600)), :interval_hours => 1)
    s.interval_passed?.should be true
    
    s.interval_hours = 3
    s.interval_passed?.should be false
  end
  
  it "should know when to backup" do
    s = Factory.build(:server)
    s.last_backup = Time.new - (2 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 1
    s.should_backup?.should be true
    
    # already running
    s.last_backup = Time.new - (24 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be false
    
    # already did one in the window
    s.last_backup = Time.new - (3 * 3600)
    s.last_started = Time.new - ( 4 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be false
    
    # The most common case
    s.window_start = nil
    s.window_stop = nil
    s.last_backup = Time.new - (23 * 3600)
    s.last_started = Time.new - ( 24 * 3600)
    s.interval_hours = 24
    s.should_backup?.should be true
  end
  
  it "should know the excludes inherited trough the profiles" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.excludes << Factory.build(:exclude, :path => '/') # exclude one
    p2.excludes << Factory.build(:exclude, :path => '/var/log') # second exclude
    s.profiles << p1
    s.profiles << p2
    s.excludes.size.should == 2
  end
  
  it "should know the includes inherited trough the profiles" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.includes << Factory.build(:include, :path => '/') # include one
    p2.includes << Factory.build(:include, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2
    s.includes.size.should == 2
  end
  
  it "should compile the list of excludes to valid rsync args" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.excludes << Factory.build(:exclude, :path => '/') # include one
    p2.excludes << Factory.build(:exclude, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2
    
    s.rsync_excludes.should == '--exclude=/ --exclude=/var/log'
  end
  
  it "should compile the list of includes to valid rsync args" do
    s = Factory.build(:server) # we need one server
    p1 = Factory.build(:profile, :name => 'linux') # profile one
    p2 = Factory.build(:profile, :name => 'standard') # and another one
    p1.includes << Factory.build(:include, :path => '/') # include one
    p2.includes << Factory.build(:include, :path => '/var/log') # second include
    s.profiles << p1
    s.profiles << p2

    s.rsync_includes.should == '--include=/ --include=/var/log'
  end
  
end

{'fraction_non_alphanumeric': 0.1123444116514821, 'fraction_numerical': 0.023867573463364558, 'mean_word_length': 2.646081871345029, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5186639', 'n_tokens_mistral': 5323, 'n_tokens_neox': 4990, 'n_words': 1402}