test_helper.bash 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 --copy-service -c "/container/service/slapd/test.sh" $EXTRA_DOCKER_RUN_FLAGS)
  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. wait_process() {
  27. wait_process_by_cid $CONTAINER_ID $@
  28. }
  29. check_container() {
  30. # "Status" = "exited", and "ExitCode" != 0,
  31. local CSTAT=$(docker inspect -f "{{ .State.Status }} {{ .State.ExitCode }}" $CONTAINER_ID)
  32. echo "$CSTAT"
  33. }
  34. # generic functions
  35. get_container_ip_by_cid() {
  36. local IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" $1)
  37. echo "$IP"
  38. }
  39. start_containers_by_cid() {
  40. for cid in "$@"
  41. do
  42. #disable outputs
  43. docker start $cid &> /dev/null
  44. done
  45. }
  46. stop_containers_by_cid() {
  47. for cid in "$@"
  48. do
  49. #disable outputs
  50. docker stop $cid &> /dev/null
  51. done
  52. }
  53. remove_containers_by_cid() {
  54. for cid in "$@"
  55. do
  56. #disable outputs
  57. docker rm $cid &> /dev/null
  58. done
  59. }
  60. clear_containers_by_cid() {
  61. stop_containers_by_cid $@
  62. remove_containers_by_cid $@
  63. }
  64. wait_process_by_cid() {
  65. cid=$1
  66. docker exec $cid /container/tool/wait-process ${@:2}
  67. }