github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/git/v2/executor_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes 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 git 18 19 import ( 20 "bytes" 21 "errors" 22 "github.com/sirupsen/logrus" 23 "reflect" 24 "testing" 25 26 "k8s.io/apimachinery/pkg/util/diff" 27 ) 28 29 func TestCensoringExecutor_Run(t *testing.T) { 30 var testCases = []struct { 31 name string 32 dir, git string 33 args []string 34 censor Censor 35 executeOut []byte 36 executeErr error 37 expectedOut []byte 38 expectedErr bool 39 }{ 40 { 41 name: "happy path with nothing to censor returns all output", 42 dir: "/somewhere/repo", 43 git: "/usr/bin/git", 44 args: []string{"status"}, 45 censor: func(content []byte) []byte { 46 return content 47 }, 48 executeOut: []byte("hi"), 49 executeErr: nil, 50 expectedOut: []byte("hi"), 51 expectedErr: false, 52 }, 53 { 54 name: "happy path with nonstandard git binary", 55 dir: "/somewhere/repo", 56 git: "/usr/local/bin/git", 57 args: []string{"status"}, 58 censor: func(content []byte) []byte { 59 return content 60 }, 61 executeOut: []byte("hi"), 62 executeErr: nil, 63 expectedOut: []byte("hi"), 64 expectedErr: false, 65 }, 66 { 67 name: "happy path with something to censor returns altered output", 68 dir: "/somewhere/repo", 69 git: "/usr/bin/git", 70 args: []string{"status"}, 71 censor: func(content []byte) []byte { 72 return bytes.ReplaceAll(content, []byte("secret"), []byte("CENSORED")) 73 }, 74 executeOut: []byte("hi secret"), 75 executeErr: nil, 76 expectedOut: []byte("hi CENSORED"), 77 expectedErr: false, 78 }, 79 { 80 name: "error is propagated", 81 dir: "/somewhere/repo", 82 git: "/usr/bin/git", 83 args: []string{"status"}, 84 censor: func(content []byte) []byte { 85 return bytes.ReplaceAll(content, []byte("secret"), []byte("CENSORED")) 86 }, 87 executeOut: []byte("hi secret"), 88 executeErr: errors.New("oops"), 89 expectedOut: []byte("hi CENSORED"), 90 expectedErr: true, 91 }, 92 } 93 94 for _, testCase := range testCases { 95 t.Run(testCase.name, func(t *testing.T) { 96 e := censoringExecutor{ 97 logger: logrus.WithField("name", testCase.name), 98 dir: testCase.dir, 99 git: testCase.git, 100 censor: testCase.censor, 101 execute: func(dir, command string, args ...string) (bytes []byte, e error) { 102 if dir != testCase.dir { 103 t.Errorf("%s: got incorrect dir: %v", testCase.name, diff.StringDiff(dir, testCase.dir)) 104 } 105 if command != testCase.git { 106 t.Errorf("%s: got incorrect command: %v", testCase.name, diff.StringDiff(command, testCase.git)) 107 } 108 if !reflect.DeepEqual(args, testCase.args) { 109 t.Errorf("%s: got incorrect args: %v", testCase.name, diff.ObjectReflectDiff(args, testCase.args)) 110 } 111 return testCase.executeOut, testCase.executeErr 112 }, 113 } 114 actual, actualErr := e.Run(testCase.args...) 115 if testCase.expectedErr && actualErr == nil { 116 t.Errorf("%s: expected an error but got none", testCase.name) 117 } 118 if !testCase.expectedErr && actualErr != nil { 119 t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr) 120 } 121 if !reflect.DeepEqual(actual, testCase.expectedOut) { 122 t.Errorf("%s: got incorrect command output: %v", testCase.name, diff.StringDiff(string(actual), string(testCase.expectedOut))) 123 } 124 }) 125 } 126 }