From e6a97d41603ff9ff388237c2aaeb7cafdc53082c Mon Sep 17 00:00:00 2001 From: Alexandru Claudius Virtopeanu Date: Wed, 8 Jan 2025 16:55:18 +0200 Subject: [PATCH] test: improve makefile, add func --- Makefile | 10 ++++++++-- commands/cfg/cfg_test.go | 7 +++---- commands/dns/dns_integration_test.go | 7 +++---- commands/token/token_integration_test.go | 7 +++---- internal/utils/utils.go | 14 ++++++++++++++ 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 0cc7eaac8..c27611c37 100644 --- a/Makefile +++ b/Makefile @@ -29,11 +29,17 @@ utest: # `// +build integration` is still maintained for compatibility reasons # `go fmt` still maintains these lines, if one is removed. If it stops this behaviour, then we can remove them +# run the go-based e2e tests +.PHONY: itest +itest: + @echo "--- Run unit tests and go-based integration tests ---" + @go test $(TEST_FLAGS) -tags=integration $(TEST_DIRS) + .PHONY: test test: - @echo "--- Run bats-core, integration and unit tests ---" + @echo "--- Run bats-core tests ---" @test/run.sh # bats-core tests and other - @go test $(TEST_FLAGS) -tags=integration $(TEST_DIRS) + @$(MAKE) itest # go-based tests (unit and integration) .PHONY: mocks mocks: diff --git a/commands/cfg/cfg_test.go b/commands/cfg/cfg_test.go index 03010656e..648ad1877 100644 --- a/commands/cfg/cfg_test.go +++ b/commands/cfg/cfg_test.go @@ -19,6 +19,7 @@ import ( "github.com/ionos-cloud/ionosctl/v6/internal/config" "github.com/ionos-cloud/ionosctl/v6/internal/constants" "github.com/ionos-cloud/ionosctl/v6/internal/core" + "github.com/ionos-cloud/ionosctl/v6/internal/utils" "github.com/spf13/viper" "github.com/ionos-cloud/ionosctl/v6/internal/client" @@ -296,11 +297,9 @@ func teardown() { // Delete tokens generated since setup for _, t := range *toks.Tokens { - strDate := *t.CreatedDate - - date, err := time.Parse(time.RFC3339, strDate) + date, err := utils.ParseDate(*t.CreatedDate) if err != nil { - panic(fmt.Errorf("they changed the date format: %w", err)) + panic(fmt.Errorf("couldn't parse date %s: %w", *t.CreatedDate, err)) } // Delete the token if it was created after setup diff --git a/commands/dns/dns_integration_test.go b/commands/dns/dns_integration_test.go index 9e62a5a33..5604579bc 100644 --- a/commands/dns/dns_integration_test.go +++ b/commands/dns/dns_integration_test.go @@ -15,6 +15,7 @@ import ( "github.com/ionos-cloud/ionosctl/v6/commands/dns/record" "github.com/ionos-cloud/ionosctl/v6/commands/dns/utils" "github.com/ionos-cloud/ionosctl/v6/internal/constants" + utils2 "github.com/ionos-cloud/ionosctl/v6/internal/utils" "github.com/ionos-cloud/ionosctl/v6/pkg/functional" dns "github.com/ionos-cloud/sdk-go-dns" @@ -226,11 +227,9 @@ func cleanupTokensCreatedAfterDate(taym time.Time) { // Delete tokens generated since setup for _, t := range *toks.Tokens { - strDate := *t.CreatedDate - - date, err := time.Parse(time.RFC3339, strDate) + date, err := utils2.ParseDate(*t.CreatedDate) if err != nil { - panic(fmt.Errorf("they changed the date format: %w", err)) + panic(fmt.Errorf("couldn't parse date %s: %w", *t.CreatedDate, err)) } // Delete the token if it was created after setup diff --git a/commands/token/token_integration_test.go b/commands/token/token_integration_test.go index 8164ca31a..98f0ec74a 100644 --- a/commands/token/token_integration_test.go +++ b/commands/token/token_integration_test.go @@ -14,6 +14,7 @@ import ( "github.com/ionos-cloud/ionosctl/v6/commands/token" "github.com/ionos-cloud/ionosctl/v6/internal/client" "github.com/ionos-cloud/ionosctl/v6/internal/constants" + "github.com/ionos-cloud/ionosctl/v6/internal/utils" authservice "github.com/ionos-cloud/ionosctl/v6/services/auth-v1" sdkgoauth "github.com/ionos-cloud/sdk-go-auth" "github.com/spf13/viper" @@ -81,11 +82,9 @@ func testCreateToken(t *testing.T) { foundTokenViaSdk = nil for _, tok := range *allTokens { - strDate := *tok.CreatedDate - - date, err := time.Parse(time.RFC3339, strDate) + date, err := utils.ParseDate(*tok.CreatedDate) if err != nil { - panic(fmt.Errorf("they changed the date format: %w", err)) + panic(fmt.Errorf("couldn't parse date %s: %w", *tok.CreatedDate, err)) } if date.After(tokFirstCreationTime) { diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 74bc5332b..f91fb9ff0 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -4,6 +4,8 @@ import ( "fmt" "io/ioutil" "net" + "strings" + "time" "golang.org/x/crypto/ssh" ) @@ -50,3 +52,15 @@ func ValidateIPv6CidrBlockAgainstParentCidrBlock(cidr string, expectedMask int, return nil } + +// ParseDate parses a date string in RFC3339 format +// and returns a time.Time object +// It also removes the [UTC] suffix if present +func ParseDate(strDate string) (time.Time, error) { + strDate = strings.TrimSuffix(strDate, "[UTC]") + date, err := time.Parse(time.RFC3339, strDate) + if err != nil { + return time.Time{}, fmt.Errorf("date is not a valid RFC3339: %w", err) + } + return date, nil +}