awa_unit_2.4.0_59135a52/awa/regtests/awa-commands-tests.adb

  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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
-----------------------------------------------------------------------
--  awa-commands-tests -- Test the AWA.Commands
--  Copyright (C) 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  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.
-----------------------------------------------------------------------

with Ada.Text_IO;
with Util.Processes;
with Util.Streams.Pipes;
with Util.Streams.Buffered;
with Util.Test_Caller;
with Util.Log.Loggers;

package body AWA.Commands.Tests is

   Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("AWA.Commands.Tests");

   package Caller is new Util.Test_Caller (Test, "Commands");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test AWA.Commands.Start_Stop",
                       Test_Start_Stop'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.List (tables)",
                       Test_List_Tables'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.List (users)",
                       Test_List_Users'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.List (sessions)",
                       Test_List_Sessions'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.List (jobs)",
                       Test_List_Jobs'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.Info (secure configuration 1)",
                       Test_Secure_Configuration'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.Info (secure configuration 2)",
                       Test_Secure_Configuration_2'Access);
      Caller.Add_Test (Suite, "Test AWA.Commands.Info (verbose)",
                       Test_Verbose_Command'Access);
   end Add_Tests;

   --  ------------------------------
   --  Execute the command and get the output in a string.
   --  ------------------------------
   procedure Execute (T       : in out Test;
                      Command : in String;
                      Input   : in String;
                      Output  : in String;
                      Result  : out Ada.Strings.Unbounded.Unbounded_String;
                      Status  : in Natural := 0) is
      P        : aliased Util.Streams.Pipes.Pipe_Stream;
      Buffer   : Util.Streams.Buffered.Input_Buffer_Stream;
   begin
      if Input'Length > 0 then
         Log.Info ("Execute: {0} < {1}", Command, Input);
      elsif Output'Length > 0 then
         Log.Info ("Execute: {0} > {1}", Command, Output);
      else
         Log.Info ("Execute: {0}", Command);
      end if;
      P.Set_Input_Stream (Input);
      P.Set_Output_Stream (Output);
      P.Open (Command, Util.Processes.READ_ALL);

      --  Write on the process input stream.
      Result := Ada.Strings.Unbounded.Null_Unbounded_String;
      Buffer.Initialize (P'Unchecked_Access, 8192);
      Buffer.Read (Result);
      P.Close;
      Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (Result));
      Log.Info ("Command result: {0}", Result);
      Util.Tests.Assert_Equals (T, Status, P.Get_Exit_Status, "Command '" & Command & "' failed");
   end Execute;

   --  ------------------------------
   --  Test start and stop command.
   --  ------------------------------
   procedure Test_Start_Stop (T : in out Test) is

      Config : constant String := Util.Tests.Get_Parameter ("test_config_path");

      task Start_Server is
         entry Start;
         entry Wait (Result : out Ada.Strings.Unbounded.Unbounded_String);
      end Start_Server;

      task body Start_Server is
         Output : Ada.Strings.Unbounded.Unbounded_String;
      begin
         accept Start do
            null;
         end Start;
         begin
            T.Execute ("bin/awa_command -c " & Config & " start -m 26123",
                       "", "", Output, 0);
         exception
            when others =>
               Output := Ada.Strings.Unbounded.To_Unbounded_String ("* exception *");
         end;
         accept Wait (Result : out Ada.Strings.Unbounded.Unbounded_String) do
            Result := Output;
         end Wait;
      end Start_Server;

      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      --  Launch the 'start' command in a separate task because the command will hang.
      Start_Server.Start;
      delay 5.0;

      --  Launch the 'stop' command, this should terminate the 'start' command.
      T.Execute ("bin/awa_command -c " & Config & " stop -m 26123",
                 "", "", Result, 0);

      --  Wait for the task result.
      Start_Server.Wait (Result);
      Util.Tests.Assert_Matches (T, "Starting...", Result, "AWA start command output");
   end Test_Start_Stop;

   procedure Test_List_Tables (T : in out Test) is
      Config : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " list -t",
                 "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "awa_audit *[0-9]+", Result, "Missing awa_audit");
      Util.Tests.Assert_Matches (T, "awa_session *[0-9]+", Result, "Missing awa_session");
      Util.Tests.Assert_Matches (T, "entity_type *[0-9]+", Result, "Missing entity_type");
      Util.Tests.Assert_Matches (T, "awa_wiki_page *[0-9]+", Result, "Missing awa_wiki_page");
   end Test_List_Tables;

   --  ------------------------------
   --  Test the list -u command.
   --  ------------------------------
   procedure Test_List_Users (T : in out Test) is
      Config : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " list -u",
                 "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "Joe Pot", Result, "Missing user");
      Util.Tests.Assert_Matches (T, "test-wiki@test.com", Result, "Missing email");
   end Test_List_Users;

   --  ------------------------------
   --  Test the list -s command.
   --  ------------------------------
   procedure Test_List_Sessions (T : in out Test) is
      Config : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " list -s",
                 "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "Joe Pot", Result, "Missing user");
   end Test_List_Sessions;

   --  ------------------------------
   --  Test the list -j command.
   --  ------------------------------
   procedure Test_List_Jobs (T : in out Test) is
      Config : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " list -j",
                 "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "S_FACTORY", Result, "Missing factory");
      Util.Tests.Assert_Matches (T, "Joe Pot", Result, "Missing user");
   end Test_List_Jobs;

   --  ------------------------------
   --  Test the command with a secure keystore configuration.
   --  ------------------------------
   procedure Test_Secure_Configuration (T : in out Test) is
      Config   : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Keystore : constant String := Util.Tests.Get_Parameter ("test_keystore_file");
      Result   : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " info --keystore " & Keystore
                   & " --password=unit-test-password", "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "app_name *AWA Secure Demo", Result,
                                 "Secure property not accessed");
   end Test_Secure_Configuration;

   --  ------------------------------
   --  Test the command with a secure keystore configuration.
   --  ------------------------------
   procedure Test_Secure_Configuration_2 (T : in out Test) is
      Config   : constant String := Util.Tests.Get_Parameter ("test_secure_config_path");
      Result   : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -c " & Config & " info", "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "app_name *AWA Secure Demo", Result,
                                 "Secure property not accessed");
      Util.Tests.Assert_Matches (T, "users.auth_key *auth-", Result,
                                 "Secure property not accessed");
      Util.Tests.Assert_Matches (T, "users.server_id *[123]0", Result,
                                 "Secure property not accessed");
   end Test_Secure_Configuration_2;

   --  ------------------------------
   --  Test the command with various logging options.
   --  ------------------------------
   procedure Test_Verbose_Command (T : in out Test) is
      Config   : constant String := Util.Tests.Get_Parameter ("test_config_path");
      Result   : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("bin/awa_command -v -c " & Config & " info ", "", "", Result, 0);
      Util.Tests.Assert_Matches (T, "INFO : Using application search dir:", Result,
                                 "Missing log message");
   end Test_Verbose_Command;

end AWA.Commands.Tests;