istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/status/helper_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  
    15  package status
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"istio.io/api/meta/v1alpha1"
    22  )
    23  
    24  func Test_updateCondition(t *testing.T) {
    25  	type args struct {
    26  		conditions []*v1alpha1.IstioCondition
    27  		condition  *v1alpha1.IstioCondition
    28  	}
    29  	tests := []struct {
    30  		name string
    31  		args args
    32  		want []*v1alpha1.IstioCondition
    33  	}{
    34  		{
    35  			name: "Update conditions",
    36  			args: args{
    37  				conditions: []*v1alpha1.IstioCondition{
    38  					{
    39  						Type:    "PassedValidation",
    40  						Status:  "True",
    41  						Message: "just a test, here",
    42  					},
    43  					{
    44  						Type:    "Reconciled",
    45  						Status:  "False",
    46  						Message: "1/2 proxies up to date.",
    47  					},
    48  				},
    49  				condition: &v1alpha1.IstioCondition{
    50  					Type:    "PassedValidation",
    51  					Status:  "False",
    52  					Message: "just a update test, here",
    53  				},
    54  			},
    55  			want: []*v1alpha1.IstioCondition{
    56  				{
    57  					Type:    "PassedValidation",
    58  					Status:  "False",
    59  					Message: "just a update test, here",
    60  				},
    61  				{
    62  					Type:    "Reconciled",
    63  					Status:  "False",
    64  					Message: "1/2 proxies up to date.",
    65  				},
    66  			},
    67  		},
    68  		{
    69  			name: "New conditions",
    70  			args: args{
    71  				conditions: []*v1alpha1.IstioCondition{
    72  					{
    73  						Type:    "PassedValidation",
    74  						Status:  "True",
    75  						Message: "just a test, here",
    76  					},
    77  					{
    78  						Type:    "Reconciled",
    79  						Status:  "False",
    80  						Message: "1/2 proxies up to date.",
    81  					},
    82  				},
    83  				condition: &v1alpha1.IstioCondition{
    84  					Type:    "SomeRandomType",
    85  					Status:  "True",
    86  					Message: "just a new condition",
    87  				},
    88  			},
    89  			want: []*v1alpha1.IstioCondition{
    90  				{
    91  					Type:    "PassedValidation",
    92  					Status:  "True",
    93  					Message: "just a test, here",
    94  				},
    95  				{
    96  					Type:    "Reconciled",
    97  					Status:  "False",
    98  					Message: "1/2 proxies up to date.",
    99  				},
   100  				{
   101  					Type:    "SomeRandomType",
   102  					Status:  "True",
   103  					Message: "just a new condition",
   104  				},
   105  			},
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			if got := updateCondition(tt.args.conditions, tt.args.condition); !reflect.DeepEqual(got, tt.want) {
   111  				t.Errorf("updateCondition() = %v, want %v", got, tt.want)
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func Test_deleteCondition(t *testing.T) {
   118  	type args struct {
   119  		conditions []*v1alpha1.IstioCondition
   120  		condition  string
   121  	}
   122  	tests := []struct {
   123  		name string
   124  		args args
   125  		want []*v1alpha1.IstioCondition
   126  	}{
   127  		{
   128  			name: "Delete conditions",
   129  			args: args{
   130  				conditions: []*v1alpha1.IstioCondition{
   131  					{
   132  						Type:    "PassedValidation",
   133  						Status:  "True",
   134  						Message: "just a test, here",
   135  					},
   136  					{
   137  						Type:    "PassedValidation",
   138  						Status:  "True",
   139  						Message: "just a test, here2",
   140  					},
   141  					{
   142  						Type:    "Reconciled",
   143  						Status:  "False",
   144  						Message: "1/2 proxies up to date.",
   145  					},
   146  				},
   147  				condition: "PassedValidation",
   148  			},
   149  			want: []*v1alpha1.IstioCondition{
   150  				{
   151  					Type:    "Reconciled",
   152  					Status:  "False",
   153  					Message: "1/2 proxies up to date.",
   154  				},
   155  			},
   156  		},
   157  	}
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			if got := deleteCondition(tt.args.conditions, tt.args.condition); !reflect.DeepEqual(got, tt.want) {
   161  				t.Errorf("deleteCondition() = %v, want %v", got, tt.want)
   162  			}
   163  		})
   164  	}
   165  }