github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/cluster/jackal_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package cluster contains Jackal-specific cluster management functions.
     5  package cluster
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/Racer159/jackal/src/types"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  // TestPackageSecretNeedsWait verifies that Jackal waits for webhooks to complete correctly.
    15  func TestPackageSecretNeedsWait(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	type testCase struct {
    19  		name            string
    20  		deployedPackage *types.DeployedPackage
    21  		component       types.JackalComponent
    22  		skipWebhooks    bool
    23  		needsWait       bool
    24  		waitSeconds     int
    25  		hookName        string
    26  	}
    27  
    28  	var (
    29  		componentName = "test-component"
    30  		packageName   = "test-package"
    31  		webhookName   = "test-webhook"
    32  	)
    33  
    34  	testCases := []testCase{
    35  		{
    36  			name:      "NoWebhooks",
    37  			component: types.JackalComponent{Name: componentName},
    38  			deployedPackage: &types.DeployedPackage{
    39  				Name:              packageName,
    40  				ComponentWebhooks: map[string]map[string]types.Webhook{},
    41  			},
    42  			needsWait:   false,
    43  			waitSeconds: 0,
    44  			hookName:    "",
    45  		},
    46  		{
    47  			name:      "WebhookRunning",
    48  			component: types.JackalComponent{Name: componentName},
    49  			deployedPackage: &types.DeployedPackage{
    50  				Name: packageName,
    51  				ComponentWebhooks: map[string]map[string]types.Webhook{
    52  					componentName: {
    53  						webhookName: types.Webhook{
    54  							Status:              types.WebhookStatusRunning,
    55  							WaitDurationSeconds: 10,
    56  						},
    57  					},
    58  				},
    59  			},
    60  			needsWait:   true,
    61  			waitSeconds: 10,
    62  			hookName:    webhookName,
    63  		},
    64  		// Ensure we only wait on running webhooks for the provided component
    65  		{
    66  			name:      "WebhookRunningOnDifferentComponent",
    67  			component: types.JackalComponent{Name: componentName},
    68  			deployedPackage: &types.DeployedPackage{
    69  				Name: packageName,
    70  				ComponentWebhooks: map[string]map[string]types.Webhook{
    71  					"different-component": {
    72  						webhookName: types.Webhook{
    73  							Status:              types.WebhookStatusRunning,
    74  							WaitDurationSeconds: 10,
    75  						},
    76  					},
    77  				},
    78  			},
    79  			needsWait:   false,
    80  			waitSeconds: 0,
    81  			hookName:    "",
    82  		},
    83  		{
    84  			name:      "WebhookSucceeded",
    85  			component: types.JackalComponent{Name: componentName},
    86  			deployedPackage: &types.DeployedPackage{
    87  				Name: packageName,
    88  				ComponentWebhooks: map[string]map[string]types.Webhook{
    89  					componentName: {
    90  						webhookName: types.Webhook{
    91  							Status: types.WebhookStatusSucceeded,
    92  						},
    93  					},
    94  				},
    95  			},
    96  			needsWait:   false,
    97  			waitSeconds: 0,
    98  			hookName:    "",
    99  		},
   100  		{
   101  			name:      "WebhookFailed",
   102  			component: types.JackalComponent{Name: componentName},
   103  			deployedPackage: &types.DeployedPackage{
   104  				Name: packageName,
   105  				ComponentWebhooks: map[string]map[string]types.Webhook{
   106  					componentName: {
   107  						webhookName: types.Webhook{
   108  							Status: types.WebhookStatusFailed,
   109  						},
   110  					},
   111  				},
   112  			},
   113  			needsWait:   false,
   114  			waitSeconds: 0,
   115  			hookName:    "",
   116  		},
   117  		{
   118  			name:      "WebhookRemoving",
   119  			component: types.JackalComponent{Name: componentName},
   120  			deployedPackage: &types.DeployedPackage{
   121  				Name: packageName,
   122  				ComponentWebhooks: map[string]map[string]types.Webhook{
   123  					componentName: {
   124  						webhookName: types.Webhook{
   125  							Status: types.WebhookStatusRemoving,
   126  						},
   127  					},
   128  				},
   129  			},
   130  			needsWait:   false,
   131  			waitSeconds: 0,
   132  			hookName:    "",
   133  		},
   134  		{
   135  			name:      "SkipWaitForYOLO",
   136  			component: types.JackalComponent{Name: componentName},
   137  			deployedPackage: &types.DeployedPackage{
   138  				Name: packageName,
   139  				Data: types.JackalPackage{
   140  					Metadata: types.JackalMetadata{
   141  						YOLO: true,
   142  					},
   143  				},
   144  				ComponentWebhooks: map[string]map[string]types.Webhook{
   145  					componentName: {
   146  						webhookName: types.Webhook{
   147  							Status:              types.WebhookStatusRunning,
   148  							WaitDurationSeconds: 10,
   149  						},
   150  					},
   151  				},
   152  			},
   153  			needsWait:   false,
   154  			waitSeconds: 0,
   155  			hookName:    "",
   156  		},
   157  		{
   158  			name:         "SkipWebhooksFlagUsed",
   159  			component:    types.JackalComponent{Name: componentName},
   160  			skipWebhooks: true,
   161  			deployedPackage: &types.DeployedPackage{
   162  				Name: packageName,
   163  				ComponentWebhooks: map[string]map[string]types.Webhook{
   164  					componentName: {
   165  						webhookName: types.Webhook{
   166  							Status:              types.WebhookStatusRunning,
   167  							WaitDurationSeconds: 10,
   168  						},
   169  					},
   170  				},
   171  			},
   172  			needsWait:   false,
   173  			waitSeconds: 0,
   174  			hookName:    "",
   175  		},
   176  	}
   177  
   178  	for _, testCase := range testCases {
   179  		testCase := testCase
   180  
   181  		t.Run(testCase.name, func(t *testing.T) {
   182  			t.Parallel()
   183  
   184  			c := &Cluster{}
   185  
   186  			needsWait, waitSeconds, hookName := c.PackageSecretNeedsWait(testCase.deployedPackage, testCase.component, testCase.skipWebhooks)
   187  
   188  			require.Equal(t, testCase.needsWait, needsWait)
   189  			require.Equal(t, testCase.waitSeconds, waitSeconds)
   190  			require.Equal(t, testCase.hookName, hookName)
   191  		})
   192  	}
   193  }