【问题标题】:Script fails at build but works from Docker container脚本在构建时失败,但在 Docker 容器中工作
【发布时间】:2017-11-19 00:05:07
【问题描述】:

我正在尝试通过 Docker 为目录监视器运行单元测试,该目录监视器会对文件和目录的创建、移动和重命名作出反应。

这是脚本:

#/bin/bash

# Launch Redis server
redis-server --daemonize yes
# Start directory_observer
python /test/directory_observer/src/directory_observer.py --config=/test/directory_observer/tests/directory_observer.ini &
# Execute test script (operations in the folders)
bash /test/directory_observer/tests/Dockerfile_testing/directory_observer_test_cases.sh
# Run Python unit test based on the operations executed above
python /test/directory_observer/tests/Dockerfile_testing/test_directory_observer.py
cd /test/directory_observer/ && nosetests
  • redis 是存储监视器注意到的事件的位置

  • directory_observer.py 是目录监视器

  • directory_observer_test_cases.sh 只是一个 bash 脚本,用于创建、重命名和移动一些文件和目录。

  • test_directory_observer.py 只是一个 Python 单元测试脚本(在此处运行以进行调试)

问题是一些测试在构建时没有任何明显原因就失败了,但是如果我从正在运行的容器中尝试完全相同的 bash 指令,它就会按预期工作。

如果需要directory_observer_test_cases.sh的代码:

#!/bin/bash

cd /test/monitored1

# Create, rename and delete file
touch file1
mv file1 rename1

# Same as above, but in a sub-directory
mkdir sub-monitored
cd /test/monitored1/sub-monitored
touch file2
mv file2 rename2

cd /test/monitored2

# Create, rename and delete file to test second monitored dir
touch file12
mv file12 rename12


### NOT MONITORED FROM HERE ###


# Rename the monitored directory
cd /test
mv monitored1 watched1

# Same as #1, but in the newly renamed directory
cd /test/watched1
touch file3
mv file3 rename3

# Move the second monitored directory
cd /test
mv monitored2 directory_host

# Same as #1, but in the newly moved directory
cd /test/directory_host/monitored2
touch file4
mv file4 rename4

# Operations in the non-monitored directory
cd /test/ingored.t
touch file5
mv file5 rename5

在你问之前,是的,我已经阅读了this question,但这对我没有帮助(或者我遗漏了一些东西)。

【问题讨论】:

    标签: python bash docker dockerfile


    【解决方案1】:

    事实证明,由于目录监视器是作为后台进程启动的,因此测试脚本在监视器初始化之前就已启动。因此,错过了一些事件,导致测试失败。

    在启动测试脚本之前添加sleep 1 解决了问题,现在一切运行顺利。

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多