istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/authz/analyzer_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 authz
    16  
    17  import (
    18  	"bytes"
    19  	"reflect"
    20  	"testing"
    21  
    22  	envoy_admin "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
    23  	"google.golang.org/protobuf/types/known/anypb"
    24  	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
    25  
    26  	"istio.io/istio/istioctl/pkg/util/configdump"
    27  )
    28  
    29  func TestNewAnalyzer(t *testing.T) {
    30  	tests := []struct {
    31  		name    string
    32  		input   *configdump.Wrapper
    33  		wantErr error
    34  	}{
    35  		{
    36  			name: "Test1",
    37  			input: &configdump.Wrapper{
    38  				ConfigDump: &envoy_admin.ConfigDump{
    39  					Configs: []*anypb.Any{
    40  						{
    41  							TypeUrl: "type.googleapis.com/envoy.admin.v3.ListenersConfigDump",
    42  						},
    43  					},
    44  				},
    45  			},
    46  			wantErr: nil,
    47  		},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			if _, wanted2 := NewAnalyzer(tt.input); wanted2 != nil {
    52  				t.Errorf("Wanted %v, got %v", tt.wantErr, wanted2)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestPrint(t *testing.T) {
    59  	a := &Analyzer{
    60  		listenerDump: &envoy_admin.ListenersConfigDump{
    61  			DynamicListeners: []*envoy_admin.ListenersConfigDump_DynamicListener{
    62  				{
    63  					Name: "10.102.11.148_15021",
    64  					ActiveState: &envoy_admin.ListenersConfigDump_DynamicListenerState{
    65  						VersionInfo: "2023-06-20T09:07:41Z/3",
    66  						Listener: &anypb.Any{
    67  							TypeUrl: "type.googleapis.com/envoy.config.listener.v3.Listener",
    68  						},
    69  						LastUpdated: timestamppb.Now(),
    70  					},
    71  					ClientStatus: 453,
    72  				},
    73  			},
    74  		},
    75  	}
    76  	tests := []struct {
    77  		name  string
    78  		input *envoy_admin.ListenersConfigDump_DynamicListener
    79  	}{
    80  		{
    81  			name: "Test2",
    82  			input: &envoy_admin.ListenersConfigDump_DynamicListener{
    83  				Name: "First_Listener",
    84  				ActiveState: &envoy_admin.ListenersConfigDump_DynamicListenerState{
    85  					VersionInfo: "version1.5",
    86  					Listener: &anypb.Any{
    87  						TypeUrl: "type.googleapis.com/envoy.admin.v3.ListenersConfigDump",
    88  					},
    89  					LastUpdated: timestamppb.Now(),
    90  				},
    91  				ClientStatus: 453,
    92  			},
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		t.Run(tt.name, func(t *testing.T) {
    97  			var buf bytes.Buffer
    98  			a.Print(&buf)
    99  			expectedOutput := "ACTION   AuthorizationPolicy   RULES\n"
   100  			actualOutput := buf.String()
   101  			if !reflect.DeepEqual(expectedOutput, actualOutput) {
   102  				t.Errorf("Found %v, wanted %v", actualOutput, expectedOutput)
   103  			}
   104  		})
   105  	}
   106  }