github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/verrazzano-backup-hook/utilities/k8s/fake/spydy_utils.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package fake
     5  
     6  import (
     7  	"bytes"
     8  	"k8s.io/client-go/rest"
     9  	"k8s.io/client-go/tools/remotecommand"
    10  	"net/url"
    11  )
    12  
    13  // PodExecResult can be used to output arbitrary strings during unit testing
    14  var PodExecResult = func(url *url.URL) (string, string, error) { return "", "", nil }
    15  
    16  // NewPodExecutor should be used instead of remotecommand.NewSPDYExecutor in unit tests
    17  func NewPodExecutor(config *rest.Config, method string, url *url.URL) (remotecommand.Executor, error) {
    18  	return &dummyExecutor{method: method, url: url}, nil
    19  }
    20  
    21  // dummyExecutor is for unit testing
    22  type dummyExecutor struct {
    23  	method string
    24  	url    *url.URL
    25  }
    26  
    27  // Stream on a dummyExecutor sets stdout to PodExecResult
    28  func (f *dummyExecutor) Stream(options remotecommand.StreamOptions) error {
    29  	stdout, stderr, err := PodExecResult(f.url)
    30  	if options.Stdout != nil {
    31  		buf := new(bytes.Buffer)
    32  		buf.WriteString(stdout)
    33  		if _, err := options.Stdout.Write(buf.Bytes()); err != nil {
    34  			return err
    35  		}
    36  	}
    37  	if options.Stderr != nil {
    38  		buf := new(bytes.Buffer)
    39  		buf.WriteString(stderr)
    40  		if _, err := options.Stderr.Write(buf.Bytes()); err != nil {
    41  			return err
    42  		}
    43  	}
    44  	return err
    45  }