|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package decoder_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "compress/gzip" |
| 9 | + "context" |
| 10 | + "io" |
| 11 | + "io/fs" |
| 12 | + "log" |
| 13 | + "path" |
| 14 | + "path/filepath" |
| 15 | + "sync" |
| 16 | + "testing" |
| 17 | + "testing/fstest" |
| 18 | + |
| 19 | + "github.com/hashicorp/go-version" |
| 20 | + "github.com/hashicorp/hcl-lang/decoder" |
| 21 | + "github.com/hashicorp/hcl-lang/lang" |
| 22 | + lsctx "github.com/hashicorp/terraform-ls/internal/context" |
| 23 | + fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder" |
| 24 | + "github.com/hashicorp/terraform-ls/internal/features/modules/jobs" |
| 25 | + "github.com/hashicorp/terraform-ls/internal/features/modules/state" |
| 26 | + globalState "github.com/hashicorp/terraform-ls/internal/state" |
| 27 | + tfmod "github.com/hashicorp/terraform-schema/module" |
| 28 | +) |
| 29 | + |
| 30 | +type RootReaderMock struct{} |
| 31 | + |
| 32 | +func (r RootReaderMock) InstalledModuleCalls(modPath string) (map[string]tfmod.InstalledModuleCall, error) { |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (r RootReaderMock) TerraformVersion(modPath string) *version.Version { |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestDecoder_CodeLensesForFile_concurrencyBug(t *testing.T) { |
| 41 | + globalStore, err := globalState.NewStateStore() |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + ss, err := state.NewModuleStore(globalStore.ProviderSchemas, globalStore.RegistryModules, globalStore.ChangeStore) |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + |
| 50 | + logger := log.New(io.Discard, "", 0) |
| 51 | + testCfg := `data "terraform_remote_state" "vpc" { } |
| 52 | +` |
| 53 | + dirNames := []string{"testdir1", "testdir2"} |
| 54 | + |
| 55 | + mapFs := fstest.MapFS{} |
| 56 | + for _, dirName := range dirNames { |
| 57 | + mapFs[dirName] = &fstest.MapFile{Mode: fs.ModeDir} |
| 58 | + mapFs[path.Join(dirName, "main.tf")] = &fstest.MapFile{Data: []byte(testCfg)} |
| 59 | + mapFs[filepath.Join(dirName, "main.tf")] = &fstest.MapFile{Data: []byte(testCfg)} |
| 60 | + } |
| 61 | + |
| 62 | + ctx := context.Background() |
| 63 | + |
| 64 | + dataDir := "data" |
| 65 | + schemasFs := fstest.MapFS{ |
| 66 | + dataDir: &fstest.MapFile{Mode: fs.ModeDir}, |
| 67 | + dataDir + "/terraform.io": &fstest.MapFile{Mode: fs.ModeDir}, |
| 68 | + dataDir + "/terraform.io/builtin": &fstest.MapFile{Mode: fs.ModeDir}, |
| 69 | + dataDir + "/terraform.io/builtin/terraform": &fstest.MapFile{Mode: fs.ModeDir}, |
| 70 | + dataDir + "/terraform.io/builtin/terraform/1.0.0": &fstest.MapFile{Mode: fs.ModeDir}, |
| 71 | + dataDir + "/terraform.io/builtin/terraform/1.0.0/schema.json.gz": &fstest.MapFile{ |
| 72 | + Data: gzipCompressBytes(t, []byte(tfSchemaJSON)), |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, dirName := range dirNames { |
| 77 | + err := ss.Add(dirName) |
| 78 | + if err != nil { |
| 79 | + t.Error(err) |
| 80 | + } |
| 81 | + ctx = lsctx.WithDocumentContext(ctx, lsctx.Document{}) |
| 82 | + err = jobs.ParseModuleConfiguration(ctx, mapFs, ss, dirName) |
| 83 | + if err != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + err = jobs.LoadModuleMetadata(ctx, ss, dirName) |
| 87 | + if err != nil { |
| 88 | + t.Error(err) |
| 89 | + } |
| 90 | + err = jobs.PreloadEmbeddedSchema(ctx, logger, schemasFs, ss, globalStore.ProviderSchemas, dirName) |
| 91 | + if err != nil { |
| 92 | + t.Error(err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + d := decoder.NewDecoder(&fdecoder.PathReader{ |
| 97 | + StateReader: ss, |
| 98 | + RootReader: RootReaderMock{}, |
| 99 | + }) |
| 100 | + |
| 101 | + var wg sync.WaitGroup |
| 102 | + for _, dirName := range dirNames { |
| 103 | + dirName := dirName |
| 104 | + wg.Add(1) |
| 105 | + go func(t *testing.T) { |
| 106 | + defer wg.Done() |
| 107 | + _, err := d.CodeLensesForFile(ctx, lang.Path{ |
| 108 | + Path: dirName, |
| 109 | + LanguageID: "terraform", |
| 110 | + }, "main.tf") |
| 111 | + if err != nil { |
| 112 | + t.Error(err) |
| 113 | + } |
| 114 | + }(t) |
| 115 | + } |
| 116 | + wg.Wait() |
| 117 | +} |
| 118 | + |
| 119 | +func gzipCompressBytes(t *testing.T, b []byte) []byte { |
| 120 | + var compressedBytes bytes.Buffer |
| 121 | + gw := gzip.NewWriter(&compressedBytes) |
| 122 | + _, err := gw.Write(b) |
| 123 | + if err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + err = gw.Close() |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + return compressedBytes.Bytes() |
| 131 | +} |
| 132 | + |
| 133 | +var tfSchemaJSON = `{ |
| 134 | + "format_version": "1.0", |
| 135 | + "provider_schemas": { |
| 136 | + "terraform.io/builtin/terraform": { |
| 137 | + "data_source_schemas": { |
| 138 | + "terraform_remote_state": { |
| 139 | + "version": 0, |
| 140 | + "block": { |
| 141 | + "attributes": { |
| 142 | + "backend": { |
| 143 | + "type": "string", |
| 144 | + "description": "The remote backend to use, e.g. remote or http.", |
| 145 | + "description_kind": "markdown", |
| 146 | + "required": true |
| 147 | + }, |
| 148 | + "config": { |
| 149 | + "type": "dynamic", |
| 150 | + "description": "The configuration of the remote backend. Although this is optional, most backends require some configuration.\n\nThe object can use any arguments that would be valid in the equivalent terraform { backend \"\u003cTYPE\u003e\" { ... } } block.", |
| 151 | + "description_kind": "markdown", |
| 152 | + "optional": true |
| 153 | + }, |
| 154 | + "defaults": { |
| 155 | + "type": "dynamic", |
| 156 | + "description": "Default values for outputs, in case the state file is empty or lacks a required output.", |
| 157 | + "description_kind": "markdown", |
| 158 | + "optional": true |
| 159 | + }, |
| 160 | + "outputs": { |
| 161 | + "type": "dynamic", |
| 162 | + "description": "An object containing every root-level output in the remote state.", |
| 163 | + "description_kind": "markdown", |
| 164 | + "computed": true |
| 165 | + }, |
| 166 | + "workspace": { |
| 167 | + "type": "string", |
| 168 | + "description": "The Terraform workspace to use, if the backend supports workspaces.", |
| 169 | + "description_kind": "markdown", |
| 170 | + "optional": true |
| 171 | + } |
| 172 | + }, |
| 173 | + "description_kind": "plain" |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | +}` |
0 commit comments