|
| 1 | +package logging_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/hashicorp/terraform-plugin-log/tfsdklog" |
| 10 | + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging" |
| 12 | +) |
| 13 | + |
| 14 | +func TestInitContext(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + var output bytes.Buffer |
| 18 | + |
| 19 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 20 | + |
| 21 | + // Simulate root logger fields that would have been associated by |
| 22 | + // terraform-plugin-go prior to the InitContext() call. |
| 23 | + ctx = tfsdklog.With(ctx, "tf_rpc", "GetProviderSchema") |
| 24 | + ctx = tfsdklog.With(ctx, "tf_req_id", "123-testing-123") |
| 25 | + |
| 26 | + ctx = logging.InitContext(ctx) |
| 27 | + |
| 28 | + logging.HelperSchemaTrace(ctx, "test message") |
| 29 | + |
| 30 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 34 | + } |
| 35 | + |
| 36 | + expectedEntries := []map[string]interface{}{ |
| 37 | + { |
| 38 | + "@level": "trace", |
| 39 | + "@message": "test message", |
| 40 | + "@module": "sdk.helper_schema", |
| 41 | + "tf_rpc": "GetProviderSchema", |
| 42 | + "tf_req_id": "123-testing-123", |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 47 | + t.Errorf("unexpected difference: %s", diff) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestTestNameContext(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + var output bytes.Buffer |
| 55 | + |
| 56 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 57 | + |
| 58 | + // InitTestContext messes with the standard library log package, which |
| 59 | + // we want to avoid in this unit testing. Instead, just create the |
| 60 | + // helper_resource subsystem and avoid the other InitTestContext logic. |
| 61 | + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) |
| 62 | + |
| 63 | + ctx = logging.TestNameContext(ctx, "TestTestTest") |
| 64 | + |
| 65 | + logging.HelperResourceTrace(ctx, "test message") |
| 66 | + |
| 67 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 71 | + } |
| 72 | + |
| 73 | + expectedEntries := []map[string]interface{}{ |
| 74 | + { |
| 75 | + "@level": "trace", |
| 76 | + "@message": "test message", |
| 77 | + "@module": "sdk.helper_resource", |
| 78 | + "test_name": "TestTestTest", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 83 | + t.Errorf("unexpected difference: %s", diff) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestTestStepNumberContext(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + var output bytes.Buffer |
| 91 | + |
| 92 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 93 | + |
| 94 | + // InitTestContext messes with the standard library log package, which |
| 95 | + // we want to avoid in this unit testing. Instead, just create the |
| 96 | + // helper_resource subsystem and avoid the other InitTestContext logic. |
| 97 | + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) |
| 98 | + |
| 99 | + ctx = logging.TestStepNumberContext(ctx, 123) |
| 100 | + |
| 101 | + logging.HelperResourceTrace(ctx, "test message") |
| 102 | + |
| 103 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + expectedEntries := []map[string]interface{}{ |
| 110 | + { |
| 111 | + "@level": "trace", |
| 112 | + "@message": "test message", |
| 113 | + "@module": "sdk.helper_resource", |
| 114 | + "test_step_number": float64(123), // float64 due to default JSON unmarshalling |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 119 | + t.Errorf("unexpected difference: %s", diff) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestTestTerraformPathContext(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + |
| 126 | + var output bytes.Buffer |
| 127 | + |
| 128 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 129 | + |
| 130 | + // InitTestContext messes with the standard library log package, which |
| 131 | + // we want to avoid in this unit testing. Instead, just create the |
| 132 | + // helper_resource subsystem and avoid the other InitTestContext logic. |
| 133 | + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) |
| 134 | + |
| 135 | + ctx = logging.TestTerraformPathContext(ctx, "/usr/local/bin/terraform") |
| 136 | + |
| 137 | + logging.HelperResourceTrace(ctx, "test message") |
| 138 | + |
| 139 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 140 | + |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 143 | + } |
| 144 | + |
| 145 | + expectedEntries := []map[string]interface{}{ |
| 146 | + { |
| 147 | + "@level": "trace", |
| 148 | + "@message": "test message", |
| 149 | + "@module": "sdk.helper_resource", |
| 150 | + "test_terraform_path": "/usr/local/bin/terraform", |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 155 | + t.Errorf("unexpected difference: %s", diff) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestTestWorkingDirectoryContext(t *testing.T) { |
| 160 | + t.Parallel() |
| 161 | + |
| 162 | + var output bytes.Buffer |
| 163 | + |
| 164 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 165 | + |
| 166 | + // InitTestContext messes with the standard library log package, which |
| 167 | + // we want to avoid in this unit testing. Instead, just create the |
| 168 | + // helper_resource subsystem and avoid the other InitTestContext logic. |
| 169 | + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) |
| 170 | + |
| 171 | + ctx = logging.TestWorkingDirectoryContext(ctx, "/tmp/test") |
| 172 | + |
| 173 | + logging.HelperResourceTrace(ctx, "test message") |
| 174 | + |
| 175 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 176 | + |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 179 | + } |
| 180 | + |
| 181 | + expectedEntries := []map[string]interface{}{ |
| 182 | + { |
| 183 | + "@level": "trace", |
| 184 | + "@message": "test message", |
| 185 | + "@module": "sdk.helper_resource", |
| 186 | + "test_working_directory": "/tmp/test", |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 191 | + t.Errorf("unexpected difference: %s", diff) |
| 192 | + } |
| 193 | +} |
0 commit comments