22
33This directory contains end-to-end tests for the migration script. Each object type has its own folder with test configuration.
44
5- ## Test Approach
6-
7- The test validates the migration script by:
8- 1 . Creating test objects on Snowflake
9- 2 . Fetching objects via data source and generating a CSV
10- 3 . Running the migration script to generate Terraform code with import blocks
11- 4 . Applying the generated code (which imports existing objects)
12- 5 . ** Verifying the plan is empty** (no changes needed = successful import)
13-
145## Quick Start
156
167### Step 1: Navigate to object type folder
@@ -22,183 +13,73 @@ cd grants # or schemas, warehouses, users, etc.
2213### Step 2: Clean up any previous state
2314
2415``` bash
25- rm -rf .terraform terraform.tfstate terraform.tfstate.backup generated_output .tf
16+ rm -rf .terraform terraform.tfstate terraform.tfstate.backup import/.terraform import/terraform.tfstate import/terraform.tfstate.backup import/main .tf
2617```
2718
28- ### Step 3: Initialize Terraform
19+ ### Step 3: Initialize and create test objects
2920
3021``` bash
3122terraform init
32- ```
33-
34- ### Step 4: Create test objects AND generate CSV
35-
36- ``` bash
3723terraform apply -auto-approve
3824```
3925
4026This creates objects from ` objects_def.tf ` and generates ` objects.csv ` via ` datasource.tf ` .
4127
42- ### Step 5 : Run migration script
28+ ### Step 4 : Run migration script (output to import directory)
4329
4430``` bash
45- go run github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/scripts/migration_script@main -import=block grants < objects.csv > generated_output.tf
31+ go run github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/scripts/migration_script@dev \
32+ -import=block grants < objects.csv > import/main.tf
4633```
4734
48- ### Step 6: Apply generated output (imports existing objects)
35+ ### Step 5: Import in the separate directory
4936
5037``` bash
51- terraform apply -auto-approve
38+ cd import
39+ terraform init
40+ terraform apply
5241```
5342
54- This imports the existing Snowflake objects into Terraform state.
43+ This imports the existing Snowflake objects into a fresh state.
5544
56- ### Step 7 : Verify plan is empty
45+ ### Step 6 : Verify plan is empty
5746
5847``` bash
5948terraform plan
6049```
6150
62- ** Success criteria:** The plan should show "No changes. Your infrastructure matches the configuration."
63-
64- If there are changes, the migration script generated incorrect values.
65-
66- ### Step 8: Cleanup (when done)
51+ ### Step 7: Cleanup
6752
6853``` bash
69- terraform destroy -auto-approve
70- ```
71-
72- ## Directory Structure
73-
74- ```
75- manual_tests/
76- ├── README.md # This file
77- ├── users/ # Users test
78- │ ├── objects_def.tf # Creates test users on Snowflake
79- │ ├── datasource.tf # Fetches users, generates CSV
80- │ ├── objects.csv # Generated CSV (after terraform apply)
81- │ └── generated_output.tf # Generated by migration script
82- ├── schemas/
83- ├── warehouses/
84- ├── grants/
85- └── <new_object_type>/ # Add new object types here
54+ cd ..
55+ terraform destroy
8656```
8757
88- ## Adding a New Object Type
58+ ## Test Assertions
8959
90- To add tests for a new object type (e.g., ` warehouses ` ):
60+ The ` datasource.tf ` includes a ** precondition assertion ** that fails if no grants are found. This prevents generating an empty CSV that would cause silent failures.
9161
92- ### Step 1: Create the folder
93-
94- ``` bash
95- mkdir -p warehouses
96- ```
97-
98- ### Step 2: Create ` objects_def.tf `
99-
100- Create test objects with various configurations:
62+ ### How it works
10163
10264``` hcl
103- terraform {
104- required_providers {
105- snowflake = {
106- source = "snowflakedb/snowflake"
65+ resource "local_file" "grants_csv" {
66+ # ...
67+ lifecycle {
68+ precondition {
69+ condition = length(local.grants_csv_rows_unique) > 0
70+ error_message = "TEST ASSERTION FAILED: No grants found. Make sure objects_def.tf resources were created first."
10771 }
10872 }
10973}
110-
111- provider "snowflake" {}
112-
113- # Basic warehouse
114- resource "snowflake_warehouse" "basic" {
115- name = "MIGRATION_TEST_WH_BASIC"
116- }
117-
118- # Warehouse with all parameters
119- resource "snowflake_warehouse" "complete" {
120- name = "MIGRATION_TEST_WH_COMPLETE"
121- comment = "Test warehouse for migration"
122- warehouse_size = "XSMALL"
123- # ... more parameters
124- }
12574```
12675
127- ** Important naming convention: ** Use ` MIGRATION_TEST_ ` prefix for all test objects.
76+ ### Testing the assertion
12877
129- ### Step 3: Create ` datasource.tf `
130-
131- Fetch the objects and generate CSV:
132-
133- ``` hcl
134- # Fetch test warehouses
135- data "snowflake_warehouses" "test_warehouses" {
136- like = "MIGRATION_TEST_WH_%"
137- }
138-
139- locals {
140- # Flatten the data source output
141- warehouses_flattened = [
142- for wh in data.snowflake_warehouses.test_warehouses.warehouses :
143- wh.show_output[0]
144- ]
145-
146- # Create CSV header
147- csv_header = length(local.warehouses_flattened) > 0 ? join(",", [
148- for key in keys(local.warehouses_flattened[0]) : "\"${key}\""
149- ]) : ""
150-
151- # CSV escape function
152- csv_escape = length(local.warehouses_flattened) > 0 ? {
153- for wh in local.warehouses_flattened :
154- wh.name => {
155- for key in keys(local.warehouses_flattened[0]) :
156- key => replace(
157- replace(
158- replace(tostring(lookup(wh, key, "")), "\\", "\\\\"),
159- "\n", "\\n"
160- ),
161- "\"", "\"\""
162- )
163- }
164- } : {}
165-
166- # Create CSV rows
167- csv_rows = length(local.warehouses_flattened) > 0 ? [
168- for wh in local.warehouses_flattened :
169- join(",", [
170- for key in keys(local.warehouses_flattened[0]) :
171- "\"${local.csv_escape[wh.name][key]}\""
172- ])
173- ] : []
174-
175- csv_content = join("\n", concat([local.csv_header], local.csv_rows))
176- }
177-
178- # Write CSV file
179- resource "local_file" "csv" {
180- content = local.csv_content
181- filename = "${path.module}/objects.csv"
182- }
183-
184- # Debug outputs
185- output "objects_found" {
186- value = length(local.warehouses_flattened)
187- }
188- ```
189-
190- ### Step 4: Test it
78+ To verify the assertion works correctly, use the ` test_assertion/ ` subdirectory:
19179
19280``` bash
193- cd warehouses
194- rm -rf .terraform terraform.tfstate* generated_output.tf
81+ cd test_assertion
19582terraform init
196- terraform apply -auto-approve
197-
198- cd ..
199- go run . -import=block warehouses < manual_tests/warehouses/objects.csv > manual_tests/warehouses/generated_output.tf
200-
201- cd manual_tests/warehouses
202- terraform apply -auto-approve
203- terraform plan # Should show no changes!
83+ terraform apply
84+ # Expected error: "TEST ASSERTION FAILED: No grants found..."
20485```
0 commit comments