github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/reconciler/grpc_polling_test.go (about)

     1  package reconciler
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  
     9  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
    10  	"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
    11  )
    12  
    13  func TestSyncRegistryUpdateInterval(t *testing.T) {
    14  	now := time.Date(2021, time.January, 29, 14, 47, 0, 0, time.UTC)
    15  	tests := []struct {
    16  		name     string
    17  		source   *v1alpha1.CatalogSource
    18  		expected time.Duration
    19  	}{
    20  		{
    21  			name: "PollingInterval10Minutes/FirstUpdate",
    22  			source: &v1alpha1.CatalogSource{
    23  				Spec: v1alpha1.CatalogSourceSpec{
    24  					UpdateStrategy: &v1alpha1.UpdateStrategy{
    25  						RegistryPoll: &v1alpha1.RegistryPoll{
    26  							Interval: &metav1.Duration{
    27  								Duration: 10 * time.Minute,
    28  							},
    29  						},
    30  					},
    31  				},
    32  			},
    33  			expected: 10 * time.Minute,
    34  		},
    35  		{
    36  			name: "PollingInterval15Minutes/FirstUpdate",
    37  			source: &v1alpha1.CatalogSource{
    38  				Spec: v1alpha1.CatalogSourceSpec{
    39  					UpdateStrategy: &v1alpha1.UpdateStrategy{
    40  						RegistryPoll: &v1alpha1.RegistryPoll{
    41  							Interval: &metav1.Duration{
    42  								Duration: 15 * time.Minute,
    43  							},
    44  						},
    45  					},
    46  				},
    47  			},
    48  			expected: 15 * time.Minute,
    49  		},
    50  		{
    51  			name: "PollingIntervalMultipleOfDefaultResyncPeriod",
    52  			source: &v1alpha1.CatalogSource{
    53  				Spec: v1alpha1.CatalogSourceSpec{
    54  					UpdateStrategy: &v1alpha1.UpdateStrategy{
    55  						RegistryPoll: &v1alpha1.RegistryPoll{
    56  							Interval: &metav1.Duration{
    57  								Duration: 2 * queueinformer.DefaultResyncPeriod,
    58  							},
    59  						},
    60  					},
    61  				},
    62  				Status: v1alpha1.CatalogSourceStatus{
    63  					LatestImageRegistryPoll: &metav1.Time{
    64  						Time: now.Add(1*time.Second - 2*queueinformer.DefaultResyncPeriod),
    65  					},
    66  				},
    67  			},
    68  			expected: 1 * time.Second,
    69  		},
    70  		{
    71  			name: "PollingInterval10Minutes/AlreadyUpdated",
    72  			source: &v1alpha1.CatalogSource{
    73  				Status: v1alpha1.CatalogSourceStatus{
    74  					LatestImageRegistryPoll: &metav1.Time{
    75  						Time: now.Add(-(5 * time.Minute)),
    76  					},
    77  				},
    78  				Spec: v1alpha1.CatalogSourceSpec{
    79  					UpdateStrategy: &v1alpha1.UpdateStrategy{
    80  						RegistryPoll: &v1alpha1.RegistryPoll{
    81  							Interval: &metav1.Duration{
    82  								Duration: 10 * time.Minute,
    83  							},
    84  						},
    85  					},
    86  				},
    87  			},
    88  			expected: 10 * time.Minute,
    89  		},
    90  		{
    91  			name: "PollingInterval40Minutes/FirstUpdate",
    92  			source: &v1alpha1.CatalogSource{
    93  				ObjectMeta: metav1.ObjectMeta{
    94  					CreationTimestamp: metav1.Time{
    95  						Time: now.Add(-(35 * time.Minute)),
    96  					},
    97  				},
    98  				Spec: v1alpha1.CatalogSourceSpec{
    99  					UpdateStrategy: &v1alpha1.UpdateStrategy{
   100  						RegistryPoll: &v1alpha1.RegistryPoll{
   101  							Interval: &metav1.Duration{
   102  								Duration: 40 * time.Minute,
   103  							},
   104  						},
   105  					},
   106  				},
   107  			},
   108  			expected: 5 * time.Minute,
   109  		},
   110  		{
   111  			name: "PollingInterval40Minutes/AlreadyUpdated30MinutesAgo",
   112  			source: &v1alpha1.CatalogSource{
   113  				Status: v1alpha1.CatalogSourceStatus{
   114  					LatestImageRegistryPoll: &metav1.Time{
   115  						Time: now.Add(-(30 * time.Minute)),
   116  					},
   117  				},
   118  				Spec: v1alpha1.CatalogSourceSpec{
   119  					UpdateStrategy: &v1alpha1.UpdateStrategy{
   120  						RegistryPoll: &v1alpha1.RegistryPoll{
   121  							Interval: &metav1.Duration{
   122  								Duration: 40 * time.Minute,
   123  							},
   124  						},
   125  					},
   126  				},
   127  			},
   128  			expected: 10 * time.Minute,
   129  		},
   130  		{
   131  			name: "PollingInterval1hour/FirstUpdate",
   132  			source: &v1alpha1.CatalogSource{
   133  				ObjectMeta: metav1.ObjectMeta{
   134  					CreationTimestamp: metav1.Time{
   135  						Time: now.Add(-(15 * time.Minute)),
   136  					},
   137  				},
   138  				Spec: v1alpha1.CatalogSourceSpec{
   139  					UpdateStrategy: &v1alpha1.UpdateStrategy{
   140  						RegistryPoll: &v1alpha1.RegistryPoll{
   141  							Interval: &metav1.Duration{
   142  								Duration: 1 * time.Hour,
   143  							},
   144  						},
   145  					},
   146  				},
   147  			},
   148  			expected: queueinformer.DefaultResyncPeriod,
   149  		},
   150  		{
   151  			name: "PollingInterval10Hours/AlreadyUpdated",
   152  			source: &v1alpha1.CatalogSource{
   153  				Status: v1alpha1.CatalogSourceStatus{
   154  					LatestImageRegistryPoll: &metav1.Time{
   155  						Time: now.Add(-(15 * time.Minute)),
   156  					},
   157  				},
   158  				Spec: v1alpha1.CatalogSourceSpec{
   159  					UpdateStrategy: &v1alpha1.UpdateStrategy{
   160  						RegistryPoll: &v1alpha1.RegistryPoll{
   161  							Interval: &metav1.Duration{
   162  								Duration: 10 * time.Hour,
   163  							},
   164  						},
   165  					},
   166  				},
   167  			},
   168  			expected: queueinformer.DefaultResyncPeriod,
   169  		},
   170  	}
   171  
   172  	for _, tt := range tests {
   173  		t.Run(tt.name, func(t *testing.T) {
   174  			d := SyncRegistryUpdateInterval(tt.source, now)
   175  			if d != tt.expected {
   176  				t.Errorf("unexpected registry sync interval for %s: expected %s got %s", tt.name, tt.expected, d)
   177  			}
   178  		})
   179  	}
   180  }