mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
yesterday 6e6cbc7128ebc845f968ae95be36be29a0a05ef8
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.quicksetup.util;
 
import static com.forgerock.opendj.util.OperatingSystem.*;
import static org.opends.messages.QuickSetupMessages.INFO_ERROR_STARTING_SERVER_CODE;
import static org.testng.Assert.*;
 
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.quicksetup.Application;
import org.opends.quicksetup.ApplicationException;
import org.opends.quicksetup.Installation;
import org.opends.quicksetup.ProgressStep;
import org.opends.quicksetup.ReturnCode;
import org.opends.server.DirectoryServerTestCase;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
 
/**
 * A failed start reports what the start command printed (issue #1160), unless the listeners of
 * the application have already shown it. The installation is a bare directory whose start command
 * is a stand-in script that prints and exits with 1.
 */
@SuppressWarnings("javadoc")
@Test(sequential = true)
public class ServerControllerStartFailureTest extends DirectoryServerTestCase
{
  private File root;
 
  @BeforeMethod
  public void createInstallation() throws IOException
  {
    root = Files.createTempDirectory("server-controller-start-failure").toFile();
  }
 
  @AfterMethod(alwaysRun = true)
  public void deleteInstallation() throws ApplicationException
  {
    new FileManager().deleteRecursively(root);
  }
 
  @Test
  public void failedStartReportsBothOutputStreams() throws Exception
  {
    writeStartCommandPrintingToBothStreams();
 
    String message = startAndExpectFailure(null, false);
 
    assertTrue(message.startsWith(exitCodeMessage()), message);
    assertTrue(message.contains("cause on stdout"), message);
    assertTrue(message.contains("cause on stderr"), message);
  }
 
  @Test
  public void failedStartReportsOnlyTheLastLines() throws Exception
  {
    if (isWindows())
    {
      writeStartCommand("@for /L %%i in (1,1,150) do @echo L%%iL", "@exit /b 1");
    }
    else
    {
      writeStartCommand("#!/bin/sh", "i=1", "while [ $i -le 150 ]; do echo \"L${i}L\"; i=$((i+1)); done", "exit 1");
    }
 
    String message = startAndExpectFailure(null, false);
 
    assertFalse(message.contains("L50L"), message);
    assertTrue(message.contains("L51L"), message);
    assertTrue(message.contains("L150L"), message);
  }
 
  @Test
  public void failedStartReportsWhatIsPrintedAfterTheExit() throws Exception
  {
    if (isWindows())
    {
      throw new SkipException("needs a child process that outlives the start command");
    }
    // The child holds the inherited pipes after sh exits, so its line arrives after waitFor().
    // When the process exits, the JDK drains what is already in a pipe and closes it, unless a
    // reader is blocked in read() on that stream, which the drain then waits for. The first
    // line and the pause before the exit leave the stdout reader blocked there, so that the
    // line of the child is read rather than cut off by the drain.
    writeStartCommand("#!/bin/sh", "echo 'printed before the exit'", "sleep 1",
        "(sleep 1; echo 'printed after the exit') &", "exit 1");
 
    String message = startAndExpectFailure(null, false);
 
    assertTrue(message.contains("printed before the exit"), message);
    assertTrue(message.contains("printed after the exit"), message);
  }
 
  @Test
  public void silentFailedStartKeepsTheExitCodeMessage() throws Exception
  {
    if (isWindows())
    {
      writeStartCommand("@exit /b 1");
    }
    else
    {
      writeStartCommand("#!/bin/sh", "exit 1");
    }
 
    assertEquals(startAndExpectFailure(null, false), exitCodeMessage());
  }
 
  @Test
  public void failedStartShownToTheListenersKeepsTheExitCodeMessage() throws Exception
  {
    writeStartCommandPrintingToBothStreams();
    ListeningApplication application = new ListeningApplication();
 
    assertEquals(startAndExpectFailure(application, false), exitCodeMessage());
    assertTrue(application.getShown().contains("cause on stdout"), application.getShown());
    assertTrue(application.getShown().contains("cause on stderr"), application.getShown());
  }
 
  @Test
  public void failedStartHiddenFromTheListenersReportsBothOutputStreams() throws Exception
  {
    writeStartCommandPrintingToBothStreams();
    ListeningApplication application = new ListeningApplication();
 
    String message = startAndExpectFailure(application, true);
 
    assertTrue(message.startsWith(exitCodeMessage()), message);
    assertTrue(message.contains("cause on stdout"), message);
    assertTrue(message.contains("cause on stderr"), message);
    assertEquals(application.getShown(), "");
  }
 
  private static String exitCodeMessage()
  {
    return INFO_ERROR_STARTING_SERVER_CODE.get(1).toString();
  }
 
  private void writeStartCommandPrintingToBothStreams() throws IOException
  {
    if (isWindows())
    {
      writeStartCommand("@echo cause on stdout", "@echo cause on stderr 1>&2", "@exit /b 1");
    }
    else
    {
      writeStartCommand("#!/bin/sh", "echo 'cause on stdout'", "echo 'cause on stderr' >&2", "exit 1");
    }
  }
 
  private void writeStartCommand(String... lines) throws IOException
  {
    Installation installation = new Installation(root, root);
    File command = installation.getServerStartCommandFile();
    assertTrue(command.getParentFile().mkdirs());
    Files.write(command.toPath(), String.join(isWindows() ? "\r\n" : "\n", lines).concat("\n")
        .getBytes(StandardCharsets.US_ASCII));
    assertTrue(command.setExecutable(true));
  }
 
  private String startAndExpectFailure(Application application, boolean suppressOutput)
  {
    try
    {
      new ServerController(application, new Installation(root, root)).startServer(suppressOutput);
      fail("The start command exited with 1, but the start did not fail");
      return null;
    }
    catch (ApplicationException e)
    {
      assertEquals(e.getType(), ReturnCode.START_ERROR);
      return e.getMessage();
    }
  }
 
  /** An application whose listener keeps the log details it is shown. */
  private static final class ListeningApplication extends Application
  {
    private final StringBuilder shown = new StringBuilder();
 
    ListeningApplication()
    {
      setProgressMessageFormatter(new PlainTextProgressMessageFormatter());
      addProgressUpdateListener(ev ->
      {
        synchronized (shown)
        {
          shown.append(ev.getNewLogs());
        }
      });
    }
 
    String getShown()
    {
      synchronized (shown)
      {
        return shown.toString();
      }
    }
 
    @Override
    public String getInstallationPath()
    {
      return null;
    }
 
    @Override
    public String getInstancePath()
    {
      return null;
    }
 
    @Override
    public ProgressStep getCurrentProgressStep()
    {
      return null;
    }
 
    @Override
    public Integer getRatio(ProgressStep step)
    {
      return null;
    }
 
    @Override
    public LocalizableMessage getSummary(ProgressStep step)
    {
      return null;
    }
 
    @Override
    public boolean isFinished()
    {
      return false;
    }
 
    @Override
    public boolean isCancellable()
    {
      return false;
    }
 
    @Override
    public void cancel()
    {
      // Nothing to cancel.
    }
 
    @Override
    public void run()
    {
      // Never run: the test drives the ServerController directly.
    }
  }
}