github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/pod-utils/gcs/metadata_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 gcs
    18  
    19  import (
    20  	"mime"
    21  	"reflect"
    22  	"testing"
    23  
    24  	utilpointer "k8s.io/utils/pointer"
    25  
    26  	"sigs.k8s.io/prow/pkg/io"
    27  )
    28  
    29  func TestWriterOptionsFromFileName(t *testing.T) {
    30  	mime.AddExtensionType(".log", "text/plain")
    31  
    32  	testCases := []struct {
    33  		name             string
    34  		filename         string
    35  		expectedFileName string
    36  		expectedAttrs    io.WriterOptions
    37  	}{
    38  		{
    39  			name:             "txt",
    40  			filename:         "build-log.txt",
    41  			expectedFileName: "build-log.txt",
    42  			expectedAttrs: io.WriterOptions{
    43  				ContentType: utilpointer.String("text/plain; charset=utf-8"),
    44  			},
    45  		},
    46  		{
    47  			name:             "txt.gz",
    48  			filename:         "build-log.txt.gz",
    49  			expectedFileName: "build-log.txt",
    50  			expectedAttrs: io.WriterOptions{
    51  				ContentEncoding: utilpointer.String("gzip"),
    52  				ContentType:     utilpointer.String("text/plain; charset=utf-8"),
    53  			},
    54  		},
    55  		{
    56  			name:             "txt.gzip",
    57  			filename:         "build-log.txt.gzip",
    58  			expectedFileName: "build-log.txt",
    59  			expectedAttrs: io.WriterOptions{
    60  				ContentEncoding: utilpointer.String("gzip"),
    61  				ContentType:     utilpointer.String("text/plain; charset=utf-8"),
    62  			},
    63  		},
    64  		{
    65  			name:             "bare gz",
    66  			filename:         "gz",
    67  			expectedFileName: "gz",
    68  			expectedAttrs: io.WriterOptions{
    69  				ContentType: utilpointer.String("application/gzip"),
    70  			},
    71  		},
    72  		{
    73  			name:             "gz",
    74  			filename:         "build-log.gz",
    75  			expectedFileName: "build-log",
    76  			expectedAttrs: io.WriterOptions{
    77  				ContentType: utilpointer.String("application/gzip"),
    78  			},
    79  		},
    80  		{
    81  			name:             "gzip",
    82  			filename:         "build-log.gzip",
    83  			expectedFileName: "build-log",
    84  			expectedAttrs: io.WriterOptions{
    85  				ContentType: utilpointer.String("application/gzip"),
    86  			},
    87  		},
    88  		{
    89  			name:             "json",
    90  			filename:         "events.json",
    91  			expectedFileName: "events.json",
    92  			expectedAttrs: io.WriterOptions{
    93  				ContentType: utilpointer.String("application/json"),
    94  			},
    95  		},
    96  		{
    97  			name:             "json.gz",
    98  			filename:         "events.json.gz",
    99  			expectedFileName: "events.json",
   100  			expectedAttrs: io.WriterOptions{
   101  				ContentEncoding: utilpointer.String("gzip"),
   102  				ContentType:     utilpointer.String("application/json"),
   103  			},
   104  		},
   105  		{
   106  			name:             "log",
   107  			filename:         "journal.log",
   108  			expectedFileName: "journal.log",
   109  			expectedAttrs: io.WriterOptions{
   110  				ContentType: utilpointer.String("text/plain; charset=utf-8"),
   111  			},
   112  		},
   113  		{
   114  			name:             "empty",
   115  			filename:         "",
   116  			expectedFileName: "",
   117  			expectedAttrs:    io.WriterOptions{},
   118  		},
   119  		{
   120  			name:             "dot",
   121  			filename:         ".",
   122  			expectedFileName: ".",
   123  			expectedAttrs:    io.WriterOptions{},
   124  		},
   125  	}
   126  
   127  	for _, test := range testCases {
   128  		t.Run(test.name, func(t *testing.T) {
   129  			actualFileName, actualAttrs := WriterOptionsFromFileName(test.filename)
   130  
   131  			if actualFileName != test.expectedFileName {
   132  				t.Errorf("expected file name %q but got %q", test.expectedFileName, actualFileName)
   133  			}
   134  
   135  			if !reflect.DeepEqual(actualAttrs, test.expectedAttrs) {
   136  				t.Errorf("expected attributes %#+v but got %#+v", test.expectedAttrs, actualAttrs)
   137  			}
   138  		})
   139  	}
   140  }