istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/status/distribution/state_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 distribution 16 17 import ( 18 "encoding/json" 19 "reflect" 20 "testing" 21 22 "istio.io/api/meta/v1alpha1" 23 "istio.io/istio/pilot/pkg/status" 24 "istio.io/istio/pkg/config" 25 "istio.io/istio/pkg/test/util/assert" 26 ) 27 28 var statusStillPropagating = &v1alpha1.IstioStatus{ 29 Conditions: []*v1alpha1.IstioCondition{ 30 { 31 Type: "PassedValidation", 32 Status: "True", 33 Message: "just a test, here", 34 }, 35 { 36 Type: "Reconciled", 37 Status: "False", 38 Message: "1/2 proxies up to date.", 39 }, 40 }, 41 ValidationMessages: nil, 42 } 43 44 func TestReconcileStatuses(t *testing.T) { 45 type args struct { 46 current *config.Config 47 desired Progress 48 } 49 tests := []struct { 50 name string 51 args args 52 want bool 53 want1 *v1alpha1.IstioStatus 54 }{ 55 { 56 name: "Don't Reconcile when other fields are the only diff", 57 args: args{ 58 current: &config.Config{Status: statusStillPropagating}, 59 desired: Progress{1, 2}, 60 }, 61 want: false, 62 }, { 63 name: "Simple Reconcile to true", 64 args: args{ 65 current: &config.Config{Status: statusStillPropagating}, 66 desired: Progress{1, 3}, 67 }, 68 want: true, 69 want1: &v1alpha1.IstioStatus{ 70 Conditions: []*v1alpha1.IstioCondition{ 71 { 72 Type: "PassedValidation", 73 Status: "True", 74 Message: "just a test, here", 75 }, 76 { 77 Type: "Reconciled", 78 Status: "False", 79 Message: "1/3 proxies up to date.", 80 }, 81 }, 82 ValidationMessages: nil, 83 }, 84 }, { 85 name: "Simple Reconcile to false", 86 args: args{ 87 current: &config.Config{Status: statusStillPropagating}, 88 desired: Progress{2, 2}, 89 }, 90 want: true, 91 want1: &v1alpha1.IstioStatus{ 92 Conditions: []*v1alpha1.IstioCondition{ 93 { 94 Type: "PassedValidation", 95 Status: "True", 96 Message: "just a test, here", 97 }, 98 { 99 Type: "Reconciled", 100 Status: "True", 101 Message: "2/2 proxies up to date.", 102 }, 103 }, 104 ValidationMessages: nil, 105 }, 106 }, { 107 name: "Reconcile for message difference", 108 args: args{ 109 current: &config.Config{Status: statusStillPropagating}, 110 desired: Progress{2, 3}, 111 }, 112 want: true, 113 want1: &v1alpha1.IstioStatus{ 114 Conditions: []*v1alpha1.IstioCondition{ 115 { 116 Type: "PassedValidation", 117 Status: "True", 118 Message: "just a test, here", 119 }, 120 { 121 Type: "Reconciled", 122 Status: "False", 123 Message: "2/3 proxies up to date.", 124 }, 125 }, 126 }, 127 }, 128 } 129 for _, tt := range tests { 130 t.Run(tt.name, func(t *testing.T) { 131 got, got1 := ReconcileStatuses(tt.args.current.Status.(*v1alpha1.IstioStatus), tt.args.desired) 132 if got != tt.want { 133 t.Errorf("ReconcileStatuses() got = %v, want %v", got, tt.want) 134 } 135 if tt.want1 != nil { 136 for i := range tt.want1.Conditions { 137 if got1 != nil && i < len(got1.Conditions) { 138 tt.want1.Conditions[i].LastTransitionTime = got1.Conditions[i].LastTransitionTime 139 tt.want1.Conditions[i].LastProbeTime = got1.Conditions[i].LastProbeTime 140 } 141 } 142 assert.Equal(t, got1, tt.want1) 143 } 144 }) 145 } 146 } 147 148 func Test_getTypedStatus(t *testing.T) { 149 x := v1alpha1.IstioStatus{} 150 b, _ := json.Marshal(statusStillPropagating) 151 _ = json.Unmarshal(b, &x) 152 type args struct { 153 in any 154 } 155 tests := []struct { 156 name string 157 args args 158 wantOut *v1alpha1.IstioStatus 159 wantErr bool 160 }{ 161 { 162 name: "Nondestructive cast", 163 args: args{in: statusStillPropagating}, 164 wantOut: statusStillPropagating, 165 }, 166 } 167 for _, tt := range tests { 168 t.Run(tt.name, func(t *testing.T) { 169 gotOut, err := status.GetTypedStatus(tt.args.in) 170 if (err != nil) != tt.wantErr { 171 t.Errorf("GetTypedStatus() error = %v, wantErr %v", err, tt.wantErr) 172 return 173 } 174 if !reflect.DeepEqual(gotOut, tt.wantOut) { 175 t.Errorf("GetTypedStatus() gotOut = %v, want %v", gotOut, tt.wantOut) 176 } 177 }) 178 } 179 }