github.com/kubeshop/testkube@v1.17.23/contrib/executor/example/pkg/runner/runner.go (about)

     1  package runner
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    11  	"github.com/kubeshop/testkube/pkg/executor/runner"
    12  )
    13  
    14  func NewRunner() *ExampleRunner {
    15  	return &ExampleRunner{}
    16  }
    17  
    18  // ExampleRunner for template - change me to some valid runner
    19  type ExampleRunner struct{}
    20  
    21  var _ runner.Runner = &ExampleRunner{}
    22  
    23  func (r *ExampleRunner) Run(ctx context.Context, execution testkube.Execution) (result testkube.ExecutionResult, err error) {
    24  	// ScriptContent will have URI
    25  	uri := ""
    26  	if execution.Content != nil {
    27  		uri = execution.Content.Uri
    28  	}
    29  
    30  	resp, err := http.Get(uri)
    31  	if err != nil {
    32  		return result, err
    33  	}
    34  	defer resp.Body.Close()
    35  
    36  	b, err := io.ReadAll(resp.Body)
    37  	if err != nil {
    38  		return result, err
    39  	}
    40  
    41  	// if get is successful return success result
    42  	if resp.StatusCode == 200 {
    43  		return testkube.ExecutionResult{
    44  			Status: testkube.ExecutionStatusPassed,
    45  			Output: string(b),
    46  		}, nil
    47  	}
    48  
    49  	// else we'll return error to simplify example
    50  	err = errors.Errorf("invalid status code %d, (uri:%s)", resp.StatusCode, uri)
    51  	return *result.Err(err), nil
    52  }
    53  
    54  // GetType returns runner type
    55  func (r *ExampleRunner) GetType() runner.Type {
    56  	return runner.TypeMain
    57  }