Skip to content

Commit

Permalink
test: improve makefile, add func
Browse files Browse the repository at this point in the history
  • Loading branch information
avirtopeanu-ionos committed Jan 8, 2025
1 parent c267341 commit e6a97d4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions commands/cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions commands/dns/dns_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions commands/token/token_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"io/ioutil"
"net"
"strings"
"time"

"golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -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
}

0 comments on commit e6a97d4

Please sign in to comment.