|
| 1 | +/* |
| 2 | +Copyright 2025 Red Hat |
| 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 operator |
| 18 | + |
| 19 | +import ( |
| 20 | + "strconv" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetEnvInDuration(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + envName string |
| 32 | + envValue string |
| 33 | + wantErr bool |
| 34 | + expect time.Duration |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "Test valid", |
| 38 | + envName: "VALID", |
| 39 | + envValue: "30", |
| 40 | + wantErr: false, |
| 41 | + expect: time.Duration(30 * time.Second), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Test invvalid", |
| 45 | + envName: "INVALID", |
| 46 | + envValue: "3x0", |
| 47 | + wantErr: true, |
| 48 | + expect: time.Duration(60 * time.Second), |
| 49 | + }, |
| 50 | + } |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + t.Setenv(tt.envName, tt.envValue) |
| 54 | + res, err := getEnvInDuration(tt.envName) |
| 55 | + if tt.wantErr { |
| 56 | + if err == nil { |
| 57 | + t.Errorf("getEnvInDuration() expected error but got none") |
| 58 | + } |
| 59 | + return |
| 60 | + } else if err != nil { |
| 61 | + t.Errorf("getEnvInDuration() unexpected error: %v", err) |
| 62 | + return |
| 63 | + } |
| 64 | + if res != tt.expect { |
| 65 | + t.Errorf("getEnvInDuration() got = %v, want %v", res, tt.expect) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestSetManagerOptions(t *testing.T) { |
| 72 | + var durationInt int64 |
| 73 | + var expectedValue time.Duration |
| 74 | + setupLog := logr.New(nil) |
| 75 | + |
| 76 | + tests := []struct { |
| 77 | + name string |
| 78 | + leaseDuration string |
| 79 | + renewDeadline string |
| 80 | + retryPeriod string |
| 81 | + wantErr bool |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "Test SetOperatorOptions valid values", |
| 85 | + leaseDuration: "137", |
| 86 | + renewDeadline: "107", |
| 87 | + retryPeriod: "26", |
| 88 | + wantErr: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Test SetOperatorOptions invalid values", |
| 92 | + leaseDuration: "foo", |
| 93 | + renewDeadline: "bar", |
| 94 | + retryPeriod: "INVALID", |
| 95 | + wantErr: true, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + t.Setenv("LEASE_DURATION", tt.leaseDuration) |
| 102 | + t.Setenv("RENEW_DEADLINE", tt.renewDeadline) |
| 103 | + t.Setenv("RETRY_PERIOD", tt.retryPeriod) |
| 104 | + options := ctrl.Options{} |
| 105 | + err := SetManagerOptions(&options, setupLog) |
| 106 | + if tt.wantErr { |
| 107 | + if err == nil { |
| 108 | + t.Errorf("SetOperatorOptions() expected error but got none") |
| 109 | + } |
| 110 | + return |
| 111 | + } else if err != nil { |
| 112 | + t.Errorf("SetOperatorOptions() unexpected error: %v", err) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + durationInt, _ = strconv.ParseInt(tt.leaseDuration, 10, 64) |
| 117 | + expectedValue = time.Duration(durationInt) * time.Second |
| 118 | + if *options.LeaseDuration != expectedValue { |
| 119 | + t.Errorf("SetOperatorOptions() got = %v, want %v", options.LeaseDuration, expectedValue) |
| 120 | + } |
| 121 | + |
| 122 | + durationInt, _ = strconv.ParseInt(tt.renewDeadline, 10, 64) |
| 123 | + expectedValue = time.Duration(durationInt) * time.Second |
| 124 | + if *options.RenewDeadline != expectedValue { |
| 125 | + t.Errorf("SetOperatorOptions() got = %v, want %v", options.RenewDeadline, expectedValue) |
| 126 | + } |
| 127 | + |
| 128 | + durationInt, _ = strconv.ParseInt(tt.retryPeriod, 10, 64) |
| 129 | + expectedValue = time.Duration(durationInt) * time.Second |
| 130 | + if *options.RetryPeriod != expectedValue { |
| 131 | + t.Errorf("SetOperatorOptions() got = %v, want %v", options.RetryPeriod, expectedValue) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments