Handle large mqsc files (#295)
* buffer mqsc input to runmqsc * imports * error handling on runmqsc
This commit is contained in:
@@ -16,7 +16,7 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -86,45 +86,37 @@ func configureQueueManager() error {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if strings.HasSuffix(file.Name(), ".mqsc") {
|
if strings.HasSuffix(file.Name(), ".mqsc") {
|
||||||
abs := filepath.Join(configDir, file.Name())
|
abs := filepath.Join(configDir, file.Name())
|
||||||
// #nosec G204
|
// #nosec G204
|
||||||
cmd := exec.Command("runmqsc")
|
cmd := exec.Command("runmqsc")
|
||||||
stdin, err := cmd.StdinPipe()
|
// Read mqsc file into variable
|
||||||
|
mqsc, err := ioutil.ReadFile(abs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Printf("Error reading file %v: %v", abs, err)
|
||||||
return err
|
continue
|
||||||
}
|
}
|
||||||
// Open the MQSC file for reading
|
// Write mqsc to buffer
|
||||||
// #nosec G304
|
var buffer bytes.Buffer
|
||||||
f, err := os.Open(abs)
|
_, err = buffer.Write(mqsc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error opening %v: %v", abs, err)
|
log.Printf("Error writing MQSC file %v to buffer: %v", abs, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
// Copy the contents to stdin of the runmqsc process
|
// Buffer mqsc to stdin of runmqsc
|
||||||
_, err = io.Copy(stdin, f)
|
cmd.Stdin = &buffer
|
||||||
if err != nil {
|
// Run runmqsc command
|
||||||
log.Errorf("Error reading %v: %v", abs, err)
|
|
||||||
}
|
|
||||||
err = f.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to close MQSC file handle: %v", err)
|
|
||||||
}
|
|
||||||
err = stdin.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to close MQSC stdin: %v", err)
|
|
||||||
}
|
|
||||||
// Run the command and wait for completion
|
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error running MQSC file %v (%v):\n\t%v", file.Name(), err, strings.Replace(string(out), "\n", "\n\t", -1))
|
log.Errorf("Error running MQSC file %v (%v):\n\t%v", file.Name(), err, strings.Replace(string(out), "\n", "\n\t", -1))
|
||||||
}
|
continue
|
||||||
|
} else {
|
||||||
// Print the runmqsc output, adding tab characters to make it more readable as part of the log
|
// Print the runmqsc output, adding tab characters to make it more readable as part of the log
|
||||||
log.Printf("Output for \"runmqsc\" with %v:\n\t%v", abs, strings.Replace(string(out), "\n", "\n\t", -1))
|
log.Printf("Output for \"runmqsc\" with %v:\n\t%v", abs, strings.Replace(string(out), "\n", "\n\t", -1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -554,6 +554,49 @@ func TestMQSC(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestLargeMQSC creates a new image with a large MQSC file in, starts a container based
|
||||||
|
// on that image, and checks that the MQSC has been applied correctly.
|
||||||
|
func TestLargeMQSC(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cli, err := client.NewEnvClient()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
const numQueues = 1000
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for i := 1; i <= numQueues; i++ {
|
||||||
|
fmt.Fprintf(&buf, "* Test processing of a large MQSC file, defining queue test%v\nDEFINE QLOCAL(test%v)\n", i, i)
|
||||||
|
}
|
||||||
|
var files = []struct {
|
||||||
|
Name, Body string
|
||||||
|
}{
|
||||||
|
{"Dockerfile", fmt.Sprintf(`
|
||||||
|
FROM %v
|
||||||
|
USER root
|
||||||
|
RUN rm -f /etc/mqm/*.mqsc
|
||||||
|
ADD test.mqsc /etc/mqm/
|
||||||
|
RUN chmod 0660 /etc/mqm/test.mqsc
|
||||||
|
USER mqm`, imageName())},
|
||||||
|
{"test.mqsc", buf.String()},
|
||||||
|
}
|
||||||
|
tag := createImage(t, cli, files)
|
||||||
|
defer deleteImage(t, cli, tag)
|
||||||
|
|
||||||
|
containerConfig := container.Config{
|
||||||
|
Env: []string{"LICENSE=accept", "MQ_QMGR_NAME=qm1"},
|
||||||
|
Image: tag,
|
||||||
|
}
|
||||||
|
id := runContainer(t, cli, &containerConfig)
|
||||||
|
defer cleanContainer(t, cli, id)
|
||||||
|
waitForReady(t, cli, id)
|
||||||
|
|
||||||
|
rc, mqscOutput := execContainer(t, cli, id, "mqm", []string{"bash", "-c", "echo 'DISPLAY QLOCAL(test" + strconv.Itoa(numQueues) + ")' | runmqsc"})
|
||||||
|
if rc != 0 {
|
||||||
|
r := regexp.MustCompile("AMQ[0-9][0-9][0-9][0-9]E")
|
||||||
|
t.Fatalf("Expected runmqsc to exit with rc=0, got %v with error %v", rc, r.FindString(mqscOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestInvalidMQSC creates a new image with an MQSC file containing invalid MQSC,
|
// TestInvalidMQSC creates a new image with an MQSC file containing invalid MQSC,
|
||||||
// tries to start a container based on that image, and checks that container terminates
|
// tries to start a container based on that image, and checks that container terminates
|
||||||
// func TestInvalidMQSC(t *testing.T) {
|
// func TestInvalidMQSC(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user