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