knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/upgrade/shell/executor_test.go (about)

     1  /*
     2  Copyright 2020 The Knative Authors
     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 shell_test
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"knative.dev/pkg/test/upgrade/shell"
    24  )
    25  
    26  func TestNewExecutor(t *testing.T) {
    27  	assert := assertions{t: t}
    28  	tests := []testcase{
    29  		helloWorldTestCase(t),
    30  		abortTestCase(t),
    31  		failExampleCase(t),
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			var outB, errB bytes.Buffer
    36  			executor := shell.NewExecutor(t, tt.config.ProjectLocation, func(cfg *shell.ExecutorConfig) {
    37  				cfg.Streams.Out = &outB
    38  				cfg.Streams.Err = &errB
    39  			})
    40  			err := tt.op(executor)
    41  			if err != nil && !tt.wants.failed {
    42  				t.Errorf("%s: \n got: %#v\nfailed: %#v", tt.name, err, tt.failed)
    43  			}
    44  
    45  			for _, wantOut := range tt.wants.outs {
    46  				assert.Contains(outB.String(), wantOut)
    47  			}
    48  			for _, wantErr := range tt.wants.errs {
    49  				assert.Contains(errB.String(), wantErr)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestExecutorDefaults(t *testing.T) {
    56  	assert := assertions{t: t}
    57  	loc, err := shell.NewProjectLocation("../../..")
    58  	assert.NoError(err)
    59  	exec := shell.NewExecutor(t, loc)
    60  	err = exec.RunFunction(fn("true"))
    61  	assert.NoError(err)
    62  }
    63  
    64  func helloWorldTestCase(t *testing.T) testcase {
    65  	return testcase{
    66  		"echo Hello, World!",
    67  		config(t, func(cfg *shell.ExecutorConfig) {
    68  			cfg.SkipDate = true
    69  		}),
    70  		func(exec shell.Executor) error {
    71  			return exec.RunFunction(fn("echo"), "Hello, World!")
    72  		},
    73  		wants{
    74  			outs: []string{
    75  				"echo [OUT] Hello, World!",
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func abortTestCase(t *testing.T) testcase {
    82  	return testcase{
    83  		"abort function",
    84  		config(t, func(cfg *shell.ExecutorConfig) {}),
    85  		func(exec shell.Executor) error {
    86  			return exec.RunFunction(fn("abort"), "reason")
    87  		},
    88  		wants{
    89  			failed: true,
    90  		},
    91  	}
    92  }
    93  
    94  func failExampleCase(t *testing.T) testcase {
    95  	return testcase{
    96  		"fail-example.sh",
    97  		config(t, func(cfg *shell.ExecutorConfig) {}),
    98  		func(exec shell.Executor) error {
    99  			return exec.RunScript(shell.Script{
   100  				Label:      "fail-example.sh",
   101  				ScriptPath: "test/upgrade/shell/fail-example.sh",
   102  			}, "expected err")
   103  		},
   104  		wants{
   105  			failed: true,
   106  			errs: []string{
   107  				"expected err",
   108  			},
   109  		},
   110  	}
   111  }
   112  
   113  type wants struct {
   114  	failed bool
   115  	outs   []string
   116  	errs   []string
   117  }
   118  
   119  type testcase struct {
   120  	name   string
   121  	config shell.ExecutorConfig
   122  	op     func(exec shell.Executor) error
   123  	wants
   124  }
   125  
   126  func config(t *testing.T, customize func(cfg *shell.ExecutorConfig)) shell.ExecutorConfig {
   127  	assert := assertions{t: t}
   128  	loc, err := shell.NewProjectLocation("../../..")
   129  	assert.NoError(err)
   130  	cfg := shell.ExecutorConfig{
   131  		ProjectLocation: loc,
   132  	}
   133  	customize(&cfg)
   134  	return cfg
   135  }
   136  
   137  func fn(name string) shell.Function {
   138  	return shell.Function{
   139  		Script: shell.Script{
   140  			Label:      name,
   141  			ScriptPath: "vendor/knative.dev/hack/library.sh",
   142  		},
   143  		FunctionName: name,
   144  	}
   145  }