Skip to content

Commit

Permalink
Testing and new command tree. (#36)
Browse files Browse the repository at this point in the history
* Added BDD test using Ginkgo and Gomega
* Try new cmd tree

* New CLI structure

* Add Ginkgo test

* Add Ginkgo Action

* Change ginkgo install cmd

* Date different for TZ

* Finish rebasing
  • Loading branch information
Etourneau Gwenn authored Jan 31, 2023
1 parent d84621c commit 369907c
Show file tree
Hide file tree
Showing 46 changed files with 1,367 additions and 311 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
ssh-add - <<< '${{ secrets.GO_CLIENT_SECRET }}'
git config --global url."[email protected]:".insteadOf https://github.com/
make vet
make test
make build
make clean
name: Build CLI
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ default: build
vet:
go vet ./...

test:
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo
go get github.com/onsi/gomega/...
ginkgo run -r -v


build:
go build -ldflags="-X 'main.version=v${VERSION}'" -o ${BINARY}

Expand Down
29 changes: 19 additions & 10 deletions cmd/backup.go → cmd/backup/backup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package backup

import (
"fmt"
Expand All @@ -12,8 +12,17 @@ import (
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

var getBackupCmd = &cobra.Command{
var BackupCmd = &cobra.Command{
Use: "backup",
Short: "Backup",
Long: "Backup commands",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var getBackupCmd = &cobra.Command{
Use: "get",
Short: "Get backups in YugabyteDB Managed",
Long: "Get backups in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -49,7 +58,7 @@ var getBackupCmd = &cobra.Command{
}

var restoreBackupCmd = &cobra.Command{
Use: "backup",
Use: "restore",
Short: "Restore backups in YugabyteDB Managed",
Long: "Restore backups in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -83,7 +92,7 @@ var restoreBackupCmd = &cobra.Command{
}

var createBackupCmd = &cobra.Command{
Use: "backup",
Use: "create",
Short: "Create backup in YugabyteDB Managed",
Long: "Create backup in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -136,7 +145,7 @@ var createBackupCmd = &cobra.Command{
}

var deleteBackupCmd = &cobra.Command{
Use: "backup",
Use: "delete",
Short: "Delete backup in YugabyteDB Managed",
Long: "Delete backup in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -161,22 +170,22 @@ var deleteBackupCmd = &cobra.Command{
}

func init() {
getCmd.AddCommand(getBackupCmd)
BackupCmd.AddCommand(getBackupCmd)
getBackupCmd.Flags().String("cluster-name", "", "Name of the cluster to fetch backups")

restoreCmd.AddCommand(restoreBackupCmd)
BackupCmd.AddCommand(restoreBackupCmd)
restoreBackupCmd.Flags().String("cluster-name", "", "Name of the cluster to restore backups")
restoreBackupCmd.MarkFlagRequired("cluster-name")
restoreBackupCmd.Flags().String("backup-id", "", "ID of the backup to be restored")
restoreBackupCmd.MarkFlagRequired("backup-id")

createCmd.AddCommand(createBackupCmd)
BackupCmd.AddCommand(createBackupCmd)
createBackupCmd.Flags().String("cluster-name", "", "Name for the cluster")
createBackupCmd.MarkFlagRequired("name")
createBackupCmd.MarkFlagRequired("cluster-name")
createBackupCmd.Flags().Int32("retention-period", 0, "Retention period of the backup")
createBackupCmd.Flags().String("description", "", "Description of the backup")

deleteCmd.AddCommand(deleteBackupCmd)
BackupCmd.AddCommand(deleteBackupCmd)
deleteBackupCmd.Flags().String("backup-id", "", "The backup ID")
deleteBackupCmd.MarkFlagRequired("backup-id")
}
69 changes: 69 additions & 0 deletions cmd/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd_test

import (
"fmt"
"net/http"
"os"
"os/exec"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
"github.com/yugabyte/ybm-cli/internal/formatter"
openapi "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

var _ = Describe("Backup", func() {

var (
server *ghttp.Server
statusCode int
args []string
responseAccount openapi.AccountListResponse
responseProject openapi.ProjectListResponse
responseBackup openapi.BackupListResponse
//cbr *cobra.Command
)

BeforeEach(func() {
args = os.Args
os.Args = []string{}
var err error
server, err = newGhttpServer(responseAccount, responseProject)
Expect(err).ToNot(HaveOccurred())
os.Setenv("YBM_HOST", fmt.Sprintf("http://%s", server.Addr()))
os.Setenv("YBM_APIKEY", "test-token")
})

Context("When running with a valid Api token", func() {

It("should return list of available backup", func() {
statusCode = 200
err := loadJson("./test/fixtures/backups.json", &responseBackup)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/backups"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseBackup),
),
)
cmd := exec.Command(compiledCLIPath, "backup", "get")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Out).Should(gbytes.Say(fmt.Sprintf(
`Created On Expire On Clusters Description State Type Retains\(day\)
%s %s proficient-parrotfish scdasfdadfasdsad SUCCEEDED MANUAL 25`, formatter.FormatDate("2023-01-17T08:31:35.818Z"), formatter.FormatDate("2023-01-17T08:31:35.818Z"))))
session.Kill()
})

})

AfterEach(func() {
os.Args = args
server.Close()
})

})
35 changes: 22 additions & 13 deletions cmd/cdc_sink.go → cmd/cdcsink/cdc_sink.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
package cdcsink

import (
"fmt"
Expand All @@ -26,8 +26,17 @@ func printCdcSinkOutput(resp ybmclient.CDCSinkResponse) {

}

var getCdcSinkCmd = &cobra.Command{
var CDCSinkCmd = &cobra.Command{
Use: "cdc_sink",
Short: "cdc_sink",
Long: "Cdc Sink commands",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var getCdcSinkCmd = &cobra.Command{
Use: "get",
Short: "Get CDC Sink in YugabyteDB Managed",
Long: `Get CDC Sink in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -37,6 +46,7 @@ var getCdcSinkCmd = &cobra.Command{
os.Exit(1)
}
authApi.GetInfo("", "")

cdcSinkName, _ := cmd.Flags().GetString("name")
cdcSinkID, err := authApi.GetCdcSinkIDBySinkName(cdcSinkName)
if err != nil {
Expand All @@ -56,7 +66,7 @@ var getCdcSinkCmd = &cobra.Command{
}

var createCdcSinkCmd = &cobra.Command{
Use: "cdc_sink",
Use: "create",
Short: "Create CDC Sink in YugabyteDB Managed",
Long: `Create CDC Sink in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -102,9 +112,9 @@ var createCdcSinkCmd = &cobra.Command{
}

var editCdcSinkCmd = &cobra.Command{
Use: "cdc_sink",
Short: "Edit CDC Sink in YugabyteDB Managed",
Long: `Edit CDC Sink in YugabyteDB Managed`,
Use: "update",
Short: "Update CDC Sink in YugabyteDB Managed",
Long: "Update CDC Sink in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand Down Expand Up @@ -155,7 +165,7 @@ var editCdcSinkCmd = &cobra.Command{
}

var deleteCdcSinkCmd = &cobra.Command{
Use: "cdc_sink",
Use: "delete",
Short: "Delete CDC Sink in YugabyteDB Managed",
Long: `Delete CDC Sink in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -181,30 +191,29 @@ var deleteCdcSinkCmd = &cobra.Command{
return
}

fmt.Fprintf(os.Stdout, "CDC sink deleted successfully\n")

fmt.Fprintf(os.Stdout, "CDC sink deleted successfully")
},
}

func init() {
getCmd.AddCommand(getCdcSinkCmd)
CDCSinkCmd.AddCommand(getCdcSinkCmd)
getCdcSinkCmd.Flags().String("name", "", "Name of the CDC Sink")

createCmd.AddCommand(createCdcSinkCmd)
CDCSinkCmd.AddCommand(createCdcSinkCmd)
createCdcSinkCmd.Flags().String("name", "", "Name of the CDC sink")
createCdcSinkCmd.Flags().String("cdc-sink-type", "", "Name of the CDC sink type")
createCdcSinkCmd.Flags().String("auth-type", "", "Name of the CDC sink authentication type")
createCdcSinkCmd.Flags().String("hostname", "", "Hostname of the CDC sink")
createCdcSinkCmd.Flags().String("username", "", "Username of the CDC sink")
createCdcSinkCmd.Flags().String("password", "", "Password of the CDC sink")

updateCmd.AddCommand(editCdcSinkCmd)
CDCSinkCmd.AddCommand(editCdcSinkCmd)
editCdcSinkCmd.Flags().String("name", "", "Name of the CDC Sink")
editCdcSinkCmd.Flags().String("new-name", "", "Name of the new CDC Sink")
editCdcSinkCmd.Flags().String("username", "", "Username of the CDC Sink")
editCdcSinkCmd.Flags().String("password", "", "Password of the CDC Sink")

deleteCmd.AddCommand(deleteCdcSinkCmd)
CDCSinkCmd.AddCommand(deleteCdcSinkCmd)
deleteCdcSinkCmd.Flags().String("name", "", "Name of the CDC Sink")

}
35 changes: 21 additions & 14 deletions cmd/cdc_stream.go → cmd/cdcstream/cdc_stream.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
package cdcstream

import (
"fmt"
Expand All @@ -25,10 +25,19 @@ func printCdcStreamOutput(resp ybmclient.CDCStreamResponse) {
formatter.CdcStreamWrite(cdcStreamCtx, cdcStreamData)
}

var getCdcStreamCmd = &cobra.Command{
var CDCStreamCmd = &cobra.Command{
Use: "cdc_stream",
Short: "cdc_stream",
Long: "CDC stream commands",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var getCdcStreamCmd = &cobra.Command{
Use: "get",
Short: "Get CDC Stream in YugabyteDB Managed",
Long: `Get CDC Stream in YugabyteDB Managed`,
Long: "Get CDC Stream in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand Down Expand Up @@ -56,14 +65,12 @@ var getCdcStreamCmd = &cobra.Command{
logrus.Debugf("Full HTTP response: %v", r)
return
}

printCdcStreamOutput(resp)

},
}

var createCdcStreamCmd = &cobra.Command{
Use: "cdc_stream",
Use: "create",
Short: "Create CDC Stream in YugabyteDB Managed",
Long: `Create CDC Stream in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -110,9 +117,9 @@ var createCdcStreamCmd = &cobra.Command{
}

var editCdcStreamCmd = &cobra.Command{
Use: "cdc_stream",
Short: "Edit CDC Stream in YugabyteDB Managed",
Long: `Edit CDC Stream in YugabyteDB Managed`,
Use: "update",
Short: "Update CDC Stream in YugabyteDB Managed",
Long: "Update CDC Stream in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand Down Expand Up @@ -157,7 +164,7 @@ var editCdcStreamCmd = &cobra.Command{
}

var deleteCdcStreamCmd = &cobra.Command{
Use: "cdc_stream",
Use: "delete",
Short: "Delete CDC Stream in YugabyteDB Managed",
Long: `Delete CDC Stream in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -192,11 +199,11 @@ var deleteCdcStreamCmd = &cobra.Command{
}

func init() {
getCmd.AddCommand(getCdcStreamCmd)
CDCStreamCmd.AddCommand(getCdcStreamCmd)
getCdcStreamCmd.Flags().String("name", "", "Name of the CDC Stream")
getCdcStreamCmd.Flags().String("cluster-name", "", "Name of the Cluster")

createCmd.AddCommand(createCdcStreamCmd)
CDCStreamCmd.AddCommand(createCdcStreamCmd)
createCdcStreamCmd.Flags().String("name", "", "Name of the CDC Stream")
createCdcStreamCmd.Flags().String("cluster-name", "", "Name of the Cluster")
createCdcStreamCmd.Flags().StringArray("tables", []string{}, "Database tables the Cdc Stream will listen to")
Expand All @@ -205,13 +212,13 @@ func init() {
createCdcStreamCmd.Flags().String("snapshot-existing-data", "", "Whether to snapshot the existing data in the database")
createCdcStreamCmd.Flags().String("kafka-prefix", "", "A prefix for the Kafka topics")

updateCmd.AddCommand(editCdcStreamCmd)
CDCStreamCmd.AddCommand(editCdcStreamCmd)
editCdcStreamCmd.Flags().String("name", "", "Name of the CDC Stream")
editCdcStreamCmd.Flags().String("cluster-name", "", "Name of the Cluster")
editCdcStreamCmd.Flags().String("new-name", "", "Updated name of the CDC Stream")
editCdcStreamCmd.Flags().StringArray("tables", []string{}, "Tables the Cdc Stream will listen to")

deleteCmd.AddCommand(deleteCdcStreamCmd)
CDCStreamCmd.AddCommand(deleteCdcStreamCmd)
deleteCdcStreamCmd.Flags().String("name", "", "Name of the CDC Stream")
deleteCdcStreamCmd.Flags().String("cluster-name", "", "Name of the Cluster")

Expand Down
6 changes: 3 additions & 3 deletions cmd/cloud_regions.go → cmd/cluster/cloud_regions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
package cluster

import (
"os"
Expand All @@ -14,7 +14,7 @@ import (
)

var getCloudRegionsCmd = &cobra.Command{
Use: "cloud_regions",
Use: "describe-regions",
Short: "Get Cloud Regions in YugabyteDB Managed",
Long: `Get Cloud Regions in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -45,7 +45,7 @@ var getCloudRegionsCmd = &cobra.Command{
}

func init() {
getCmd.AddCommand(getCloudRegionsCmd)
ClusterCmd.AddCommand(getCloudRegionsCmd)
getCloudRegionsCmd.Flags().String("cloud-provider", "", "The cloud provider for which the regions have to be fetched. AWS or GCP.")
getCloudRegionsCmd.MarkFlagRequired("cloud-provider")

Expand Down
Loading

0 comments on commit 369907c

Please sign in to comment.