Skip to content

Commit

Permalink
K8s-8514 Add fixes of maintenancecronjob tests (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Mert Dogan <[email protected]>
  • Loading branch information
mmertdogann and Mert Dogan authored Jul 31, 2023
1 parent 9b63606 commit db3b991
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 38 deletions.
4 changes: 2 additions & 2 deletions metakube/resource_maintenance_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ func metakubeResourceMaintenanceCronJobRead(ctx context.Context, d *schema.Resou

_ = d.Set("spec", metakubeMaintenanceCronJobFlattenSpec(r.Payload.Spec))

_ = d.Set("creation_timestamp", r.Payload.CreationTimestamp)
_ = d.Set("creation_timestamp", r.Payload.CreationTimestamp.String())

_ = d.Set("deletion_timestamp", r.Payload.DeletionTimestamp)
_ = d.Set("deletion_timestamp", r.Payload.DeletionTimestamp.String())

return nil
}
Expand Down
142 changes: 106 additions & 36 deletions metakube/resource_maintenance_cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package metakube
import (
"fmt"
"os"
"regexp"
"strings"
"testing"

Expand All @@ -27,34 +26,27 @@ func TestAccMetakubeCluster_MaintenanceCronJob_Basic(t *testing.T) {
OpenstackApplicationCredentialID: os.Getenv(testEnvOpenstackApplicationCredentialsID),
OpenstackApplicationCredentialSecret: os.Getenv(testEnvOpenstackApplicationCredentialsSecret),

MaintenanceCronJobName: "test_maintenance_cron_job_name",
MaintenanceJobTemplateName: "test_maintenance_job_template_name",
MaintenanceJobType: "test_maintenance_job_type",
}
var config strings.Builder
if err := testAccCheckMetaKubeMaintenanceCronJobBasicTemplate.Execute(&config, params); err != nil {
t.Fatal(err)
MaintenanceCronJobName: randomName("test-maintenancecronjob", 5),
MaintenanceJobTemplateName: randomName("test-maintenancecronjob-template", 5),
MaintenanceJobType: "kubernetesPatchUpdate",
Schedule: "5 4 * * *",
}

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckForOpenstack(t)
},
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"openstack": {
Source: "terraform-provider-openstack/openstack",
},
},
Providers: testAccProviders,
CheckDestroy: testAccCheckMetaKubeMaintenanceCronJobDestroy,
Steps: []resource.TestStep{
{
Config: config.String(),
Config: testAccCheckMetaKubeMaintenanceCronJobBasicConfig(t, params),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMetaKubeMaintenanceCronJobExists(&maintenanceCronJob),
resource.TestCheckResourceAttr(resourceName, "maintenance_cron_job_name", params.MaintenanceCronJobName),
testAccCheckMetaKubeMaintenanceCronJobFields(&maintenanceCronJob, params.MaintenanceCronJobName, params.Schedule, params.MaintenanceJobType),
resource.TestCheckResourceAttr(resourceName, "name", params.MaintenanceCronJobName),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.schedule", "5 4 * * *"),
resource.TestCheckResourceAttr(resourceName, "spec.0.schedule", params.Schedule),
resource.TestCheckResourceAttr(resourceName, "spec.0.maintenance_job_template.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.maintenance_job_template.0.rollback", "false"),
resource.TestCheckResourceAttr(resourceName, "spec.0.maintenance_job_template.0.type", params.MaintenanceJobType),
Expand All @@ -63,11 +55,8 @@ func TestAccMetakubeCluster_MaintenanceCronJob_Basic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: false,
ImportStateId: "x:y:123xyz",
ExpectError: regexp.MustCompile(`(Please verify the ID is correct|Cannot import non-existent remote object)`),
Config: testAccCheckMetaKubeMaintenanceCronJobBasicConfig(t, params),
PlanOnly: true,
},
},
})
Expand All @@ -84,9 +73,14 @@ type testAccCheckMetaKubeMaintenanceCronJobBasicParams struct {
MaintenanceCronJobName string
MaintenanceJobTemplateName string
MaintenanceJobType string
Schedule string
}

var testAccCheckMetaKubeMaintenanceCronJobBasicTemplate = mustParseTemplate("maintenanceCronJobBasic", `
func testAccCheckMetaKubeMaintenanceCronJobBasicConfig(t *testing.T, params *testAccCheckMetaKubeMaintenanceCronJobBasicParams) string {
t.Helper()

var result strings.Builder
err := mustParseTemplate("metakube maintenance cron job test template", `
resource "metakube_cluster" "acctest" {
name = "{{ .ClusterName }}"
dc_name = "{{ .DatacenterName }}"
Expand All @@ -106,26 +100,65 @@ var testAccCheckMetaKubeMaintenanceCronJobBasicTemplate = mustParseTemplate("mai
}
resource "metakube_maintenance_cron_job" "acctest" {
project_id = "{{ .ProjectID }}"
cluster_id = metakube_cluster.acctest.id
name = "{{ .MaintenanceCronJobName }}"
timeouts {
create = "15m"
update = "15m"
delete = "15m"
}
spec {
schedule = "5 4 * * *"
schedule = "{{ .Schedule }}"
maintenance_job_template {
options = {
"a" = "b"
"c" = "d"
}
rollback = false
type = "{{ .MaintenanceJobType }}"
}
}
}`)
}
`).Execute(&result, params)
if err != nil {
t.Fatal(err)
}
return result.String()
}

func testAccCheckMetaKubeMaintenanceCronJobBasicSecondConfig(t *testing.T, params *testAccCheckMetaKubeMaintenanceCronJobBasicParams) string {
t.Helper()

var result strings.Builder
err := mustParseTemplate("metakube maintenance cron job test template", `
resource "metakube_cluster" "acctest" {
name = "{{ .ClusterName }}"
dc_name = "{{ .DatacenterName }}"
project_id = "{{ .ProjectID }}"
spec {
version = "{{ .Version }}"
cloud {
openstack {
application_credentials {
id = "{{ .OpenstackApplicationCredentialID }}"
secret ="{{ .OpenstackApplicationCredentialSecret }}"
}
}
}
}
}
resource "metakube_maintenance_cron_job" "acctest" {
cluster_id = metakube_cluster.acctest.id
name = "{{ .MaintenanceCronJobName }}"
spec {
schedule = "{{ .Schedule }}"
maintenance_job_template {
rollback = true
type = "{{ .MaintenanceJobType }}"
}
}
}
`).Execute(&result, params)
if err != nil {
t.Fatal(err)
}
return result.String()
}

func testAccCheckMetaKubeMaintenanceCronJobDestroy(s *terraform.State) error {
k := testAccProvider.Meta().(*metakubeProviderMeta)
Expand Down Expand Up @@ -160,7 +193,8 @@ func testAccCheckMetaKubeMaintenanceCronJobExists(maintenanceCronJob *models.Mai

k := testAccProvider.Meta().(*metakubeProviderMeta)
projectID := rs.Primary.Attributes["project_id"]
p := project.NewGetMaintenanceCronJobParams().WithProjectID(projectID).WithMaintenanceCronJobID(rs.Primary.ID)
clusterID := rs.Primary.Attributes["cluster_id"]
p := project.NewGetMaintenanceCronJobParams().WithProjectID(projectID).WithClusterID(clusterID).WithMaintenanceCronJobID(rs.Primary.ID)
ret, err := k.client.Project.GetMaintenanceCronJob(p, k.auth)
if err != nil {
return fmt.Errorf("GetMaintenanceCronJob %v", err)
Expand All @@ -174,3 +208,39 @@ func testAccCheckMetaKubeMaintenanceCronJobExists(maintenanceCronJob *models.Mai
return nil
}
}

func testAccCheckMetaKubeMaintenanceCronJobFields(mcj *models.MaintenanceCronJob, name, schedule, maintenanceJobType string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if mcj == nil {
return fmt.Errorf("No Record")
}

if mcj.Spec == nil {
return fmt.Errorf("No Maintenance Cron Job spec present")
}

if mcj.Spec.Schedule == "" {
return fmt.Errorf("No Maintenance Cron Job schedule present")
}

if mcj.Spec.MaintenanceJobTemplate == nil {
return fmt.Errorf("No Maintenance Job Template present")
}

if mcj.Name != name {
return fmt.Errorf("want MaintenanceCronJob.Name=%s, got %s", name, mcj.Name)
}

if mcj.Spec.Schedule != schedule {
return fmt.Errorf("want MaintenanceCronJob.Schedule=%s, got %s", schedule, mcj.Spec.Schedule)
}

maintenanceJobTemplate := mcj.Spec.MaintenanceJobTemplate

if maintenanceJobTemplate.Type != maintenanceJobType {
return fmt.Errorf("want MaintenanceJobTemplate.Type=%s, got %s", maintenanceJobType, maintenanceJobTemplate.Type)
}

return nil
}
}

0 comments on commit db3b991

Please sign in to comment.