aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssh/test/ssh_upgrade_SUITE.erl
blob: 20df865b557bdf8dc5feb3c83cf808ec2930a437 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2014-2015. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(ssh_upgrade_SUITE).

%% Note: This directive should only be used in test suites.
-compile(export_all).

-include_lib("common_test/include/ct.hrl").

-record(state, {
	  config,
	  root_dir,
	  server,
	  client,
	  connection,
	  soft
	 }).


%%%================================================================
%%%
%%% CommonTest callbacks
%%% 
suite() ->
    [{timetrap,{seconds,180}}].

all() -> 
    [
     minor_upgrade,
     major_upgrade
    ].

init_per_suite(Config0) ->
    case ct_release_test:init(Config0) of
	{skip, Reason} ->
	    {skip, Reason};
	Config ->
	    ssh:start(),
	    Config
    end.

end_per_suite(Config) ->
    ct_release_test:cleanup(Config),
    ssh:stop(),
    UserDir = ?config(priv_dir, Config),
    ssh_test_lib:clean_rsa(UserDir).

init_per_testcase(_TestCase, Config) ->
    Config.
end_per_testcase(_TestCase, Config) ->
    Config.

%%%================================================================
%%%
%%% Test cases
%%% 
major_upgrade(Config) when is_list(Config) ->
    ct_release_test:upgrade(ssh, major,{?MODULE, #state{config = Config}}, Config).

minor_upgrade(Config) when is_list(Config) ->
    ct_release_test:upgrade(ssh, minor,{?MODULE, #state{config = Config}}, Config).

%%%================================================================
%%%
%%% ct_release_test callbacks
%%% 

%%%----------------------------------------------------------------
%%% Initialyze system before upgrade test starts.
%%% Called by ct_release_test:upgrade/4
upgrade_init(CTData, State) -> 
    {ok, AppUp={_, _, Up, _Down}} = ct_release_test:get_appup(CTData, ssh),
    ct:log("AppUp: ~p", [AppUp]),
    ct:log("Up: ~p", [Up]),
    case Soft = is_soft(Up) of
	%% It is symmetrical, if upgrade is soft so is downgrade  
	true ->
	    setup_server_client(State#state{soft = Soft});
	false ->
	    State#state{soft = Soft}
    end.

%%%----------------------------------------------------------------
%%% Check that upgrade was successful
%%% Called by ct_release_test:upgrade/4
upgrade_upgraded(_, #state{soft=false} = State) ->
    test_hard(State, "upgrade");

upgrade_upgraded(_, State) -> 
    test_soft(State, "upgrade1").

%%%----------------------------------------------------------------
%%% Check that downgrade was successful.
%%% Called by ct_release_test:upgrade/4
upgrade_downgraded(_, #state{soft=false} = State) -> 
    test_hard(State, "downgrade");

upgrade_downgraded(_, #state{soft=true} = State) -> 
    test_soft(State, "downgrade1").
 
%%%================================================================
%%%
%%% Private functions
%%% 

is_soft([{restart_application, ssh}]) -> 
    false;
is_soft(_) ->
    true.


test_hard(State0, FileName) ->
    ct:log("test_hard State0=~p, FileName=~p",[State0, FileName]),
    State = setup_server_client(State0),
    test_connection(FileName, random_contents(), State).

test_soft(State0, FileName) ->
    ct:log("test_soft State0=~p, FileName=~p",[State0, FileName]),
    State = test_connection(FileName, random_contents(), State0),
    setup_server_client( close(State) ).


setup_server_client(#state{config=Config} = State) ->
    DataDir = ?config(data_dir, Config),
    PrivDir = ?config(priv_dir, Config),
	    
    FtpRootDir = filename:join(PrivDir, "ftp_root"),
    catch file:make_dir(FtpRootDir),
	
    SFTP = ssh_sftpd:subsystem_spec([{root,FtpRootDir},{cwd,FtpRootDir}]),

    {Server,Host,Port} = ssh_test_lib:daemon([{system_dir,DataDir},
					      {user_passwords,[{"hej","hopp"}]},
					      {subsystems,[SFTP]}]),
    
    {ok, ChannelPid, Connection} = 
	ssh_sftp:start_channel(Host, Port, [{user_interaction,false},
					    {silently_accept_hosts,true},
					    {user_dir,DataDir},
					    {user,"hej"},
					    {password,"hopp"}]),
    State#state{server = Server,
		client = ChannelPid,
		connection = Connection}.


test_connection(FileName, FileContents,
		#state{client = ChannelPid,
		       root_dir = FtpRootDir} = State) ->
    ct:log("test_connection Writing with ssh_sftp:write_file",[]),
    case ssh_sftp:write_file(ChannelPid, FileName, FileContents) of
	ok ->
	    case ssh_sftp:read_file(ChannelPid, FileName) of
		{ok,FileContents} ->
			    State;
		{ok,Unexpected} ->
		    ct:fail("Expected ~p but got ~p from sftp:read_file(~p,..) in RootDir ~p",
			    [FileContents,Unexpected,FileName,FtpRootDir]
			   );
		Other ->
		    ct:fail("ssh_sftp:read_file(~p,~p) -> ~p~n" 
			    "ssh_sftp:list_dir(~p,\".\") -> ~p", 
			    [ChannelPid,FileName,Other,
			     ChannelPid, catch ssh_sftp:list_dir(ChannelPid, ".")])
	    end;

	Other ->
	    ct:fail("ssh_sftp:write_file(~p,~p,~p) -> ~p",[ChannelPid,FileName,FileContents,Other])
    end.


close(#state{server = Server,
	     connection = Connection} = State) ->
    ssh:close(Connection),
    ssh:stop_daemon(Server),
    State#state{server = undefined,
		client = undefined,
		connection = undefined}.


random_contents() -> list_to_binary( random_chars(3) ).

random_chars(N) -> [crypto:rand_uniform($a,$z) || _<-lists:duplicate(N,x)].