|
| 1 | +// Copyright (c) JFrog Ltd. (2026) |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package configuration |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 22 | +) |
| 23 | + |
| 24 | +func TestPackageCleanupPolicyFromAPIModelPreservesNullCronExpression(t *testing.T) { |
| 25 | + model := PackageCleanupPolicyResourceModelV1{ |
| 26 | + PackageCleanupPolicyResourceModelV0: PackageCleanupPolicyResourceModelV0{ |
| 27 | + CronExpression: types.StringNull(), |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + diags := model.fromAPIModel(context.Background(), PackageCleanupPolicyAPIModel{ |
| 32 | + CronExpression: "", |
| 33 | + SearchCriteria: packageCleanupPolicyTestSearchCriteria(), |
| 34 | + }) |
| 35 | + |
| 36 | + if diags.HasError() { |
| 37 | + t.Fatalf("unexpected diagnostics: %s", diags.Errors()) |
| 38 | + } |
| 39 | + |
| 40 | + if !model.CronExpression.IsNull() { |
| 41 | + t.Fatalf("expected null cron_expression, got %q", model.CronExpression.ValueString()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestPackageCleanupPolicyFromAPIModelKeepsConfiguredEmptyCronExpression(t *testing.T) { |
| 46 | + model := PackageCleanupPolicyResourceModelV1{ |
| 47 | + PackageCleanupPolicyResourceModelV0: PackageCleanupPolicyResourceModelV0{ |
| 48 | + CronExpression: types.StringValue(""), |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + diags := model.fromAPIModel(context.Background(), PackageCleanupPolicyAPIModel{ |
| 53 | + CronExpression: "", |
| 54 | + SearchCriteria: packageCleanupPolicyTestSearchCriteria(), |
| 55 | + }) |
| 56 | + |
| 57 | + if diags.HasError() { |
| 58 | + t.Fatalf("unexpected diagnostics: %s", diags.Errors()) |
| 59 | + } |
| 60 | + |
| 61 | + if model.CronExpression.IsNull() { |
| 62 | + t.Fatal("expected configured empty cron_expression to stay as an empty string") |
| 63 | + } |
| 64 | + |
| 65 | + if model.CronExpression.ValueString() != "" { |
| 66 | + t.Fatalf("expected empty cron_expression, got %q", model.CronExpression.ValueString()) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestPackageCleanupPolicyFromAPIModelSetsNonEmptyCronExpression(t *testing.T) { |
| 71 | + model := PackageCleanupPolicyResourceModelV1{ |
| 72 | + PackageCleanupPolicyResourceModelV0: PackageCleanupPolicyResourceModelV0{ |
| 73 | + CronExpression: types.StringNull(), |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + diags := model.fromAPIModel(context.Background(), PackageCleanupPolicyAPIModel{ |
| 78 | + CronExpression: "0 0 2 ? * MON-SAT *", |
| 79 | + SearchCriteria: packageCleanupPolicyTestSearchCriteria(), |
| 80 | + }) |
| 81 | + |
| 82 | + if diags.HasError() { |
| 83 | + t.Fatalf("unexpected diagnostics: %s", diags.Errors()) |
| 84 | + } |
| 85 | + |
| 86 | + if model.CronExpression.ValueString() != "0 0 2 ? * MON-SAT *" { |
| 87 | + t.Fatalf("expected non-empty cron_expression, got %q", model.CronExpression.ValueString()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func packageCleanupPolicyTestSearchCriteria() PackageCleanupPolicySearchCriteriaAPIModel { |
| 92 | + return PackageCleanupPolicySearchCriteriaAPIModel{ |
| 93 | + PackageTypes: []string{"docker"}, |
| 94 | + Repos: []string{"example-repo"}, |
| 95 | + } |
| 96 | +} |
0 commit comments