...

Source file src/github.com/fluxcd/helm-controller/api/v2beta2/annotations_test.go

Documentation: github.com/fluxcd/helm-controller/api/v2beta2

     1  /*
     2  Copyright 2023 The Flux authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v2beta2
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	"github.com/fluxcd/pkg/apis/meta"
    25  )
    26  
    27  func TestShouldHandleResetRequest(t *testing.T) {
    28  	t.Run("should handle reset request", func(t *testing.T) {
    29  		obj := &HelmRelease{
    30  			ObjectMeta: metav1.ObjectMeta{
    31  				Annotations: map[string]string{
    32  					meta.ReconcileRequestAnnotation: "b",
    33  					ResetRequestAnnotation:          "b",
    34  				},
    35  			},
    36  			Status: HelmReleaseStatus{
    37  				LastHandledResetAt: "a",
    38  				ReconcileRequestStatus: meta.ReconcileRequestStatus{
    39  					LastHandledReconcileAt: "a",
    40  				},
    41  			},
    42  		}
    43  
    44  		if !ShouldHandleResetRequest(obj) {
    45  			t.Error("ShouldHandleResetRequest() = false")
    46  		}
    47  
    48  		if obj.Status.LastHandledResetAt != "b" {
    49  			t.Error("ShouldHandleResetRequest did not update LastHandledResetAt")
    50  		}
    51  	})
    52  }
    53  
    54  func TestShouldHandleForceRequest(t *testing.T) {
    55  	t.Run("should handle force request", func(t *testing.T) {
    56  		obj := &HelmRelease{
    57  			ObjectMeta: metav1.ObjectMeta{
    58  				Annotations: map[string]string{
    59  					meta.ReconcileRequestAnnotation: "b",
    60  					ForceRequestAnnotation:          "b",
    61  				},
    62  			},
    63  			Status: HelmReleaseStatus{
    64  				LastHandledForceAt: "a",
    65  				ReconcileRequestStatus: meta.ReconcileRequestStatus{
    66  					LastHandledReconcileAt: "a",
    67  				},
    68  			},
    69  		}
    70  
    71  		if !ShouldHandleForceRequest(obj) {
    72  			t.Error("ShouldHandleForceRequest() = false")
    73  		}
    74  
    75  		if obj.Status.LastHandledForceAt != "b" {
    76  			t.Error("ShouldHandleForceRequest did not update LastHandledForceAt")
    77  		}
    78  	})
    79  }
    80  
    81  func Test_handleRequest(t *testing.T) {
    82  	const requestAnnotation = "requestAnnotation"
    83  
    84  	tests := []struct {
    85  		name                     string
    86  		annotations              map[string]string
    87  		lastHandledReconcile     string
    88  		lastHandledRequest       string
    89  		want                     bool
    90  		expectLastHandledRequest string
    91  	}{
    92  		{
    93  			name: "valid request and reconcile annotations",
    94  			annotations: map[string]string{
    95  				meta.ReconcileRequestAnnotation: "b",
    96  				requestAnnotation:               "b",
    97  			},
    98  			want:                     true,
    99  			expectLastHandledRequest: "b",
   100  		},
   101  		{
   102  			name: "mismatched annotations",
   103  			annotations: map[string]string{
   104  				meta.ReconcileRequestAnnotation: "b",
   105  				requestAnnotation:               "c",
   106  			},
   107  			want:                     false,
   108  			expectLastHandledRequest: "c",
   109  		},
   110  		{
   111  			name: "reconcile matches previous request",
   112  			annotations: map[string]string{
   113  				meta.ReconcileRequestAnnotation: "b",
   114  				requestAnnotation:               "b",
   115  			},
   116  			lastHandledReconcile:     "a",
   117  			lastHandledRequest:       "b",
   118  			want:                     false,
   119  			expectLastHandledRequest: "b",
   120  		},
   121  		{
   122  			name: "request matches previous reconcile",
   123  			annotations: map[string]string{
   124  				meta.ReconcileRequestAnnotation: "b",
   125  				requestAnnotation:               "b",
   126  			},
   127  			lastHandledReconcile:     "b",
   128  			lastHandledRequest:       "a",
   129  			want:                     false,
   130  			expectLastHandledRequest: "b",
   131  		},
   132  		{
   133  			name:                     "missing annotations",
   134  			annotations:              map[string]string{},
   135  			lastHandledRequest:       "a",
   136  			want:                     false,
   137  			expectLastHandledRequest: "a",
   138  		},
   139  	}
   140  
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			obj := &HelmRelease{
   144  				ObjectMeta: metav1.ObjectMeta{
   145  					Annotations: tt.annotations,
   146  				},
   147  				Status: HelmReleaseStatus{
   148  					ReconcileRequestStatus: meta.ReconcileRequestStatus{
   149  						LastHandledReconcileAt: tt.lastHandledReconcile,
   150  					},
   151  				},
   152  			}
   153  
   154  			lastHandled := tt.lastHandledRequest
   155  			result := handleRequest(obj, requestAnnotation, &lastHandled)
   156  
   157  			if result != tt.want {
   158  				t.Errorf("handleRequest() = %v, want %v", result, tt.want)
   159  			}
   160  			if lastHandled != tt.expectLastHandledRequest {
   161  				t.Errorf("lastHandledRequest = %v, want %v", lastHandled, tt.expectLastHandledRequest)
   162  			}
   163  		})
   164  	}
   165  }
   166  

View as plain text