@@ -3,6 +3,7 @@ package main
33import (
44 "context"
55 "fmt"
6+ "net/http"
67 "os"
78 "path"
89 "testing"
@@ -124,7 +125,15 @@ func TestRun(t *testing.T) {
124125 httpmock .NewStringResponder (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain1.com.","type":1}],"Answer":[{"name":"domain1.com.","type":1,"TTL":30,"data":"67.67.67.67"}]}` ))
125126
126127 httpmock .RegisterResponder ("GET" , "https://dns.google/resolve?name=domain2.com.&type=a" ,
127- httpmock .NewStringResponder (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}` ))
128+ httpmock .ResponderFromMultipleResponses (
129+ []* http.Response {
130+ httpmock .NewStringResponse (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}` ),
131+ httpmock .NewStringResponse (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}` ),
132+ httpmock .NewStringResponse (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"70.70.70.70"}]}` ),
133+ httpmock .NewStringResponse (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}` ),
134+ httpmock .NewStringResponse (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"70.70.70.70"}]}` ),
135+ },
136+ t .Log ))
128137
129138 httpmock .RegisterResponder ("GET" , "https://dns.google/resolve" , // no query params to match all other requests
130139 httpmock .NewStringResponder (200 , `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}` ))
@@ -134,28 +143,21 @@ func TestRun(t *testing.T) {
134143 args args
135144 wantErr bool
136145 }{
137- {name : "success" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
146+ {name : "success egress audit " , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
138147 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
139148 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : false },
140149
141- {name : "success monitor process" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
142- iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : nil , resolvdConfigPath : createTempFileWithContents ("" ),
143- dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
144-
145- {name : "success allowed endpoints" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
150+ {name : "success egress blocked" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
146151 hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
147152 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
148153 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : false },
149154
150- {name : "success allowed endpoints CI Test" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
155+ // ctx will cancel after 35 seconds
156+ // DNS refresh will be done after 30 seconds
157+ {name : "success egress blocked DNS refresh" , args : args {ctxCancelDuration : 35 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
151158 hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
152- iptables : nil , nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
153- dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
154-
155- {name : "success allowed endpoints DNS refresh CI Test" , args : args {ctxCancelDuration : 60 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
156- hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
157- iptables : nil , nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
158- dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
159+ iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
160+ dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : false },
159161
160162 {name : "dns failure" , args : args {ctxCancelDuration : 5 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServerWithError {},
161163 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
@@ -168,6 +170,21 @@ func TestRun(t *testing.T) {
168170 {name : "nflog failure" , args : args {ctxCancelDuration : 5 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
169171 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNfloggerWithErr {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
170172 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : true },
173+
174+ // CI only tests
175+ {name : "success monitor process CI Test" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
176+ iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : nil , resolvdConfigPath : createTempFileWithContents ("" ),
177+ dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
178+
179+ {name : "success allowed endpoints CI Test" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
180+ hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
181+ iptables : nil , nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
182+ dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
183+
184+ {name : "success allowed endpoints DNS refresh CI Test" , args : args {ctxCancelDuration : 60 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
185+ hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
186+ iptables : nil , nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
187+ dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
171188 }
172189 _ , ciTest := os .LookupEnv ("CI" )
173190 fmt .Printf ("ci-test: %t\n " , ciTest )
0 commit comments