|
| 1 | +//go:build !race |
| 2 | +// +build !race |
| 3 | + |
| 4 | +// Copyright (c) HashiCorp, Inc. |
| 5 | +// SPDX-License-Identifier: BUSL-1.1 |
| 6 | + |
| 7 | +package command |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/hashicorp/cli" |
| 17 | + testing_command "github.com/hashicorp/terraform/internal/command/testing" |
| 18 | + "github.com/hashicorp/terraform/internal/command/views" |
| 19 | + "github.com/hashicorp/terraform/internal/terminal" |
| 20 | + "github.com/zclconf/go-cty/cty" |
| 21 | +) |
| 22 | + |
| 23 | +// The test contains a data race due to the disabling of the provider lock. |
| 24 | +// The provider lock was disabled, so that we can measure the true duration of the |
| 25 | +// test operation. Without disabling the provider lock, runs may block each other |
| 26 | +// when working with the provider, which does not happen by default in the real-world. |
| 27 | +func TestTest_ParallelJSON(t *testing.T) { |
| 28 | + td := t.TempDir() |
| 29 | + testCopyDir(t, testFixturePath(path.Join("test", "parallel")), td) |
| 30 | + defer testChdir(t, td)() |
| 31 | + |
| 32 | + provider := testing_command.NewProvider(&testing_command.ResourceStore{ |
| 33 | + Data: make(map[string]cty.Value), |
| 34 | + Nolock: true, |
| 35 | + }) |
| 36 | + providerSource, close := newMockProviderSource(t, map[string][]string{ |
| 37 | + "test": {"1.0.0"}, |
| 38 | + }) |
| 39 | + defer close() |
| 40 | + |
| 41 | + streams, done := terminal.StreamsForTesting(t) |
| 42 | + view := views.NewView(streams) |
| 43 | + ui := new(cli.MockUi) |
| 44 | + |
| 45 | + meta := Meta{ |
| 46 | + testingOverrides: metaOverridesForProvider(provider.Provider), |
| 47 | + Ui: ui, |
| 48 | + View: view, |
| 49 | + Streams: streams, |
| 50 | + ProviderSource: providerSource, |
| 51 | + } |
| 52 | + |
| 53 | + init := &InitCommand{Meta: meta} |
| 54 | + if code := init.Run(nil); code != 0 { |
| 55 | + output := done(t) |
| 56 | + t.Fatalf("expected status code %d but got %d: %s", 9, code, output.All()) |
| 57 | + } |
| 58 | + |
| 59 | + c := &TestCommand{Meta: meta} |
| 60 | + c.Run([]string{"-json", "-no-color"}) |
| 61 | + output := done(t).All() |
| 62 | + |
| 63 | + if !strings.Contains(output, "40 passed, 0 failed") { |
| 64 | + t.Errorf("output didn't produce the right output:\n\n%s", output) |
| 65 | + } |
| 66 | + |
| 67 | + // Split the log into lines |
| 68 | + lines := strings.Split(output, "\n") |
| 69 | + |
| 70 | + // Find the start of the teardown and complete timestamps |
| 71 | + // The difference is the approximate duration of the test teardown operation. |
| 72 | + // This test is running in parallel, so we expect the teardown to also run in parallel. |
| 73 | + // We sleep for 3 seconds in the test teardown to simulate a long-running destroy. |
| 74 | + // There are 6 unique state keys in the parallel test, so we expect the teardown to take less than 3*6 (18) seconds. |
| 75 | + var startTimestamp, completeTimestamp string |
| 76 | + for _, line := range lines { |
| 77 | + if strings.Contains(line, `{"path":"parallel.tftest.hcl","progress":"teardown"`) { |
| 78 | + var obj map[string]interface{} |
| 79 | + if err := json.Unmarshal([]byte(line), &obj); err == nil { |
| 80 | + if ts, ok := obj["@timestamp"].(string); ok { |
| 81 | + startTimestamp = ts |
| 82 | + } |
| 83 | + } |
| 84 | + } else if strings.Contains(line, `{"path":"parallel.tftest.hcl","progress":"complete"`) { |
| 85 | + var obj map[string]interface{} |
| 86 | + if err := json.Unmarshal([]byte(line), &obj); err == nil { |
| 87 | + if ts, ok := obj["@timestamp"].(string); ok { |
| 88 | + completeTimestamp = ts |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if startTimestamp == "" || completeTimestamp == "" { |
| 95 | + t.Fatalf("could not find start or complete timestamp in log output") |
| 96 | + } |
| 97 | + |
| 98 | + startTime, err := time.Parse(time.RFC3339Nano, startTimestamp) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("failed to parse start timestamp: %v", err) |
| 101 | + } |
| 102 | + completeTime, err := time.Parse(time.RFC3339Nano, completeTimestamp) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("failed to parse complete timestamp: %v", err) |
| 105 | + } |
| 106 | + dur := completeTime.Sub(startTime) |
| 107 | + if dur > 10*time.Second { |
| 108 | + t.Fatalf("parallel.tftest.hcl duration took too long: %0.2f seconds", dur.Seconds()) |
| 109 | + } |
| 110 | +} |
0 commit comments