-
Notifications
You must be signed in to change notification settings - Fork 17
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
193 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,97 @@ | ||
package storage | ||
|
||
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 Storage struct { | ||
Data | ||
filename string | ||
capacityMain int | ||
capacityTesting int | ||
} | ||
|
||
func New(filename string, capacityMain, capacityTesting int) Storage { | ||
s := Storage{ | ||
filename: filename, | ||
capacityMain: capacityMain, | ||
capacityTesting: capacityTesting, | ||
} | ||
s.Deployments = make([]deployment.Deployment, 0) | ||
s.Version = "1" | ||
return s | ||
|
||
} | ||
|
||
// DeploymentInsert inserts a deployment and return an evicted | ||
// deployment because the capacity has been reached. | ||
func (s *Storage) 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 *Storage) DeploymentList() []deployment.Deployment { | ||
return s.Deployments | ||
} | ||
|
||
func (s *Storage) LastDeployment() (ok bool, d deployment.Deployment) { | ||
if len(s.DeploymentList()) > 1 { | ||
return true, s.DeploymentList()[0] | ||
} | ||
return | ||
} | ||
|
||
func (s *Storage) 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 *Storage) 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 storage | ||
|
||
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) | ||
} |