TestTimingsFromProvision uses a fake terraform binary which spits out expected log content. This log content is then used to usher the provisioning process along as if terraform has run, and consequently the timing data is extracted from the log content and validated against the expected values.
(t *testing.T)
| 23 | // This log content is then used to usher the provisioning process along as if terraform has run, and consequently |
| 24 | // the timing data is extracted from the log content and validated against the expected values. |
| 25 | func TestTimingsFromProvision(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | |
| 28 | cwd, err := os.Getwd() |
| 29 | require.NoError(t, err) |
| 30 | |
| 31 | // Given: a fake terraform bin that behaves as we expect it to. |
| 32 | fakeBin := filepath.Join(cwd, "testdata", "timings-aggregation/fake-terraform.sh") |
| 33 | |
| 34 | t.Log(fakeBin) |
| 35 | |
| 36 | ctx, api := setupProvisioner(t, &provisionerServeOptions{ |
| 37 | binaryPath: fakeBin, |
| 38 | }) |
| 39 | sess := configure(ctx, t, api, &proto.Config{}) |
| 40 | |
| 41 | ctx, cancel := context.WithTimeout(ctx, testutil.WaitLong) |
| 42 | t.Cleanup(cancel) |
| 43 | |
| 44 | var timings []*proto.Timing |
| 45 | |
| 46 | handleResponse := func(t *testing.T, stage string) { |
| 47 | t.Helper() |
| 48 | for { |
| 49 | select { |
| 50 | case <-ctx.Done(): |
| 51 | t.Fatal(ctx.Err()) |
| 52 | default: |
| 53 | } |
| 54 | |
| 55 | msg, err := sess.Recv() |
| 56 | require.NoError(t, err) |
| 57 | |
| 58 | if log := msg.GetLog(); log != nil { |
| 59 | t.Logf("%s: %s: %s", stage, log.Level.String(), log.Output) |
| 60 | continue |
| 61 | } |
| 62 | switch { |
| 63 | case msg.GetInit() != nil: |
| 64 | timings = append(timings, msg.GetInit().GetTimings()...) |
| 65 | case msg.GetPlan() != nil: |
| 66 | timings = append(timings, msg.GetPlan().GetTimings()...) |
| 67 | case msg.GetApply() != nil: |
| 68 | timings = append(timings, msg.GetApply().GetTimings()...) |
| 69 | case msg.GetGraph() != nil: |
| 70 | timings = append(timings, msg.GetGraph().GetTimings()...) |
| 71 | } |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // When: configured, our fake terraform will fake an init setup |
| 77 | err = sendInit(sess, testutil.CreateTar(t, nil)) |
| 78 | require.NoError(t, err) |
| 79 | handleResponse(t, "init") |
| 80 | |
| 81 | // When: a plan is executed in the provisioner, our fake terraform will be executed and will produce a |
| 82 | // state file and some log content. |
nothing calls this directly
no test coverage detected