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

Do not truncate local state file when pulling remote changes #382

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion bundle/deploy/terraform/state_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ func (l *statePull) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutat
}

// Expect the state file to live under dir.
local, err := os.OpenFile(filepath.Join(dir, TerraformStateFileName), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
local, err := os.OpenFile(filepath.Join(dir, TerraformStateFileName), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}
defer local.Close()

if !IsLocalStateStale(local, remote) {
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
log.Infof(ctx, "Local state is the same or newer, ignoring remote state")
return nil, nil
}

// Truncating the file before writing
local.Truncate(0)
local.Seek(0, 0)

// Write file to disk.
log.Infof(ctx, "Writing remote state file to local cache directory")
Expand Down
38 changes: 38 additions & 0 deletions bundle/deploy/terraform/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package terraform

import (
"encoding/json"
"io"
)

type state struct {
Serial int `json:"serial"`
}

func IsLocalStateStale(local io.Reader, remote io.Reader) bool {
localState, err := loadState(local)
if err != nil {
return true
}

remoteState, err := loadState(remote)
if err != nil {
return false
}

return localState.Serial < remoteState.Serial
}

func loadState(input io.Reader) (*state, error) {
content, err := io.ReadAll(input)
if err != nil {
return nil, err
}
var s state
err = json.Unmarshal(content, &s)
if err != nil {
return nil, err
}

return &s, nil
}
93 changes: 93 additions & 0 deletions bundle/deploy/terraform/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package terraform

import (
"fmt"
"io"
"testing"
"testing/iotest"

"github.com/stretchr/testify/assert"
)

type mockedReader struct {
content string
}
andrewnester marked this conversation as resolved.
Show resolved Hide resolved

func (r *mockedReader) Read(p []byte) (n int, err error) {
content := []byte(r.content)
n = copy(p, content)
return n, io.EOF
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is implemented by https://pkg.go.dev/strings#NewReader, FWIW. Feel free to address async.


func TestLocalStateIsNewer(t *testing.T) {
local := &mockedReader{content: `
{
"serial": 5
}
`}
remote := &mockedReader{content: `
{
"serial": 4
}
`}

stale := IsLocalStateStale(local, remote)

assert.False(t, stale)
}

func TestLocalStateIsOlder(t *testing.T) {
local := &mockedReader{content: `
{
"serial": 5
}
`}
remote := &mockedReader{content: `
{
"serial": 6
}
`}

stale := IsLocalStateStale(local, remote)
assert.True(t, stale)
}

func TestLocalStateIsTheSame(t *testing.T) {
local := &mockedReader{content: `
{
"serial": 5
}
`}
remote := &mockedReader{content: `
{
"serial": 5
}
`}

stale := IsLocalStateStale(local, remote)
assert.False(t, stale)
}

func TestLocalStateMarkStaleWhenFailsToLoad(t *testing.T) {
local := iotest.ErrReader(fmt.Errorf("Random error"))
remote := &mockedReader{content: `
{
"serial": 5
}
`}

stale := IsLocalStateStale(local, remote)
assert.True(t, stale)
}

func TestLocalStateMarkNonStaleWhenRemoteFailsToLoad(t *testing.T) {
local := &mockedReader{content: `
{
"serial": 5
}
`}
remote := iotest.ErrReader(fmt.Errorf("Random error"))

stale := IsLocalStateStale(local, remote)
assert.False(t, stale)
}