2023-05-16 15:02:33 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-24 11:15:46 +00:00
|
|
|
"strings"
|
2023-05-16 15:02:33 +00:00
|
|
|
"testing"
|
|
|
|
"testing/iotest"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalStateIsNewer(t *testing.T) {
|
2023-11-24 11:15:46 +00:00
|
|
|
local := strings.NewReader(`{"serial": 5}`)
|
|
|
|
remote := strings.NewReader(`{"serial": 4}`)
|
|
|
|
assert.False(t, IsLocalStateStale(local, remote))
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStateIsOlder(t *testing.T) {
|
2023-11-24 11:15:46 +00:00
|
|
|
local := strings.NewReader(`{"serial": 5}`)
|
|
|
|
remote := strings.NewReader(`{"serial": 6}`)
|
|
|
|
assert.True(t, IsLocalStateStale(local, remote))
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStateIsTheSame(t *testing.T) {
|
2023-11-24 11:15:46 +00:00
|
|
|
local := strings.NewReader(`{"serial": 5}`)
|
|
|
|
remote := strings.NewReader(`{"serial": 5}`)
|
|
|
|
assert.False(t, IsLocalStateStale(local, remote))
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStateMarkStaleWhenFailsToLoad(t *testing.T) {
|
|
|
|
local := iotest.ErrReader(fmt.Errorf("Random error"))
|
2023-11-24 11:15:46 +00:00
|
|
|
remote := strings.NewReader(`{"serial": 5}`)
|
|
|
|
assert.True(t, IsLocalStateStale(local, remote))
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStateMarkNonStaleWhenRemoteFailsToLoad(t *testing.T) {
|
2023-11-24 11:15:46 +00:00
|
|
|
local := strings.NewReader(`{"serial": 5}`)
|
2023-05-16 15:02:33 +00:00
|
|
|
remote := iotest.ErrReader(fmt.Errorf("Random error"))
|
2023-11-24 11:15:46 +00:00
|
|
|
assert.False(t, IsLocalStateStale(local, remote))
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|