...

Source file src/sigs.k8s.io/cli-utils/pkg/printers/testutil/common.go

Documentation: sigs.k8s.io/cli-utils/pkg/printers/testutil

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package testutil
     5  
     6  import (
     7  	"fmt"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"k8s.io/apimachinery/pkg/runtime/schema"
    13  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    14  	"sigs.k8s.io/cli-utils/pkg/common"
    15  	"sigs.k8s.io/cli-utils/pkg/object"
    16  	printcommon "sigs.k8s.io/cli-utils/pkg/print/common"
    17  	"sigs.k8s.io/cli-utils/pkg/print/stats"
    18  	"sigs.k8s.io/cli-utils/pkg/printers/printer"
    19  )
    20  
    21  type PrinterFactoryFunc func() printer.Printer
    22  
    23  func PrintResultErrorTest(t *testing.T, f PrinterFactoryFunc) {
    24  	deploymentIdentifier := object.ObjMetadata{
    25  		GroupKind: schema.GroupKind{
    26  			Group: "apps",
    27  			Kind:  "Deployment",
    28  		},
    29  		Name:      "foo",
    30  		Namespace: "bar",
    31  	}
    32  
    33  	testCases := map[string]struct {
    34  		events      []event.Event
    35  		expectedErr error
    36  	}{
    37  		"successful apply, prune and reconcile": {
    38  			events: []event.Event{
    39  				{
    40  					Type: event.InitType,
    41  					InitEvent: event.InitEvent{
    42  						ActionGroups: event.ActionGroupList{
    43  							{
    44  								Name:   "apply-1",
    45  								Action: event.ApplyAction,
    46  								Identifiers: []object.ObjMetadata{
    47  									deploymentIdentifier,
    48  								},
    49  							},
    50  							{
    51  								Name:   "wait-1",
    52  								Action: event.WaitAction,
    53  								Identifiers: []object.ObjMetadata{
    54  									deploymentIdentifier,
    55  								},
    56  							},
    57  						},
    58  					},
    59  				},
    60  				{
    61  					Type: event.ActionGroupType,
    62  					ActionGroupEvent: event.ActionGroupEvent{
    63  						GroupName: "apply-1",
    64  						Action:    event.ApplyAction,
    65  						Status:    event.Started,
    66  					},
    67  				},
    68  				{
    69  					Type: event.ApplyType,
    70  					ApplyEvent: event.ApplyEvent{
    71  						GroupName:  "apply-1",
    72  						Status:     event.ApplySuccessful,
    73  						Identifier: deploymentIdentifier,
    74  					},
    75  				},
    76  				{
    77  					Type: event.ActionGroupType,
    78  					ActionGroupEvent: event.ActionGroupEvent{
    79  						GroupName: "apply-1",
    80  						Action:    event.ApplyAction,
    81  						Status:    event.Finished,
    82  					},
    83  				},
    84  				{
    85  					Type: event.ActionGroupType,
    86  					ActionGroupEvent: event.ActionGroupEvent{
    87  						GroupName: "wait-1",
    88  						Action:    event.WaitAction,
    89  						Status:    event.Started,
    90  					},
    91  				},
    92  				{
    93  					Type: event.WaitType,
    94  					WaitEvent: event.WaitEvent{
    95  						GroupName:  "wait-1",
    96  						Status:     event.ReconcileSuccessful,
    97  						Identifier: deploymentIdentifier,
    98  					},
    99  				},
   100  				{
   101  					Type: event.ActionGroupType,
   102  					ActionGroupEvent: event.ActionGroupEvent{
   103  						GroupName: "wait-1",
   104  						Action:    event.WaitAction,
   105  						Status:    event.Finished,
   106  					},
   107  				},
   108  			},
   109  			expectedErr: nil,
   110  		},
   111  		"successful apply, failed reconcile": {
   112  			events: []event.Event{
   113  				{
   114  					Type: event.InitType,
   115  					InitEvent: event.InitEvent{
   116  						ActionGroups: event.ActionGroupList{
   117  							{
   118  								Name:   "apply-1",
   119  								Action: event.ApplyAction,
   120  								Identifiers: []object.ObjMetadata{
   121  									deploymentIdentifier,
   122  								},
   123  							},
   124  							{
   125  								Name:   "wait-1",
   126  								Action: event.WaitAction,
   127  								Identifiers: []object.ObjMetadata{
   128  									deploymentIdentifier,
   129  								},
   130  							},
   131  						},
   132  					},
   133  				},
   134  				{
   135  					Type: event.ActionGroupType,
   136  					ActionGroupEvent: event.ActionGroupEvent{
   137  						GroupName: "apply-1",
   138  						Action:    event.ApplyAction,
   139  						Status:    event.Started,
   140  					},
   141  				},
   142  				{
   143  					Type: event.ApplyType,
   144  					ApplyEvent: event.ApplyEvent{
   145  						GroupName:  "apply-1",
   146  						Status:     event.ApplySuccessful,
   147  						Identifier: deploymentIdentifier,
   148  					},
   149  				},
   150  				{
   151  					Type: event.ActionGroupType,
   152  					ActionGroupEvent: event.ActionGroupEvent{
   153  						GroupName: "apply-1",
   154  						Action:    event.ApplyAction,
   155  						Status:    event.Finished,
   156  					},
   157  				},
   158  				{
   159  					Type: event.ActionGroupType,
   160  					ActionGroupEvent: event.ActionGroupEvent{
   161  						GroupName: "wait-1",
   162  						Action:    event.WaitAction,
   163  						Status:    event.Started,
   164  					},
   165  				},
   166  				{
   167  					Type: event.WaitType,
   168  					WaitEvent: event.WaitEvent{
   169  						GroupName:  "wait-1",
   170  						Status:     event.ReconcileFailed,
   171  						Identifier: deploymentIdentifier,
   172  					},
   173  				},
   174  				{
   175  					Type: event.ActionGroupType,
   176  					ActionGroupEvent: event.ActionGroupEvent{
   177  						GroupName: "wait-1",
   178  						Action:    event.WaitAction,
   179  						Status:    event.Finished,
   180  					},
   181  				},
   182  			},
   183  			expectedErr: &printcommon.ResultError{
   184  				Stats: stats.Stats{
   185  					ApplyStats: stats.ApplyStats{
   186  						Successful: 1,
   187  					},
   188  					WaitStats: stats.WaitStats{
   189  						Failed: 1,
   190  					},
   191  				},
   192  			},
   193  		},
   194  		"failed apply": {
   195  			events: []event.Event{
   196  				{
   197  					Type: event.InitType,
   198  					InitEvent: event.InitEvent{
   199  						ActionGroups: event.ActionGroupList{
   200  							{
   201  								Name:   "apply-1",
   202  								Action: event.ApplyAction,
   203  								Identifiers: []object.ObjMetadata{
   204  									deploymentIdentifier,
   205  								},
   206  							},
   207  							{
   208  								Name:   "wait-1",
   209  								Action: event.WaitAction,
   210  								Identifiers: []object.ObjMetadata{
   211  									deploymentIdentifier,
   212  								},
   213  							},
   214  						},
   215  					},
   216  				},
   217  				{
   218  					Type: event.ActionGroupType,
   219  					ActionGroupEvent: event.ActionGroupEvent{
   220  						GroupName: "apply-1",
   221  						Action:    event.ApplyAction,
   222  						Status:    event.Started,
   223  					},
   224  				},
   225  				{
   226  					Type: event.ApplyType,
   227  					ApplyEvent: event.ApplyEvent{
   228  						GroupName:  "apply-1",
   229  						Status:     event.ApplyFailed,
   230  						Identifier: deploymentIdentifier,
   231  						Error:      fmt.Errorf("apply failed"),
   232  					},
   233  				},
   234  				{
   235  					Type: event.ActionGroupType,
   236  					ActionGroupEvent: event.ActionGroupEvent{
   237  						GroupName: "apply-1",
   238  						Action:    event.ApplyAction,
   239  						Status:    event.Finished,
   240  					},
   241  				},
   242  				{
   243  					Type: event.ActionGroupType,
   244  					ActionGroupEvent: event.ActionGroupEvent{
   245  						GroupName: "wait-1",
   246  						Action:    event.WaitAction,
   247  						Status:    event.Started,
   248  					},
   249  				},
   250  				{
   251  					Type: event.WaitType,
   252  					WaitEvent: event.WaitEvent{
   253  						GroupName:  "wait-1",
   254  						Status:     event.ReconcileSkipped,
   255  						Identifier: deploymentIdentifier,
   256  					},
   257  				},
   258  				{
   259  					Type: event.ActionGroupType,
   260  					ActionGroupEvent: event.ActionGroupEvent{
   261  						GroupName: "wait-1",
   262  						Action:    event.WaitAction,
   263  						Status:    event.Finished,
   264  					},
   265  				},
   266  			},
   267  			expectedErr: &printcommon.ResultError{
   268  				Stats: stats.Stats{
   269  					ApplyStats: stats.ApplyStats{
   270  						Failed: 1,
   271  					},
   272  					WaitStats: stats.WaitStats{
   273  						Skipped: 1,
   274  					},
   275  				},
   276  			},
   277  		},
   278  	}
   279  
   280  	for tn := range testCases {
   281  		tc := testCases[tn]
   282  		t.Run(tn, func(t *testing.T) {
   283  			p := f()
   284  
   285  			eventChannel := make(chan event.Event)
   286  
   287  			var wg sync.WaitGroup
   288  			var err error
   289  
   290  			wg.Add(1)
   291  			go func() {
   292  				err = p.Print(eventChannel, common.DryRunNone, false)
   293  				wg.Done()
   294  			}()
   295  
   296  			for i := range tc.events {
   297  				e := tc.events[i]
   298  				eventChannel <- e
   299  			}
   300  			close(eventChannel)
   301  
   302  			wg.Wait()
   303  
   304  			assert.Equal(t, tc.expectedErr, err)
   305  		})
   306  	}
   307  }
   308  

View as plain text