Extract inode inspection functions into testhelpers

This commit is contained in:
Gabe Rosenhouse 2016-04-17 18:35:49 -07:00
parent 5d932e4716
commit 54d7f73092
2 changed files with 28 additions and 27 deletions

View File

@ -22,28 +22,12 @@ import (
"os/exec"
"path/filepath"
"golang.org/x/sys/unix"
"github.com/appc/cni/pkg/ns"
"github.com/appc/cni/pkg/testhelpers"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func getInode(path string) (uint64, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
return getInodeF(file)
}
func getInodeF(file *os.File) (uint64, error) {
stat := &unix.Stat_t{}
err := unix.Fstat(int(file.Fd()), stat)
return stat.Ino, err
}
const CurrentNetNS = "/proc/self/ns/net"
var _ = Describe("Linux namespace operations", func() {
@ -81,13 +65,13 @@ var _ = Describe("Linux namespace operations", func() {
})
It("executes the callback within the target network namespace", func() {
expectedInode, err := getInode(targetNetNSPath)
expectedInode, err := testhelpers.GetInode(targetNetNSPath)
Expect(err).NotTo(HaveOccurred())
var actualInode uint64
var innerErr error
err = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
actualInode, innerErr = getInode(CurrentNetNS)
actualInode, innerErr = testhelpers.GetInode(CurrentNetNS)
return nil
})
Expect(err).NotTo(HaveOccurred())
@ -97,13 +81,13 @@ var _ = Describe("Linux namespace operations", func() {
})
It("provides the original namespace as the argument to the callback", func() {
hostNSInode, err := getInode(CurrentNetNS)
hostNSInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
var inputNSInode uint64
var innerErr error
err = ns.WithNetNS(targetNetNS, false, func(inputNS *os.File) error {
inputNSInode, err = getInodeF(inputNS)
inputNSInode, err = testhelpers.GetInodeF(inputNS)
return nil
})
Expect(err).NotTo(HaveOccurred())
@ -113,7 +97,7 @@ var _ = Describe("Linux namespace operations", func() {
})
It("restores the calling thread to the original network namespace", func() {
preTestInode, err := getInode(CurrentNetNS)
preTestInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
err = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
@ -121,7 +105,7 @@ var _ = Describe("Linux namespace operations", func() {
})
Expect(err).NotTo(HaveOccurred())
postTestInode, err := getInode(CurrentNetNS)
postTestInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
Expect(postTestInode).To(Equal(preTestInode))
@ -129,14 +113,14 @@ var _ = Describe("Linux namespace operations", func() {
Context("when the callback returns an error", func() {
It("restores the calling thread to the original namespace before returning", func() {
preTestInode, err := getInode(CurrentNetNS)
preTestInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
_ = ns.WithNetNS(targetNetNS, false, func(*os.File) error {
return errors.New("potato")
})
postTestInode, err := getInode(CurrentNetNS)
postTestInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
Expect(postTestInode).To(Equal(preTestInode))
@ -152,10 +136,10 @@ var _ = Describe("Linux namespace operations", func() {
Describe("validating inode mapping to namespaces", func() {
It("checks that different namespaces have different inodes", func() {
hostNSInode, err := getInode(CurrentNetNS)
hostNSInode, err := testhelpers.GetInode(CurrentNetNS)
Expect(err).NotTo(HaveOccurred())
testNsInode, err := getInode(targetNetNSPath)
testNsInode, err := testhelpers.GetInode(targetNetNSPath)
Expect(err).NotTo(HaveOccurred())
Expect(hostNSInode).NotTo(Equal(0))

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package testhelpers provides common support behavior for tests
package testhelpers
import (
@ -24,6 +26,21 @@ import (
. "github.com/onsi/gomega"
)
func GetInode(path string) (uint64, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
return GetInodeF(file)
}
func GetInodeF(file *os.File) (uint64, error) {
stat := &unix.Stat_t{}
err := unix.Fstat(int(file.Fd()), stat)
return stat.Ino, err
}
func MakeNetworkNS(containerID string) string {
namespace := "/var/run/netns/" + containerID
pid := unix.Getpid()