github.com/databricks/cli@v0.203.0/bundle/deploy/terraform/util_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 "testing/iotest" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 type mockedReader struct { 13 content string 14 } 15 16 func (r *mockedReader) Read(p []byte) (n int, err error) { 17 content := []byte(r.content) 18 n = copy(p, content) 19 return n, io.EOF 20 } 21 22 func TestLocalStateIsNewer(t *testing.T) { 23 local := &mockedReader{content: ` 24 { 25 "serial": 5 26 } 27 `} 28 remote := &mockedReader{content: ` 29 { 30 "serial": 4 31 } 32 `} 33 34 stale := IsLocalStateStale(local, remote) 35 36 assert.False(t, stale) 37 } 38 39 func TestLocalStateIsOlder(t *testing.T) { 40 local := &mockedReader{content: ` 41 { 42 "serial": 5 43 } 44 `} 45 remote := &mockedReader{content: ` 46 { 47 "serial": 6 48 } 49 `} 50 51 stale := IsLocalStateStale(local, remote) 52 assert.True(t, stale) 53 } 54 55 func TestLocalStateIsTheSame(t *testing.T) { 56 local := &mockedReader{content: ` 57 { 58 "serial": 5 59 } 60 `} 61 remote := &mockedReader{content: ` 62 { 63 "serial": 5 64 } 65 `} 66 67 stale := IsLocalStateStale(local, remote) 68 assert.False(t, stale) 69 } 70 71 func TestLocalStateMarkStaleWhenFailsToLoad(t *testing.T) { 72 local := iotest.ErrReader(fmt.Errorf("Random error")) 73 remote := &mockedReader{content: ` 74 { 75 "serial": 5 76 } 77 `} 78 79 stale := IsLocalStateStale(local, remote) 80 assert.True(t, stale) 81 } 82 83 func TestLocalStateMarkNonStaleWhenRemoteFailsToLoad(t *testing.T) { 84 local := &mockedReader{content: ` 85 { 86 "serial": 5 87 } 88 `} 89 remote := iotest.ErrReader(fmt.Errorf("Random error")) 90 91 stale := IsLocalStateStale(local, remote) 92 assert.False(t, stale) 93 }