File size: 2,928 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
defmodule Plausible.InstallationSupport.CheckTest do
  use Plausible.DataCase, async: true

  @moduletag :ee_only

  on_ee do
    import ExUnit.CaptureLog
    import Plausible.AssertMatches
    alias Plausible.InstallationSupport.{State, Verification}

    @moduletag :capture_log

    test "a check that raises" do
      defmodule FaultyCheckRaise do
        use Plausible.InstallationSupport.Check

        @impl true
        def report_progress_as, do: "Faulty check"

        @impl true
        def perform(_, _), do: raise("boom")
      end

      state = %State{
        url: "https://example.com",
        report_to: self(),
        diagnostics: %Verification.Diagnostics{}
      }

      {result, log} =
        with_log(fn ->
          FaultyCheckRaise.perform_safe(state, [])
        end)

      assert log =~
               ~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckRaise on https://example.com: %RuntimeError{message: "boom"}|

      assert_matches %Verification.Diagnostics{
                       service_error: %{
                         code: :internal_check_error,
                         extra: %RuntimeError{message: "boom"}
                       }
                     } =
                       result.diagnostics
    end

    test "a check that throws" do
      defmodule FaultyCheckThrow do
        use Plausible.InstallationSupport.Check

        @impl true
        def report_progress_as, do: "Faulty check"

        @impl true
        def perform(_, _), do: :erlang.throw(:boom)
      end

      state = %State{
        url: "https://example.com",
        report_to: self(),
        diagnostics: %Verification.Diagnostics{}
      }

      {result, log} =
        with_log(fn ->
          FaultyCheckThrow.perform_safe(state, [])
        end)

      assert log =~
               ~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckThrow on https://example.com: :boom|

      assert_matches %Verification.Diagnostics{
                       service_error: %{code: :internal_check_error, extra: :boom}
                     } = result.diagnostics
    end

    test "a check that times out" do
      defmodule FaultyCheckTimeout do
        use Plausible.InstallationSupport.Check

        @impl true
        def report_progress_as, do: "Faulty check"

        @impl true
        def perform(_, _), do: :timer.sleep(500)
      end

      state = %State{
        url: "https://example.com",
        report_to: self(),
        diagnostics: %Verification.Diagnostics{}
      }

      result = FaultyCheckTimeout.perform_safe(state, timeout: 100)

      assert_matches %Verification.Diagnostics{
                       service_error: %{
                         code: :internal_check_timeout,
                         extra: "FaultyCheckTimeout timed out after 100ms"
                       }
                     } = result.diagnostics
    end
  end
end