Refactored cleanup to not use deprecated methods anymore (#66)

This commit is contained in:
Tim Zöller 2022-11-16 22:46:41 +01:00
parent 94e582a1aa
commit 667a2d0850
2 changed files with 139 additions and 119 deletions

View file

@ -1,9 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [0.7.4] - 22-11-16
## [0.7.4] - 2022-11-16
### Changed
- [#64](https://github.com/javahippie/clj-test-containers/pull/64): Fix container path parameter for copy-file-to-container! in README
- [#65](https://github.com/javahippie/clj-test-containers/issues/65): Upgrade to testcontainers-java 1.17.6
- [#66](https://github.com/javahippie/clj-test-containers/issues/66): Remove usage of deprecated API calls for cleanup
## [0.7.3] - 2202-09-30
### Changed

View file

@ -8,6 +8,7 @@
Paths)
(java.time
Duration)
(org.testcontainers DockerClientFactory)
(org.testcontainers.containers
BindMode
GenericContainer
@ -21,8 +22,7 @@
(org.testcontainers.images.builder
ImageFromDockerfile)
(org.testcontainers.utility
MountableFile
ResourceReaper)))
MountableFile)))
(defn- resolve-bind-mode
(^BindMode [bind-mode]
@ -30,9 +30,7 @@
BindMode/READ_WRITE
BindMode/READ_ONLY)))
(defn- reaper-instance
[]
(ResourceReaper/instance))
(defonce started-instances (atom #{}))
(defmulti wait
"Sets a wait strategy to the container. Supports :http, :health and :log as
@ -352,9 +350,7 @@
container-id ^String (.getContainerId container)
image-name ^String (.getDockerImageName container)
logger (log log-to container)]
(.registerContainerForCleanup ^ResourceReaper (reaper-instance)
container-id
image-name)
(swap! started-instances conj {:type :container :id container-id})
(-> container-config
(merge {:id container-id
:mapped-ports mapped-ports
@ -388,7 +384,7 @@
(let [network (.build builder)
network-name (.getName network)]
(.registerNetworkIdForCleanup ^ResourceReaper (reaper-instance) network-name)
(swap! started-instances conj {:type :network :id :network-name})
{:network network
:name network-name
:ipv6 (.getEnableIpv6 network)
@ -396,10 +392,32 @@
(def ^:deprecated init-network create-network)
(defn- remove-network! [instance]
(-> (DockerClientFactory/instance)
(.client)
(.removeNetworkCmd (:id instance))
(.exec))
instance)
(defn- stop-and-remove-container! [instance]
(let [docker-client (DockerClientFactory/instance)]
(-> docker-client
(.client)
(.stopContainerCmd (:id instance))
(.exec))
(-> docker-client
(.client)
(.removeContainerCmd (:id instance))
(.exec)))
instance)
(defn perform-cleanup!
"Stops and removes all container instances which were created in the active JVM or REPL session"
[]
(.performCleanup ^ResourceReaper (reaper-instance)))
(for [instance @started-instances]
(swap! started-instances disj (case (:type instance)
:container (stop-and-remove-container! instance)
:network (remove-network! instance)))))
;; REPL Helpers