2023-05-16 15:02:33 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2024-05-01 08:22:35 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-05-16 15:02:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-05-01 08:22:35 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-05-16 15:02:33 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-05-01 08:22:35 +00:00
|
|
|
func TestParseResourcesStateWithNoFile(t *testing.T) {
|
|
|
|
b := &bundle.Bundle{
|
2024-09-27 10:03:05 +00:00
|
|
|
BundleRootPath: t.TempDir(),
|
2024-05-01 08:22:35 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "whatever",
|
|
|
|
Terraform: &config.Terraform{
|
|
|
|
ExecPath: "terraform",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := ParseResourcesState(context.Background(), b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &resourcesState{Version: SupportedStateVersion}, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseResourcesStateWithExistingStateFile(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
b := &bundle.Bundle{
|
2024-09-27 10:03:05 +00:00
|
|
|
BundleRootPath: t.TempDir(),
|
2024-05-01 08:22:35 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "whatever",
|
|
|
|
Terraform: &config.Terraform{
|
|
|
|
ExecPath: "terraform",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cacheDir, err := Dir(ctx, b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
data := []byte(`{
|
|
|
|
"version": 4,
|
|
|
|
"unknown_field": "hello",
|
|
|
|
"resources": [
|
|
|
|
{
|
|
|
|
"mode": "managed",
|
|
|
|
"type": "databricks_pipeline",
|
|
|
|
"name": "test_pipeline",
|
|
|
|
"provider": "provider[\"registry.terraform.io/databricks/databricks\"]",
|
|
|
|
"instances": [
|
|
|
|
{
|
|
|
|
"schema_version": 0,
|
|
|
|
"attributes": {
|
|
|
|
"allow_duplicate_names": false,
|
|
|
|
"catalog": null,
|
|
|
|
"channel": "CURRENT",
|
|
|
|
"cluster": [],
|
|
|
|
"random_field": "random_value",
|
|
|
|
"configuration": {
|
|
|
|
"bundle.sourcePath": "/Workspace//Users/user/.bundle/test/dev/files/src"
|
|
|
|
},
|
|
|
|
"continuous": false,
|
|
|
|
"development": true,
|
|
|
|
"edition": "ADVANCED",
|
|
|
|
"filters": [],
|
|
|
|
"id": "123",
|
|
|
|
"library": [],
|
|
|
|
"name": "test_pipeline",
|
|
|
|
"notification": [],
|
|
|
|
"photon": false,
|
|
|
|
"serverless": false,
|
|
|
|
"storage": "dbfs:/123456",
|
|
|
|
"target": "test_dev",
|
|
|
|
"timeouts": null,
|
|
|
|
"url": "https://test.com"
|
|
|
|
},
|
|
|
|
"sensitive_attributes": []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
err = os.WriteFile(filepath.Join(cacheDir, TerraformStateFileName), data, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
state, err := ParseResourcesState(ctx, b)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expected := &resourcesState{
|
|
|
|
Version: 4,
|
|
|
|
Resources: []stateResource{
|
|
|
|
{
|
|
|
|
Mode: "managed",
|
|
|
|
Type: "databricks_pipeline",
|
|
|
|
Name: "test_pipeline",
|
|
|
|
Instances: []stateResourceInstance{
|
|
|
|
{Attributes: stateInstanceAttributes{ID: "123"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, state)
|
|
|
|
}
|