Phoenix with exq: How do I execute mix test without redis running - redis

I use exq in my Phoenix application with Phoenix 1.4.16 to run some background jobs.
One of them can be as simple as this:
defmodule PeopleJob do
def perform(request) do
IO.puts("Hello from PeopleJob:\n#{inspect(request)}")
end
end
It runs with redis in dev environment perfectly.
The problem is that when I push the code to a CI server that has no redis, all the tests fail.
The test config is like this
In config/test.exs:
config :exq, queue_adapter: Exq.Adapters.Queue.Mock
In test/test_helper.exs:
Exq.Mock.start_link(mode: :inline)
When I run "mix test" on a machine without redis running, it fails like this:
** (Mix) Could not start application exq: Exq.start(:normal, []) returned an error: shutdown: failed to start child: Exq.Manager.Server
** (EXIT) an exception was raised:
** (RuntimeError)
====================================================================================================
ERROR! Could not connect to Redis!
Configuration passed in: [host: "127.0.0.1", port: 6379, database: 0, password: nil]
Error: :error
Reason: {:badmatch, {:error, %Redix.ConnectionError{reason: :closed}}}
Make sure Redis is running, and your configuration matches Redis settings.
====================================================================================================
(exq) lib/exq/manager/server.ex:393: Exq.Manager.Server.check_redis_connection/1
(exq) lib/exq/manager/server.ex:173: Exq.Manager.Server.init/1
(stdlib) gen_server.erl:374: :gen_server.init_it/2
(stdlib) gen_server.erl:342: :gen_server.init_it/6
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Actually I try all 3 modes :redis, :fake and :inline, but all of them fail to start the mix test.
Question: Can I run "mix test" on a machine that has no redis?
The reason is that our company doesn't want to install redis on the Travis CI machine.
I expected that using Exq Mock in the test environment would allow the test to run without redis, but it is not the case.

I figure it out.
In config/test.exs:
config :exq, queue_adapter: Exq.Adapters.Queue.Mock
config :exq, start_on_application: false
In test/test_helper.exs:
Exq.Mock.start_link(mode: :inline)
Adding config :exq, start_on_application: false to config/test.exs solved this problem.

Related

Trivy on EKS unable to scan any images

I am trying to scan all images deployed on my EKS cluster I am setting up for high security (will be deployed to classified IL5 environment). Kubernetes v1.23, all worker nodes run on Bottlerocket OS.
I expect images to be scanned and available in the VulnerabilityReports CRD.
I was able to successfully install Falco to the cluster (uses containerd). However, when deploying the official Helm chart (0.6.0-rc3) the scan-vulnerability containers start and then immediately error out. I set this environment variable on the trivy-operator deployment:
- name: CONTAINER_RUNTIME_ENDPOINT
value: /run/containerd/containerd.sock
Output of run with -debug:
{
"level": "error",
"ts": 1668286646.865245,
"logger": "reconciler.vulnerabilityreport",
"msg": "Scan job container",
"job": "trivy-system/scan-vulnerabilityreport-74f54b6cd",
"container": "discovery",
"status.reason": "Error",
"status.message": "2022-11-12T20:57:13.674Z\t\u001b[31mFATAL\u001b[0m\timage scan error: scan error: unable to initialize a scanner: unable to initialize a docker scanner: 4 errors occurred:\n\t* unable to inspect the image (023620263533.dkr.ecr.us-gov-east-1.amazonaws.com/docker.io/istio/pilot:1.15.2): Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?\n\t* unable to initialize Podman client: no podman socket found: stat podman/podman.sock: no such file or directory\n\t* containerd socket not found: /run/containerd/containerd.sock\n\t* GET https://023620263533.dkr.ecr.us-gov-east-1.amazonaws.com/v2/docker.io/istio/pilot/manifests/1.15.2: unexpected status code 401 Unauthorized: Not Authorized\n\n\n\n",
"stacktrace": "github.com/aquasecurity/trivy-operator/pkg/vulnerabilityreport.(*WorkloadController).processFailedScanJob\n\t/home/runner/work/trivy-operator/trivy-operator/pkg/vulnerabilityreport/controller.go:551\ngithub.com/aquasecurity/trivy-operator/pkg/vulnerabilityreport.(*WorkloadController).reconcileJobs.func1\n\t/home/runner/work/trivy-operator/trivy-operator/pkg/vulnerabilityreport/controller.go:376\nsigs.k8s.io/controller-runtime/pkg/reconcile.Func.Reconcile\n\t/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime#v0.13.1/pkg/reconcile/reconcile.go:102\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile\n\t/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime#v0.13.1/pkg/internal/controller/controller.go:121\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\t/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime#v0.13.1/pkg/internal/controller/controller.go:320\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\t/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime#v0.13.1/pkg/internal/controller/controller.go:273\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\t/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime#v0.13.1/pkg/internal/controller/controller.go:234"
}
I confirmed that bottlerocket uses containerd, as /run/containerd/containerd.sock is specified on my Falco deployment. Even when I mount this as volume onto the pod and set the CONTAINER_RUNTIME_ENDPOINT to this path I get the same error.
Edit
I added the following security context:
seLinuxOptions:
user: system_u
role: system_r
type: control_t
level: s0-s0:c0.c1023
Initially I mounted the dockershim.sock from the host to the pod, then realized that was not necessary, the error messages were a little misleading, it was really an authentication with ECR issue. Furthermore, the seLinux flags needed to be specified at the pod level, and not the container level.

Hyperledger Fabric error: "TLS: bad certificate server" when installing chaincode

I'm just starting learning HLF, and I have an error while following tutorial from the docs: link
I downloaded fabric-samples using this command (replaced bit.ly link with the destination):
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s -- 2.2.2 1.4.9
I run logspout in one terminal and try to execute peer lifecycle chaincode install basic.tar.gz in another one, and this is the result i get
Error: failed to retrieve endorser client for install: endorser client
failed to connect to localhost:7051: failed to create new connection:
context deadline exceeded
Log presented by Logspout:
peer0.org1.example.com|2022-03-15 13:03:24.452 UTC [core.comm]
ServerHandshake -> ERRO 04a Server TLS handshake failed in 2.650245ms
with error remote error: tls: bad certificate server=PeerServer
remoteaddress=172.22.0.1:61126
I set the envs in terminal as instructed in the docs, and I checked that CORE_PEER_TLS_ROOTCERT_FILE variable points to an existing file. The content of the file is the same as on the container.
What I tried to do:
download fabric-samples again and redo all the setup with copy-pasting the commands directly from docs
Do you have any suggestions where I can look for an issue?
I resolved the problem, I was using peer version 2.2.1 from previous experiments, it probably collided with FABRIC_CFG_PATH

How do I prevent node from sending errors to the console?

Working on a React project.
I start up my environment using npm start. I then start my testing infrastructure using npm test in a different terminal. While my tests are running, errors are generated and printed in the terminal that my environment is running in.
npm start
Compiled successfully!
You can now view v2 in the browser.
Local: http://localhost:3000
Note that the development build is not optimized.
To create a production build, use yarn build.
Open another terminal and run npm test
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 2.415s
Ran all test suites.
Yet, some UI errors that the tests generate appear in the terminal for my local environment
Compiled successfully!
You can now view v2 in the browser.
Local: http://localhost:3000
Note that the development build is not optimized.
To create a production build, use yarn build.
[HPM] Error occurred while trying to proxy request /directory from localhost:3000 to <TARGET> (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /directory from localhost:3000 to <TARGET> (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /directory from localhost:3000 to <TARGET> (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /directory from localhost:3000 to <TARGET> (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /directory from localhost:3000 to <TARGET> (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
How do I prevent errors - and anything else for that matter - from being printed out to the terminal where I started my dev environment?
looking at the output of your npm test, it seems like you are using jest, which has a support for configuring setup files
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed.
you can utilize it by pointing to a file, where you can mock the implmentation for console.
add to jest.config.js the following
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js'],
};
then in jest.setup.js file
global.beforeAll(() => {
// disable console.error for clarity
console.error = jest.fn().mockImplementation(() => {});
});
note that it will achieve what you desire only if the module that prints to the console during the testing is using console.error(). otherwise, you might need to mock other functions, such as console.info(), console.log(), console.debug()

Trying to read a webpage in Hound, I get a compilation error for Hound.start_session

I started a new project and configured it like so:
mix new example
cd example
I emptied ´lib/example.ex´ and placed the following code there:
Application.start :hound
defmodule Example do
use Hound.Helpers
def run do
Hound.start_session
navigate_to "http://akash.im"
IO.inspect page_title()
# Automatically invoked if the session owner process crashes
Hound.end_session
end
end
Example.run
This is the sample code provided at https://github.com/HashNuke/hound/blob/master/notes/simple-browser-automation.md
Then I installed Selenium server via brew install selenium-server-standalone (I'm on MacOS), started it via brew services start selenium-server-standalone and added config :hound, driver: "selenium" to config/config.exs
I added Application.ensure_all_started(:hound) as the first line of test/test_helper.exs.
Finally, I added {:hound, "~> 1.0"} to mix.exs and ran mix test. That is when I get the following compilation error:
localhost:example alex$ mix test
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> jason
Compiling 8 files (.ex)
Generated jason app
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> hound
Compiling 37 files (.ex)
Generated hound app
==> example
Compiling 1 file (.ex)
== Compilation error in file lib/example.ex ==
** (ArgumentError) argument error
(stdlib) :ets.lookup(Hound.SessionServer, #PID<0.592.0>)
(hound) lib/hound/session_server.ex:19: Hound.SessionServer.current_session_id/1
(hound) lib/hound/session_server.ex:13: Hound.SessionServer.session_for_pid/2
lib/example.ex:7: Example.run/0
localhost:example alex$ mix test
Compiling 1 file (.ex)
== Compilation error in file lib/example.ex ==
** (ArgumentError) argument error
(stdlib) :ets.lookup(Hound.SessionServer, #PID<0.160.0>)
(hound) lib/hound/session_server.ex:19: Hound.SessionServer.current_session_id/1
(hound) lib/hound/session_server.ex:13: Hound.SessionServer.session_for_pid/2
lib/example.ex:7: Example.run/0
Am I forgetting a step somewhere or configuring things incorrectly? Any help immensely appreciated, thanks!
I emptied lib/example.ex and placed the following code there:
defmodule Example do
...
end
Example.run
There is a difference between .ex files and .exs files. You decided to put that code in the application's main .ex file. Get rid of this line:
Example.run
Then, to execute Example.run() you do this:
.../example$ iex -S mix
iex(1)> Example.run
"Akash Manohar // #HashNuke"
:ok
Or, you can change the extension to .exs, then run the code with this:
.../example$ mix run lib/example.exs
On the other hand, if you want mix test to run a test, then you have to put the test in the test directory. For example:
defmodule ExampleTest do
use ExUnit.Case
use Hound.Helpers
test "page title is correct" do
Hound.start_session
navigate_to "http://akash.im"
#IO.inspect page_title()
assert page_title() == "Akash Manohar // #HashNuke"
Hound.end_session
end
end
In the hound exunit example here, the hound_session() call caused an error for me:
15:06:33.736 [error] GenServer Hound.SessionServer terminating
** (RuntimeError) could not create a new session: timeout, check webdriver is running
(hound) lib/hound/session_server.ex:101: Hound.SessionServer.create_session/2

Rabbitmq installation error : Applying plugin configuration to rabbit#... failed. * Could not contact node rabbit#

I am new to RabbitMq .I am installing the rabbitmq on my system .while running this command rabbitmq-plugins enable rabbitmq_management i am getting an error like
'call "C:\Program Files\erl7.2.1\bin\erl.exe" -A0 -noinput -boot start_clean -ev
al "net_kernel:start([list_to_atom(""rabbit-gethostname-"" ++ os:getpid()), shor
tnames]), [_, H] = string:tokens(atom_to_list(node()), ""#""), io:format(""~s~n"
", [H]), init:stop()."' is not recognized as an internal or external command,
operable program or batch file.
Plugin configuration unchanged.
Applying plugin configuration to rabbit#... failed.
* Could not contact node rabbit#.
Changes will take effect at broker restart.
* Options: --online - fail if broker cannot be contacted.
--offline - do not try to contact broker.
Can any one help me to solve this error ?
try to reinstall erlang and check Microsoft's DLL Component.