Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created packagestate module #579

Merged
merged 6 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packagestate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/goleak v1.1.12 // indirect
go.uber.org/multierr v1.8.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions packagestate/go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68=
github.com/open-telemetry/opamp-go v0.2.0 h1:dV7wTkG5XNiorU62N1CJPr3f5dM0PGEtUUBtvK+LEG0=
github.com/open-telemetry/opamp-go v0.2.0/go.mod h1:IMdeuHGVc5CjKSu5/oNV0o+UmiXuahoHvoZ4GOmAI9M=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -65,6 +71,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
63 changes: 63 additions & 0 deletions packagestate/mocks/mock_package_state_manager.go

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

24 changes: 17 additions & 7 deletions packagestate/packages_state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ import (
// CollectorPackageName is the name for the top level packages for this collector
const CollectorPackageName = "observiq-otel-collector"

// PackagesStateManager manages state on disk via a JSON file
type PackagesStateManager struct {
// PackageStateManager tracks Package states
type PackageStateManager interface {
// LoadStatuses retrieves the previously saved PackagesStatuses.
// If none were saved returns error
LoadStatuses() (*protobufs.PackageStatuses, error)

// SaveStatuses saves the given PackageStatuses
SaveStatuses(statuses *protobufs.PackageStatuses) error
}

// FilePackagesStateManager manages state on disk via a JSON file
type FilePackagesStateManager struct {
jsonPath string
logger *zap.Logger
}
Expand All @@ -49,16 +59,16 @@ type packageStates struct {
PackageStates map[string]*packageState `json:"package_states"`
}

// NewPackagesStateManager creates a new PackagesStateManager
func NewPackagesStateManager(logger *zap.Logger, jsonPath string) *PackagesStateManager {
return &PackagesStateManager{
// NewFilePackagesStateManager creates a new PackagesStateManager
func NewFilePackagesStateManager(logger *zap.Logger, jsonPath string) PackageStateManager {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpheps minor question: i know this ensures the implementation satisfies the interface, but what dictates whether or not we have that line ensuring that the implementation can be cast as the interface. cause i don't see that line near the top of this file like i do in some others

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the below syntax?

var Thing Interface = (*Implementation)(nil)

That's not required in Go. Sometimes it's useful if you don't have a "constructor" method that returns the interface or the interface you're implementing is an another package. It's a personal preference. I felt like it wasn't needed here because of the constructor.

return &FilePackagesStateManager{
jsonPath: filepath.Clean(jsonPath),
logger: logger,
}
}

// LoadStatuses retrieves the PackagesStatuses from a saved json file
func (p *PackagesStateManager) LoadStatuses() (*protobufs.PackageStatuses, error) {
func (p *FilePackagesStateManager) LoadStatuses() (*protobufs.PackageStatuses, error) {
p.logger.Debug("Loading package statuses")

statusesBytes, err := os.ReadFile(p.jsonPath)
Expand All @@ -75,7 +85,7 @@ func (p *PackagesStateManager) LoadStatuses() (*protobufs.PackageStatuses, error
}

// SaveStatuses saves the given PackageStatuses into a json file
func (p *PackagesStateManager) SaveStatuses(statuses *protobufs.PackageStatuses) error {
func (p *FilePackagesStateManager) SaveStatuses(statuses *protobufs.PackageStatuses) error {
p.logger.Debug("Saving package statuses")

// If there is any problem saving the new package statuses, make sure that we delete any existing file
Expand Down
4 changes: 2 additions & 2 deletions packagestate/packages_state_manager_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestLoadStatusesLinux(t *testing.T) {
cantReadJSON := filepath.Join(tmpDir, "noread.json")
os.WriteFile(cantReadJSON, nil, 0000)
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: cantReadJSON,
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestSaveStatusesLinux(t *testing.T) {
os.Chmod(tmpDir, 0400)
testJSON := filepath.Join(tmpDir, "test.json")
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: testJSON,
}
Expand Down
20 changes: 12 additions & 8 deletions packagestate/packages_state_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ func TestNewPackagesStateManager(t *testing.T) {
testFunc: func(t *testing.T) {
jsonPath := "test.json"
logger := zap.NewNop()
actual := NewPackagesStateManager(logger, jsonPath)
actual := NewFilePackagesStateManager(logger, jsonPath)

assert.Equal(t, logger, actual.logger)
assert.Equal(t, jsonPath, actual.jsonPath)
var expected PackageStateManager = &FilePackagesStateManager{
jsonPath: jsonPath,
logger: logger,
}

assert.Equal(t, expected, actual)
},
},
}
Expand All @@ -58,7 +62,7 @@ func TestLoadStatuses(t *testing.T) {
testFunc: func(t *testing.T) {
noExistJSON := "garbage.json"
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: noExistJSON,
}
Expand All @@ -74,7 +78,7 @@ func TestLoadStatuses(t *testing.T) {
testFunc: func(t *testing.T) {
badJSON := "testdata/package_statuses_bad.json"
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: badJSON,
}
Expand All @@ -98,7 +102,7 @@ func TestLoadStatuses(t *testing.T) {
allHash := []byte("hash")
allErrMsg := "whoops"
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: goodJSON,
}
Expand Down Expand Up @@ -144,7 +148,7 @@ func TestSaveStatuses(t *testing.T) {
tmpDir := t.TempDir()
testJSON := filepath.Join(tmpDir, "test.json")
logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: testJSON,
}
Expand Down Expand Up @@ -194,7 +198,7 @@ func TestSaveStatuses(t *testing.T) {
os.WriteFile(testJSON, nil, 0600)

logger := zap.NewNop()
p := &PackagesStateManager{
p := &FilePackagesStateManager{
logger: logger,
jsonPath: testJSON,
}
Expand Down