|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" |
| 8 | + "github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/gitops" |
| 9 | + //"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccCodefreshAccountGitopsSettings_basic(t *testing.T) { |
| 15 | + resourceName := "codefresh_account_gitops_settings.test" |
| 16 | + |
| 17 | + expectedDefaultApiURLGithub, _ := gitops.GetDefaultAPIUrlForProvider(gitops.GitProviderGitHub) |
| 18 | + //expectedDefaultApiURLGithub := "https://bnlah.com" |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + Providers: testAccProviders, |
| 23 | + //CheckDestroy: testAccCheckCodefreshProjectDestroy, |
| 24 | + Steps: []resource.TestStep{ |
| 25 | + { |
| 26 | + Config: testAccountGitopsSettingsGithubDefaultApiUrl("https://github.com/codefresh-io/terraform-provider-isc-test.git"), |
| 27 | + Check: resource.ComposeTestCheckFunc( |
| 28 | + testAccCheckGitopsSettings(resourceName, gitops.GitProviderGitHub, *expectedDefaultApiURLGithub, "https://github.com/codefresh-io/terraform-provider-isc-test.git"), |
| 29 | + resource.TestCheckResourceAttr(resourceName, "git_provider", gitops.GitProviderGitHub), |
| 30 | + ), |
| 31 | + }, |
| 32 | + { |
| 33 | + ResourceName: resourceName, |
| 34 | + ImportState: true, |
| 35 | + ImportStateVerify: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func testAccCheckGitopsSettings(resource string, gitProvider string, gitProviderApiUrl string, sharedConfigRepository string) resource.TestCheckFunc { |
| 42 | + return func(state *terraform.State) error { |
| 43 | + rs, ok := state.RootModule().Resources[resource] |
| 44 | + if !ok { |
| 45 | + return fmt.Errorf("Not found: %s", resource) |
| 46 | + } |
| 47 | + if rs.Primary.ID == "" { |
| 48 | + return fmt.Errorf("No Record ID is set") |
| 49 | + } |
| 50 | + |
| 51 | + apiClient := testAccProvider.Meta().(*cfclient.Client) |
| 52 | + |
| 53 | + accGitopsInfo, err := apiClient.GetActiveGitopsAccountInfo() |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("failed getting gitops settings with error %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + if accGitopsInfo.GitApiUrl != gitProviderApiUrl { |
| 60 | + return fmt.Errorf("expecting APIUrl to be %s but got %s", gitProviderApiUrl, accGitopsInfo.GitApiUrl) |
| 61 | + } |
| 62 | + |
| 63 | + if accGitopsInfo.GitProvider != gitProvider { |
| 64 | + return fmt.Errorf("expecting provider to be %s but got %s", gitProvider, accGitopsInfo.GitProvider) |
| 65 | + } |
| 66 | + |
| 67 | + if accGitopsInfo.SharedConfigRepo != sharedConfigRepository { |
| 68 | + return fmt.Errorf("expecting shared config repository to be %s but got %s", sharedConfigRepository, accGitopsInfo.SharedConfigRepo) |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// CONFIGS |
| 76 | +func testAccountGitopsSettingsGithubDefaultApiUrl(sharedConfigRepository string) string { |
| 77 | + return fmt.Sprintf(` |
| 78 | + resource "codefresh_account_gitops_settings" "test" { |
| 79 | + git_provider = "GITHUB" |
| 80 | + shared_config_repository = "%s" |
| 81 | + }`, sharedConfigRepository) |
| 82 | +} |
0 commit comments