|
8 | 8 | "time" |
9 | 9 |
|
10 | 10 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
11 | 12 | "github.com/stretchr/testify/assert" |
12 | 13 | ) |
13 | 14 |
|
@@ -241,3 +242,101 @@ resource "argocd_account_token" "renew_after" { |
241 | 242 | } |
242 | 243 | `, renewAfter) |
243 | 244 | } |
| 245 | + |
| 246 | +func testCheckTokenIssuedAt(resourceName string) resource.TestCheckFunc { |
| 247 | + return func(s *terraform.State) error { |
| 248 | + rs, ok := s.RootModule().Resources[resourceName] |
| 249 | + if !ok { |
| 250 | + return fmt.Errorf("not found: %s", resourceName) |
| 251 | + } |
| 252 | + |
| 253 | + if rs.Primary.ID == "" { |
| 254 | + return fmt.Errorf("token ID is not set") |
| 255 | + } |
| 256 | + |
| 257 | + _issuedAt, ok := rs.Primary.Attributes["issued_at"] |
| 258 | + if !ok { |
| 259 | + return fmt.Errorf("testCheckTokenIssuedAt: issued_at is not set") |
| 260 | + } |
| 261 | + |
| 262 | + _, err := convertStringToInt64(_issuedAt) |
| 263 | + if err != nil { |
| 264 | + return fmt.Errorf("testCheckTokenIssuedAt: string attribute 'issued_at' stored in state cannot be converted to int64: %s", err) |
| 265 | + } |
| 266 | + |
| 267 | + return nil |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +func testCheckTokenExpiresAt(resourceName string, expiresIn int64) resource.TestCheckFunc { |
| 272 | + return func(s *terraform.State) error { |
| 273 | + rs, ok := s.RootModule().Resources[resourceName] |
| 274 | + if !ok { |
| 275 | + return fmt.Errorf("not found: %s", resourceName) |
| 276 | + } |
| 277 | + |
| 278 | + if rs.Primary.ID == "" { |
| 279 | + return fmt.Errorf("token ID is not set") |
| 280 | + } |
| 281 | + |
| 282 | + _expiresAt, ok := rs.Primary.Attributes["expires_at"] |
| 283 | + if !ok { |
| 284 | + return fmt.Errorf("expires_at is not set") |
| 285 | + } |
| 286 | + |
| 287 | + _issuedAt, ok := rs.Primary.Attributes["issued_at"] |
| 288 | + if !ok { |
| 289 | + return fmt.Errorf("testCheckTokenExpiresAt: issued_at is not set") |
| 290 | + } |
| 291 | + |
| 292 | + expiresAt, err := convertStringToInt64(_expiresAt) |
| 293 | + if err != nil { |
| 294 | + return fmt.Errorf("testCheckTokenExpiresAt: string attribute 'expires_at' stored in state cannot be converted to int64: %s", err) |
| 295 | + } |
| 296 | + |
| 297 | + issuedAt, err := convertStringToInt64(_issuedAt) |
| 298 | + if err != nil { |
| 299 | + return fmt.Errorf("testCheckTokenExpiresAt: string attribute 'issued_at' stored in state cannot be converted to int64: %s", err) |
| 300 | + } |
| 301 | + |
| 302 | + if issuedAt+expiresIn != expiresAt { |
| 303 | + return fmt.Errorf("testCheckTokenExpiresAt: issuedAt + expiresIn != expiresAt : %d + %d != %d", issuedAt, expiresIn, expiresAt) |
| 304 | + } |
| 305 | + |
| 306 | + return nil |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +func testTokenIssuedAtSet(name string, count int) resource.TestCheckFunc { |
| 311 | + return func(s *terraform.State) error { |
| 312 | + key := "issued_at" |
| 313 | + |
| 314 | + for i := 0; i < count; i++ { |
| 315 | + ms := s.RootModule() |
| 316 | + _name := fmt.Sprintf("%s.%d", name, i) |
| 317 | + |
| 318 | + rs, ok := ms.Resources[_name] |
| 319 | + if !ok { |
| 320 | + return fmt.Errorf("not found: %s in %s", _name, ms.Path) |
| 321 | + } |
| 322 | + |
| 323 | + is := rs.Primary |
| 324 | + if is == nil { |
| 325 | + return fmt.Errorf("no primary instance: %s in %s", _name, ms.Path) |
| 326 | + } |
| 327 | + |
| 328 | + if val, ok := is.Attributes[key]; !ok || val == "" { |
| 329 | + return fmt.Errorf("%s: Attribute '%s' expected to be set", _name, key) |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + return nil |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +func testDelay(seconds int) resource.TestCheckFunc { |
| 338 | + return func(s *terraform.State) error { |
| 339 | + time.Sleep(time.Duration(seconds) * time.Second) |
| 340 | + return nil |
| 341 | + } |
| 342 | +} |
0 commit comments