From 176b1b4bb7d547560f835a7def01b91307a9fdfb Mon Sep 17 00:00:00 2001 From: Alex Khoury <53697093+m3talux@users.noreply.github.com> Date: Tue, 21 May 2024 17:25:55 +0300 Subject: [PATCH] Manager unit tests (#12) * Manager unit tests * Finalized tests for manager * Fixed tests for CI * fixed CI trigger * Removed incompatible 1.19 Go version in CI --- .github/workflows/ci.yml | 4 +- go.mod | 47 +++ go.sum | 177 +++++++++- manager_test.go | 730 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 954 insertions(+), 4 deletions(-) create mode 100644 manager_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 39b7004..83a8234 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,13 +1,13 @@ name: CI -on: [push] +on: [push, pull_request] jobs: test-library: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.19', '1.20', '1.21', '1.22.x' ] + go-version: [ '1.20', '1.21', '1.22.x' ] steps: - uses: actions/checkout@v4 diff --git a/go.mod b/go.mod index 2f18d64..b6a7b97 100644 --- a/go.mod +++ b/go.mod @@ -8,11 +8,58 @@ require ( github.com/rabbitmq/amqp091-go v1.9.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.9.0 + github.com/testcontainers/testcontainers-go v0.30.0 + github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.30.0 ) require ( + dario.cat/mergo v1.0.0 // indirect + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect + github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/Microsoft/hcsshim v0.11.4 // indirect + github.com/cenkalti/backoff/v4 v4.2.1 // indirect + github.com/containerd/containerd v1.7.12 // indirect + github.com/containerd/log v0.1.0 // indirect + github.com/cpuguy83/dockercfg v0.3.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/distribution/reference v0.5.0 // indirect + github.com/docker/docker v25.0.5+incompatible // indirect + github.com/docker/go-connections v0.5.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/klauspost/compress v1.16.0 // indirect + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/moby/patternmatcher v0.6.0 // indirect + github.com/moby/sys/sequential v0.5.0 // indirect + github.com/moby/sys/user v0.1.0 // indirect + github.com/moby/term v0.5.0 // indirect + github.com/morikuni/aec v1.0.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/shirou/gopsutil/v3 v3.23.12 // indirect + github.com/shoenig/go-m1cpu v0.1.6 // indirect + github.com/tklauser/go-sysconf v0.3.12 // indirect + github.com/tklauser/numcpus v0.6.1 // indirect + github.com/yusufpapurcu/wmi v1.2.3 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect + golang.org/x/mod v0.16.0 // indirect golang.org/x/sys v0.19.0 // indirect + golang.org/x/tools v0.13.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect + google.golang.org/grpc v1.58.3 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ae8709f..daa5a23 100644 --- a/go.sum +++ b/go.sum @@ -1,36 +1,209 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8= +github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w= github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d h1:wvStE9wLpws31NiWUx+38wny1msZ/tm+eL5xmm4Y7So= github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d/go.mod h1:9XMFaCeRyW7fC9XJOWQ+NdAv8VLG7ys7l3x4ozEGLUQ= +github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= +github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/containerd/containerd v1.7.12 h1:+KQsnv4VnzyxWcfO9mlxxELaoztsDEjOuCMPAuPqgU0= +github.com/containerd/containerd v1.7.12/go.mod h1:/5OMpE1p0ylxtEUGY8kuCYkDRzJm9NO1TFMWjUpdevk= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= +github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E= +github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= +github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= +github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0= +github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v25.0.5+incompatible h1:UmQydMduGkrD5nQde1mecF/YnSbTOaPeFIeP5C4W+DE= +github.com/docker/docker v25.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= +github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4= +github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk= +github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= +github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc= +github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo= +github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg= +github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU= +github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= +github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= +github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= +github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo= github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc= +github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= +github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= +github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4= +github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM= +github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= +github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= +github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= +github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/testcontainers/testcontainers-go v0.30.0 h1:jmn/XS22q4YRrcMwWg0pAwlClzs/abopbsBzrepyc4E= +github.com/testcontainers/testcontainers-go v0.30.0/go.mod h1:K+kHNGiM5zjklKjgTtcrEetF3uhWbMUyqAQoyoh8Pf0= +github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.30.0 h1:FtkqA628qBpEmPj+yTaCgWzpR4ERT1A4oad8nvhDYgQ= +github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.30.0/go.mod h1:JC5EnBLOGC5eEL0Vcf3bwvUAKixuoZsJ4K3g0ioZ+WU= +github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= +github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= +github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= +github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= +github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= +go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= +go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4= +golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 h1:Z0hjGZePRE0ZBWotvtrwxFNrNE9CUAGtplaDK5NNI/g= +google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 h1:FmF5cCW94Ij59cfpoLiwTgodWmm60eEV0CjlsVg2fuw= +google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98/go.mod h1:rsr7RhLuwsDKL7RmgDDCUc6yaGr1iqceVb5Wv6f6YvQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= +google.golang.org/grpc v1.58.3 h1:BjnpXut1btbtgN/6sp+brB2Kbm2LjNXnidYujAVbSoQ= +google.golang.org/grpc v1.58.3/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY= +gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= diff --git a/manager_test.go b/manager_test.go new file mode 100644 index 0000000..7d42601 --- /dev/null +++ b/manager_test.go @@ -0,0 +1,730 @@ +package gorabbit_test + +import ( + "context" + "log" + "strconv" + "strings" + "testing" + "time" + + "github.com/KardinalAI/gorabbit" + amqp "github.com/rabbitmq/amqp091-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/modules/rabbitmq" +) + +type RabbitMQContainer struct { + *rabbitmq.RabbitMQContainer + ContainerHost string + ContainerPort uint + Username string + Password string +} + +func CreateRabbitMQContainer(ctx context.Context) (*RabbitMQContainer, error) { + rContainer, err := rabbitmq.RunContainer(ctx, + testcontainers.WithImage("rabbitmq:3.12.11-management-alpine"), + rabbitmq.WithAdminUsername("guest"), + rabbitmq.WithAdminPassword("guest"), + ) + + if err != nil { + return nil, err + } + + ep, err := rContainer.AmqpURL(ctx) + if err != nil { + return nil, err + } + + uri, err := amqp.ParseURI(ep) + if err != nil { + return nil, err + } + + rabbitMQContainer := &RabbitMQContainer{ + RabbitMQContainer: rContainer, + ContainerHost: uri.Host, + ContainerPort: uint(uri.Port), + Username: uri.Username, + Password: uri.Password, + } + + return rabbitMQContainer, nil +} + +type ManagerTestSuite struct { + suite.Suite + rabbitMQContainer *RabbitMQContainer + ctx context.Context +} + +func (suite *ManagerTestSuite) SetupSuite() { + suite.ctx = context.Background() + + rContainer, err := CreateRabbitMQContainer(suite.ctx) + if err != nil { + log.Fatal(err) + } + + suite.rabbitMQContainer = rContainer +} + +func (suite *ManagerTestSuite) TearDownSuite() { + if err := suite.rabbitMQContainer.Terminate(suite.ctx); err != nil { + log.Fatalf("error terminating RabbitMQ container: %s", err) + } +} + +func (suite *ManagerTestSuite) TestNewManager() { + t := suite.T() + + t.Run("Instantiating new manager with correct parameters", func(t *testing.T) { + managerOpts := gorabbit.NewManagerOptions(). + SetHost(suite.rabbitMQContainer.ContainerHost). + SetPort(suite.rabbitMQContainer.ContainerPort). + SetCredentials(suite.rabbitMQContainer.Username, suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManager(managerOpts) + + require.NoError(t, err) + assert.NotNil(t, manager) + + assert.NotEmpty(t, manager.GetHost()) + assert.NotZero(t, manager.GetPort()) + assert.NotEmpty(t, manager.GetUsername()) + + require.NoError(t, manager.Disconnect()) + }) + + t.Run("Instantiating new manager with incorrect credentials", func(t *testing.T) { + managerOpts := gorabbit.NewManagerOptions(). + SetHost(suite.rabbitMQContainer.ContainerHost). + SetPort(suite.rabbitMQContainer.ContainerPort). + SetCredentials("bad", "password") + + manager, err := gorabbit.NewManager(managerOpts) + + require.Error(t, err) + assert.Equal(t, "Exception (403) Reason: \"username or password not allowed\"", err.Error()) + assert.NotNil(t, manager) + + // Running any operation at that point should fail, except for disconnecting + require.NoError(t, manager.Disconnect()) + + err = manager.CreateQueue(gorabbit.QueueConfig{}) + + require.Error(t, err) + assert.Equal(t, "connection is closed", err.Error()) + }) + + t.Run("Instantiating new manager with incorrect host", func(t *testing.T) { + managerOpts := gorabbit.NewManagerOptions(). + SetHost("incorrect_host"). + SetPort(suite.rabbitMQContainer.ContainerPort). + SetCredentials(suite.rabbitMQContainer.Username, suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManager(managerOpts) + + require.Error(t, err) + + hasDialPrefix := strings.HasPrefix(err.Error(), "dial tcp: lookup incorrect_host on") + assert.True(t, hasDialPrefix) + + hasDialSuffix := strings.HasSuffix(err.Error(), "server misbehaving") + assert.True(t, hasDialSuffix) + + assert.NotNil(t, manager) + + // Running any operation at that point should fail, except for disconnecting + require.NoError(t, manager.Disconnect()) + + err = manager.CreateQueue(gorabbit.QueueConfig{}) + + require.Error(t, err) + assert.Equal(t, "connection is closed", err.Error()) + }) + + t.Run("Instantiating new manager with incorrect port", func(t *testing.T) { + managerOpts := gorabbit.NewManagerOptions(). + SetHost(suite.rabbitMQContainer.ContainerHost). + SetPort(uint(123)). + SetCredentials(suite.rabbitMQContainer.Username, suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManager(managerOpts) + + require.Error(t, err) + + hasDialPrefix := strings.HasPrefix(err.Error(), "dial tcp") + assert.True(t, hasDialPrefix) + + hasDialSuffix := strings.HasSuffix(err.Error(), "connection refused") + assert.True(t, hasDialSuffix) + + assert.NotNil(t, manager) + + // Running any operation at that point should fail, except for disconnecting + require.NoError(t, manager.Disconnect()) + + err = manager.CreateQueue(gorabbit.QueueConfig{}) + + require.Error(t, err) + assert.Equal(t, "connection is closed", err.Error()) + }) +} + +func (suite *ManagerTestSuite) TestNewFromEnv() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + require.NoError(t, manager.Disconnect()) + + t.Run("Instantiating a manager with disabled flag", func(t *testing.T) { + t.Setenv("GORABBIT_DISABLED", "true") + + manager, err = gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + assert.True(t, manager.IsDisabled()) + }) +} + +func (suite *ManagerTestSuite) TestCreateQueue() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Creating queue with valid parameters", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + err = manager.DeleteQueue(queueConfig.Name) + + require.NoError(t, err) + }) + + t.Run("Creating queue with empty name should work", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{} + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + err = manager.DeleteQueue(queueConfig.Name) + + require.NoError(t, err) + }) + + t.Run("Creating queue with bindings but non-existent exchange", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + Bindings: []gorabbit.BindingConfig{ + { + RoutingKey: "routing_key", + Exchange: "test_exchange", + }, + }, + } + + err = manager.CreateQueue(queueConfig) + + require.Error(t, err) + assert.Equal(t, "Exception (404) Reason: \"NOT_FOUND - no exchange 'test_exchange' in vhost '/'\"", err.Error()) + }) + + t.Run("Creating queue with bindings and existing exchange", func(t *testing.T) { + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange_with_bindings", + Type: "topic", + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + Bindings: []gorabbit.BindingConfig{ + { + RoutingKey: "routing_key", + Exchange: "test_exchange_with_bindings", + }, + }, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + err = manager.DeleteExchange(exchangeConfig.Name) + + require.NoError(t, err) + + err = manager.DeleteQueue(queueConfig.Name) + + require.NoError(t, err) + }) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestCreateExchange() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Creating exchange with valid parameters", func(t *testing.T) { + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange", + Type: gorabbit.ExchangeTypeTopic, + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + err = manager.DeleteExchange(exchangeConfig.Name) + + require.NoError(t, err) + }) + + t.Run("Creating exchange with empty parameters", func(t *testing.T) { + exchangeConfig := gorabbit.ExchangeConfig{} + + err = manager.CreateExchange(exchangeConfig) + + require.Error(t, err) + assert.Equal(t, "Exception (503) Reason: \"COMMAND_INVALID - invalid exchange type ''\"", err.Error()) + + // By now the manager's connection should be closed + err = manager.CreateExchange(exchangeConfig) + + require.Error(t, err) + assert.Equal(t, "connection is closed", err.Error()) + + manager, err = gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + }) + + t.Run("Creating exchange with empty name", func(t *testing.T) { + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "", + Type: gorabbit.ExchangeTypeTopic, + } + + err = manager.CreateExchange(exchangeConfig) + + require.Error(t, err) + assert.Equal(t, "Exception (403) Reason: \"ACCESS_REFUSED - operation not permitted on the default exchange\"", err.Error()) + + manager, err = gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + }) + + t.Run("Creating all type of exchanges", func(t *testing.T) { + topicExchange := gorabbit.ExchangeConfig{ + Name: "topic_exchange", + Type: gorabbit.ExchangeTypeTopic, + } + + err = manager.CreateExchange(topicExchange) + + require.NoError(t, err) + + directExchange := gorabbit.ExchangeConfig{ + Name: "direct_exchange", + Type: gorabbit.ExchangeTypeDirect, + } + + err = manager.CreateExchange(directExchange) + + require.NoError(t, err) + + fanoutExchange := gorabbit.ExchangeConfig{ + Name: "fanout_exchange", + Type: gorabbit.ExchangeTypeFanout, + } + + err = manager.CreateExchange(fanoutExchange) + + require.NoError(t, err) + + headersExchange := gorabbit.ExchangeConfig{ + Name: "headers_exchange", + Type: gorabbit.ExchangeTypeHeaders, + } + + err = manager.CreateExchange(headersExchange) + + require.NoError(t, err) + + require.NoError(t, manager.DeleteExchange(topicExchange.Name)) + require.NoError(t, manager.DeleteExchange(directExchange.Name)) + require.NoError(t, manager.DeleteExchange(fanoutExchange.Name)) + require.NoError(t, manager.DeleteExchange(headersExchange.Name)) + }) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestBindExchangeToQueueViaRoutingKey() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange", + Type: "topic", + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + t.Run("Binding existing exchange to existing queue via routing key", func(t *testing.T) { + err = manager.BindExchangeToQueueViaRoutingKey(exchangeConfig.Name, queueConfig.Name, "routing_key") + + require.NoError(t, err) + }) + + t.Run("Binding non-existing exchange to existing queue via routing key", func(t *testing.T) { + err = manager.BindExchangeToQueueViaRoutingKey("non_existing_exchange", queueConfig.Name, "routing_key") + + require.Error(t, err) + assert.Equal(t, "Exception (404) Reason: \"NOT_FOUND - no exchange 'non_existing_exchange' in vhost '/'\"", err.Error()) + }) + + t.Run("Binding existing exchange to non-existing queue via routing key", func(t *testing.T) { + err = manager.BindExchangeToQueueViaRoutingKey(exchangeConfig.Name, "non_existing_queue", "routing_key") + + require.Error(t, err) + assert.Equal(t, "Exception (404) Reason: \"NOT_FOUND - no queue 'non_existing_queue' in vhost '/'\"", err.Error()) + }) + + require.NoError(t, manager.DeleteQueue(exchangeConfig.Name)) + require.NoError(t, manager.DeleteQueue(queueConfig.Name)) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestGetNumberOfMessages() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Getting the number of messages from existing queue", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange", + Type: "topic", + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + err = manager.BindExchangeToQueueViaRoutingKey(exchangeConfig.Name, queueConfig.Name, "routing_key") + + require.NoError(t, err) + + count, countErr := manager.GetNumberOfMessages(queueConfig.Name) + + require.NoError(t, countErr) + assert.Zero(t, count) + + require.NoError(t, manager.DeleteExchange(exchangeConfig.Name)) + require.NoError(t, manager.DeleteQueue(queueConfig.Name)) + }) + + t.Run("Getting the number of messages from non-existing queue", func(t *testing.T) { + count, countErr := manager.GetNumberOfMessages("non_existing_queue") + + require.Error(t, countErr) + assert.Equal(t, "Exception (404) Reason: \"NOT_FOUND - no queue 'non_existing_queue' in vhost '/'\"", countErr.Error()) + assert.Equal(t, -1, count) + }) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestPushMessageToExchange() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Push message to exchange", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange", + Type: "topic", + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + err = manager.BindExchangeToQueueViaRoutingKey(exchangeConfig.Name, queueConfig.Name, "routing_key") + + require.NoError(t, err) + + err = manager.PushMessageToExchange(exchangeConfig.Name, "routing_key", "Some message") + + // Small sleep for allowing message to be sent. + time.Sleep(50 * time.Millisecond) + + require.NoError(t, err) + + count, countErr := manager.GetNumberOfMessages(queueConfig.Name) + + require.NoError(t, countErr) + assert.Equal(t, 1, count) + + require.NoError(t, manager.PurgeQueue(queueConfig.Name)) + + count, countErr = manager.GetNumberOfMessages(queueConfig.Name) + + require.NoError(t, countErr) + assert.Zero(t, count) + + require.NoError(t, manager.DeleteExchange(exchangeConfig.Name)) + require.NoError(t, manager.DeleteQueue(queueConfig.Name)) + }) + + t.Run("Pushing message to non-existing exchange should still work", func(t *testing.T) { + err = manager.PushMessageToExchange("non_existing_exchange", "routing_key", "Some message") + + // Small sleep for allowing message to be sent. + time.Sleep(50 * time.Millisecond) + + require.NoError(t, err) + }) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestPopMessageFromQueue() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Push message to exchange and consume it", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + exchangeConfig := gorabbit.ExchangeConfig{ + Name: "test_exchange", + Type: "topic", + } + + err = manager.CreateExchange(exchangeConfig) + + require.NoError(t, err) + + err = manager.BindExchangeToQueueViaRoutingKey(exchangeConfig.Name, queueConfig.Name, "routing_key") + + require.NoError(t, err) + + err = manager.PushMessageToExchange(exchangeConfig.Name, "routing_key", "Some message") + + // Small sleep for allowing message to be sent. + time.Sleep(50 * time.Millisecond) + + require.NoError(t, err) + + count, countErr := manager.GetNumberOfMessages(queueConfig.Name) + + require.NoError(t, countErr) + assert.Equal(t, 1, count) + + delivery, popErr := manager.PopMessageFromQueue(queueConfig.Name, true) + + require.NoError(t, popErr) + assert.Equal(t, "\"Some message\"", string(delivery.Body)) + + require.NoError(t, manager.PurgeQueue(queueConfig.Name)) + + count, countErr = manager.GetNumberOfMessages(queueConfig.Name) + + require.NoError(t, countErr) + assert.Zero(t, count) + + require.NoError(t, manager.DeleteExchange(exchangeConfig.Name)) + require.NoError(t, manager.DeleteQueue(queueConfig.Name)) + }) + + t.Run("Popping message from non-existing queue", func(t *testing.T) { + delivery, popErr := manager.PopMessageFromQueue("non_existing_queue", true) + + require.Error(t, popErr) + assert.Nil(t, delivery) + assert.Equal(t, "Exception (404) Reason: \"NOT_FOUND - no queue 'non_existing_queue' in vhost '/'\"", popErr.Error()) + }) + + t.Run("Popping message from existent empty queue", func(t *testing.T) { + queueConfig := gorabbit.QueueConfig{ + Name: "test_queue", + Durable: true, + Exclusive: false, + } + + err = manager.CreateQueue(queueConfig) + + require.NoError(t, err) + + delivery, popErr := manager.PopMessageFromQueue(queueConfig.Name, true) + + require.Error(t, popErr) + require.Equal(t, "queue is empty", popErr.Error()) + assert.Nil(t, delivery) + + require.NoError(t, manager.DeleteQueue(queueConfig.Name)) + }) + + require.NoError(t, manager.Disconnect()) +} + +func (suite *ManagerTestSuite) TestSetupFromDefinitions() { + t := suite.T() + + t.Setenv("RABBITMQ_HOST", suite.rabbitMQContainer.ContainerHost) + t.Setenv("RABBITMQ_PORT", strconv.Itoa(int(suite.rabbitMQContainer.ContainerPort))) + t.Setenv("RABBITMQ_USERNAME", suite.rabbitMQContainer.Username) + t.Setenv("RABBITMQ_PASSWORD", suite.rabbitMQContainer.Password) + + manager, err := gorabbit.NewManagerFromEnv() + + require.NoError(t, err) + assert.NotNil(t, manager) + + t.Run("Setting up from definitions with wrong path", func(t *testing.T) { + err = manager.SetupFromDefinitions("wrong-path.json") + + require.Error(t, err) + assert.Equal(t, "open wrong-path.json: no such file or directory", err.Error()) + }) + + t.Run("Setting up from definitions with right path", func(t *testing.T) { + err = manager.SetupFromDefinitions("assets/definitions.example.json") + + require.NoError(t, err) + }) + + require.NoError(t, manager.Disconnect()) +} + +func TestManagerTestSuite(t *testing.T) { + suite.Run(t, new(ManagerTestSuite)) +}