Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Refactor to use cf filelock lib
Browse files Browse the repository at this point in the history
[#155992950]

Signed-off-by: Christian Ang <cang@pivotal.io>
  • Loading branch information
Aidan Obley authored and christianang committed Mar 28, 2018
1 parent bd4ceed commit e965a01
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 373 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@
[submodule "vendor/github.com/pivotal-cf-experimental/gomegamatchers"]
path = vendor/github.com/pivotal-cf-experimental/gomegamatchers
url = https://github.com/pivotal-cf-experimental/gomegamatchers.git
[submodule "vendor/code.cloudfoundry.org/filelock"]
path = vendor/code.cloudfoundry.org/filelock
url = https://github.com/cloudfoundry/filelock
4 changes: 2 additions & 2 deletions cmd/silk-cni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/validator.v2"

"code.cloudfoundry.org/cf-networking-helpers/json_client"
"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/lager"

"code.cloudfoundry.org/silk/cni/adapter"
Expand All @@ -20,7 +21,6 @@ import (
"code.cloudfoundry.org/silk/daemon"
libAdapter "code.cloudfoundry.org/silk/lib/adapter"
"code.cloudfoundry.org/silk/lib/datastore"
"code.cloudfoundry.org/silk/lib/filelock"
"code.cloudfoundry.org/silk/lib/serial"
"github.com/containernetworking/cni/pkg/invoke"
"github.com/containernetworking/cni/pkg/skel"
Expand Down Expand Up @@ -65,7 +65,7 @@ func main() {
}
store := &datastore.Store{
Serializer: &serial.Serial{},
Locker: &filelock.Locker{},
LockerNew: filelock.NewLocker,
}

plugin := &CNIPlugin{
Expand Down
4 changes: 2 additions & 2 deletions cmd/silk-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.cloudfoundry.org/cf-networking-helpers/metrics"
"code.cloudfoundry.org/cf-networking-helpers/mutualtls"
"code.cloudfoundry.org/debugserver"
"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/silk/client/config"
"code.cloudfoundry.org/silk/controller"
Expand All @@ -22,7 +23,6 @@ import (
"code.cloudfoundry.org/silk/daemon/vtep"
"code.cloudfoundry.org/silk/lib/adapter"
"code.cloudfoundry.org/silk/lib/datastore"
"code.cloudfoundry.org/silk/lib/filelock"
"code.cloudfoundry.org/silk/lib/serial"

"github.com/cloudfoundry/dropsonde"
Expand Down Expand Up @@ -96,7 +96,7 @@ func mainWithError() error {

store := &datastore.Store{
Serializer: &serial.Serial{},
Locker: &filelock.Locker{},
LockerNew: filelock.NewLocker,
}

_, overlayNetwork, err := net.ParseCIDR(cfg.OverlayNetwork)
Expand Down
23 changes: 18 additions & 5 deletions lib/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import (
"fmt"
"net"

"code.cloudfoundry.org/silk/lib/filelock"
"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/silk/lib/serial"
)

//go:generate counterfeiter -o ../fakes/file_locker.go --fake-name FileLocker . FileLocker
type FileLocker interface {
filelock.FileLocker
}

//go:generate counterfeiter -o ../fakes/locked_file.go --fake-name LockedFile . LockedFile
type LockedFile interface {
filelock.LockedFile
}

//go:generate counterfeiter -o ../fakes/datastore.go --fake-name Datastore . Datastore
type Datastore interface {
Add(handle, ip string, metadata map[string]interface{}) error
Expand All @@ -23,7 +33,7 @@ type Container struct {

type Store struct {
Serializer serial.Serializer
Locker filelock.FileLocker
LockerNew func(filePath string) filelock.FileLocker
}

func validate(handle, ip string) error {
Expand All @@ -42,7 +52,8 @@ func (c *Store) Add(filePath, handle, ip string, metadata map[string]interface{}
return err
}

file, err := c.Locker.Open(filePath)
locker := c.LockerNew(filePath)
file, err := locker.Open()
if err != nil {
return fmt.Errorf("open lock: %s", err)
}
Expand Down Expand Up @@ -74,7 +85,8 @@ func (c *Store) Delete(filePath, handle string) (Container, error) {
return deleted, fmt.Errorf("invalid handle")
}

file, err := c.Locker.Open(filePath)
locker := c.LockerNew(filePath)
file, err := locker.Open()
if err != nil {
return deleted, fmt.Errorf("open lock: %s", err)
}
Expand All @@ -98,7 +110,8 @@ func (c *Store) Delete(filePath, handle string) (Container, error) {
}

func (c *Store) ReadAll(filePath string) (map[string]Container, error) {
file, err := c.Locker.Open(filePath)
locker := c.LockerNew(filePath)
file, err := locker.Open()
if err != nil {
return nil, fmt.Errorf("open lock: %s", err)
}
Expand Down
6 changes: 2 additions & 4 deletions lib/datastore/datastore_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"sync/atomic"

"code.cloudfoundry.org/cf-networking-helpers/testsupport"
"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/silk/lib/datastore"
"code.cloudfoundry.org/silk/lib/serial"

"code.cloudfoundry.org/silk/lib/filelock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -39,12 +38,11 @@ var _ = Describe("Datastore Lifecycle", func() {
Expect(err).NotTo(HaveOccurred())
filepath = file.Name()

locker := &filelock.Locker{}
serializer := &serial.Serial{}

store = &datastore.Store{
Serializer: serializer,
Locker: locker,
LockerNew: filelock.NewLocker,
}
})

Expand Down
28 changes: 19 additions & 9 deletions lib/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"os"

"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/silk/lib/datastore"

libfakes "code.cloudfoundry.org/silk/lib/fakes"
Expand All @@ -19,8 +20,11 @@ var _ = Describe("Datastore", func() {
store *datastore.Store
metadata map[string]interface{}

serializer *libfakes.Serializer
locker *libfakes.FileLocker
serializer *libfakes.Serializer
locker *libfakes.FileLocker
lockerNewCallCount int
lockerNewFilePath string

lockedFile *os.File
filePath string
)
Expand All @@ -41,24 +45,30 @@ var _ = Describe("Datastore", func() {

store = &datastore.Store{
Serializer: serializer,
Locker: locker,
LockerNew: func(filePath string) filelock.FileLocker {
lockerNewCallCount++
lockerNewFilePath = filePath
return locker
},
}

lockedFile = &os.File{}
locker.OpenReturns(lockedFile, nil)

lockerNewCallCount = 0
})

Context("when adding an entry to store", func() {
It("deserializes the data from the file", func() {
err := store.Add(filePath, handle, ip, metadata)
Expect(err).NotTo(HaveOccurred())

Expect(lockerNewCallCount).To(Equal(1))
Expect(lockerNewFilePath).To(Equal(filePath))
Expect(locker.OpenCallCount()).To(Equal(1))
Expect(serializer.DecodeAllCallCount()).To(Equal(1))
Expect(serializer.EncodeAndOverwriteCallCount()).To(Equal(1))

Expect(locker.OpenArgsForCall(0)).To(Equal(filePath))

file, _ := serializer.DecodeAllArgsForCall(0)
Expect(file).To(Equal(lockedFile))

Expand Down Expand Up @@ -125,12 +135,12 @@ var _ = Describe("Datastore", func() {
_, err := store.Delete(filePath, handle)
Expect(err).NotTo(HaveOccurred())

Expect(lockerNewCallCount).To(Equal(1))
Expect(lockerNewFilePath).To(Equal(filePath))
Expect(locker.OpenCallCount()).To(Equal(1))
Expect(serializer.DecodeAllCallCount()).To(Equal(1))
Expect(serializer.EncodeAndOverwriteCallCount()).To(Equal(1))

Expect(locker.OpenArgsForCall(0)).To(Equal(filePath))

file, _ := serializer.DecodeAllArgsForCall(0)
Expect(file).To(Equal(lockedFile))

Expand Down Expand Up @@ -191,12 +201,12 @@ var _ = Describe("Datastore", func() {
Expect(err).NotTo(HaveOccurred())
Expect(data).NotTo(BeNil())

Expect(lockerNewCallCount).To(Equal(1))
Expect(lockerNewFilePath).To(Equal(filePath))
Expect(locker.OpenCallCount()).To(Equal(1))
Expect(serializer.DecodeAllCallCount()).To(Equal(1))
Expect(serializer.EncodeAndOverwriteCallCount()).To(Equal(0))

Expect(locker.OpenArgsForCall(0)).To(Equal(filePath))

file, _ := serializer.DecodeAllArgsForCall(0)
Expect(file).To(Equal(lockedFile))
})
Expand Down
8 changes: 6 additions & 2 deletions lib/fakes/datastore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 23 additions & 29 deletions lib/fakes/file_locker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e965a01

Please sign in to comment.