Skip to content

Commit 1f7334e

Browse files
vgavinashGitHub Enterprise
authored andcommitted
To fix web server logs' header missing issue (#424)
* Fix for issue * Tests case added * Update main.go Keep the mirror code ready before web server comes up and would start logging. For this, moved the postInit() function call after checkLogSourceForMirroring("web"). * Updates post review meeting * Update docker_api_test.go To fix travis build error * Comment altered
1 parent dfa8e1b commit 1f7334e

File tree

4 files changed

+88
-26
lines changed

4 files changed

+88
-26
lines changed

cmd/runmqserver/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func mirrorHTPasswdLogs(ctx context.Context, wg *sync.WaitGroup, name string, fr
211211

212212
// mirrorWebServerLogs starts a goroutine to mirror the contents of the Liberty web server messages.log
213213
func mirrorWebServerLogs(ctx context.Context, wg *sync.WaitGroup, name string, fromStart bool, mf mirrorFunc) (chan error, error) {
214-
return mirrorLog(ctx, wg, "/var/mqm/web/installations/Installation1/servers/mqweb/logs/messages.log", false, mf, true)
214+
return mirrorLog(ctx, wg, "/var/mqm/web/installations/Installation1/servers/mqweb/logs/messages.log", fromStart, mf, true)
215215
}
216216

217217
func getDebug() bool {

cmd/runmqserver/main.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ func doMain() error {
165165
log.Println("One or more invalid value is provided for MQ_LOGGING_CONSOLE_SOURCE. Allowed values are 'qmgr' & 'web' in csv format")
166166
}
167167

168+
var wg sync.WaitGroup
169+
defer func() {
170+
log.Debug("Waiting for log mirroring to complete")
171+
wg.Wait()
172+
}()
173+
ctx, cancelMirror := context.WithCancel(context.Background())
174+
defer func() {
175+
log.Debug("Cancel log mirroring")
176+
cancelMirror()
177+
}()
178+
179+
//For mirroring web server logs if source variable is set
180+
if checkLogSourceForMirroring("web") {
181+
// Always log from the end of the web server messages.log, because the log rotation should happen as soon as the web server starts
182+
_, err = mirrorWebServerLogs(ctx, &wg, name, false, mf)
183+
if err != nil {
184+
logTermination(err)
185+
return err
186+
}
187+
}
188+
168189
err = postInit(name, keyLabel, defaultP12Truststore)
169190
if err != nil {
170191
logTermination(err)
@@ -205,17 +226,6 @@ func doMain() error {
205226
}
206227
}
207228

208-
var wg sync.WaitGroup
209-
defer func() {
210-
log.Debug("Waiting for log mirroring to complete")
211-
wg.Wait()
212-
}()
213-
ctx, cancelMirror := context.WithCancel(context.Background())
214-
defer func() {
215-
log.Debug("Cancel log mirroring")
216-
cancelMirror()
217-
}()
218-
219229
//For mirroring mq system logs and qm logs, if environment variable is set
220230
if checkLogSourceForMirroring("qmgr") {
221231
//Mirror MQ system logs
@@ -241,17 +251,6 @@ func doMain() error {
241251
}
242252
}
243253

244-
//For mirroring web server logs if source variable is set
245-
if checkLogSourceForMirroring("web") {
246-
// Always log from the start of the web server messages.log, as
247-
// Liberty resets it.
248-
_, err = mirrorWebServerLogs(ctx, &wg, name, true, mf)
249-
if err != nil {
250-
logTermination(err)
251-
return err
252-
}
253-
}
254-
255254
err = updateCommandLevel()
256255
if err != nil {
257256
logTermination(err)

test/container/docker_api_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,51 @@ func TestLoggingConsoleSetToQmgr(t *testing.T) {
16721672
stopContainer(t, cli, id)
16731673
}
16741674

1675+
func TestWebLogsHeaderRotation(t *testing.T) {
1676+
1677+
t.Parallel()
1678+
cli := ce.NewContainerClient()
1679+
containerConfig := ce.ContainerConfig{
1680+
Env: []string{
1681+
"LICENSE=accept",
1682+
"MQ_QMGR_NAME=qm1",
1683+
"MQ_ENABLE_EMBEDDED_WEB_SERVER=true",
1684+
"MQ_LOGGING_CONSOLE_SOURCE=qmgr,web",
1685+
},
1686+
}
1687+
id := runContainer(t, cli, &containerConfig)
1688+
defer cleanContainer(t, cli, id)
1689+
waitForReady(t, cli, id)
1690+
1691+
consoleLogs, errJson := waitForMessageInLog(t, cli, id, "CWWKF0011I")
1692+
if errJson != nil {
1693+
t.Errorf("%v", errJson)
1694+
}
1695+
//The below variable represents the first message in messages.log of web server, considered as the header message
1696+
webLogheader := "product = WebSphere Application Server"
1697+
1698+
if !strings.Contains(consoleLogs, webLogheader) {
1699+
t.Errorf("Console log is without web server header message\n \"%v\"", consoleLogs)
1700+
}
1701+
1702+
// Stop the container cleanly
1703+
stopContainer(t, cli, id)
1704+
startContainer(t, cli, id)
1705+
waitForReady(t, cli, id)
1706+
1707+
consoleLogs2, errJson := waitForMessageCountInLog(t, cli, id, "CWWKF0011I", 2)
1708+
if errJson != nil {
1709+
t.Errorf("%v", errJson)
1710+
}
1711+
t.Logf("Total headers found is %v", strings.Count(consoleLogs2, webLogheader))
1712+
if strings.Count(consoleLogs2, webLogheader) != 2 {
1713+
t.Errorf("Console logs do not contain header message after restart \"%v\"", consoleLogs2)
1714+
}
1715+
1716+
// Stop the container cleanly
1717+
stopContainer(t, cli, id)
1718+
}
1719+
16751720
// Test queue manager with both personal and CA certificate having the same DN
16761721
func TestSameSubDNError(t *testing.T) {
16771722
expectedOutput := "Error: The Subject DN of the Issuer Certificate and the Queue Manager are same"

test/container/docker_api_test_util.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,19 +827,37 @@ func testLogFilePages(t *testing.T, cli ce.ContainerInterface, id string, qmName
827827
}
828828

829829
// waitForMessageInLog will check for a particular message with wait
830-
func waitForMessageInLog(t *testing.T, cli ce.ContainerInterface, id string, expecteMessageId string) (string, error) {
830+
func waitForMessageInLog(t *testing.T, cli ce.ContainerInterface, id string, expectedMessageId string) (string, error) {
831831
var jsonLogs string
832832
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
833833
defer cancel()
834834
for {
835835
select {
836836
case <-time.After(1 * time.Second):
837837
jsonLogs = inspectLogs(t, cli, id)
838-
if strings.Contains(jsonLogs, expecteMessageId) {
838+
if strings.Contains(jsonLogs, expectedMessageId) {
839839
return jsonLogs, nil
840840
}
841841
case <-ctx.Done():
842-
return "", fmt.Errorf("Expected message Id %s was not logged.", expecteMessageId)
842+
return "", fmt.Errorf("expected message Id %s was not logged", expectedMessageId)
843+
}
844+
}
845+
}
846+
847+
// waitForMessageCountInLog will check for a particular message with wait and must occur exact number of times in log as specified by count
848+
func waitForMessageCountInLog(t *testing.T, cli ce.ContainerInterface, id string, expectedMessageId string, count int) (string, error) {
849+
var jsonLogs string
850+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
851+
defer cancel()
852+
for {
853+
select {
854+
case <-time.After(1 * time.Second):
855+
jsonLogs = inspectLogs(t, cli, id)
856+
if strings.Contains(jsonLogs, expectedMessageId) && strings.Count(jsonLogs, expectedMessageId) == count {
857+
return jsonLogs, nil
858+
}
859+
case <-ctx.Done():
860+
return "", fmt.Errorf("expected message Id %s was not logged or it was not logged %v times", expectedMessageId, count)
843861
}
844862
}
845863
}

0 commit comments

Comments
 (0)