詳細検索

Chef(서버스펙)로 작성된 테스트 레시피

아바타
글쓴이 komi

Chef(서버스펙)로 작성된 테스트 레시피
日本語에서 번역 • 원문 보기

안녕하세요. 코미야입니다.

이번이 마지막이야.
지난번에 소개한 레시피를 테스트하는 방법은 다음과 같습니다. 서버스펙.

아래 링크에 왜 서버스펙이 좋은지 썼지만, 다음 점들이 좋다고 생각합니다.
・Chef 테스트 도구가 아니라 외부 도구이므로 의존성이 없습니다(퍼펫에서도 사용할 수 있습니다).
‧디자인 컨셉이 간단하고 사용하기 쉬워서 별다른 노력 없이 사용할 수 있었다.

참고문헌:
서버스펙 at hbstudy #45 입문 셰프 솔로야크/신입 교육 "입문 인형" 자료_type 매뉴얼 고급/_tips ncstudy#05 실습 자료 병렬_tests

·세팅
0.3과 0.6 버전은 테스트 작성 방식이 약간 다르다고 느껴서, 매뉴얼을 따르는 새 버전을 추천합니다.
[조개껍데기]# 루비젬 설치해 줘

보석 설치 서버스펙 레이크

서버스펙-이닛

백엔드 유형을 선택하세요: 

  1) SSH
  2) 임원 (지역)

숫자 선택: 1

방랑자 인스턴스 y/n: y
입력 vagrant 인스턴스 이름: 10.0.0.241
 + spec/10.0.0.241/ 
 + spec/10.0.0.241/httpd_spec.rb[/shell]  
  
  
  
  
  
  
<br>명령어가 실행이 끝나면 위에서 설명한 대로 몇 가지 파일이 생성됩니다.<br>```

\[shell\]# serverspec-init ~생략~ 입력 대상 호스트 이름: 10.0.0.240 + spec/10.0.0.240/ + spec/10.0.0.240/httpd\_spec.rb\[/shell\]  
SSH를 지정하고 대상 호스트를 입력한 후, 각 호스트마다 디렉터리가 생성되었습니다.   
  
시험지를 쓸 거야.   
  
\[shell\]# vi spec/10.0.0.240/httpd\_spec.rb require 'spec\_helper' 

'httpd' do 설명 그것이 { be_installed } 그것이 { be_enabled } 그건 { be_running } 끝

'포트 80'을 설명해 주세요 그것이 { be_listening } 끝

'/etc/httpd/conf/httpd.conf'를 설명해 주세요. 그것이 { be_file } it { "ServerName localhost:80" } 끝[/shell]
DetectOS에서 RedHat으로 변경했습니다 (CentOS이기 때문입니다)
```

[shell]# vi spec/spec_helper.rb 포함 Serverspec::Helper::RedHat[/shell]
ssh 설정을 수정해서 연결 정보를 전달할 수 있게 하세요
[shell]# vi .ssh/config Host 10.0.0.240 호스트네임 10.0.0.240 사용자 루트 포트 22 UserKnownHostsFile /dev/null strictHostKeyChecking no passwordAuthentication no identityFile "/root/.ssh/komi-test.pem" IdentitiesOnly yes LogLevel FATAL Host 10.0.0.241 HostName 10.0.0.241 User root Port 22 UserKnownHostsFile /dev/null strictHostKeyChecking no passwordAuthentication no identityFile "/root/.ssh/komi-test.pem" IdentitiesOnly yes[/shell]
DB 측의 테스트 파일 이름을 수정하고 내용을 엔터티에 맞게 수정하세요
[shell]# MV spec/10.0.0.241/httpd_spec.rb spec/10.0.0.241/mysqld_spec.rb

vi spec/10.0.0.241/mysqld_spec.rb

'spec_helper' 요구사항 

'mysql-server' do 설명
  그것이 { be_installed }
끝

'mysqld'를 설명하세요
  그것이 { be_enabled }
  그건 { be_running }
끝

'포트 3306'을 설명해 주세요
  그것이 { be_listening }
끝

'/etc/my.cnf'를 설명해 주세요.
  그것이 { be_file }
  it {는 &quot;server-id = 103&quot;을 포함해야 합니다 }
끝[/shell]<br>당분간은 시험 결과가 그대로인지 확인하겠습니다<br>```

\[shell\]# rake spec (in /root) /root/.rbenv/versions/1.9.2-p290/bin/ruby -S rspec spec/10.0.0.240/httpd\_spec.rb spec/10.0.0.241/mysqld\_spec.rb ............ 

완주 기록은 0.74866초였습니다. 12개의 예시, 0개의 실패[/shell]
제가 쓴 글이 잘 된 것 같아요.
오류가 발생할 경우, 실패한 명령어와 반환 명령어가 출력되어 이해하기 매우 쉽습니다.
```

[shell]실패:

  1) mysql-server
     실패/오류: { be_enabled }
       chkconfig --list mysql-server | grep 3:on
       서비스에서 정보 읽기 오류: 해당 파일이나 디렉터리가 없음

  2) mysql-server
     실패/오류: { be_running }
       PS aux | grep -w -- mysql-server | grep -qv grep[/shell]<br>→ chkconfig와 ps 명령어에서는 mysql-server라고 하므로, should be_installed, enabled, running 블록을 분리해서 해결할 예정입니다.<br>
<br>
<br>다음으로, 필요한 모든 검사를 추가하고 시험해 보겠습니다<br>레시피와 테스트를 세트로 재사용할 때는 테스트 파일을 레시피와 같은 방식으로 나누는 것이 더 낫다고 생각됩니다.<br>```

    당분간은 테스트 파일을 요리책 단위로 나눌 예정입니다.   
각 호스트 디렉터리의 레시피에 맞는 이름으로 테스트 파일을 만들어 볼 수도 있겠네요.   
기본 및 레시피 테스트_setting 중복되어 각 역할별로 관리하고 싶지만, 앞으로 문제가 될 것 같습니다.   
  
  

\[shell\]# vi spec/10.0.0.240/base\_spec.rb need 'spec\_helper'' 

백업 디렉터리 만들어

file ('/etc/.backup') 그것이 { be_directory } 끝

describe file('/etc/hosts') it { '10.0.0.240 xxx-web03' } 를 포함해야 합니다 끝

기본 게이트웨이 설정

default_gateway 해야 할 일을 설명해 주세요 its(:ipaddress) { 이쯤 이랬어야 해 '10.0.0.93' } its(:interface) { eq 'eth0' } 끝

SELinux가 비활성화되었습니다

SELinux DO를 설명하세요 그것이 { be_disabled } 끝

yum.conf에서 업데이트 커널 제외

file ('/etc/yum.conf') 설명 it {는 'exclude=kernel*' }를 포함해야 합니다 끝

modprobe.conf에서 ipv6 비활성화

file ('/etc/modprobe.conf') { 'options ipv6 disable=1' } 를 포함해야 합니다 끝

필요한 패키지가 반드시 포함되어야 합니다

%w{ sendmail ntp }.each do |pkg| Describe package("#{pkg}") do 그것이 { be_installed } 끝 끝

불필요한 서비스 중단

%w{ ip6tables iptables messagebus kudzu }.each do |services| 서비스("서비스") do를 설명하세요 그것 { should_not be_enabled } 그거 { should_not be_running } 끝 끝

로그 컷오프 설정

file 설명('/etc/logrotate.d/syslog') it { 'compress'를 포함해야 합니다 } { 'rotate 53'을 포함해야 한다 } 끝

불필요한 크론 허가는 0이어야 한다

%w{ makewhatis.cron mlocate.cron prelink }.each do |files| file 설명("/etc/cron.daily/#{files}")을 수행합니다 it { be_mode 0 } 끝 끝

file('etc/cron.weekly/makewhatis.cron')을 설명해 주세요 it { be_mode 0 } 끝

ntpdate for cron

크론 도를 설명해 it { have_entry '0 * * * */USR/SBIN/NTPDATE -BS 10.0.0.93' } 끝

시간대

#describe 파일('/etc/localtime')이 작동합니다

it { be_linked_to '/usr/share/zoneinfo/Japan' }

#end

웹서버용 kernelparams

'Linux 커널 매개변수'를 설명하는 것이 중요합니다 맥락 linux_kernel_parameter('net.ipv4.tcp_syncookies')가 하는 its(:value) { EQ 1 } 끝

맥락 linux_kernel_parameter('vm.swappiness')는 its(:value) { eq 30 } 끝

맥락 linux_kernel_parameter('net.ipv4.tcp_tw_reuse') its(:value) { 이쯤 0 } 끝

맥락 linux_kernel_parameter('net.ipv4.tcp_tw_recycle') its(:value) { 이쯤 0 } 끝

문맥 linux_kernel_parameter('net.ipv4.tcp_fin_timeout') its(:value) { 이중률 60 } 끝

맥락 linux_kernel_parameter('net.ipv4.tcp_max_syn_backlog') its(:value) { EQ 4096 } 끝

Context linux_kernel_parameter('net.core.somaxconn') do its(:value) { EQ 4096 } 끝 끝

로그인 사용자 테스트

%w{ xxx-op yyy-op dev }.each do |u| 사용자 설명("#{u}") 존재해야 한다 } { '바퀴'로 belong_to_group어야 한다 } 끝 끝


# 레이크 스펙

생략 3.13초에 완주 40가지 예시, 0건의 실패[/shell]
당분간은 꽤 멀리 이사했어요.
서버 사양 자체를 0.6으로 업그레이드하지 않으면, 일부 linux_kernel_paramater와 크론이 작동하지 않을 수 있습니다. 그런 방법이 없기 때문입니다.

```

[shell]# CP -p spec/10.0.0.240/base_spec.rb spec/10.0.0.241/

vi spec/10.0.0.241/base_spec.rb

diff spec/10.0.0.240/base_spec.rb spec/10.0.0.241/base_spec.rb

9c9
&lt; { '10.0.0.240 xxx-web03' } 포함되어야 합니다 }
--- 
&gt; { 포함되어야 한다 '10.0.0.241 xxx-db03' }
75c75
&lt; # kernelparams for webserver
--- 
&gt; # kernelparams for dbserver
82c82
&lt; its(:value) { should eq 30 }
--- 
&gt; its(:value) { should eq 0 }
86c86
&lt; its(:value) { should eq 0 }
--- 
&gt; its(:value) { should eq 1 }
90c90
&lt; its(:value) { should eq 0 }
--- 
&gt; its(:value) { should eq 1 }
94c94
&lt; its(:value) { should eq 60 }
--- 
&gt; its(:value) { should eq 10 }
98c98
&lt; its(:value) { should eq 4096 }
--- 
&gt; its(:value) { should eq 8192 }
102c102
&lt; its(:value) { should eq 4096 }
--- 
&gt; its(:value) { should eq 8192 }[/shell]<br>```

‧httpd 요리책 테스트 조금 썼어  
\[shell\]는 'spec\_helper'을 요구합니다. 

%w{ httpd php php-pecl-ssh2 php-mysql php-common php-devel php-pear php-pdo php-mbstring php-pecl-apc php-mcrypt php-CLI mysql-libs }.each do |pkg| Describe package("#{pkg}") do 그것이 { be_installed } 끝 끝

서비스('httpd') do 설명 그것이 { be_enabled } 그건 { be_running } 끝

port(80) do 그것이 { be_listening } 끝

file 설명('/etc/httpd/conf/httpd.conf') 그것이 { be_file } it { "ServerName localhost:80" } 끝

기본 인증 테스트

file 설명('/etc/httpd/conf.d/basic_auth.conf') 그것이 { be_file } it { "Require valid-user" } 포함해야 합니다 } 끝

file ('/etc/httpd/conf/.htpasswd') do 그것이 { be_file } { "dev"가 포함되어야 한다 } 끝

WP 내용 존재 테스트

file ('/var/www/html/wp-config.php') do 그것이 { be_file } 끝

마운트 퓨즈 테스트

#describe 파일('/var/www/html/assets')은

it { should be_mounted.with(:type => '퓨즈') }

it { should be_mounted.with(:options => { :rw => true } ) }

#end[/shell]
```

  • mysqld용 테스트 추가
    [shell]는 'spec_helper'을 요구합니다.
describe package('mysql-server') do
  그것이 { be_installed }
끝

Service('MySQLD') Do
  그것이 { be_enabled }
  그건 { be_running }
끝

port(3306) do 설명
  그것이 { be_listening }
끝

file (/etc/my.cnf') Do
  그것이 { be_file }
  it {는 &quot;server-id = 103&quot;을 포함해야 합니다 }
끝

file ('/etc/logrotate.d/mysqld')
  그것이 { be_file }
  { &quot;/usr/bin/mysqladmin flush-logs&quot; } 포함되어야 합니다 }
끝

file 설명('/opt/bin/mysql-back.sh') do
  그것이 { be_file }
  그것이 { be_executable }
끝[/shell]<br>·무닌 시험 작성해<br>```

\[shell\]# vi spec/10.0.0.240/munin\_spec.rb require 'spec\_helper' 

%w{ munin-node perl-DBI }.each do |pkg| Describe package("#{pkg}") do 그것이 { be_installed } 끝 끝

서비스('munin-node') do를 설명해 그것이 { be_enabled } 그건 { be_running } 끝

port(4949) do를 설명하세요 그것이 { be_listening } 끝

munin 플러그인

%w{ CPU 메모리 DF load TCP iostat if_eth0 if_err_eth0 }.each do |plg| describe file("/etc/munin/plugins/#{plg}") do 그것이 { be_file } 끝 끝

httpd용 munin 플러그인

%w{ apache_accesses apache_processes }.each do |plg| describe file("/etc/munin/plugins/#{plg}") do 그것이 { be_file } 끝 끝

MySQL용 Munin 플러그인

#%w{ mysql_slowqueries mysql_queries mysql_threads }.each do |plg|

file 설명("/etc/munin/plugins/#{plg}")이 합니다

그건 {be_file }

#end


# CP -p spec/10.0.0.240/munin\_spec.rb spec/10.0.0.241/munin\_spec.rb

# vi spec/10.0.0.241/munin\_spec.rb

# diff spec/10.0.0.240/munin\_spec.rb spec/10.0.0.241/munin\_spec.rb

26,33c26 < %w{ apache_accesses apache_processes }.each do |plg| < file 설명("/etc/munin/plugins/#{plg}")이 합니다. < { be_file } < 끝 < 끝 < < ## MySQL용 MUNIN 플러그인 < #%w{ mysql_slowqueries mysql_queries mysql_threads }.each do |plg|

> #%w{ apache_accesses apache_processes }.each do |plg| 38a32,38 > # MySQL용 munin 플러그인 > %w{ mysql_slowqueries mysql_queries mysql_threads }.each do |plg| > file 설명("/etc/munin/plugins/#{plg}")은 다음과 같습니다 > { be_file } > 끝 > 끝 >[/shell]
Zabbix 테스트를 작성하세요
```

[shell]# vi spec/10.0.0.240/zabbix_spec.rb need 'spec_helper'

%w{ zabbix-agent zabbix zabbix-jp-release }.each do |pkg|
  Describe package(&quot;#{pkg}&quot;) do
    그것이 { be_installed }
  끝
끝

Service('Zabbix-Agent') Do
  그것이 { be_enabled }
  그건 { be_running }
끝

port(10050) do를 설명하세요
  그것이 { be_listening }
끝

describe file('/opt/bin/mem_monitor.sh') do
  그것이 { be_executable }
끝

CP -p spec/10.0.0.240/zabbix_spec.rb spec/10.0.0.241/zabbix_spec.rb

레이크 스펙

~생략~
5.09초의 기록으로 결승
152개의 예시, 실패 0건[/shell]<br>문제 없이 테스트해봤어요.<br>이 검사가 충분한지는 의문이다.<br>```

  
사용자나 복제 자체는 테스트하지 않았지만, 명령어 리소스 유형이 있는 표준 출력만 확인할 수 있는 것 같습니다.   
명령은 이렇습니다 ↓,   
mysql -u root -p'cat /path_to_file' -s -e "Show grants for repl@'10.0.0.%';"   
반환 값이 이렇게 된다고 말하는 테스트를 작성하는 것밖에 생각할 수 없습니다 ↓  
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON _._ TO 'repl'@'10.0.0.%' 비밀번호로 식별됨 '\*43E209EED080057E35C2630AC06D3296\*\*\*\*\*\*''   
복잡한 것들은 단순한 반환 값으로 체크 명령을 만드는 느낌인지 궁금하네요.   
  

이 정보는  
・레이크 스펙 SPEC\_OPTS="--format html"으로 HTML을 출력할 수 있습니다. 오류가 아니면 OK만 반환되므로, 보고서를 원하는지 확인하는 것이 좋습니다.   
·gem install ci\_reporter 그리고 'ci/reporter/rake/rspec'을 Rakefile에 추가하세요.  
 JUnit 형식의 XML로 변환 가능(Jenkins에서 사용 가능)  
·[병렬을](https://github.com/grosser/parallel_tests) 쓰면_tests 병렬로 돌릴 수 있고 빠르다. 유닛이 많을 때 보기 좋다.   
·역할이나 속성을 사용하고 싶다면, 다음 사이트들이 도움이 될 수 있습니다.   
 [advanced\_tips](http://serverspec.org/advanced_tips.html)[서버스펙에서 호스트별 속성 값을 처리하는 방법](http://mizzy.org/blog/2013/05/12/2/) 서버[스펙에서 chef json 읽](http://blog.kenjiskywalker.org/blog/2013/07/31/serverspec-attribute/)기 서버[스펙으로 서버 환경을 위한 테스트를 작성](http://www.slideshare.net/ikedai/serverspec)해 봅시다  
  
  
이것이 장편 셰프 시리즈의 마지막입니다.   
시간 오래 걸려줘서 고마워.   
  
  
뒷번호는 여기를 클릭하세요↓  
 [Chef-Solo 및 Vagrant 도입 (VPC 환경)](https://blog.colorkrew.com/chef-solo_vagrant_install/) [Chef-Solo에서 레시피를 작성하기 전 환경(Vagrantfile, role, node, data\_bags)](https://blog.colorkrew.com/vagrantfile_role_data_bags/) [Chef 1에서 기존 프로시저에 대한 레시피 작성 (기본 설정)](https://blog.colorkrew.com/chef-solo_base_setting-recipe/) [Chef 2에서 기존 프로시저 레시피 작성 (사용자 생성)](https://blog.colorkrew.com/chef-solo_user_manage/) [Chef 3에서 기존 프로시저 레시피 작성 (웹 서버)](https://blog.colorkrew.com/chef-solo_webservers_recipe/) [chef4에서 기존 프로시저 레시피 작성 (DB 서버)](https://blog.colorkrew.com/chef-solo_dbservers_recipe/) [Chef 5에서 기존 프로시저 레시피 작성 (munin, zabbix)](https://blog.colorkrew.com/chef-solo_munin_zabbix_recipe/) [Chef로 작성한 테스트 레시피 (서버스펙)](https://blog.colorkrew.com/chef_serverspec/)  
   
   
   
   
   
   
   
   
   
 

Related Articles