github.com/magnusbaeck/logstash-filter-verifier/v2@v2.0.0-pre.1/logstash-filter-verifier_test.go (about)

     1  // Copyright (c) 2017 Magnus Bäck <magnus@noun.se>
     2  
     3  package main
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"regexp"
    10  	"testing"
    11  
    12  	"github.com/magnusbaeck/logstash-filter-verifier/testhelpers"
    13  )
    14  
    15  func TestFindExecutable(t *testing.T) {
    16  	cases := []struct {
    17  		// Test setup
    18  		files []testhelpers.FileWithMode
    19  
    20  		// Input & expected output
    21  		inputs      []string
    22  		expected    string
    23  		errorRegexp *regexp.Regexp
    24  	}{
    25  		// No input paths.
    26  		{
    27  			[]testhelpers.FileWithMode{},
    28  			[]string{},
    29  			"",
    30  			regexp.MustCompile(`^no existing executable found among candidates: `),
    31  		},
    32  		// No matches.
    33  		{
    34  			[]testhelpers.FileWithMode{},
    35  			[]string{
    36  				"foo",
    37  				"bar",
    38  			},
    39  			"",
    40  			regexp.MustCompile(`^no existing executable found among candidates: `),
    41  		},
    42  		// Only matching file is a directory.
    43  		{
    44  			[]testhelpers.FileWithMode{
    45  				{"foo", os.ModeDir | 0755, ""},
    46  			},
    47  			[]string{
    48  				"foo",
    49  				"bar",
    50  			},
    51  			"",
    52  			regexp.MustCompile(`^no existing executable found among candidates: `),
    53  		},
    54  		// Only matching file is not executable.
    55  		{
    56  			[]testhelpers.FileWithMode{
    57  				{"foo", 0644, ""},
    58  			},
    59  			[]string{
    60  				"foo",
    61  				"bar",
    62  			},
    63  			"",
    64  			regexp.MustCompile(`^no existing executable found among candidates: `),
    65  		},
    66  		// Multiple matches, returning first one.
    67  		{
    68  			[]testhelpers.FileWithMode{
    69  				{"foo", 0755, ""},
    70  				{"bar", 0755, ""},
    71  			},
    72  			[]string{
    73  				"foo",
    74  				"bar",
    75  			},
    76  			"foo",
    77  			nil,
    78  		},
    79  		// Multiple matches, skipping the matching directory.
    80  		{
    81  			[]testhelpers.FileWithMode{
    82  				{"foo", os.ModeDir | 0755, ""},
    83  				{"bar", 0755, ""},
    84  			},
    85  			[]string{
    86  				"foo",
    87  				"bar",
    88  			},
    89  			"bar",
    90  			nil,
    91  		},
    92  		// Multiple matches, skipping the matching non-executable.
    93  		{
    94  			[]testhelpers.FileWithMode{
    95  				{"foo", 0644, ""},
    96  				{"bar", 0755, ""},
    97  			},
    98  			[]string{
    99  				"foo",
   100  				"bar",
   101  			},
   102  			"bar",
   103  			nil,
   104  		},
   105  	}
   106  	for i, c := range cases {
   107  		tempdir, err := ioutil.TempDir("", "")
   108  		if err != nil {
   109  			t.Fatalf("Test %d: Unexpected error when creating temp dir: %s", i, err)
   110  		}
   111  		defer os.RemoveAll(tempdir)
   112  
   113  		for _, fwp := range c.files {
   114  			if err = fwp.Create(tempdir); err != nil {
   115  				t.Fatalf("Test %d: Unexpected error when creating test file: %s", i, err)
   116  			}
   117  		}
   118  
   119  		// All paths in the testcase struct are relative but
   120  		// the inputs and output of findExecutable need to be
   121  		// made absolute.
   122  		var absExpected string
   123  		if c.expected != "" {
   124  			absExpected = filepath.Join(tempdir, c.expected)
   125  		}
   126  		absInputs := make([]string, len(c.inputs))
   127  		for i, p := range c.inputs {
   128  			absInputs[i] = filepath.Join(tempdir, p)
   129  		}
   130  
   131  		result, err := findExecutable(absInputs)
   132  		if err == nil && c.errorRegexp != nil {
   133  			t.Errorf("Test %d: Expected failure, got success.", i)
   134  		} else if err != nil && c.errorRegexp == nil {
   135  			t.Errorf("Test %d: Expected success, got this error instead: %#v", i, err)
   136  		} else if err != nil && c.errorRegexp != nil && !c.errorRegexp.MatchString(err.Error()) {
   137  			t.Errorf("Test %d: Expected error to match regexp:\n%s\nGot:\n%s", i, c.errorRegexp, err)
   138  		} else if result != absExpected {
   139  			t.Errorf("Test %d:\nExpected:\n%s\nGot:\n%s", i, absExpected, result)
   140  		}
   141  	}
   142  }