Block network access of a test/process on Travis?

Based on this question and its answers: What is an applicable approach (if there is any) of blocking network access of a test or process on Travis CI?

Background: I want to test offline functionality, i.e. a cache, enabling to use a library without internet/network access. A dependency (a third-party piece of software) involved in the process tries to connect to the internet/network. Currently, I am managing to prohibit its internet/network access (confirmed locally by “pulling the plug”), though I want to find a way to implement a proper CI test for long-term maintenance. Most of the testing infrastructure is written in Python and based on pytest.

Your Travis jobs run in a fully functional Linux env, which includes the ability to create firewall rules using the iptables command just like in any other case. Consider this very simple .travis.yml file:

---
script:
  - curl http://icanhazip.com

Test this somewhere in a repo and run it, it will work just fine:

$ curl http://icanhazip.com
104.196.57.92
The command "curl http://icanhazip.com" exited with 0.

In order to reproduce offline behavior, we just add firewall rules that block outbound access on port 80, and in some cases port 8080:

---
script:
  - sudo iptables -A OUTPUT -p tcp --dport 80 -j REJECT
  - curl http://icanhazip.com

On the same token, this will fail:

$ curl http://icanhazip.com
curl: (7) Failed to connect to icanhazip.com port 80: Connection refused
The command "curl http://icanhazip.com" exited with 7.

This is what makes Travis so amazing, the flexibility of it all. I hope this helps, if you need anything else please feel free to post back and I will gladly help you.

-Montana (Travis CI Staff)

1 Like

Thanks Montana!

1 Like

Of course, the Travis team is thrilled we can try and help each and every person that posts here!

-Montana