|
8 | 8 |
|
9 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
10 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
|
| 11 | + |
| 12 | + "github.com/coder/terraform-provider-coder/v2/provider" |
11 | 13 | )
|
12 | 14 |
|
13 | 15 | func TestScript(t *testing.T) {
|
@@ -124,3 +126,73 @@ func TestScriptStartBlocksLoginRequiresRunOnStart(t *testing.T) {
|
124 | 126 | }},
|
125 | 127 | })
|
126 | 128 | }
|
| 129 | + |
| 130 | +func TestValidateCronExpression(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + |
| 133 | + tests := []struct { |
| 134 | + name string |
| 135 | + cronExpr string |
| 136 | + expectWarnings bool |
| 137 | + expectErrors bool |
| 138 | + warningContains string |
| 139 | + }{ |
| 140 | + { |
| 141 | + name: "valid 6-field expression", |
| 142 | + cronExpr: "0 0 22 * * *", |
| 143 | + expectWarnings: false, |
| 144 | + expectErrors: false, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "valid 6-field expression with seconds", |
| 148 | + cronExpr: "30 0 9 * * 1-5", |
| 149 | + expectWarnings: false, |
| 150 | + expectErrors: false, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "5-field Unix format - should warn", |
| 154 | + cronExpr: "0 22 * * *", |
| 155 | + expectWarnings: true, |
| 156 | + expectErrors: false, |
| 157 | + warningContains: "appears to be in Unix 5-field format", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "5-field every 5 minutes - should warn", |
| 161 | + cronExpr: "*/5 * * * *", |
| 162 | + expectWarnings: true, |
| 163 | + expectErrors: false, |
| 164 | + warningContains: "Consider prefixing with '0 '", |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "invalid expression", |
| 168 | + cronExpr: "invalid", |
| 169 | + expectErrors: true, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "too many fields", |
| 173 | + cronExpr: "0 0 0 0 0 0 0", |
| 174 | + expectErrors: true, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + for _, tt := range tests { |
| 179 | + t.Run(tt.name, func(t *testing.T) { |
| 180 | + warnings, errors := provider.ValidateCronExpression(tt.cronExpr) |
| 181 | + |
| 182 | + if tt.expectWarnings { |
| 183 | + require.NotEmpty(t, warnings, "Expected warnings but got none") |
| 184 | + if tt.warningContains != "" { |
| 185 | + require.Contains(t, warnings[0], tt.warningContains) |
| 186 | + } |
| 187 | + } else { |
| 188 | + require.Empty(t, warnings, "Expected no warnings but got: %v", warnings) |
| 189 | + } |
| 190 | + |
| 191 | + if tt.expectErrors { |
| 192 | + require.NotEmpty(t, errors, "Expected errors but got none") |
| 193 | + } else { |
| 194 | + require.Empty(t, errors, "Expected no errors but got: %v", errors) |
| 195 | + } |
| 196 | + }) |
| 197 | + } |
| 198 | +} |
0 commit comments