github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/util/filters_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestMatchLabelFilters(t *testing.T) {
     8  	testLabels := map[string]string{
     9  		"label1": "",
    10  		"label2": "test",
    11  		"label3": "",
    12  	}
    13  	type args struct {
    14  		filterValues []string
    15  		labels       map[string]string
    16  	}
    17  	tests := []struct {
    18  		name string
    19  		args args
    20  		want bool
    21  	}{
    22  		{
    23  			name: "Match when all filters the same as labels",
    24  			args: args{
    25  				filterValues: []string{"label1", "label3", "label2=test"},
    26  				labels:       testLabels,
    27  			},
    28  			want: true,
    29  		},
    30  		{
    31  			name: "Match when filter value not provided in args",
    32  			args: args{
    33  				filterValues: []string{"label2"},
    34  				labels:       testLabels,
    35  			},
    36  			want: true,
    37  		},
    38  		{
    39  			name: "Match when no filter value is given",
    40  			args: args{
    41  				filterValues: []string{"label2="},
    42  				labels:       testLabels,
    43  			},
    44  			want: true,
    45  		},
    46  		{
    47  			name: "Do not match when filter value differs",
    48  			args: args{
    49  				filterValues: []string{"label2=differs"},
    50  				labels:       testLabels,
    51  			},
    52  			want: false,
    53  		},
    54  		{
    55  			name: "Do not match when filter value not listed in labels",
    56  			args: args{
    57  				filterValues: []string{"label1=xyz"},
    58  				labels:       testLabels,
    59  			},
    60  			want: false,
    61  		},
    62  		{
    63  			name: "Do not match when one from many not ok",
    64  			args: args{
    65  				filterValues: []string{"label1=xyz", "invalid=valid"},
    66  				labels:       testLabels,
    67  			},
    68  			want: false,
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		tt := tt
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			if got := MatchLabelFilters(tt.args.filterValues, tt.args.labels); got != tt.want {
    75  				t.Errorf("MatchLabelFilters() = %v, want %v", got, tt.want)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func TestComputeUntilTimestamp(t *testing.T) {
    82  	tests := []struct {
    83  		name    string
    84  		args    []string
    85  		wantErr bool
    86  	}{
    87  		{
    88  			name:    "Return error when more values in list",
    89  			args:    []string{"5h", "6s"},
    90  			wantErr: true,
    91  		},
    92  		{
    93  			name:    "Return error when invalid time",
    94  			args:    []string{"invalidTime"},
    95  			wantErr: true,
    96  		},
    97  		{
    98  			name:    "Do not return error when correct time format supplied",
    99  			args:    []string{"44m"},
   100  			wantErr: false,
   101  		},
   102  	}
   103  	for _, tt := range tests {
   104  		tt := tt
   105  		t.Run(tt.name, func(t *testing.T) {
   106  			_, err := ComputeUntilTimestamp(tt.args)
   107  			if (err != nil) != tt.wantErr {
   108  				t.Errorf("ComputeUntilTimestamp() error = %v, wantErr %v", err, tt.wantErr)
   109  				return
   110  			}
   111  		})
   112  	}
   113  }