-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
|
||
"github.com/nlewo/comin/internal/deployment" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Data struct { | ||
Version string `json:"version"` | ||
// Deployments are order from the most recent to older | ||
Deployments []deployment.Deployment `json:"deployments"` | ||
} | ||
|
||
type Store struct { | ||
Data | ||
filename string | ||
capacityMain int | ||
capacityTesting int | ||
} | ||
|
||
func New(filename string, capacityMain, capacityTesting int) Store { | ||
s := Store{ | ||
filename: filename, | ||
capacityMain: capacityMain, | ||
capacityTesting: capacityTesting, | ||
} | ||
s.Deployments = make([]deployment.Deployment, 0) | ||
s.Version = "1" | ||
return s | ||
|
||
} | ||
|
||
func (s *Store) DeploymentInsertAndCommit(dpl deployment.Deployment) { | ||
s.DeploymentInsert(dpl) | ||
if err := s.Commit(); err != nil { | ||
logrus.Errorf("Error while commiting the state.json file: %s", err) | ||
} | ||
} | ||
|
||
// DeploymentInsert inserts a deployment and return an evicted | ||
// deployment because the capacity has been reached. | ||
func (s *Store) DeploymentInsert(dpl deployment.Deployment) (getsEvicted bool, evicted deployment.Deployment) { | ||
var qty, older int | ||
capacity := s.capacityMain | ||
if dpl.IsTesting() { | ||
capacity = s.capacityTesting | ||
} | ||
for i, d := range s.Deployments { | ||
if dpl.IsTesting() == d.IsTesting() { | ||
older = i | ||
qty += 1 | ||
} | ||
} | ||
// If the capacity is reached, we remove the older elements | ||
if qty >= capacity { | ||
evicted = s.Deployments[older] | ||
getsEvicted = true | ||
s.Deployments = append(s.Deployments[:older], s.Deployments[older+1:]...) | ||
} | ||
s.Deployments = append([]deployment.Deployment{dpl}, s.Deployments...) | ||
return | ||
} | ||
|
||
func (s *Store) DeploymentList() []deployment.Deployment { | ||
return s.Deployments | ||
} | ||
|
||
func (s *Store) LastDeployment() (ok bool, d deployment.Deployment) { | ||
if len(s.DeploymentList()) > 1 { | ||
return true, s.DeploymentList()[0] | ||
} | ||
return | ||
} | ||
|
||
func (s *Store) Load() (err error) { | ||
var data Data | ||
content, err := os.ReadFile(s.filename) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return nil | ||
} else if err != nil { | ||
return | ||
} | ||
err = json.Unmarshal(content, &data) | ||
if err != nil { | ||
return | ||
} | ||
// FIXME: we should check the version | ||
s.Deployments = data.Deployments | ||
logrus.Infof("Loaded %d deployments from %s", len(s.Deployments), s.filename) | ||
return | ||
} | ||
|
||
func (s *Store) Commit() (err error) { | ||
content, err := json.Marshal(s) | ||
if err != nil { | ||
return | ||
} | ||
err = os.WriteFile(s.filename, content, 0644) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nlewo/comin/internal/deployment" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDeploymentCommitAndLoad(t *testing.T) { | ||
tmp := t.TempDir() | ||
filename := tmp + "/state.json" | ||
s := New(filename, 2, 2) | ||
err := s.Commit() | ||
assert.Nil(t, err) | ||
|
||
s1 := New(filename, 2, 2) | ||
err = s1.Load() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(s.Deployments)) | ||
|
||
s.DeploymentInsert(deployment.Deployment{UUID: "1", Operation: "switch"}) | ||
s.Commit() | ||
assert.Nil(t, err) | ||
|
||
s1 = New(filename, 2, 2) | ||
err = s1.Load() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(s.Deployments)) | ||
} | ||
|
||
func TestLastDeployment(t *testing.T) { | ||
s := New("", 2, 2) | ||
ok, _ := s.LastDeployment() | ||
assert.False(t, ok) | ||
s.DeploymentInsert(deployment.Deployment{UUID: "1", Operation: "switch"}) | ||
s.DeploymentInsert(deployment.Deployment{UUID: "2", Operation: "switch"}) | ||
ok, last := s.LastDeployment() | ||
assert.True(t, ok) | ||
assert.Equal(t, "2", last.UUID) | ||
} | ||
|
||
func TestDeploymentInsert(t *testing.T) { | ||
s := New("", 2, 2) | ||
var hasEvicted bool | ||
var evicted deployment.Deployment | ||
hasEvicted, _ = s.DeploymentInsert(deployment.Deployment{UUID: "1", Operation: "switch"}) | ||
assert.False(t, hasEvicted) | ||
hasEvicted, _ = s.DeploymentInsert(deployment.Deployment{UUID: "2", Operation: "switch"}) | ||
assert.False(t, hasEvicted) | ||
hasEvicted, evicted = s.DeploymentInsert(deployment.Deployment{UUID: "3", Operation: "switch"}) | ||
assert.True(t, hasEvicted) | ||
assert.Equal(t, "1", evicted.UUID) | ||
|
||
hasEvicted, _ = s.DeploymentInsert(deployment.Deployment{UUID: "4", Operation: "testing"}) | ||
assert.False(t, hasEvicted) | ||
hasEvicted, _ = s.DeploymentInsert(deployment.Deployment{UUID: "5", Operation: "testing"}) | ||
assert.False(t, hasEvicted) | ||
hasEvicted, evicted = s.DeploymentInsert(deployment.Deployment{UUID: "6", Operation: "testing"}) | ||
assert.True(t, hasEvicted) | ||
assert.Equal(t, "4", evicted.UUID) | ||
|
||
hasEvicted, evicted = s.DeploymentInsert(deployment.Deployment{UUID: "7", Operation: "switch"}) | ||
assert.True(t, hasEvicted) | ||
assert.Equal(t, "2", evicted.UUID) | ||
} |