99 "time"
1010
1111 "github.com/florianl/go-nflog/v2"
12+ "github.com/jarcoal/httpmock"
1213)
1314
1415type mockDNSServer struct {
@@ -36,6 +37,14 @@ func (m *MockIPTables) Append(table, chain string, rulespec ...string) error {
3637 return nil
3738}
3839
40+ func (m * MockIPTables ) Exists (table , chain string , rulespec ... string ) (bool , error ) {
41+ return false , nil
42+ }
43+
44+ func (m * MockIPTables ) Insert (table , chain string , post int , rulespec ... string ) error {
45+ return nil
46+ }
47+
3948func (m * MockIPTables ) ClearChain (table , chain string ) error {
4049 return nil
4150}
@@ -83,39 +92,14 @@ func (m *MockCommandWithError) Run() error {
8392 return fmt .Errorf ("failed to run command" )
8493}
8594
86- /*
87- func TestRunWithNflogError(t *testing.T) {
88-
89- ctx := context.Background()
90- ctx, cancel := context.WithCancel(ctx)
91- time.AfterFunc(5*time.Second, cancel) // this should not be used, it should error out earlier
92-
93- httpmock.Activate()
94- defer httpmock.DeactivateAndReset()
95-
96- httpmock.RegisterResponder("POST", fmt.Sprintf("%s/owner/repo/actions/runs/1287185438/monitor", agentApiBaseUrl),
97- httpmock.NewStringResponder(200, ""))
98-
99- err := Run(ctx, "./testfiles/agent.json",
100- &mockDNSServer{}, &mockDNSServer{}, &Firewall{&MockIPTables{}},
101- &MockAgentNfloggerWithErr{}, &MockCommand{}, createTempFileWithContents(""), createTempFileWithContents("{}"), nil)
102-
103- // if 2 seconds pass
104- if err == nil {
105- t.Fail()
106- }
107-
108- }
109- */
110-
11195func deleteTempFile (path string ) {
11296 os .Remove (path )
11397}
11498
11599func getContext (seconds int ) context.Context {
116100 ctx := context .Background ()
117101 ctx , cancel := context .WithCancel (ctx )
118- time .AfterFunc (2 * time .Second , cancel )
102+ time .AfterFunc (time . Duration ( seconds ) * time .Second , cancel )
119103
120104 return ctx
121105}
@@ -134,6 +118,17 @@ func TestRun(t *testing.T) {
134118 ciTestOnly bool
135119 }
136120
121+ httpmock .Activate ()
122+
123+ httpmock .RegisterResponder ("GET" , "https://dns.google/resolve?name=domain1.com.&type=a" ,
124+ 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"}]}` ))
125+
126+ 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+
129+ httpmock .RegisterResponder ("GET" , "https://dns.google/resolve" , // no query params to match all other requests
130+ 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"}]}` ))
131+
137132 tests := []struct {
138133 name string
139134 args args
@@ -142,19 +137,34 @@ func TestRun(t *testing.T) {
142137 {name : "success" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
143138 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
144139 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : false },
140+
145141 {name : "success monitor process" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
146142 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : nil , resolvdConfigPath : createTempFileWithContents ("" ),
147143 dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
144+
148145 {name : "success allowed endpoints" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
146+ hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
147+ iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
148+ dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : false },
149+
150+ {name : "success allowed endpoints CI Test" , args : args {ctxCancelDuration : 2 , configFilePath : "./testfiles/agent-allowed-endpoints.json" ,
151+ 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" ,
149156 hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
150157 iptables : nil , nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
151158 dockerDaemonConfigPath : createTempFileWithContents ("{}" ), ciTestOnly : true }, wantErr : false },
159+
152160 {name : "dns failure" , args : args {ctxCancelDuration : 5 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServerWithError {},
153161 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
154162 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : true },
163+
155164 {name : "cmd failure" , args : args {ctxCancelDuration : 5 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
156165 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNflogger {}, cmd : & MockCommandWithError {}, resolvdConfigPath : createTempFileWithContents ("" ),
157166 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : true },
167+
158168 {name : "nflog failure" , args : args {ctxCancelDuration : 5 , configFilePath : "./testfiles/agent.json" , hostDNSServer : & mockDNSServer {}, dockerDNSServer : & mockDNSServer {},
159169 iptables : & Firewall {& MockIPTables {}}, nflog : & MockAgentNfloggerWithErr {}, cmd : & MockCommand {}, resolvdConfigPath : createTempFileWithContents ("" ),
160170 dockerDaemonConfigPath : createTempFileWithContents ("{}" )}, wantErr : true },
0 commit comments