Explorar el Código

Error handling: fix up code from sanbornm, add tests

Fixes: #35
Casey Callendrello hace 7 años
padre
commit
fa79f602b7
Se han modificado 2 ficheros con 38 adiciones y 1 borrados
  1. 1 1
      iptables/iptables.go
  2. 37 0
      iptables/iptables_test.go

+ 1 - 1
iptables/iptables.go

@@ -324,7 +324,7 @@ func (ipt *IPTables) runWithOutput(args []string, stdout io.Writer) error {
 	if err := cmd.Run(); err != nil {
 		switch e := err.(type) {
 		case *exec.ExitError:
-			return &Error{*e, stderr.String()}
+			return &Error{*e, cmd, stderr.String()}
 		default:
 			return err
 		}

+ 37 - 0
iptables/iptables_test.go

@@ -18,6 +18,7 @@ import (
 	"crypto/rand"
 	"fmt"
 	"math/big"
+	"os"
 	"reflect"
 	"testing"
 )
@@ -305,3 +306,39 @@ func runRulesTests(t *testing.T, ipt *IPTables) {
 		t.Fatalf("Failed to delete test chain: %v", err)
 	}
 }
+
+// TestError checks that we're OK when iptables fails to execute
+func TestError(t *testing.T) {
+	ipt, err := New()
+	if err != nil {
+		t.Fatalf("failed to init: %v", err)
+	}
+
+	chain := randChain(t)
+	_, err = ipt.List("filter", chain)
+	if err == nil {
+		t.Fatalf("no error with invalid params")
+	}
+	switch e := err.(type) {
+	case *Error:
+		// OK
+	default:
+		t.Fatalf("expected type iptables.Error, got %t", e)
+	}
+
+	// Now set an invalid binary path
+	ipt.path = "/does-not-exist"
+
+	_, err = ipt.ListChains("filter")
+
+	if err == nil {
+		t.Fatalf("no error with invalid ipt binary")
+	}
+
+	switch e := err.(type) {
+	case *os.PathError:
+		// OK
+	default:
+		t.Fatalf("expected type os.PathError, got %t", e)
+	}
+}