istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/compare/sds/writer_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 sdscompare
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestSDSWriterSecretItems(t *testing.T) {
    24  	tests := []struct {
    25  		name       string
    26  		format     Format
    27  		items      []SecretItem
    28  		expected   []string
    29  		unexpected []string
    30  	}{
    31  		{
    32  			name:       "test tabular output with no secret items is equivalent to the header",
    33  			format:     TABULAR,
    34  			items:      []SecretItem{},
    35  			expected:   []string{},
    36  			unexpected: secretItemColumns,
    37  		},
    38  		{
    39  			name:   "test tabular output with a single secret item",
    40  			format: TABULAR,
    41  			items: []SecretItem{
    42  				{
    43  					Name:        "olinger",
    44  					Data:        "certdata",
    45  					Source:      "source",
    46  					Destination: "destination",
    47  					SecretMeta: SecretMeta{
    48  						Valid:        true,
    49  						SerialNumber: "serial_number",
    50  						NotAfter:     "expires",
    51  						NotBefore:    "valid",
    52  						Type:         "type",
    53  					},
    54  				},
    55  			},
    56  			expected: append(
    57  				[]string{"olinger", "serial_number", "expires", "valid", "type"},
    58  				secretItemColumns...),
    59  			unexpected: []string{"source", "destination", "certdata"},
    60  		},
    61  		{
    62  			name:   "test JSON output with a single secret item",
    63  			format: JSON,
    64  			items: []SecretItem{
    65  				{
    66  					Name:        "olinger",
    67  					Data:        "certdata",
    68  					Source:      "source",
    69  					Destination: "destination",
    70  					SecretMeta: SecretMeta{
    71  						Valid:        true,
    72  						SerialNumber: "serial_number",
    73  						NotAfter:     "expires",
    74  						NotBefore:    "valid",
    75  						Type:         "type",
    76  					},
    77  				},
    78  			},
    79  			expected: []string{"olinger", "source", "destination", "serial_number", "expires", "valid", "type", "certdata"},
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			w := &bytes.Buffer{}
    85  			mockWriter := NewSDSWriter(w, tt.format)
    86  			err := mockWriter.PrintSecretItems(tt.items)
    87  			if err != nil {
    88  				t.Errorf("error printing secret items: %v", err)
    89  			}
    90  			checkOutput(t, w.String(), tt.expected, tt.unexpected)
    91  		})
    92  	}
    93  }
    94  
    95  func TestSDSWriterSecretDiff(t *testing.T) {
    96  	tests := []struct {
    97  		name       string
    98  		format     Format
    99  		diffs      []SecretItemDiff
   100  		expected   []string
   101  		unexpected []string
   102  	}{
   103  		{
   104  			name:       "test tabular output with no secret items is equivalent to the header",
   105  			format:     TABULAR,
   106  			diffs:      []SecretItemDiff{},
   107  			expected:   []string{},
   108  			unexpected: secretDiffColumns,
   109  		},
   110  		{
   111  			name:   "test tabular output with a single secret diff",
   112  			format: TABULAR,
   113  			diffs: []SecretItemDiff{
   114  				{
   115  					Agent: "alligator",
   116  					Proxy: "proxy",
   117  					SecretItem: SecretItem{
   118  						Name:        "fields",
   119  						Data:        "certdata",
   120  						Source:      "should",
   121  						Destination: "destination",
   122  						SecretMeta: SecretMeta{
   123  							Valid:        true,
   124  							SerialNumber: "serial_number",
   125  							NotAfter:     "expires",
   126  							NotBefore:    "valid",
   127  							Type:         "type",
   128  						},
   129  					},
   130  				},
   131  			},
   132  			expected: append(
   133  				[]string{"fields", "should", "serial_number", "expires", "valid", "type", "proxy"},
   134  				secretDiffColumns...),
   135  			unexpected: []string{"alligator", "certdata"},
   136  		},
   137  		{
   138  			name:   "test JSON output with a single secret diff",
   139  			format: JSON,
   140  			diffs: []SecretItemDiff{
   141  				{
   142  					Agent: "alligator",
   143  					Proxy: "proxy",
   144  					SecretItem: SecretItem{
   145  						Name:        "fields",
   146  						Data:        "certdata",
   147  						Source:      "should",
   148  						Destination: "destination",
   149  						SecretMeta: SecretMeta{
   150  							Valid:        true,
   151  							SerialNumber: "serial_number",
   152  							NotAfter:     "expires",
   153  							NotBefore:    "valid",
   154  							Type:         "type",
   155  						},
   156  					},
   157  				},
   158  			},
   159  			expected: []string{"fields", "should", "serial_number", "expires", "valid", "type", "proxy", "certdata"},
   160  		},
   161  	}
   162  	for _, tt := range tests {
   163  		t.Run(tt.name, func(t *testing.T) {
   164  			w := &bytes.Buffer{}
   165  			mockWriter := NewSDSWriter(w, tt.format)
   166  			err := mockWriter.PrintDiffs(tt.diffs)
   167  			if err != nil {
   168  				t.Errorf("error printing secret items: %v", err)
   169  			}
   170  			checkOutput(t, w.String(), tt.expected, tt.unexpected)
   171  		})
   172  	}
   173  }
   174  
   175  func checkOutput(t *testing.T, output string, expected, unexpected []string) {
   176  	t.Helper()
   177  	for _, expected := range expected {
   178  		if !strings.Contains(output, expected) {
   179  			t.Errorf("expected %s included in writer output, did not find", expected)
   180  		}
   181  	}
   182  	for _, unexpected := range unexpected {
   183  		if strings.Contains(output, unexpected) {
   184  			t.Errorf("unexpected string %s included in writer output", unexpected)
   185  		}
   186  	}
   187  }