github.com/opentofu/opentofu@v1.7.1/internal/tofu/eval_import_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package tofu 7 8 import ( 9 "testing" 10 11 "github.com/davecgh/go-spew/spew" 12 "github.com/hashicorp/hcl/v2" 13 "github.com/hashicorp/hcl/v2/hcltest" 14 "github.com/opentofu/opentofu/internal/lang" 15 "github.com/opentofu/opentofu/internal/lang/marks" 16 "github.com/zclconf/go-cty/cty" 17 ) 18 19 func TestEvaluateImportIdExpression_SensitiveValue(t *testing.T) { 20 ctx := &MockEvalContext{} 21 ctx.installSimpleEval() 22 ctx.EvaluationScopeScope = &lang.Scope{} 23 24 testCases := []struct { 25 name string 26 expr hcl.Expression 27 wantErr string 28 }{ 29 { 30 name: "sensitive_value", 31 expr: hcltest.MockExprLiteral(cty.StringVal("value").Mark(marks.Sensitive)), 32 wantErr: "Invalid import id argument: The import ID cannot be sensitive.", 33 }, 34 { 35 name: "expr_is_nil", 36 expr: nil, 37 wantErr: "Invalid import id argument: The import ID cannot be null.", 38 }, 39 { 40 name: "evaluates_to_null", 41 expr: hcltest.MockExprLiteral(cty.NullVal(cty.String)), 42 wantErr: "Invalid import id argument: The import ID cannot be null.", 43 }, 44 { 45 name: "evaluates_to_unknown", 46 expr: hcltest.MockExprLiteral(cty.UnknownVal(cty.String)), 47 wantErr: "Invalid import id argument: The import block \"id\" argument depends on resource attributes that cannot be determined until apply, so OpenTofu cannot plan to import this resource.", // Adapted the message from your original code 48 }, 49 { 50 name: "valid_value", 51 expr: hcltest.MockExprLiteral(cty.StringVal("value")), 52 wantErr: "", 53 }, 54 } 55 56 for _, tc := range testCases { 57 t.Run(tc.name, func(t *testing.T) { 58 _, diags := evaluateImportIdExpression(tc.expr, ctx, EvalDataForNoInstanceKey) 59 60 if tc.wantErr != "" { 61 if len(diags) != 1 { 62 t.Errorf("expected diagnostics, got %s", spew.Sdump(diags)) 63 } else if diags.Err().Error() != tc.wantErr { 64 t.Errorf("unexpected error diagnostic %s", diags.Err().Error()) 65 } 66 } else { 67 if len(diags) != 0 { 68 t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) 69 } 70 } 71 }) 72 } 73 }