github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/utils/fake/command.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  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  
    17  package fake
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"strings"
    23  
    24  	"github.com/Mirantis/virtlet/pkg/utils"
    25  	testutils "github.com/Mirantis/virtlet/pkg/utils/testing"
    26  )
    27  
    28  // CmdSpec specifies a command for the fake Commander
    29  type CmdSpec struct {
    30  	// Regular expression to match against the " "-joined command
    31  	Match string
    32  	// Stdout to return from the command
    33  	Stdout string
    34  }
    35  
    36  type fakeCommand struct {
    37  	rec       testutils.Recorder
    38  	cmd       []string
    39  	commander *Commander
    40  }
    41  
    42  var _ utils.Command = &fakeCommand{}
    43  
    44  func (c *fakeCommand) subst(text string) string {
    45  	if c.commander.replacePath == nil {
    46  		return text
    47  	}
    48  	return c.commander.replacePath.ReplaceAllString(text, c.commander.replacement)
    49  }
    50  
    51  func (c *fakeCommand) Run(stdin []byte) ([]byte, error) {
    52  	fullCmd := c.subst(strings.Join(c.cmd, " "))
    53  	r := map[string]string{
    54  		"cmd": fullCmd,
    55  	}
    56  	if c.rec != nil {
    57  		defer c.rec.Rec("CMD", r)
    58  	}
    59  	for _, spec := range c.commander.specs {
    60  		matched, err := regexp.MatchString(spec.Match, fullCmd)
    61  		if err != nil {
    62  			return nil, fmt.Errorf("failed to match regexp %q: %v", spec.Match, err)
    63  		}
    64  		if matched {
    65  			if stdin != nil {
    66  				r["stdin"] = c.subst(string(stdin))
    67  			}
    68  			if spec.Stdout != "" {
    69  				r["stdout"] = spec.Stdout
    70  			}
    71  			return []byte(spec.Stdout), nil
    72  		}
    73  	}
    74  	return nil, fmt.Errorf("unexpected command %q", fullCmd)
    75  }
    76  
    77  // Commander records the commands instead of executing them.  It
    78  // also provides stdout based on a table of (cmd_regexp, stdout)
    79  // pairs. The regexp is matched against the command and its arguments
    80  // joined with " ".
    81  type Commander struct {
    82  	rec         testutils.Recorder
    83  	specs       []CmdSpec
    84  	replacePath *regexp.Regexp
    85  	replacement string
    86  }
    87  
    88  var _ utils.Commander = &Commander{}
    89  
    90  // NewCommander creates a new Commander.
    91  // If rec is nil, all the commands will not be recorded
    92  func NewCommander(rec testutils.Recorder, specs []CmdSpec) *Commander {
    93  	return &Commander{rec: rec, specs: specs}
    94  }
    95  
    96  // ReplaceTempPath makes the commander replace the path with specified
    97  // suffix with the specified string. The replacement is done on the
    98  // word boundary.
    99  func (c *Commander) ReplaceTempPath(pathSuffix, replacement string) {
   100  	c.replacePath = regexp.MustCompile(`\S*` + regexp.QuoteMeta(pathSuffix))
   101  	c.replacement = replacement
   102  }
   103  
   104  // Command implements the Command method of Commander interface.
   105  func (c *Commander) Command(name string, arg ...string) utils.Command {
   106  	return &fakeCommand{
   107  		rec:       c.rec,
   108  		cmd:       append([]string{name}, arg...),
   109  		commander: c,
   110  	}
   111  }