github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/internal/jars/jars.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // Package jars contains functionality for running jars for integration tests. The main entry point
    17  // for running a jar is the Run function. The Process interface is used to interact with the running
    18  // jars, and most importantly for shutting down the jars once finished with them.
    19  package jars
    20  
    21  import (
    22  	"fmt"
    23  	"os/exec"
    24  	"time"
    25  )
    26  
    27  type runCallback func(dur time.Duration, jar string, args ...string) (Process, error)
    28  
    29  var runner runCallback // Saves which behavior to use when Run is called.
    30  
    31  func init() {
    32  	runner = getRunner()
    33  }
    34  
    35  // getRunner is used to determine the appropriate behavior for run during initialization time,
    36  // based on the OS and installed binaries of the system. This is returned as a runCallback which
    37  // can be called whenever Run is called. If an error prevents Run from being used at all (for
    38  // example, Java is not installed), then the runCallback will return that error.
    39  func getRunner() runCallback {
    40  	// First check if we can even run jars.
    41  	_, err := exec.LookPath("java")
    42  	if err != nil {
    43  		err := fmt.Errorf("cannot run jar: 'java' command not installed: %w", err)
    44  		return func(_ time.Duration, _ string, _ ...string) (Process, error) {
    45  			return nil, err
    46  		}
    47  	}
    48  
    49  	// Defer to OS-specific logic for checking for presence of timeout command.
    50  	return getTimeoutRunner()
    51  }
    52  
    53  // Run runs a jar given an optional duration, a path to the jar, and any desired arguments to the
    54  // jar. It returns a Process object which can be used to shut down the jar once finished.
    55  //
    56  // The dur parameter is a duration for the timeout command which can be used to automatically kill
    57  // the jar after a set duration, in order to avoid resource leakage. Timeout is described here:
    58  // https://man7.org/linux/man-pages/man1/timeout.1.html. Durations will be translated from
    59  // time.Duration to a string based on the number of minutes. If a duration is provided but the
    60  // system is unable to use the timeout is unable to use the timeout command, this function will
    61  // return an error. To indicate that a duration isn't needed, pass in 0.
    62  func Run(dur time.Duration, jar string, args ...string) (Process, error) {
    63  	return runner(dur, jar, args...)
    64  }