istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/resource/flags_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package resource
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/google/go-cmp/cmp"
    20  )
    21  
    22  func TestValidate(t *testing.T) {
    23  	tcs := []struct {
    24  		name         string
    25  		settings     *Settings
    26  		expectErr    bool
    27  		expectedRevs RevVerMap
    28  	}{
    29  		{
    30  			name: "fail on deprecation and nocleanup",
    31  			settings: &Settings{
    32  				FailOnDeprecation: true,
    33  				NoCleanup:         true,
    34  			},
    35  			expectErr: true,
    36  		},
    37  		{
    38  			name: "fail on both revision and revisions flag",
    39  			settings: &Settings{
    40  				Revision:      "a",
    41  				Compatibility: false,
    42  				Revisions: RevVerMap{
    43  					"b": "",
    44  				},
    45  			},
    46  			expectErr: true,
    47  		},
    48  		{
    49  			name: "fail when compatibility mode but no revisions",
    50  			settings: &Settings{
    51  				Compatibility: true,
    52  			},
    53  			expectErr: true,
    54  		},
    55  		{
    56  			name: "revision flag converted to revvermap",
    57  			settings: &Settings{
    58  				Revision:      "a",
    59  				Compatibility: false,
    60  			},
    61  			expectedRevs: RevVerMap{
    62  				"a": "",
    63  			},
    64  		},
    65  	}
    66  
    67  	for _, tc := range tcs {
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			err := validate(tc.settings)
    70  			if tc.expectErr {
    71  				if err == nil {
    72  					t.Error("expected error but got none")
    73  				}
    74  				return
    75  			}
    76  			if tc.expectedRevs != nil {
    77  				if diff := cmp.Diff(tc.expectedRevs, tc.settings.Revisions); diff != "" {
    78  					t.Errorf("unexpected revisions, got: %v, want: %v, diff: %v",
    79  						tc.settings.Revisions, tc.expectedRevs, diff)
    80  				}
    81  			}
    82  		})
    83  	}
    84  }