github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/git/v2/publisher_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  	"errors"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/sirupsen/logrus"
    25  	"k8s.io/apimachinery/pkg/util/diff"
    26  )
    27  
    28  func TestPublisher_Commit(t *testing.T) {
    29  	var testCases = []struct {
    30  		name          string
    31  		title, body   string
    32  		info          GitUserGetter
    33  		responses     map[string]execResponse
    34  		expectedCalls [][]string
    35  		expectedErr   bool
    36  	}{
    37  		{
    38  			name:  "no errors works fine",
    39  			title: "title",
    40  			body:  `body\nmore`,
    41  			info: func() (string, string, error) {
    42  				return "robot", "boop@beep.zoop", nil
    43  			},
    44  			responses: map[string]execResponse{
    45  				"add --all": {
    46  					out: []byte("ok"),
    47  				},
    48  				`commit --message title --message body\nmore --author robot <boop@beep.zoop>`: {
    49  					out: []byte("ok"),
    50  				},
    51  			},
    52  			expectedCalls: [][]string{
    53  				{"add", "--all"},
    54  				{"commit", "--message", "title", "--message", `body\nmore`, "--author", "robot <boop@beep.zoop>"},
    55  			},
    56  			expectedErr: false,
    57  		},
    58  		{
    59  			name:  "add fails",
    60  			title: "title",
    61  			body:  `body\nmore`,
    62  			info: func() (string, string, error) {
    63  				return "robot", "boop@beep.zoop", nil
    64  			},
    65  			responses: map[string]execResponse{
    66  				"add --all": {
    67  					err: errors.New("oops"),
    68  				},
    69  			},
    70  			expectedCalls: [][]string{
    71  				{"add", "--all"},
    72  			},
    73  			expectedErr: true,
    74  		},
    75  		{
    76  			name:  "info fails",
    77  			title: "title",
    78  			body:  `body\nmore`,
    79  			info: func() (string, string, error) {
    80  				return "", "", errors.New("oops")
    81  			},
    82  			responses:     map[string]execResponse{},
    83  			expectedCalls: [][]string{},
    84  			expectedErr:   true,
    85  		},
    86  		{
    87  			name:  "commit fails",
    88  			title: "title",
    89  			body:  `body\nmore`,
    90  			info: func() (string, string, error) {
    91  				return "robot", "boop@beep.zoop", nil
    92  			},
    93  			responses: map[string]execResponse{
    94  				"add --all": {
    95  					out: []byte("ok"),
    96  				},
    97  				`commit --message title --message body\nmore --author robot <boop@beep.zoop>`: {
    98  					err: errors.New("oops"),
    99  				},
   100  			},
   101  			expectedCalls: [][]string{
   102  				{"add", "--all"},
   103  				{"commit", "--message", "title", "--message", `body\nmore`, "--author", "robot <boop@beep.zoop>"},
   104  			},
   105  			expectedErr: true,
   106  		},
   107  	}
   108  
   109  	for _, testCase := range testCases {
   110  		t.Run(testCase.name, func(t *testing.T) {
   111  			e := fakeExecutor{
   112  				records:   [][]string{},
   113  				responses: testCase.responses,
   114  			}
   115  			p := publisher{
   116  				executor: &e,
   117  				info:     testCase.info,
   118  				logger:   logrus.WithField("test", testCase.name),
   119  			}
   120  			actualErr := p.Commit(testCase.title, testCase.body)
   121  			if testCase.expectedErr && actualErr == nil {
   122  				t.Errorf("%s: expected an error but got none", testCase.name)
   123  			}
   124  			if !testCase.expectedErr && actualErr != nil {
   125  				t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
   126  			}
   127  			if actual, expected := e.records, testCase.expectedCalls; !reflect.DeepEqual(actual, expected) {
   128  				t.Errorf("%s: got incorrect git calls: %v", testCase.name, diff.ObjectReflectDiff(actual, expected))
   129  			}
   130  		})
   131  	}
   132  }
   133  
   134  func TestPublisher_PushToFork(t *testing.T) {
   135  	var testCases = []struct {
   136  		name          string
   137  		branch        string
   138  		remote        string
   139  		force         bool
   140  		resolveErr    error
   141  		responses     map[string]execResponse
   142  		expectedCalls [][]string
   143  		expectedErr   bool
   144  	}{
   145  		{
   146  			name:   "no errors, no force",
   147  			branch: "master",
   148  			remote: "http.com",
   149  			force:  false,
   150  			responses: map[string]execResponse{
   151  				"push http.com master": {
   152  					out: []byte("ok"),
   153  				},
   154  			},
   155  			expectedCalls: [][]string{
   156  				{"push", "http.com", "master"},
   157  			},
   158  			expectedErr: false,
   159  		},
   160  		{
   161  			name:   "no errors with force",
   162  			branch: "master",
   163  			remote: "http.com",
   164  			force:  true,
   165  			responses: map[string]execResponse{
   166  				"push --force http.com master": {
   167  					out: []byte("ok"),
   168  				},
   169  			},
   170  			expectedCalls: [][]string{
   171  				{"push", "--force", "http.com", "master"},
   172  			},
   173  			expectedErr: false,
   174  		},
   175  		{
   176  			name:          "error resolving remote makes no calls",
   177  			branch:        "master",
   178  			resolveErr:    errors.New("oops"),
   179  			expectedCalls: [][]string{},
   180  			expectedErr:   true,
   181  		},
   182  		{
   183  			name:   "errors pushing propagates, without force",
   184  			branch: "master",
   185  			remote: "http.com",
   186  			responses: map[string]execResponse{
   187  				"push http.com master": {
   188  					err: errors.New("oops"),
   189  				},
   190  			},
   191  			expectedCalls: [][]string{
   192  				{"push", "http.com", "master"},
   193  			},
   194  			expectedErr: true,
   195  		},
   196  		{
   197  			name:   "errors pushing propagates, with force",
   198  			branch: "master",
   199  			remote: "http.com",
   200  			force:  true,
   201  			responses: map[string]execResponse{
   202  				"push --force http.com master": {
   203  					err: errors.New("oops"),
   204  				},
   205  			},
   206  			expectedCalls: [][]string{
   207  				{"push", "--force", "http.com", "master"},
   208  			},
   209  			expectedErr: true,
   210  		},
   211  	}
   212  
   213  	for _, testCase := range testCases {
   214  		t.Run(testCase.name, func(t *testing.T) {
   215  			e := fakeExecutor{
   216  				records:   [][]string{},
   217  				responses: testCase.responses,
   218  			}
   219  			r := fakeResolver{
   220  				out: testCase.remote,
   221  				err: testCase.resolveErr,
   222  			}
   223  			p := publisher{
   224  				executor: &e,
   225  				remotes:  remotes{publishRemote: r.ForkResolver},
   226  				logger:   logrus.WithField("test", testCase.name),
   227  			}
   228  			actualErr := p.PushToFork(testCase.branch, testCase.force)
   229  			if testCase.expectedErr && actualErr == nil {
   230  				t.Errorf("%s: expected an error but got none", testCase.name)
   231  			}
   232  			if !testCase.expectedErr && actualErr != nil {
   233  				t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
   234  			}
   235  			if actual, expected := e.records, testCase.expectedCalls; !reflect.DeepEqual(actual, expected) {
   236  				t.Errorf("%s: got incorrect git calls: %v", testCase.name, diff.ObjectReflectDiff(actual, expected))
   237  			}
   238  		})
   239  	}
   240  }
   241  
   242  func TestPublisher_PushToCentral(t *testing.T) {
   243  	var testCases = []struct {
   244  		name          string
   245  		branch        string
   246  		remote        string
   247  		force         bool
   248  		resolveErr    error
   249  		responses     map[string]execResponse
   250  		expectedCalls [][]string
   251  		expectedErr   bool
   252  	}{
   253  		{
   254  			name:   "no errors, no force",
   255  			branch: "master",
   256  			remote: "http.com",
   257  			force:  false,
   258  			responses: map[string]execResponse{
   259  				"push http.com master": {
   260  					out: []byte("ok"),
   261  				},
   262  			},
   263  			expectedCalls: [][]string{
   264  				{"push", "http.com", "master"},
   265  			},
   266  			expectedErr: false,
   267  		},
   268  		{
   269  			name:   "no errors with force",
   270  			branch: "master",
   271  			remote: "http.com",
   272  			force:  true,
   273  			responses: map[string]execResponse{
   274  				"push --force http.com master": {
   275  					out: []byte("ok"),
   276  				},
   277  			},
   278  			expectedCalls: [][]string{
   279  				{"push", "--force", "http.com", "master"},
   280  			},
   281  			expectedErr: false,
   282  		},
   283  		{
   284  			name:          "error resolving remote makes no calls",
   285  			branch:        "master",
   286  			resolveErr:    errors.New("oops"),
   287  			expectedCalls: [][]string{},
   288  			expectedErr:   true,
   289  		},
   290  		{
   291  			name:   "errors pushing propagates, without force",
   292  			branch: "master",
   293  			remote: "http.com",
   294  			responses: map[string]execResponse{
   295  				"push http.com master": {
   296  					err: errors.New("oops"),
   297  				},
   298  			},
   299  			expectedCalls: [][]string{
   300  				{"push", "http.com", "master"},
   301  			},
   302  			expectedErr: true,
   303  		},
   304  		{
   305  			name:   "errors pushing propagates, with force",
   306  			branch: "master",
   307  			remote: "http.com",
   308  			force:  true,
   309  			responses: map[string]execResponse{
   310  				"push --force http.com master": {
   311  					err: errors.New("oops"),
   312  				},
   313  			},
   314  			expectedCalls: [][]string{
   315  				{"push", "--force", "http.com", "master"},
   316  			},
   317  			expectedErr: true,
   318  		},
   319  	}
   320  
   321  	for _, testCase := range testCases {
   322  		t.Run(testCase.name, func(t *testing.T) {
   323  			e := fakeExecutor{
   324  				records:   [][]string{},
   325  				responses: testCase.responses,
   326  			}
   327  			r := fakeResolver{
   328  				out: testCase.remote,
   329  				err: testCase.resolveErr,
   330  			}
   331  			p := publisher{
   332  				executor: &e,
   333  				remotes:  remotes{centralRemote: r.Resolve},
   334  				logger:   logrus.WithField("test", testCase.name),
   335  			}
   336  			actualErr := p.PushToCentral(testCase.branch, testCase.force)
   337  			if testCase.expectedErr && actualErr == nil {
   338  				t.Errorf("%s: expected an error but got none", testCase.name)
   339  			}
   340  			if !testCase.expectedErr && actualErr != nil {
   341  				t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
   342  			}
   343  			if actual, expected := e.records, testCase.expectedCalls; !reflect.DeepEqual(actual, expected) {
   344  				t.Errorf("%s: got incorrect git calls: %v", testCase.name, diff.ObjectReflectDiff(actual, expected))
   345  			}
   346  		})
   347  	}
   348  }