File 3864-ssh-extend-ssh_cth-hooks-to-report-updated-known_hos.patch of Package erlang

From 715d08fd731c0982c04843024aedb1812e7a6171 Mon Sep 17 00:00:00 2001
From: Hans Nilsson <hans@erlang.org>
Date: Tue, 25 May 2021 16:54:33 +0200
Subject: [PATCH 4/4] ssh: extend ssh_cth hooks to report updated known_hosts

---
 lib/ssh/test/ssh_cth.erl | 72 +++++++++++++++++++++++++++++++---------
 1 file changed, 56 insertions(+), 16 deletions(-)

diff --git a/lib/ssh/test/ssh_cth.erl b/lib/ssh/test/ssh_cth.erl
index 09d863cc51..caa4b55ce6 100644
--- a/lib/ssh/test/ssh_cth.erl
+++ b/lib/ssh/test/ssh_cth.erl
@@ -5,13 +5,18 @@
          pre_init_per_suite/3,
          pre_end_per_suite/3,
          pre_init_per_group/4,
+         post_init_per_group/5,
          pre_end_per_group/4,
          post_end_per_group/5,
-         pre_init_per_testcase/3,
-         pre_end_per_testcase/4
+         pre_init_per_testcase/4,
+         post_init_per_testcase/5,
+         pre_end_per_testcase/4,
+         post_end_per_testcase/5
         ]).
 
 -record(c, {
+          known_hosts_file_name = filename:join(os:getenv("HOME"), ".ssh/known_hosts"),
+          known_hosts_last_contents = <<>>,
           suite, % suite name
           groups = [], % Group path in reversed order
           n % test case number
@@ -23,33 +28,58 @@ id(Opts) ->
 init(_Id, _Opts) ->
     {ok, #c{n=1}}.
 
-pre_init_per_suite(Suite, Config, CTHState) ->
-    {Config, CTHState#c{suite=Suite}}.
+pre_init_per_suite(Suite, Config, State0) ->
+    {_, State} = read_known_hosts_diff(State0#c{suite=Suite}),
+    {Config, State}.
 
 pre_end_per_suite(Suite, Config, State) ->
     ct:pal("BEGIN ~p:end_per_suite(...)", [Suite]),
-     {Config, State}.
+    {Config, State}.
+
 
+pre_init_per_group(Suite, Group, Config, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sBEGIN ~p:init_per_group(~p,...)", [log_diff(Diff),Suite,Group]),
+    {Config, State#c{groups = (State#c.groups ++ [Group])}}.
 
-pre_init_per_group(_Suite, Group, Config ,State) ->
-     {Config, State#c{groups = (State#c.groups ++ [Group])}}.
+post_init_per_group(Suite, Group, _Config, Return, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sEND ~p:init_per_group(~p,...)", [log_diff(Diff),Suite,Group]),
+    {Return, State}.
 
-pre_end_per_group(Suite, Group, Config, State) ->
-    ct:pal("BEGIN ~p:end_per_group(~p,...)", [Suite,Group]),
+pre_end_per_group(Suite, Group, Config, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sBEGIN ~p:end_per_group(~p,...)", [log_diff(Diff), Suite, Group]),
     {Config, State}.
 
-post_end_per_group(_Suite, Group, _Config, Return, State) ->
-     {Return, State#c{groups = lists:reverse(lists:reverse(State#c.groups)--[Group])}}.
+post_end_per_group(Suite, Group, _Config, Return, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sEND ~p:end_per_group(~p,...)", [log_diff(Diff),Suite,Group]),
+    {Return, State#c{groups = lists:reverse(lists:reverse(State#c.groups)--[Group])}}.
 
 
-pre_init_per_testcase(SuiteName, Config, CTHState) ->
-    ct:pal("########## ~p ~p ~s ~p~n", [CTHState#c.suite, CTHState#c.n, groups(Config), SuiteName]),
-    {Config, CTHState#c{n = CTHState#c.n + 1}}.
+pre_init_per_testcase(SuiteName, _TC, Config, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~s########## ~p ~p ~s ~p", [log_diff(Diff), State0#c.suite, State0#c.n, groups(Config), SuiteName]),
+    {Config, State#c{n = State#c.n + 1}}.
 
-pre_end_per_testcase(Suite,TC,Config,State) ->
-    ct:pal("BEGIN ~p:end_per_testcase(~p,...)", [Suite,TC]),
+post_init_per_testcase(SuiteName, TestcaseName, _Config, Return, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~send ~p:init_per_testcase(~p,...)", [log_diff(Diff), SuiteName, TestcaseName]),
+    {Return, State}.
+
+pre_end_per_testcase(Suite, TC, Config, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sBEGIN ~p:end_per_testcase(~p,...)", [log_diff(Diff), Suite, TC]),
     {Config, State}.
 
+post_end_per_testcase(SuiteName, TC, _Config, Return, State0) ->
+    {Diff, State} = read_known_hosts_diff(State0),
+    ct:pal("~sEND ~p:end_per_testcase(~p,...)", [log_diff(Diff), SuiteName, TC]),
+    {Return, State}.
+
+
+
 
 groups(Config) ->
     F = fun(X) -> io_lib:format("~w",[X]) end,
@@ -62,3 +92,13 @@ get_groups(Config) ->
              is_list(L),
              {name,Name} <- L].
     
+
+
+read_known_hosts_diff(S = #c{known_hosts_file_name = File,
+                             known_hosts_last_contents = Bin0}) ->
+    {ok,  <<Bin0:(size(Bin0))/binary, Diff/binary>> = Bin} = file:read_file(File),
+    {Diff, S#c{known_hosts_last_contents = Bin}}.
+
+log_diff(<<>>) -> "";
+log_diff(Bin) -> io_lib:format("~n++++ ~p~n",[Bin]).
+    
-- 
2.26.2

openSUSE Build Service is sponsored by