Skip to content

Commit

Permalink
test: add test for getVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Taowyoo committed Apr 26, 2024
1 parent a4ee708 commit a1c05db
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
61 changes: 61 additions & 0 deletions cmd/getVersion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright (c) Fortanix, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package cmd

import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/fortanix/sdkms-client-go/sdkms"
"github.com/stretchr/testify/assert"
)

func TestTestSetupCmd(t *testing.T) {
resetRootCmdStatus()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/sys/v1/version" {
t.Errorf("Expected to request '/sys/v1/version', got: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
resp := sdkms.VersionResponse{
Version: "1234",
APIVersion: "1234",
ServerMode: "httptest",
}
respBytes, err := json.Marshal(resp)
if err != nil {
t.Fatal("Error encoding fake version response:", err)
}
w.Write(respBytes)
}))
defer server.Close()
// Parse the URL
parsedServerURL, err := url.Parse(server.URL)
if err != nil {
t.Fatal("Error parsing test server URL:", err)
}
testServerHost := parsedServerURL.Hostname()
testServerPort := parsedServerURL.Port()
if testServerPort == "" {
testServerPort = "443"
}

args := []string{
"--server", testServerHost,
"--port", testServerPort,
"--insecure",
"version",
"--count", "1",
}

rootCmd.SetArgs(args)
err = ExecuteCmd()
assert.NoError(t, err)
}
19 changes: 14 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRootCmdDefaultFlags(t *testing.T) {
func resetRootCmdStatus() {
rootCmd.SetArgs([]string{})
serverName = defaultServerName
serverPort = defaultServerPort
insecureTLS = defaultTlsNotVerify
requestTimeout = defaultRequestTimeout
idleConnectionTimeout = defaultIdleConnectionTimeout
}

func TestRootCmdDefaultFlags(t *testing.T) {
resetRootCmdStatus()
err := ExecuteCmd()
assert.NoError(t, err)
assert.Equal(t, defaultServerName, serverName)
Expand All @@ -26,6 +35,7 @@ func TestRootCmdDefaultFlags(t *testing.T) {
}

func TestRootCmdWithCustomArgs(t *testing.T) {
resetRootCmdStatus()
customServerName := "sdkms.custom.fortanix.com"
customServerPort := uint16(8080)
customInsecureTLS := true
Expand All @@ -51,10 +61,7 @@ func TestRootCmdWithCustomArgs(t *testing.T) {
}

func TestRootCmdWithCustomShortArgs(t *testing.T) {
// reset values
insecureTLS = defaultTlsNotVerify
requestTimeout = defaultRequestTimeout
idleConnectionTimeout = defaultIdleConnectionTimeout
resetRootCmdStatus()

customServerName := "sdkms.custom.fortanix.com"
customServerPort := uint16(8080)
Expand All @@ -75,6 +82,8 @@ func TestRootCmdWithCustomShortArgs(t *testing.T) {
}

func TestRootCmdWithInvalidArgs(t *testing.T) {
resetRootCmdStatus()

invalidServerPort := "invalid-server-port"
invalidRequestTimeout := "invalid-request-timeout"
invalidIdleConnectionTimeout := "invalid-idle-connection-timeout"
Expand Down

0 comments on commit a1c05db

Please sign in to comment.