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 # 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/). 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 ### 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 - [#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 ## [0.7.3] - 2202-09-30
### Changed ### Changed

View file

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