test_helper.bash 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. setup() {
  2. IMAGE_NAME="$NAME:$VERSION"
  3. }
  4. # function relative to the current container / image
  5. build_image() {
  6. #disable outputs
  7. docker build -t $IMAGE_NAME $BATS_TEST_DIRNAME/../image &> /dev/null
  8. }
  9. run_image() {
  10. CONTAINER_ID=$(docker run $@ -d $IMAGE_NAME)
  11. CONTAINER_IP=$(get_container_ip_by_cid $CONTAINER_ID)
  12. }
  13. start_container() {
  14. start_containers_by_cid $CONTAINER_ID
  15. }
  16. stop_container() {
  17. stop_containers_by_cid $CONTAINER_ID
  18. }
  19. remove_container() {
  20. remove_containers_by_cid $CONTAINER_ID
  21. }
  22. clear_container() {
  23. stop_containers_by_cid $CONTAINER_ID
  24. remove_containers_by_cid $CONTAINER_ID
  25. }
  26. is_service_running() {
  27. is_service_running_by_cid $CONTAINER_ID $1
  28. }
  29. wait_service() {
  30. wait_service_by_cid $CONTAINER_ID $@
  31. }
  32. # generic functions
  33. get_container_ip_by_cid() {
  34. local IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" $1)
  35. echo "$IP"
  36. }
  37. start_containers_by_cid() {
  38. for cid in "$@"
  39. do
  40. #disable outputs
  41. docker start $cid &> /dev/null
  42. done
  43. }
  44. stop_containers_by_cid() {
  45. for cid in "$@"
  46. do
  47. #disable outputs
  48. docker stop $cid &> /dev/null
  49. done
  50. }
  51. remove_containers_by_cid() {
  52. for cid in "$@"
  53. do
  54. #disable outputs
  55. docker rm $cid &> /dev/null
  56. done
  57. }
  58. clear_containers_by_cid() {
  59. stop_containers_by_cid $@
  60. remove_containers_by_cid $@
  61. }
  62. is_service_running_by_cid() {
  63. docker exec $1 ps cax | grep $2 > /dev/null
  64. }
  65. wait_service_by_cid() {
  66. cid=$1
  67. # first wait image init end
  68. while ! is_service_running_by_cid $cid syslog-ng
  69. do
  70. sleep 1
  71. done
  72. for service in "${@:2}"
  73. do
  74. # wait service
  75. while ! is_service_running_by_cid $cid $service
  76. do
  77. sleep 1
  78. done
  79. done
  80. sleep 5
  81. }