sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/spyglass/artifacts_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 spyglass
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  
    26  	prowv1 "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    27  	"sigs.k8s.io/prow/pkg/config"
    28  	"sigs.k8s.io/prow/pkg/io"
    29  )
    30  
    31  func TestSpyglass_ListArtifacts(t *testing.T) {
    32  	type args struct {
    33  		src string
    34  	}
    35  	tests := []struct {
    36  		name    string
    37  		args    args
    38  		want    []string
    39  		wantErr bool
    40  	}{
    41  		{
    42  			name: "list artifacts (old format)",
    43  			args: args{
    44  				src: "gcs/test-bucket/logs/example-ci-run/403",
    45  			},
    46  			want: []string{
    47  				"build-log.txt",
    48  				prowv1.FinishedStatusFile,
    49  				"junit_01.xml",
    50  				"long-log.txt",
    51  				prowv1.StartedStatusFile,
    52  			},
    53  		},
    54  		{
    55  			name: "list artifacts (new format)",
    56  			args: args{
    57  				src: "gs/test-bucket/logs/example-ci-run/403",
    58  			},
    59  			want: []string{
    60  				"build-log.txt",
    61  				prowv1.FinishedStatusFile,
    62  				"junit_01.xml",
    63  				"long-log.txt",
    64  				prowv1.StartedStatusFile,
    65  			},
    66  		},
    67  		{
    68  			name: "list artifacts without results in gs (new format)",
    69  			args: args{
    70  				src: "gs/test-bucket/logs/example-ci-run/404",
    71  			},
    72  			want: []string{
    73  				"build-log.txt",
    74  			},
    75  		},
    76  		{
    77  			name: "list artifacts without results in gs with multiple containers",
    78  			args: args{
    79  				src: "gs/test-bucket/logs/multiple-container-job/123",
    80  			},
    81  			want: []string{
    82  				"test-1-build-log.txt",
    83  				"test-2-build-log.txt",
    84  			},
    85  		},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			fakeGCSClient := fakeGCSServer.Client()
    90  			ca := &config.Agent{}
    91  			ca.Set(&config.Config{
    92  				ProwConfig: config.ProwConfig{
    93  					Deck: config.Deck{
    94  						AllKnownStorageBuckets: sets.New[string]("test-bucket"),
    95  					},
    96  				},
    97  			})
    98  			sg := New(context.Background(), fakeJa, ca.Config, io.NewGCSOpener(fakeGCSClient), false)
    99  			got, err := sg.ListArtifacts(context.Background(), tt.args.src)
   100  			if (err != nil) != tt.wantErr {
   101  				t.Errorf("ListArtifacts() error = %v, wantErr %v", err, tt.wantErr)
   102  				return
   103  			}
   104  			if !reflect.DeepEqual(got, tt.want) {
   105  				t.Errorf("ListArtifacts() got = %v, want %v", got, tt.want)
   106  			}
   107  		})
   108  	}
   109  }