diff --git a/.circleci/config.yml b/.circleci/config.yml index 1a6391b..1f3e97e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -60,7 +60,7 @@ jobs: name: Performaning Acceptance Test command: | source /tmp/secrets.env - TF_ACC=1 go test -v ./provider/ -count=1 -tags=external_auth + TF_ACC=1 go test -v ./... -count=1 -tags=external_auth spellchecker: docker: diff --git a/client/misc_test.go b/client/misc_test.go new file mode 100644 index 0000000..6c3a64e --- /dev/null +++ b/client/misc_test.go @@ -0,0 +1,22 @@ +package client + +import ( + "regexp" + "testing" +) + +func TestGetSchedule(t *testing.T) { + + schedule, cron := GetSchedule("hourly") + + re := regexp.MustCompile(`([A-Z][^\s]*)`) + matched := re.MatchString(schedule) + if matched == false { + t.Error("Didn't return a Titled string") + } + + matchCronStr := regexCron.MatchString(cron) + if matchCronStr == false { + t.Error("Invalid cron string") + } +} diff --git a/client/system.go b/client/system.go index d784fd5..7a1bc21 100644 --- a/client/system.go +++ b/client/system.go @@ -47,17 +47,10 @@ func (client *Client) SetSchedule(d *schema.ResourceData, scheduleType string) ( return err } - var jsonData models.SystemBody - err = json.Unmarshal([]byte(resp), &jsonData) - if err != nil { - return err - } - - time := jsonData.Schedule.Type requestType := "POST" httpStatusCode := 201 - if time != "" { + if resp != "" { log.Printf("Schedule found performing PUT request") requestType = "PUT" httpStatusCode = 200 diff --git a/provider/resource_garbage_collection_test.go b/provider/resource_garbage_collection_test.go new file mode 100644 index 0000000..2520477 --- /dev/null +++ b/provider/resource_garbage_collection_test.go @@ -0,0 +1,80 @@ +// +build external_auth + +package provider + +import ( + "fmt" + "testing" + + "github.com/BESTSELLER/terraform-provider-harbor/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +const harborGCMain = "harbor_garbage_collection.main" + +func testAccCheckGCDestroy(s *terraform.State) error { + apiClient := testAccProvider.Meta().(*client.Client) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "harbor_garbage_collection" { + continue + } + + resp, _, err := apiClient.SendRequest("GET", rs.Primary.ID, nil, 200) + if err != nil { + return fmt.Errorf("Resouse was not delete \n %s", resp) + } + if resp != "" { + return fmt.Errorf("Resouse was not delete \n %s", resp) + } + + } + + return nil +} + +func TestAccGCUpdate(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGCDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckGCBasic(), + Check: resource.ComposeTestCheckFunc( + testAccCheckResourceExists(harborGCMain), + resource.TestCheckResourceAttr( + harborGCMain, "schedule", "daily"), + ), + }, + { + Config: testAccCheckGCUpdate(), + Check: resource.ComposeTestCheckFunc( + testAccCheckResourceExists(harborGCMain), + resource.TestCheckResourceAttr( + harborGCMain, "schedule", "hourly"), + resource.TestCheckResourceAttr( + harborGCMain, "delete_untagged", "true"), + ), + }, + }, + }) +} + +func testAccCheckGCBasic() string { + return fmt.Sprintf(` + resource "harbor_garbage_collection" "main" { + schedule = "daily" + } + `) +} + +func testAccCheckGCUpdate() string { + return fmt.Sprintf(` + resource "harbor_garbage_collection" "main" { + schedule = "hourly" + delete_untagged = true + } + `) +} diff --git a/provider/resource_project_test.go b/provider/resource_project_test.go index 575baba..b80b567 100644 --- a/provider/resource_project_test.go +++ b/provider/resource_project_test.go @@ -10,6 +10,7 @@ import ( ) const resourceHarborProjectMain = "harbor_project.main" +const enableContentTrust = "enable_content_trust" func testAccCheckProjectDestroy(s *terraform.State) error { apiClient := testAccProvider.Meta().(*client.Client) @@ -47,7 +48,7 @@ func TestAccProjectBasic(t *testing.T) { resource.TestCheckResourceAttr( resourceHarborProjectMain, "vulnerability_scanning", "false"), resource.TestCheckResourceAttr( - resourceHarborProjectMain, "enable_content_trust", "false"), + resourceHarborProjectMain, enableContentTrust, "false"), ), }, }, @@ -71,7 +72,7 @@ func TestAccProjectUpdate(t *testing.T) { resource.TestCheckResourceAttr( resourceHarborProjectMain, "vulnerability_scanning", "false"), resource.TestCheckResourceAttr( - resourceHarborProjectMain, "enable_content_trust", "false"), + resourceHarborProjectMain, enableContentTrust, "false"), ), }, { @@ -85,7 +86,7 @@ func TestAccProjectUpdate(t *testing.T) { resource.TestCheckResourceAttr( resourceHarborProjectMain, "vulnerability_scanning", "true"), resource.TestCheckResourceAttr( - resourceHarborProjectMain, "enable_content_trust", "true"), + resourceHarborProjectMain, enableContentTrust, "true"), ), }, }, @@ -98,9 +99,9 @@ func testAccCheckProjectBasic() string { name = "test_basic" public = false vulnerability_scanning = false - enable_content_trust = false + %v = false } - `) + `, enableContentTrust) } func testAccCheckItemUpdate() string { @@ -109,7 +110,7 @@ func testAccCheckItemUpdate() string { name = "test_basic" public = true vulnerability_scanning = true - enable_content_trust = true + %v = true } -`) +`, enableContentTrust) }