Ver código fonte

integrate TravisCI

Eugene Yakubovich 9 anos atrás
pai
commit
9d942e2552
6 arquivos alterados com 124 adições e 6 exclusões
  1. 9 0
      .travis.yml
  2. 2 0
      README.md
  3. 21 0
      build
  4. 36 0
      build-check
  5. 6 6
      iptables/iptables.go
  6. 50 0
      test

+ 9 - 0
.travis.yml

@@ -0,0 +1,9 @@
+language: go
+go:
+  - 1.4
+
+install:
+ - go get golang.org/x/tools/cmd/cover
+
+script:
+ - ./build-check

+ 2 - 0
README.md

@@ -1,5 +1,7 @@
 # go-iptables
 
+[![Build Status](https://travis-ci.org/coreos/go-iptables.png?branch=master)](https://travis-ci.org/coreos/go-iptables)
+
 Go bindings for iptables utility.
 
 In-kernel netfilter does not have a good userspace API. The tables are manipulated via setsockopt that sets/replaces the entire table. Changes to existing table need to be resolved by userspace code which is difficult and error-prone. Netfilter developers heavily advocate using iptables utlity for programmatic manipulation.

+ 21 - 0
build

@@ -0,0 +1,21 @@
+#!/bin/bash -e
+
+ORG_PATH="github.com/coreos"
+REPO_PATH="${ORG_PATH}/go-iptables"
+
+if [ ! -h gopath/src/${REPO_PATH} ]; then
+	mkdir -p gopath/src/${ORG_PATH}
+	ln -s ../../../.. gopath/src/${REPO_PATH} || exit 255
+fi
+
+export GOBIN=${PWD}/bin
+export GOPATH=${PWD}/gopath
+
+eval $(go env)
+
+if [ ${GOOS} = "linux" ]; then
+	echo "Building go-iptables..."
+	go build ${REPO_PATH}/iptables
+else
+	echo "Not on Linux"
+fi

+ 36 - 0
build-check

@@ -0,0 +1,36 @@
+#!/bin/bash -e
+#
+# Run all go-iptables tests
+#   ./test
+#   ./test -v
+#
+# Run tests for one package
+#   PKG=./unit ./test
+#   PKG=ssh ./test
+#
+
+# Invoke ./cover for HTML output
+COVER=${COVER:-"-cover"}
+
+source ./build
+
+FORMATTABLE="iptables"
+
+# user has not provided PKG override
+if [ -z "$PKG" ]; then
+	FMT=$FORMATTABLE
+
+# user has provided PKG override
+else
+	# strip out slashes and dots from PKG=./foo/
+	FMT=${PKG//\//}
+fi
+
+echo "Checking gofmt..."
+fmtRes=$(gofmt -l $FMT)
+if [ -n "${fmtRes}" ]; then
+	echo -e "gofmt checking failed:\n${fmtRes}"
+	exit 255
+fi
+
+echo "Success"

+ 6 - 6
iptables/iptables.go

@@ -53,7 +53,7 @@ func New() (*IPTables, error) {
 }
 
 // Exists checks if given rulespec in specified table/chain exists
-func (ipt *IPTables) Exists(table, chain string, rulespec...string) (bool, error) {
+func (ipt *IPTables) Exists(table, chain string, rulespec ...string) (bool, error) {
 	checkPresent, err := getIptablesHasCheckCommand()
 	if err != nil {
 		log.Printf("Error checking iptables version, assuming version at least 1.4.11: %v", err)
@@ -114,8 +114,8 @@ func (ipt *IPTables) Delete(table, chain string, rulespec ...string) error {
 func (ipt *IPTables) List(table, chain string) ([]string, error) {
 	var stdout, stderr bytes.Buffer
 	cmd := exec.Cmd{
-		Path: ipt.path,
-		Args: []string{ipt.path, "-t", table, "-S", chain},
+		Path:   ipt.path,
+		Args:   []string{ipt.path, "-t", table, "-S", chain},
 		Stdout: &stdout,
 		Stderr: &stderr,
 	}
@@ -158,11 +158,11 @@ func (ipt *IPTables) DeleteChain(table, chain string) error {
 	return ipt.run("-t", table, "-X", chain)
 }
 
-func (ipt *IPTables) run(args... string) error {
+func (ipt *IPTables) run(args ...string) error {
 	var stderr bytes.Buffer
 	cmd := exec.Cmd{
-		Path: ipt.path,
-		Args: append([]string{ipt.path}, args...),
+		Path:   ipt.path,
+		Args:   append([]string{ipt.path}, args...),
 		Stderr: &stderr,
 	}
 

+ 50 - 0
test

@@ -0,0 +1,50 @@
+#!/bin/bash -e
+#
+# Run all go-iptables tests
+#   ./test
+#   ./test -v
+#
+# Run tests for one package
+#   PKG=./unit ./test
+#   PKG=ssh ./test
+#
+
+# Invoke ./cover for HTML output
+COVER=${COVER:-"-cover"}
+
+source ./build
+
+TESTABLE="iptables"
+FORMATTABLE="$TESTABLE"
+
+# user has not provided PKG override
+if [ -z "$PKG" ]; then
+	TEST=$TESTABLE
+	FMT=$FORMATTABLE
+
+# user has provided PKG override
+else
+	# strip out slashes and dots from PKG=./foo/
+	TEST=${PKG//\//}
+	TEST=${TEST//./}
+
+	# only run gofmt on packages provided by user
+	FMT="$TEST"
+fi
+
+# split TEST into an array and prepend REPO_PATH to each local package
+split=(${TEST// / })
+TEST=${split[@]/#/${REPO_PATH}/}
+
+echo "Running tests..."
+go test -i ${TEST}
+go test ${COVER} $@ ${TEST}
+
+echo "Checking gofmt..."
+fmtRes=$(gofmt -l $FMT)
+if [ -n "${fmtRes}" ]; then
+	echo -e "gofmt checking failed:\n${fmtRes}"
+	exit 255
+fi
+
+echo "Success"