0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Batsのインストール手順メモ(RHEL9.x)

Last updated at Posted at 2025-04-18

Batsのインストール手順メモ(RHEL9.x)

Table Of Contents

1. はじめに

Bats(Bash Automated Testing System)をRHEL9.xへインストールする手順のメモを記載します。

bats-core, bats-support, bats-assertをGitHubリポジトリからダウンロードし、以下の様にインストールします。

Name Destination
bats-core /usr/local
bats-assert /usr/local/lib
bats-support /usr/local/lib

前提として、GitHubリポジトリからダウンロードする為、RHEL9.xからインターネット接続ができる必要があります。
また、git cloneコマンドで、GitHubリポジトリからコンポーネントを取得しますので、gitパッケージをインストールしますが、その際、ローカルのRHEL9.4インストールメディアからインストールすることにします。
その為、最初にRHEL9.4インストールメディアのマウントして、そこからgitパッケージをインストールします。

ここではroot権限での作業が多い為、rootユーザでの操作となっています。su -, sudo -iなどでrootユーザにスイッチしてから作業をしてください。

ここではRHEL9.4を使用していますが、DVD の yum リポジトリーファイル名その中に書く名前以外は基本的にマイナーバージョンに囚われず利用可能なはずです。
また、古いメジャーバージョンでもメディア内のパスを読み替えれば動作するはずです。

2. github.comへの接続性確認

github.comへのリーチャビリティを確認します。

ping -c 2 github.com

3. gitコマンドのインストール

3-1. メディアのマウントとyum リポジトリーファイルの作成

ここでは /mntにインストールメディアをマウントして、DVD の yum リポジトリーファイル/etc/yum.repos.d/rhel94dvd.repoを作成します。
この設定手順は、Redhatのナレッジ Need to set up yum repository for locally-mounted DVD on Red Hat Enterprise Linux 9
に記載されていますので適宜ご参照ください。

以降、コマンドを記載しますが、加工せずにコピーして使えるようにプロンプトの表示や出力結果を省いている場合と、ものによっては実行結果を示すためにプロンプトを含め記載している場合があることにご注意ください。

# Mount the RHEL binary DVD ISO to the /mnt directory.
df -h             /mnt
mount -r /dev/sr0 /mnt

ls -ld /etc/yum.repos.d/rhel94dvd.repo

cat <<EOF > /etc/yum.repos.d/rhel94dvd.repo 
[BaseOS]
name=BaseOS Packages Red Hat Enterprise Linux 9.4.0
metadata_expire=-1
gpgcheck=1
enabled=1
baseurl=file:///mnt/BaseOS/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[AppStream]
name=AppStream Packages Red Hat Enterprise Linux 9.4.0
metadata_expire=-1
gpgcheck=1
enabled=1
baseurl=file:///mnt/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
EOF

md5sum -c <<<'a4986cc3761d775fbacfe3933f860531  /etc/yum.repos.d/rhel94dvd.repo'

3-2. gitパッケージをインストール

3-2-1. インストール

gitパッケージをインストールメディアからインストールします。

dnf install git.x86_64 -y

3-2-2. gitコマンドのインストールを確認

インストールが成功したらgitコマンドが利用可能であることを確認します。

[root@rhel94 ~]# which git
/usr/bin/git
[root@rhel94 ~]# git --version
git version 2.43.0

3-3. メディアのアンマウントとyum リポジトリーファイルの無効化

今後、使用する必要がなければメディアはアンマウントしておきましょう。
また、作成した/etc/yum.repos.d/rhel94dvd.repoも無効状態にしておくのが良いかと思います。

# Disabling `/etc/yum.repos.d/rhel94dvd.repo`
sed    '/^enabled=/s/1/0/' /etc/yum.repos.d/rhel94dvd.repo | diff /etc/yum.repos.d/rhel94dvd.repo -
sed -i '/^enabled=/s/1/0/' /etc/yum.repos.d/rhel94dvd.repo

# eject(unmount)
eject /dev/sr0
df -h /mnt

3. bats-coreのインストール

/tmpにbats-coreをダウンロード(clone)して、そこからインストーラーを実行します。
その際、インストール先は/usr/localを指定します。

(1) 実行コマンド

cd /tmp
git clone https://github.com/bats-core/bats-core.git
bats-core/install.sh /usr/local
which bats
bats --version
rm -fr bats-core/

(2) 実行イメージ

[root@rhel94 ~]# cd /tmp
[root@rhel94 tmp]# git clone https://github.com/bats-core/bats-core.git
Cloning into 'bats-core'...
remote: Enumerating objects: 10431, done.
remote: Counting objects: 100% (2795/2795), done.
remote: Compressing objects: 100% (284/284), done.
remote: Total 10431 (delta 2712), reused 2511 (delta 2511), pack-reused 7636 (from 3)
Receiving objects: 100% (10431/10431), 2.56 MiB | 3.32 MiB/s, done.
Resolving deltas: 100% (6453/6453), done.
[root@rhel94 tmp]# bats-core/install.sh /usr/local
Installed Bats to /usr/local/bin/bats
[root@rhel94 tmp]# which bats
/usr/local/bin/bats
[root@rhel94 tmp]# bats --version
Bats 1.11.1
[root@rhel94 tmp]# rm -fr bats-core/

4. bats-assert, bats-supportの配備

bats-assert, bats-support/usr/local/libに配備します。

(1) 実行コマンド

cd /usr/local/lib
git clone https://github.com/ztombol/bats-assert
git clone https://github.com/ztombol/bats-support

(2) 実行イメージ

[root@rhel94 tmp]# cd /usr/local/lib
[root@rhel94 lib]# git clone https://github.com/ztombol/bats-assert
Cloning into 'bats-assert'...
remote: Enumerating objects: 116, done.
remote: Counting objects: 100% (53/53), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 116 (delta 47), reused 47 (delta 47), pack-reused 63 (from 1)
Receiving objects: 100% (116/116), 31.68 KiB | 6.34 MiB/s, done.
Resolving deltas: 100% (63/63), done.
[root@rhel94 lib]# git clone https://github.com/ztombol/bats-support
Cloning into 'bats-support'...
remote: Enumerating objects: 88, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 88 (delta 3), reused 3 (delta 3), pack-reused 82 (from 1)
Receiving objects: 100% (88/88), 22.96 KiB | 2.30 MiB/s, done.
Resolving deltas: 100% (33/33), done.
[root@rhel94 lib]#

5. 動作確認

簡易テストスクリプトで動作を確認します。

cd /tmp
cat <<\EOF > basic_test.bats
#! /usr/bin/env bats

BATSLIB=/usr/local/lib
load ${BATSLIB}/bats-support/load
load ${BATSLIB}/bats-assert/load

function parrot(){
    echo   $1
    return $1
}

@test "mytest" {
    run parrot 0; assert_success;   assert_output 0
    run parrot 1; assert_failure 1; assert_output 1
}
EOF

md5sum -c <<<'3753a50ec5c9cb54d1a4d30e0d47edaa  basic_test.bats'
chmod +x basic_test.bats
./basic_test.bats

rm basic_test.bats

テスト実行時の結果は以下の様になります。

[root@rhel94 tmp]# ./basic_test.bats
basic_test.bats
 ✓ mytest

1 test, 0 failures

6. おわりに

Bats(bats-core)とヘルパーライブラリ(bats-support, bats-assert)をインストールするだけであれば、bats-coreのインストーラの実行と、bats-support, bats-assertを配備するだけです。
ですが、商用環境や社内のルールでインターネット接続ができなかったり、自由にパッケージのインストールができない場合があり得ます。

この場合、インターネット接続できる環境でgit cloneを使用してそれぞれのコンポーネントを入手して、tarボールなどで纏めておいて使用するのが良いと思います。

今回はそういったことを踏まえて、RHELでBatsのインストール自体の他に、データのダウンロードするためのgitのインストールに関わる手順なども纏めてみました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?