LoginSignup
6
6

More than 5 years have passed since last update.

Serverspec のように FTP サーバー上のファイルの状態をテストできる gem を作りました

Last updated at Posted at 2014-08-17

FTP による接続しかできないサーバーでも Serverspec みたいにファイルの状態をテストしたかったので FTPSpec という gem を作りました。僕のソフトウェア開発戦闘力が高ければ Serverspec に Pull Request で提案をすることもできたかもしれませんが、できそうになかったので、初めてながら gem を作ることにしました。

GitHub

インストール

$ gem install ftpspec

使い方

ftpspec-init というコマンドがインストールされるので、そのコマンドを実行します。

$ ftpspec-init

テスト用のテンプレートが生成されます。

$ tree
.
├── Rakefile
└── spec
    ├── ftp_spec.rb
    └── spec_helper.rb

1 directory, 3 files

spec/spec_helper.rb に FTP サーバーのホスト名、ユーザー名、パスワードを書きます。

require "ftpspec"
require "rubygems"
require "rspec"
require "net/ftp"

RSpec.configure do |c| 
  c.add_setting :ftp, :default => nil 
  c.before do
    hostname = "YOUR HOSTNAME"
    user = "YOUR USER"
    password = "YOUR PASSWORD"
    c.ftp = Net::FTP.new
    c.ftp.passive = true
    c.ftp.connect(hostname)
    c.ftp.login(user, password)
    Ftpspec.set_ftp
  end 
  c.after do
    c.ftp.close
  end 
end

spec/ftp_spec.rb にテストを書きます。

require "spec_helper"

describe "/httpdocs/index.html" do
  it { should be_mode "644" }
end

rake spec を実行します。

$ rake spec

結果です。

.

Finished in 2 seconds (files took 0.14477 seconds to load)
1 examples, 0 failures

マッチャー

be_mode

テスト対象のパーミッションが想定したものになっているかテストします。

describe "/httpdocs/index.html" do
  it { should be_mode "644" }
end

be_file

テスト対象がファイルであるかテストします。

describe "/httpdocs/index.html" do
  it { should be_file }
end

be_directory

テスト対象がディレクトリであるかテストします。

describe "/httpdocs/images" do
  it { should be_directory }
end

be_owned_by

テスト対象の所有者が想定したユーザーになっているかテストします。

describe "/httpdocs/index.html" do
  it { should be_owned_by "someone" }
end

be_grouped_into

テスト対象のグループが想定したグループになっているかテストします。

describe "/httpdocs/index.html" do
  it { should be_grouped_into "admin" }
end
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6