py-py’s blog

何か書くよ

Pytest の基本

ちょくちょく更新

  • pytestのテスト対象プログラム名はtest_xxxx.py
  • テスト対象となる関数やメソッドはtest_xxxで始まるもの
  • テスト対象プログラム内でclassを用いてtestclassを使う際はclass名をTestXXXXにする
  • pytest を行う際、コマンドにオプションや引数を与えることで対象ディレクトリ、対象プログラム、対象関数やクラス、メソッドなど細かく指定できる
  • pytest -v オプションを使うと詳細にログを出してくれる
  • pytest -vv オプションを使うと超詳細にログを出してくれる
  • pytest --tb=no オプションを使うとログが簡潔になる
  • テストコード内ではassertで評価を行う
  • assertはテスト対象関数、メソッドの最後で評価すべし
  • pytest.fail("message")は明示的にエラーを発生させる

想定したエラーが発生するか確認する

def test_sample():
    with pytest.raises(ExceptionName):
        '''same_code'''

上記のpytest.raises(ExceptionName)の部分で想定したエラー名を書く
same_codeで想定したエラーが発生すればよい

  • with pytest_raises(ExceptionName, match)とすると、match部分に正規表現使ってエラーメッセージとの照合を行う
  • with pytest_raises(ExceptionName) as exec_infoなどとすることでExceptionInfo型としてあつかえる
  • テストを行う前に、対象のsetup, teardownようにフィクスチャを使う
  • フィクスチャ関数には@pytest.fixture()をつける
  • フィクスチャを使うテストコードは引数にフィクスチャ関数名を入れる
import pytest

@pytest.fixture()
def sample():
    print('set up')
    yield something
    print('tear down')

def test_code(sample):
    print('test code')

上記のようにすると、test_code関数実行時、最初にフィクスチャ関数のsampleが動き、次にtest_codeが動き、最後にsampleのprint('tear down')が実行される

nginxを用いたリバースプロキシサーバ構築

環境
- centos7
- nginx1.23.3
- openssl1.1.1

https通信をnginxでフォワードプロキシする、proxyサーバを構築する。
ただし、検証の段階で記事を書いているため動作はdocker上で行っている。
ユニットファイル作成後に

sudo systemctl status nginx 

を実行したらdockerの--privilegedオプションを付けてなかったので怒られた。

参考

www.opensourcetech.tokyo

準備

下記をインストール
- patch
- gcc
- pcre-devel
- perl
- zlib-devel

yum install patch gcc pcre-devel perl zlib-devel

下記をダウンロード及びgit clone
- openssl1.1.1

openssl1.1.1

https://www.openssl.org/source/openssl-1.1.1t.tar.gz

インストール作業

# 実行ユーザ追加
sudo useradd -M -s /sbin/nologin nginx

# opensslダウンロード
curl https://www.openssl.org/source/openssl-1.1.1t.tar.gz
tar fxz openssl-1.1.1t.tar.gz

# ngx_http_proxy_connect_moduleダウンロード
wget https://github.com/chobits/ngx_http_proxy_connect_module.git
(これでできるか検証。できなければzipなどでもってくる)

# nginx http proxy connect moduleの適用
patch -p1 < ../ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch

# nginxビルド
cd nginx1.23.3
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--add-module=../ngx_http_proxy_connect_module \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-compat \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-openssl=../openssl-1.1.1t \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

make
make install

# nginx info
/sbin/nginx -V

# ユニットファイル作成
vi /lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

ポート開放

# http向けポートを開放
firewall-cmd --zone=public --add-service=http

# https向けポートを開放  
firewall-cmd --zone=public --add-service=https

# ポートの恒久設定  
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent

# 設定読み込み  
firewall-cmd --reload

# 設定の確認  
firewall-cmd --list-all

ポート開放について参考になる

inaba.hatenablog.com

証明書設置

用意するもの
- サーバ証明書
- 鍵
- 中間証明書

サーバ証明書、鍵は発行者がいるならそいつからもらう
中間証明書は中間認証局からダウンロードする
ここで
- 鍵 key.key
- サーバ証明書 server.crt
- 中間証明書 ca.pem
とする

サーバ証明書と中間証明書の結合を行う。

# nginxがあるサーバでの作業
# ファイルの結合
cat server.crt ca.pe > cert.pem

mv cert.pem /etc/pki/tls/
mv key.key /etc/pki/tls/

証明書設置について参考になる

www.mtioutput.com

nginxの設定ファイルを編集

ざっと設定する

vi /etc/nginx/nginx.conf

でnginxの設定ファイルを開き、以下のように入力する。

設定内容

後で記入

テレワークでTeams使っていてサボってもアイコンを退席状態にしたくないあなたへ

Teamsは何らかの操作をしないと5分かそこらでアイコンが退席状態になる。(黄色のアイコンになる)

コンピューターで非アクティブの状態が続くと、Lync では状態が自動的に変更されます。コンピューターでマウスやキーボードを一定期間操作していないと、黄色の状態インジケーターと「非アクティブ」という語が表示されます。非アクティブの状態が一定期間続くと、引き続き黄色の状態インジケーターが表示されるほか、「退席中」という語が表示されます。これらのオプションの既定の期間は 5 分ですが、5 分より長い期間を指定することもできます。

引用: support.microsoft.com

Pythonでマウスを動かしてアクティブな状態にし続ければ退席状態にはならない。 と思いコードを書いた。

ターミナルにて以下のコマンドでライブラリをインストールする

pip3 intall pyautogui

main.py

import pyautogui


def main():
    print("mouse move start")
    pyautogui.moveTo(800, 500)
    while True:
        pyautogui.moveTo(900, 500, duration=0.5)
        pyautogui.moveTo(900, 800, duration=0.5)
        pyautogui.moveTo(550, 800, duration=0.5)
        pyautogui.moveTo(550, 500, duration=0.5)


if __name__ == "__main__":
    main()

moveTo()のx, yの大きさは各人のディスプレイの大きさによって変更すること。
画面中心でクルクルしてればなんかそれっぽい。
一応、別端末で作業して30分ほどほったらかしにしたけれど、アイコンは連絡可能だった。
プログラム書いて実行して作業サボって進捗が芳しくなかろうが、アイコンが退席状態になって怒られようがそこは自己判断でお願いします(>人<;)

pyautoguiが使えなかったら、他のライブラリもインストールする必要があるかもしれない。