github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/trick-or-treat/trick-or-treat_test.go (about)

     1  /*
     2  Copyright 2018 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 trickortreat
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/sirupsen/logrus"
    27  
    28  	"sigs.k8s.io/prow/pkg/github"
    29  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    30  )
    31  
    32  type fakeSnicker struct {
    33  	err error
    34  }
    35  
    36  func (c *fakeSnicker) readImage(log *logrus.Entry) (string, error) {
    37  	if c.err != nil {
    38  		return "", c.err
    39  	}
    40  	return "![fake candy image](fake)", nil
    41  }
    42  
    43  func TestReadImage(t *testing.T) {
    44  	img, err := trickOrTreat.readImage(logrus.WithContext(context.Background()))
    45  	if err != nil {
    46  		t.Errorf("Could not read candies from %#v: %v", trickOrTreat, err)
    47  		return
    48  	}
    49  	var found bool
    50  	for _, cand := range candiesImgs {
    51  		if want := fmt.Sprintf("![candy image](%s)", cand); want == img {
    52  			found = true
    53  		}
    54  	}
    55  	if !found {
    56  		t.Fatalf("Image %q not part of curated list of images", img)
    57  	}
    58  }
    59  
    60  // Small, unit tests
    61  func TestAll(t *testing.T) {
    62  	var testcases = []struct {
    63  		name          string
    64  		action        github.GenericCommentEventAction
    65  		body          string
    66  		state         string
    67  		pr            bool
    68  		readImgErr    error
    69  		shouldComment bool
    70  		shouldError   bool
    71  	}{
    72  		{
    73  			name:          "failed reading image",
    74  			state:         "open",
    75  			action:        github.GenericCommentActionCreated,
    76  			body:          "/trick-or-treat",
    77  			readImgErr:    errors.New("failed"),
    78  			shouldComment: false,
    79  			shouldError:   true,
    80  		},
    81  		{
    82  			name:          "ignore edited comment",
    83  			state:         "open",
    84  			action:        github.GenericCommentActionEdited,
    85  			body:          "/trick-or-treat",
    86  			shouldComment: false,
    87  			shouldError:   false,
    88  		},
    89  		{
    90  			name:          "leave candy on pr",
    91  			state:         "open",
    92  			action:        github.GenericCommentActionCreated,
    93  			body:          "/trick-or-treat",
    94  			pr:            true,
    95  			shouldComment: true,
    96  			shouldError:   false,
    97  		},
    98  		{
    99  			name:          "leave candy on issue",
   100  			state:         "open",
   101  			action:        github.GenericCommentActionCreated,
   102  			body:          "/trick-or-treat",
   103  			shouldComment: true,
   104  			shouldError:   false,
   105  		},
   106  		{
   107  			name:          "leave candy on issue, trailing space",
   108  			state:         "open",
   109  			action:        github.GenericCommentActionCreated,
   110  			body:          "/trick-or-treat \r",
   111  			shouldComment: true,
   112  			shouldError:   false,
   113  		},
   114  		{
   115  			name:          "Trailing random strings",
   116  			state:         "open",
   117  			action:        github.GenericCommentActionCreated,
   118  			body:          "/trick-or-treat clothes",
   119  			shouldComment: true,
   120  			shouldError:   false,
   121  		},
   122  	}
   123  	for _, tc := range testcases {
   124  		fc := fakegithub.NewFakeClient()
   125  		e := &github.GenericCommentEvent{
   126  			Action:     tc.action,
   127  			Body:       tc.body,
   128  			Number:     5,
   129  			IssueState: tc.state,
   130  			IsPR:       tc.pr,
   131  		}
   132  		err := handle(fc, logrus.WithField("plugin", pluginName), e, &fakeSnicker{tc.readImgErr})
   133  		if !tc.shouldError && err != nil {
   134  			t.Errorf("%s: didn't expect error: %v", tc.name, err)
   135  		} else if tc.shouldError && err == nil {
   136  			t.Errorf("%s: expected an error to occur", tc.name)
   137  		} else if tc.shouldComment && len(fc.IssueComments[5]) != 1 {
   138  			t.Errorf("%s: should have commented.", tc.name)
   139  		} else if tc.shouldComment {
   140  			shouldImage := !tc.shouldError
   141  			body := fc.IssueComments[5][0].Body
   142  			hasImage := strings.Contains(body, "![")
   143  			if hasImage && !shouldImage {
   144  				t.Errorf("%s: unexpected image in %s", tc.name, body)
   145  			} else if !hasImage && shouldImage {
   146  				t.Errorf("%s: no image in %s", tc.name, body)
   147  			}
   148  		} else if !tc.shouldComment && len(fc.IssueComments[5]) != 0 {
   149  			t.Errorf("%s: should not have commented.", tc.name)
   150  		}
   151  	}
   152  }