2023-11-24 11:15:46 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-02-08 15:18:53 +00:00
|
|
|
mockfiler "github.com/databricks/cli/internal/mocks/libs/filer"
|
2023-11-24 11:15:46 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-08 15:18:53 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2023-11-24 11:15:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func mockStateFilerForPush(t *testing.T, fn func(body io.Reader)) filer.Filer {
|
2024-02-08 15:18:53 +00:00
|
|
|
f := mockfiler.NewMockFiler(t)
|
|
|
|
f.
|
2023-11-24 11:15:46 +00:00
|
|
|
EXPECT().
|
2024-02-08 15:18:53 +00:00
|
|
|
Write(mock.Anything, mock.Anything, mock.Anything, filer.CreateParentDirectories, filer.OverwriteIfExists).
|
|
|
|
Run(func(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode) {
|
2023-11-24 11:15:46 +00:00
|
|
|
fn(reader)
|
|
|
|
}).
|
|
|
|
Return(nil).
|
|
|
|
Times(1)
|
2024-02-08 15:18:53 +00:00
|
|
|
return f
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func statePushTestBundle(t *testing.T) *bundle.Bundle {
|
|
|
|
return &bundle.Bundle{
|
2024-03-27 09:03:24 +00:00
|
|
|
RootPath: t.TempDir(),
|
2023-11-24 11:15:46 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "default",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatePush(t *testing.T) {
|
|
|
|
mock := mockStateFilerForPush(t, func(body io.Reader) {
|
|
|
|
dec := json.NewDecoder(body)
|
|
|
|
var contents map[string]int
|
|
|
|
err := dec.Decode(&contents)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, map[string]int{"serial": 4}, contents)
|
|
|
|
})
|
|
|
|
|
|
|
|
m := &statePush{
|
|
|
|
identityFiler(mock),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
b := statePushTestBundle(t)
|
|
|
|
|
|
|
|
// Write a stale local state file.
|
|
|
|
writeLocalState(t, ctx, b, map[string]int{"serial": 4})
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(ctx, b, m)
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|