|
1 | 1 | package argocd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
| 6 | + "os" |
5 | 7 | "regexp"
|
6 | 8 | "runtime"
|
| 9 | + "strconv" |
7 | 10 | "testing"
|
| 11 | + "time" |
8 | 12 |
|
| 13 | + "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
9 | 15 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
|
10 | 16 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
| 17 | + "github.com/oboukili/terraform-provider-argocd/internal/provider" |
11 | 18 | "k8s.io/client-go/rest"
|
12 | 19 | "k8s.io/client-go/tools/clientcmd"
|
13 | 20 | "k8s.io/client-go/util/homedir"
|
@@ -292,6 +299,74 @@ func TestAccArgoCDCluster_invalidSameServer(t *testing.T) {
|
292 | 299 | })
|
293 | 300 | }
|
294 | 301 |
|
| 302 | +func TestAccArgoCDCluster_outsideDeletion(t *testing.T) { |
| 303 | + clusterName := acctest.RandString(10) |
| 304 | + resource.Test(t, resource.TestCase{ |
| 305 | + PreCheck: func() { testAccPreCheck(t) }, |
| 306 | + ProviderFactories: testAccProviders, |
| 307 | + Steps: []resource.TestStep{ |
| 308 | + { |
| 309 | + Config: testAccArgoCDClusterMetadata(clusterName), |
| 310 | + Check: resource.ComposeTestCheckFunc( |
| 311 | + resource.TestCheckResourceAttr( |
| 312 | + "argocd_cluster.cluster_metadata", |
| 313 | + "info.0.connection_state.0.status", |
| 314 | + "Successful", |
| 315 | + ), |
| 316 | + resource.TestCheckResourceAttr( |
| 317 | + "argocd_cluster.cluster_metadata", |
| 318 | + "config.0.tls_client_config.0.insecure", |
| 319 | + "true", |
| 320 | + ), |
| 321 | + resource.TestCheckResourceAttr( |
| 322 | + "argocd_cluster.cluster_metadata", |
| 323 | + "name", |
| 324 | + clusterName, |
| 325 | + ), |
| 326 | + ), |
| 327 | + }, |
| 328 | + { |
| 329 | + PreConfig: func() { |
| 330 | + // delete cluster and validate referesh generates a plan |
| 331 | + // (non-regression test for https://github.com/oboukili/terraform-provider-argocd/issues/266) |
| 332 | + si, err := getServerInterface() |
| 333 | + if err != nil { |
| 334 | + t.Error(fmt.Errorf("failed to get server interface: %s", err.Error())) |
| 335 | + } |
| 336 | + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) |
| 337 | + defer cancel() |
| 338 | + _, err = si.ClusterClient.Delete(ctx, &cluster.ClusterQuery{Name: clusterName}) |
| 339 | + if err != nil { |
| 340 | + t.Error(fmt.Errorf("failed to delete cluster '%s': %s", clusterName, err.Error())) |
| 341 | + } |
| 342 | + }, |
| 343 | + RefreshState: true, |
| 344 | + ExpectNonEmptyPlan: true, |
| 345 | + }, |
| 346 | + { |
| 347 | + Config: testAccArgoCDClusterMetadata(clusterName), |
| 348 | + Check: resource.ComposeTestCheckFunc( |
| 349 | + resource.TestCheckResourceAttr( |
| 350 | + "argocd_cluster.cluster_metadata", |
| 351 | + "info.0.connection_state.0.status", |
| 352 | + "Successful", |
| 353 | + ), |
| 354 | + resource.TestCheckResourceAttr( |
| 355 | + "argocd_cluster.cluster_metadata", |
| 356 | + "config.0.tls_client_config.0.insecure", |
| 357 | + "true", |
| 358 | + ), |
| 359 | + resource.TestCheckResourceAttr( |
| 360 | + "argocd_cluster.cluster_metadata", |
| 361 | + "name", |
| 362 | + clusterName, |
| 363 | + ), |
| 364 | + ), |
| 365 | + }, |
| 366 | + }, |
| 367 | + }) |
| 368 | +} |
| 369 | + |
295 | 370 | func TestAccArgoCDCluster_namespacesErrorWhenEmpty(t *testing.T) {
|
296 | 371 | name := acctest.RandString(10)
|
297 | 372 |
|
@@ -619,3 +694,25 @@ func getInternalRestConfig() (*rest.Config, error) {
|
619 | 694 |
|
620 | 695 | return nil, fmt.Errorf("could not find a kind-argocd cluster from the current ~/.kube/config file")
|
621 | 696 | }
|
| 697 | + |
| 698 | +// build & init ArgoCD server interface |
| 699 | +func getServerInterface() (*provider.ServerInterface, error) { |
| 700 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 701 | + defer cancel() |
| 702 | + insecure, err := strconv.ParseBool(os.Getenv("ARGOCD_INSECURE")) |
| 703 | + if err != nil { |
| 704 | + return nil, fmt.Errorf("failed to parse 'ARGOCD_INSECURE' env var to bool: %s", err.Error()) |
| 705 | + } |
| 706 | + si := provider.NewServerInterface(provider.ArgoCDProviderConfig{ |
| 707 | + ServerAddr: types.StringValue(os.Getenv("ARGOCD_SERVER")), |
| 708 | + Insecure: types.BoolValue(insecure), |
| 709 | + Username: types.StringValue(os.Getenv("ARGOCD_AUTH_USERNAME")), |
| 710 | + Password: types.StringValue(os.Getenv("ARGOCD_AUTH_PASSWORD")), |
| 711 | + }) |
| 712 | + diag := si.InitClients(ctx) |
| 713 | + if diag.HasError() { |
| 714 | + return nil, fmt.Errorf("failed to init clients: %v", diag.Errors()) |
| 715 | + } |
| 716 | + |
| 717 | + return si, nil |
| 718 | +} |
0 commit comments