• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Infrastructure Behavior Testing Framework

Infrataster

![Gitter](https://badges.gitter.im/Join Chat.svg)

Gem Version Code Climate

Infrastructure Behavior Testing Framework.

Basic Usage with Vagrant

First, create Gemfile:

source 'https://rubygems.org'

gem 'infrataster'

Install gems:

$ bundle install

Install Vagrant: Official Docs

Create Vagrantfile:

# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"

  config.vm.define :proxy do |c|
    c.vm.network "private_network", ip: "192.168.33.10"
    c.vm.network "private_network", ip: "172.16.33.10", virtualbox__intnet: "infrataster-example"
  end

  config.vm.define :app do |c|
    c.vm.network "private_network", ip: "172.16.33.11", virtualbox__intnet: "infrataster-example"
  end
end

Start VMs:

$ vagrant up

Initialize rspec directory:

$ rspec --init
  create   spec/spec_helper.rb
  create   .rspec

require 'infrataster/rspec' and define target servers for testing in spec/spec_helper.rb:

# spec/spec_helper.rb
require 'infrataster/rspec'

Infrataster::Server.define(
  :proxy,           # name
  '192.168.0.0/16', # ip address
  vagrant: true     # for vagrant VM
)
Infrataster::Server.define(
  :app,             # name
  '172.16.0.0/16',  # ip address
  vagrant: true,    # for vagrant VM
  from: :proxy      # access to this machine via SSH port forwarding from proxy
)

# Code generated by `rspec --init` is following...

Or

# spec/spec_helper.rb
require 'infrataster/rspec'

Infrataster::Server.define(:proxy) do |server|
    server.address = '192.168.0.0/16'
    server.vagrant = true
end
Infrataster::Server.define(:app) do |server|
    server.address = '172.16.0.0/16'
    server.vagrant = true
    server.from = :proxy
end

# Code generated by `rspec --init` is following...

Then, you can write spec files:

# spec/example_spec.rb
require 'spec_helper'

describe server(:app) do
  describe http('http://app') do
    it "responds content including 'Hello Sinatra'" do
      expect(response.body).to include('Hello Sinatra')
    end
    it "responds as 'text/html'" do
      expect(response.headers['content-type']).to eq("text/html")
    end
  end
end

Run tests:

$ bundle exec rspec
2 examples, 2 failures

Currently, the tests failed because the VM doesn't respond to HTTP request.

It's time to write provisioning instruction like Chef's cookbooks or Puppet's manifests!

Server

"Server" is a server you tests. This supports Vagrant, which is very useful to run servers for testing. Of course, you can test real servers.

You should define servers in spec_helper.rb like the following:

Infrataster::Server.define(
  # Name of the server, this will be used in the spec files.
  :proxy,
  # IP address of the server
  '192.168.0.0/16',
  # If the server is provided by vagrant and this option is true,
  # SSH configuration to connect to this server is got from `vagrant ssh-config` command automatically.
  vagrant: true,
)

Infrataster::Server.define(
  # Name of the server, this will be used in the spec files.
  :app,
  # IP address of the server
  '172.16.0.0/16',
  # If the server is provided by vagrant and this option is true,
  # SSH configuration to connect to this server is got from `vagrant ssh-config` command automatically.
  vagrant: true,
  # Which gateway is used to connect to this server by SSH port forwarding?
  from: :proxy,
  # options for resources
  mysql: {user: 'app', password: 'app'},
)

You can specify SSH configuration manually too:

Infrataster::Server.define(
  # ...
  ssh: {host_name: 'hostname', user: 'testuser', keys: ['/path/to/id_rsa']}
)

fuzzy IP address

Infrataster has "fuzzy IP address" feature. You can pass IP address which has netmask (= CIDR) to Infrataster::Server#define. This needs vagrant option or ssh option which has host_name because this fetches all IP address via SSH and find the matching one.

Infrataster::Server.define(
  :app,
  # find IP address matching 172.16.0.0 ~ 172.16.255.255
  '172.16.0.0/16',
)

Of course, you can set fully-specified IP address too.

Infrataster::Server.define(
  :app,
  '172.16.11.22',
  # or
  '172.16.11.22/32',
)

#ssh_exec

You can execute a command on the server like the following:

describe server(:proxy) do
  let(:time) { Time.now }
  before do
    current_server.ssh_exec "echo 'Hello' > /tmp/test-#{time.to_i}"
  end
  it "executes a command on the current server" do
    result = current_server.ssh_exec("cat /tmp/test-#{time.to_i}")
    expect(result.chomp).to eq('Hello')
  end
end

This is useful to test cases which depends on the status of the server.

Resources

"Resource" is what you test by Infrataster. For instance, the following code describes http resource.

describe server(:app) do
  describe http('http://example.com') do
    it "responds content including 'Hello Sinatra'" do
      expect(response.body).to include('Hello Sinatra')
    end
  end
end

http resource

http resource tests HTTP response when sending HTTP request. It accepts method, params and header as options.

describe server(:app) do
  describe http(
    'http://app.example.com',
    method: :post,
    params: {'foo' => 'bar'},
    headers: {'USER' => 'VALUE'}
  ) do
    it "responds with content including 'app'" do
      expect(response.body).to include('app')

      # `response` is a instance of `Faraday::Response`
      # See: https://github.com/lostisland/faraday/blob/master/lib/faraday/response.rb
    end
  end

  # Gzip support
  describe http('http://app.example.com/gzipped') do
    it "responds with content deflated by gzip" do
      expect(response.headers['content-encoding']).to eq('gzip')
    end
  end

  describe http('http://app.example.com/gzipped', inflate_gzip: true) do
    it "responds with content inflated automatically" do
      expect(response.headers['content-encoding']).to be_nil
      expect(response.body).to eq('plain text')
    end
  end

  # Redirects
  describe http('http://app.example.com/redirect', follow_redirects: true) do
    it "follows redirects" do
      expect(response.status).to eq(200)
    end
  end

  # Custom Faraday middleware
  describe http('http://app.example.com', faraday_middlewares: [
    YourMiddleware,
    [YourMiddleware, options]
  ]) do
    it "uses the middlewares" do
      expect(response.status).to eq(200)
    end
  end
end

capybara resource

capybara resource tests your web application by simulating real user's interaction.

describe server(:app) do
  describe capybara('http://app.example.com') do
    it 'shows food list' do
      visit '/'
      click_link 'Foods'
      expect(page).to have_content 'Yummy Soup'
    end
  end
end

mysql_query resource

mysql_query resource is now in infrataster-plugin-mysql.

pgsql_query resource

pgsql_query resource sends a query to PostgreSQL server.

pgsql_query is provided by infrataster-plugin-pgsql by @SnehaM.

dns resource

dns resource sends a query to DNS server.

dns is provided by infrataster-plugin-dns by @otahi.

memcached resource

memcached resource sends a query to memcached server.

memcached is provided by infrataster-plugin-memecached by @rahulkhengare.

redis resource

redis resource sends a query to redis server.

redis is provided by infrataster-plugin-redis by @rahulkhengare.

firewall resource

firewall resource tests your firewalls.

firewall is provided by infrataster-plugin-firewall by @otahi.

Example

Tests

Unit Tests

Unit tests are under spec/unit directory.

$ bundle exec rake spec:unit

Integration Tests

Integration tests are under spec/integration directory.

$ bundle exec rake spec:integration:prepare
$ bundle exec rake spec:integration

Presentations and Articles

Introducing Infrataster

Changelog

Changelog

Contributing

  1. Fork it ( http://github.com/ryotarai/infrataster/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

More Repositories

1

waker

Wake someone up!
Ruby
134
star
2

spotscaler

Auto scaling for Amazon EC2 using spot instances
Go
69
star
3

mallet

TCP tunnel that works like VPN
Go
68
star
4

github_trends_rss

Alternative: https://mshibanami.github.io/GitHubTrendingRSS/
JavaScript
50
star
5

prometheus-query

CLI client to query Prometheus
Go
40
star
6

prometheus-tsdb-dump

prometheus-tsdb-dump reads a Prometheus TSDB block and writes metrics
Go
25
star
7

simproxy

Simple HTTP balancer (reverse proxy)
Go
23
star
8

fluent-plugin-dogstatsd

Fluend plugin for Dogstatsd, that is statsd server for Datadog.
Ruby
20
star
9

zabbix_graph

Ruby
9
star
10

itamae-server

Server for Itamae (THIS IS NOT PRODUCTION-READY)
Ruby
7
star
11

go-gist

Gist Command written in Go
Go
6
star
12

isucon7q

Perl
6
star
13

git-commit-autouser

git-commit with setting committer and author automatically
Ruby
6
star
14

infrataster-plugin-mysql

MySQL plugin for Infrataster
Ruby
5
star
15

promindexmutator

Mutator for Prometheus TSDB index
Go
5
star
16

peco_selector

Ruby
5
star
17

chrome-extension-ankinow

TypeScript
5
star
18

itamae-plugin-resource-cron

Ruby
5
star
19

chef-handler-fluentd

Handler for Chef to send logs to Fluentd
Ruby
4
star
20

dokku-nsinit

Dokku plugin to execute commmands in the Docker containers.
Shell
4
star
21

smock

Go
4
star
22

schash

Ruby hash validator
Ruby
4
star
23

misc

Go
4
star
24

ec2-snapshot-replicator

Replicate and backup EC2 snapshots to another region!
Ruby
4
star
25

vagrant-syllabus-provisioner

This plugin installs a provisioner that allows Syllabus to provision machines.
Ruby
4
star
26

ruboty-rss

RSS Watcher for Ruboty
Ruby
4
star
27

ruboty-redmine

Ruby
3
star
28

irkit-web

Ruby
3
star
29

vagrant-docker-registry

docker-registry with vagrant
3
star
30

itamae-client

Ruby
2
star
31

prometheus-remote-s3

Go
2
star
32

paramedic

A tool to diagnose and remediate instances, using Amazon EC2 Systems Manager
Go
2
star
33

confit

Download config files from S3.
Go
2
star
34

FacebookManager-deprecated

a wrapper of facebook ios sdk
Objective-C
2
star
35

consul-lock-ruby

`consul lock` in Ruby - THIS IS NOT READY FOR USE
Ruby
2
star
36

chatwork_bridge

ChatWork -> {Mail,STDOUT,...}
Ruby
2
star
37

gdr

CLI tool for Google Drive
Go
2
star
38

vscode-eda

TypeScript
2
star
39

kitchen-vagrant_sandbox

Vagrant driver for Kitchen (with sandbox)
Ruby
1
star
40

ImageCacheWithoutARC

Cache of UIImage. If there isn't the image in the cache, ImageCache class automatically download it.
Objective-C
1
star
41

github-api-auth-proxy

Go
1
star
42

opencv_on_jruby

OpenCV java binding sample on JRuby
Ruby
1
star
43

rsay

Remote say command
Go
1
star
44

twinaxos

JavaScript
1
star
45

hello-sinatra

Ruby
1
star
46

vpasscsv2qif

Convert CSV file which is generated by vpass(VISA) to QIF file.
Python
1
star
47

paramedic-agent

An agent for https://github.com/ryotarai/paramedic
Go
1
star
48

kubeoidc

OpenID Connect CLI client for kubectl
Go
1
star
49

dockertie

Tiny Docker coordinator
Go
1
star
50

ec2-describe-instances-go

Go
1
star
51

prometheus-query-proxy

Go
1
star
52

prometheus-remote-memory

Go
1
star
53

chromium_remote_debugging

Ruby
1
star
54

BagOfFeatureSample

C++
1
star
55

prom-downsampler

Downsampler for Prometheus TSDB block
Go
1
star
56

pasteit

Ruby
1
star
57

kube-daemonset-proxy

HTTP proxy to Pods under Daemonset
JavaScript
1
star
58

procon-challenge

A repository for practice of "Programming Contest Challenge Book" (プログラミングコンテストチャレンジブック)
Ruby
1
star