From ec93cb3658599f9ab2e123d95d97ba0809c39094 Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Fri, 30 Sep 2022 17:41:46 -0400 Subject: [PATCH] add simple go tests and remove e2e tests Signed-off-by: Marques Johansson --- .github/workflows/test.yml | 20 +++++ Makefile | 4 +- cmd/cli_test.go | 44 ++++++++++ tests/facilities_test.go | 38 --------- tests/main_test.go | 148 ---------------------------------- tests/organization_test.go | 125 ---------------------------- tests/plan_test.go | 38 --------- tests/project_test.go | 101 ----------------------- tests/ssh_key_test.go | 119 --------------------------- tests/virtual_network_test.go | 120 --------------------------- 10 files changed, 66 insertions(+), 691 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 cmd/cli_test.go delete mode 100644 tests/facilities_test.go delete mode 100644 tests/main_test.go delete mode 100644 tests/organization_test.go delete mode 100644 tests/plan_test.go delete mode 100644 tests/project_test.go delete mode 100644 tests/ssh_key_test.go delete mode 100644 tests/virtual_network_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..877e2f58 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: Go Tests +on: + pull_request: +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Set up Go + uses: actions/setup-go@v2.2.0 + with: + go-version: '1.19' + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + - name: Get dependencies + run: go mod download + - name: Build + run: go build -v ./... + - name: TF tests + run: go test -v -cover -parallel 4 ./... diff --git a/Makefile b/Makefile index 5edcef3c..8d5a01cd 100644 --- a/Makefile +++ b/Makefile @@ -68,7 +68,7 @@ generate-docs: clean-docs go run ./cmd/metal docs ./docs test: - go test ./tests + go test -v ./... ## -------------------------------------- @@ -77,4 +77,4 @@ test: $(GOLANGCI_LINT): ## Build golangci-lint from tools folder. GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER) -golangci-lint: $(LINTER) \ No newline at end of file +golangci-lint: $(LINTER) diff --git a/cmd/cli_test.go b/cmd/cli_test.go new file mode 100644 index 00000000..ab746dbe --- /dev/null +++ b/cmd/cli_test.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "testing" + + root "github.com/equinix/metal-cli/internal/cli" + outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/spf13/cobra" +) + +func TestCli_RegisterCommands(t *testing.T) { + type fields struct { + MainCmd *cobra.Command + Outputer outputPkg.Outputer + } + type args struct { + client *root.Client + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "test", + fields: fields{ + MainCmd: &cobra.Command{}, + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), + }, + args: args{ + client: &root.Client{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cli := &Cli{ + MainCmd: tt.fields.MainCmd, + Outputer: tt.fields.Outputer, + } + cli.RegisterCommands(tt.args.client) + }) + } +} diff --git a/tests/facilities_test.go b/tests/facilities_test.go deleted file mode 100644 index 236ae41f..00000000 --- a/tests/facilities_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" -) - -func TestFacilitiesOperations(t *testing.T) { - tests := []Test{ - {"facilities get", []string{"facilities", "get"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -} diff --git a/tests/main_test.go b/tests/main_test.go deleted file mode 100644 index 7b54d104..00000000 --- a/tests/main_test.go +++ /dev/null @@ -1,148 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" - "time" - - "github.com/packethost/packngo" -) - -const binaryName = "metal" - -const ( - consumerToken = "Equinix Metal CLI" - apiTokenEnvVar = "METAL_AUTH_TOKEN" - apiURL = "https://api.equinix.com/metal/v1/" -) - -func TestMain(m *testing.M) { - err := os.Chdir("..") - if err != nil { - fmt.Printf("could not change dir: %v", err) - os.Exit(1) - } - fmt.Println("build") - build := exec.Command("go", "build", "-o", binaryName) - err = build.Run() - if err != nil { - fmt.Printf("could not build binary for %s: %v", binaryName, err) - os.Exit(1) - } - - os.Exit(m.Run()) -} - -var ( - projectID string - deviceID string - client *packngo.Client -) - -type Test struct { - name string - args []string -} - -func testToken() string { - return os.Getenv(apiTokenEnvVar) -} - -func TestCliArgs(t *testing.T) { - client, _ = packngo.NewClientWithBaseURL(consumerToken, testToken(), nil, apiURL) - projects, _, _ := client.Projects.List(nil) - projectID = projects[0].ID - tests := []Test{ - {"no arguments", []string{}}, - {"operating-systems get", []string{"operating-systems", "get", "-j"}}, - {"plan get", []string{"plan", "get", "-j"}}, - {"organization get", []string{"organization", "get", "-j"}}, - {"project get", []string{"project", "get", "-j"}}, - { - "create device", - []string{ - "device", "create", - "--hostname", "clitest", - "--plan", "baremetal_1", - "--facility", "ewr1", - "--operating-system", "centos_7", - "--project-id", projectID, - "-j", - }, - }, - {"devices get", []string{"device", "get", "project-id", projectID, "-j"}}, - {"device update", []string{"device", "update", "hostname", "updatedfromcli", "-i"}}, - {"device reboot", []string{"device", "reboot", "-i"}}, - {"device stop", []string{"device", "stop", "-i"}}, - {"device start", []string{"device", "start", "-i"}}, - {"device delete", []string{"device", "delete", "-i"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - if tt.name == "device delete" && deviceID != "" { - tt.args = append(tt.args, deviceID, "-f") - } - - if (tt.name == "device update" || - tt.name == "device reboot" || - tt.name == "device stop" || - tt.name == "device start") && deviceID != "" { - tt.args = append(tt.args, deviceID) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - if len(tt.args) > 0 { - if tt.args[0] == "project" { - project := &[]packngo.Project{} - err := json.Unmarshal([]byte(actual), project) - if err != nil { - t.Fatal(err) - } - projectID = (*project)[0].ID - } else if tt.args[0] == "device" && tt.args[1] == "create" { - device := &packngo.Device{} - // fmt.Println(actual) - err := json.Unmarshal([]byte(actual), device) - if err != nil { - t.Fatal(err) - } - - deviceID = (*device).ID - for { - dev, _, err := client.Devices.Get(deviceID, nil) - if err != nil { - break - } - if dev.State == "active" { - break - } - time.Sleep(2 * time.Second) - } - } - } - }) - } -} diff --git a/tests/organization_test.go b/tests/organization_test.go deleted file mode 100644 index be379dd3..00000000 --- a/tests/organization_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" - - "github.com/packethost/packngo" -) - -var organizationID string - -func TestOrganizationOperations(t *testing.T) { - client, _ = packngo.NewClientWithBaseURL(consumerToken, testToken(), nil, apiURL) - setupTests := []Test{ - { - "create organization", - []string{ - "organization", "create", - "--name", "clitestOrg", - "-j", - }, - }, - } - tests := []Test{ - {"organization list", []string{"organization", "get"}}, - {"organization get", []string{"organization", "get", "-i"}}, - {"organization update", []string{"organization", "update", "-n", "updatednamefromCLI", "-i"}}, - } - cleanUp := []Test{ - {"organization delete", []string{"organization", "delete", "-i"}}, - } - - for _, tt := range setupTests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - - if tt.args[0] == "organization" && tt.args[1] == "create" { - organization := &packngo.Organization{} - err := json.Unmarshal([]byte(actual), organization) - if err != nil { - t.Fatal(err) - } - - organizationID = (*organization).ID - } - }) - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - if (tt.name == "organization get" || - tt.name == "organization update") && organizationID != "" { - tt.args = append(tt.args, organizationID) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } - - for _, tt := range cleanUp { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - if tt.name == "organization delete" && organizationID != "" { - tt.args = append(tt.args, organizationID) - tt.args = append(tt.args, "-f") - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -} diff --git a/tests/plan_test.go b/tests/plan_test.go deleted file mode 100644 index a4affec7..00000000 --- a/tests/plan_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" -) - -func TestPlanOperations(t *testing.T) { - tests := []Test{ - {"plan get", []string{"plan", "get"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -} diff --git a/tests/project_test.go b/tests/project_test.go deleted file mode 100644 index 0e5675e3..00000000 --- a/tests/project_test.go +++ /dev/null @@ -1,101 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" - - "github.com/packethost/packngo" -) - -func TestProjectOperations(t *testing.T) { - client, _ = packngo.NewClientWithBaseURL(consumerToken, testToken(), nil, apiURL) - orgs, _, _ := client.Organizations.List(nil) - orgID := orgs[0].ID - - tests := []Test{ - {"project create", []string{"project", "create", "-n", "test", "-o", orgID, "-j"}}, - {"project get", []string{"project", "get"}}, - } - - sshKeys, _, _ := client.SSHKeys.List() - for _, key := range sshKeys { - if key.Label == "test" { - sshKeyID = key.ID - fmt.Println("sshkeyID", sshKeyID) - - break - } - } - - cleanup := []Test{ - {"project get", []string{"project", "get", "-i"}}, - {"project delete", []string{"project", "delete", "-i"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - - if tt.name == "project create" { - project := &packngo.Project{} - err := json.Unmarshal([]byte(actual), project) - if err != nil { - t.Fatal(err) - } - - projectID = (*project).ID - } - }) - } - - for _, tt := range cleanup { - t.Run(tt.name, func(t *testing.T) { - if tt.name == "project get" && projectID != "" { - tt.args = append(tt.args, projectID) - } - - if tt.name == "project delete" && projectID != "" { - tt.args = append(tt.args, projectID) - tt.args = append(tt.args, "-f") - } - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -} diff --git a/tests/ssh_key_test.go b/tests/ssh_key_test.go deleted file mode 100644 index 43947b29..00000000 --- a/tests/ssh_key_test.go +++ /dev/null @@ -1,119 +0,0 @@ -package main - -import ( - "crypto/rand" - "crypto/rsa" - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" - - "github.com/packethost/packngo" - "golang.org/x/crypto/ssh" -) - -var sshKeyID string - -func TestSSHKeyOperations(t *testing.T) { - client, _ = packngo.NewClientWithBaseURL(consumerToken, testToken(), nil, apiURL) - - publicKey, err := generatePublicKey() - if err != nil { - fmt.Println("SSH Key generation error:", err) - } - - tests := []Test{ - {"ssh-key create", []string{"ssh-key", "create", "-l", "test", "-k", publicKey}}, - {"ssh-key list", []string{"ssh-key", "get", "-j"}}, - } - - cleanup := []Test{ - {"ssh-key get", []string{"ssh-key", "get", "-i"}}, - {"ssh-key delete", []string{"ssh-key", "delete", "-i"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error") { - t.Fatal(actual) - } - }) - } - - sshKeys, _, _ := client.SSHKeys.List() - for _, key := range sshKeys { - if key.Label == "test" { - sshKeyID = key.ID - break - } - } - fmt.Println("outside", sshKeyID) - - for _, tt := range cleanup { - t.Run(tt.name, func(t *testing.T) { - if tt.name == "ssh-key get" && sshKeyID != "" { - tt.args = append(tt.args, sshKeyID) - } - - if tt.name == "ssh-key delete" && sshKeyID != "" { - tt.args = append(tt.args, sshKeyID) - tt.args = append(tt.args, "-f") - } - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -} - -func generatePublicKey() (string, error) { - privateKey, err := rsa.GenerateKey(rand.Reader, 4096) - if err != nil { - return "", err - } - - // Validate Private Key - err = privateKey.Validate() - if err != nil { - return "", err - } - - publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey) - if err != nil { - return "", err - } - - pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey) - return string(pubKeyBytes), nil -} diff --git a/tests/virtual_network_test.go b/tests/virtual_network_test.go deleted file mode 100644 index 404bd713..00000000 --- a/tests/virtual_network_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "path" - "strings" - "testing" - - "github.com/packethost/packngo" -) - -var virtualNetworkID string - -func TestVirtualNetworkOperations(t *testing.T) { - client, _ = packngo.NewClientWithBaseURL(consumerToken, testToken(), nil, apiURL) - projects, _, _ := client.Projects.List(nil) - projectID = projects[0].ID - setupTests := []Test{ - { - "create virtual-network", - []string{ - "virtual-network", "create", - "--facility", "ewr1", - "--project-id", projectID, - "-j", - }, - }, - } - tests := []Test{ - {"virtual-network list", []string{"virtual-network", "get", "-p", projectID}}, - } - cleanUp := []Test{ - {"virtual-network delete", []string{"virtual-network", "delete", "-i"}}, - } - - for _, tt := range setupTests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - - if tt.args[0] == "virtual-network" && tt.args[1] == "create" { - virtualNetwork := &packngo.VirtualNetwork{} - err := json.Unmarshal([]byte(actual), virtualNetwork) - if err != nil { - t.Fatal(err) - } - - virtualNetworkID = (*virtualNetwork).ID - } - }) - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } - - for _, tt := range cleanUp { - t.Run(tt.name, func(t *testing.T) { - fmt.Println(tt.name, tt.args) - - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - if tt.name == "virtual-network delete" && virtualNetworkID != "" { - tt.args = append(tt.args, virtualNetworkID) - } - - cmd := exec.Command(path.Join(dir, binaryName), tt.args...) - - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatal(err) - } - - actual := string(output) - if strings.Contains(strings.ToLower(actual), "error:") { - t.Fatal(actual) - } - }) - } -}