github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/pod-utils/clone/format_test.go (about)

     1  /*
     2  Copyright 2020 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 clone
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  	"time"
    23  
    24  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    25  )
    26  
    27  func TestFormatRecord(t *testing.T) {
    28  	cases := []struct {
    29  		name    string
    30  		r       Record
    31  		require []string
    32  		deny    []string
    33  	}{
    34  		{
    35  			name: "basically works",
    36  		},
    37  		{
    38  			name:    "no repo creates an environment setup record",
    39  			require: []string{"Environment setup"},
    40  			deny:    []string{"Cloning"},
    41  		},
    42  		{
    43  			name: "setting repo creates a cloning record",
    44  			r: Record{
    45  				Refs: prowapi.Refs{
    46  					Org:     "foo",
    47  					Repo:    "bar",
    48  					BaseRef: "deadbeef",
    49  				},
    50  			},
    51  			require: []string{"Cloning foo/bar at deadbeef"},
    52  			deny:    []string{"Environment setup"},
    53  		},
    54  		{
    55  			name: "include base sha when set",
    56  			r: Record{
    57  				Refs: prowapi.Refs{
    58  					Repo:    "bar",
    59  					BaseSHA: "abcdef",
    60  				},
    61  			},
    62  			require: []string{"abcdef"},
    63  		},
    64  		{
    65  			name: "include passing commands",
    66  			r: Record{
    67  				Commands: []Command{
    68  					{
    69  						Command: "cat spam",
    70  						Output:  "eggs",
    71  					},
    72  					{
    73  						Command: "more",
    74  						Output:  "fun",
    75  					},
    76  				},
    77  			},
    78  			require: []string{"cat spam", "eggs", "more", "fun"},
    79  			deny:    []string{"Error:"},
    80  		},
    81  		{
    82  			name: "include failing command",
    83  			r: Record{
    84  				Commands: []Command{
    85  					{
    86  						Command: "rm /something",
    87  						Output:  "barf",
    88  						Error:   "command failed",
    89  					},
    90  				},
    91  			},
    92  			require: []string{"rm /something", "barf", "# Error: command failed"},
    93  		},
    94  		{
    95  			name: "skip pulls when missing",
    96  			deny: []string{"Checking out pulls"},
    97  		},
    98  		{
    99  			name: "include pulls when present",
   100  			r: Record{
   101  				Refs: prowapi.Refs{
   102  					Pulls: []prowapi.Pull{
   103  						{
   104  							Number: 42,
   105  							SHA:    "food",
   106  						},
   107  						{
   108  							Number: 13,
   109  						},
   110  					},
   111  				},
   112  			},
   113  			require: []string{"42", "food", "13"},
   114  		},
   115  		{
   116  			name: "include durations when present",
   117  			r: Record{
   118  				Commands: []Command{
   119  					{
   120  						Command:  "rm /something",
   121  						Output:   "barf",
   122  						Duration: time.Second * 23,
   123  					},
   124  				},
   125  				Duration: time.Second * 12,
   126  			},
   127  			require: []string{"12s", "23s"},
   128  		},
   129  	}
   130  
   131  	for _, tc := range cases {
   132  		t.Run(tc.name, func(t *testing.T) {
   133  			actual := FormatRecord(tc.r)
   134  			for _, r := range tc.require {
   135  				if !strings.Contains(actual, r) {
   136  					t.Errorf("%q missing %q", actual, r)
   137  				}
   138  			}
   139  			for _, d := range tc.deny {
   140  				if strings.Contains(actual, d) {
   141  					t.Errorf("%q should not contain %q", actual, d)
   142  				}
   143  			}
   144  		})
   145  	}
   146  
   147  }