Repository: clouditor/clouditor
Pull request #237 (2021-06-06 22:45:48)

Added more tests for CLI

codecov-commenter: # [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) Report
> Merging [#237](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (a5b5f97) into [main](https://codecov.io/gh/clouditor/clouditor/commit/9473a90a81964cd710cd07a723a33cba2ea54180?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (9473a90) will **increase** coverage by `2.81%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/clouditor/clouditor/pull/237/graphs/tree.svg?width=650&height=150&src=pr&token=syrXsw5LTX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)

```diff
@@            Coverage Diff             @@
##             main     #237      +/-   ##
==========================================
+ Coverage   44.34%   47.15%   +2.81%     
==========================================
  Files          28       28              
  Lines        1281     1281              
==========================================
+ Hits          568      604      +36     
+ Misses        648      608      -40     
- Partials       65       69       +4     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `47.15% <ø> (+2.81%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) | Coverage Δ | |
|---|---|---|
| [service/auth/auth.go](https://codecov.io/gh/clouditor/clouditor/pull/237/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-c2VydmljZS9hdXRoL2F1dGguZ28=) | `81.08% <ø> (ø)` | |
| [cli/session.go](https://codecov.io/gh/clouditor/clouditor/pull/237/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-Y2xpL3Nlc3Npb24uZ28=) | `30.55% <0.00%> (+2.77%)` | :arrow_up: |
| [cli/commands/tool/tool.go](https://codecov.io/gh/clouditor/clouditor/pull/237/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-Y2xpL2NvbW1hbmRzL3Rvb2wvdG9vbC5nbw==) | `24.08% <0.00%> (+24.08%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Last update [9473a90...a5b5f97](https://codecov.io/gh/clouditor/clouditor/pull/237?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).


Patch:
diff --git a/cli/session.go b/cli/session.go
index 783aca02..716e3633 100644
--- a/cli/session.go
+++ b/cli/session.go
@@ -31,6 +31,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"io"
 	"os"
 	"strings"
 
@@ -46,6 +47,7 @@ import (
 )
 
 var DefaultSessionFolder string
+var Output io.Writer = os.Stdout
 
 type Session struct {
 	URL   string `json:"url"`
@@ -161,7 +163,7 @@ func (s *Session) HandleResponse(msg proto.Message, err error) error {
 
 	b, _ := opt.Marshal(msg)
 
-	fmt.Printf("%s\n", string(b))
+	fmt.Fprintf(Output, "%s\n", string(b))
 
 	return err
 }
diff --git a/service/auth/auth.go b/service/auth/auth.go
index 79e53b46..20dc609f 100644
--- a/service/auth/auth.go
+++ b/service/auth/auth.go
@@ -182,8 +182,8 @@ func (s Service) issueToken(subject string, fullName string, email string, expir
 	return
 }
 
-// StartStandaloneAuthServer starts a gRPC server containing just the auth service
-func StartStandaloneAuthServer(address string) (sock net.Listener, server *grpc.Server, err error) {
+// StartDedicatedAuthServer starts a gRPC server containing just the auth service
+func StartDedicatedAuthServer(address string) (sock net.Listener, server *grpc.Server, err error) {
 	var (
 		authService *Service
 	)


Test patch:
diff --git a/cli/commands/login/login_test.go b/cli/commands/login/login_test.go
index 5b69526a..28c014cf 100644
--- a/cli/commands/login/login_test.go
+++ b/cli/commands/login/login_test.go
@@ -45,7 +45,7 @@ var server *grpc.Server
 func TestMain(m *testing.M) {
 	var err error
 
-	sock, server, err = service_auth.StartStandaloneAuthServer(":0")
+	sock, server, err = service_auth.StartDedicatedAuthServer(":0")
 
 	if err != nil {
 		panic(err)
diff --git a/cli/commands/metric/metric_test.go b/cli/commands/metric/metric_test.go
new file mode 100644
index 00000000..af224e21
--- /dev/null
+++ b/cli/commands/metric/metric_test.go
@@ -0,0 +1,78 @@
+package metric_test
+
+import (
+	"bytes"
+	"fmt"
+	"io/ioutil"
+	"net"
+	"os"
+	"testing"
+
+	"clouditor.io/clouditor/api/orchestrator"
+	"clouditor.io/clouditor/cli"
+	"clouditor.io/clouditor/cli/commands/login"
+	"clouditor.io/clouditor/cli/commands/metric"
+	service_auth "clouditor.io/clouditor/service/auth"
+	service_orchestrator "clouditor.io/clouditor/service/orchestrator"
+	"github.com/spf13/viper"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc"
+	"google.golang.org/protobuf/encoding/protojson"
+)
+
+var sock net.Listener
+var server *grpc.Server
+
+func TestMain(m *testing.M) {
+	var (
+		err error
+		dir string
+	)
+
+	sock, server, err = service_auth.StartDedicatedAuthServer(":0")
+	orchestrator.RegisterOrchestratorServer(server, &service_orchestrator.Service{})
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer sock.Close()
+	defer server.Stop()
+
+	dir, err = ioutil.TempDir(os.TempDir(), ".clouditor")
+	if err != nil {
+		panic(err)
+	}
+
+	viper.Set("username", "clouditor")
+	viper.Set("password", "clouditor")
+	viper.Set("session-directory", dir)
+
+	cmd := login.NewLoginCommand()
+	err = cmd.RunE(nil, []string{fmt.Sprintf("localhost:%d", sock.Addr().(*net.TCPAddr).Port)})
+	if err != nil {
+		panic(err)
+	}
+
+	os.Exit(m.Run())
+}
+
+func TestListTool(t *testing.T) {
+	var err error
+	var b bytes.Buffer
+
+	cli.Output = &b
+
+	cmd := metric.NewListMetricsCommand()
+	err = cmd.RunE(nil, []string{})
+
+	assert.Nil(t, err)
+
+	var response *orchestrator.ListMetricsResponse = &orchestrator.ListMetricsResponse{}
+
+	err = protojson.Unmarshal(b.Bytes(), response)
+
+	assert.Nil(t, err)
+	assert.NotNil(t, response)
+	assert.NotEmpty(t, response.Metrics)
+}
diff --git a/cli/commands/tool/tool_test.go b/cli/commands/tool/tool_test.go
new file mode 100644
index 00000000..7e2a14dd
--- /dev/null
+++ b/cli/commands/tool/tool_test.go
@@ -0,0 +1,110 @@
+package tool_test
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net"
+	"os"
+	"testing"
+
+	"clouditor.io/clouditor/api/orchestrator"
+	"clouditor.io/clouditor/cli/commands/login"
+	"clouditor.io/clouditor/cli/commands/tool"
+	service_auth "clouditor.io/clouditor/service/auth"
+	service_orchestrator "clouditor.io/clouditor/service/orchestrator"
+	"github.com/spf13/viper"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc"
+)
+
+var sock net.Listener
+var server *grpc.Server
+
+func TestMain(m *testing.M) {
+	var (
+		err error
+		dir string
+	)
+
+	sock, server, err = service_auth.StartDedicatedAuthServer(":0")
+	orchestrator.RegisterOrchestratorServer(server, &service_orchestrator.Service{})
+
+	if err != nil {
+		panic(err)
+	}
+
+	defer sock.Close()
+	defer server.Stop()
+
+	dir, err = ioutil.TempDir(os.TempDir(), ".clouditor")
+	if err != nil {
+		panic(err)
+	}
+
+	viper.Set("username", "clouditor")
+	viper.Set("password", "clouditor")
+	viper.Set("session-directory", dir)
+
+	cmd := login.NewLoginCommand()
+	err = cmd.RunE(nil, []string{fmt.Sprintf("localhost:%d", sock.Addr().(*net.TCPAddr).Port)})
+	if err != nil {
+		panic(err)
+	}
+
+	os.Exit(m.Run())
+}
+
+func TestListTool(t *testing.T) {
+	var err error
+
+	cmd := tool.NewListToolsCommand()
+	err = cmd.RunE(nil, []string{})
+
+	// unsupported for now
+	assert.NotNil(t, err)
+	assert.Equal(t, err.Error(), "method ListAssessmentTools not implemented")
+}
+
+func TestShowTool(t *testing.T) {
+	var err error
+
+	cmd := tool.NewShowToolCommand()
+	err = cmd.RunE(nil, []string{"1"})
+
+	// unsupported for now
+	assert.NotNil(t, err)
+	assert.Equal(t, err.Error(), "method GetAssessmentTool not implemented")
+}
+
+func TestUpdateTool(t *testing.T) {
+	var err error
+
+	cmd := tool.NewUpdateToolCommand()
+	err = cmd.RunE(nil, []string{"1"})
+
+	// unsupported for now
+	assert.NotNil(t, err)
+	assert.Equal(t, err.Error(), "method UpdateAssessmentTool not implemented")
+}
+
+func TestRegisterTool(t *testing.T) {
+	var err error
+
+	cmd := tool.NewRegisterToolCommand()
+	err = cmd.RunE(nil, []string{})
+
+	// unsupported for now
+	assert.NotNil(t, err)
+	assert.Equal(t, err.Error(), "method RegisterAssessmentTool not implemented")
+}
+
+func TestDeregisterTool(t *testing.T) {
+	var err error
+
+	cmd := tool.NewDeregisterToolCommand()
+	err = cmd.RunE(nil, []string{"1"})
+
+	// unsupported for now
+	assert.NotNil(t, err)
+	assert.Equal(t, err.Error(), "method DeregisterAssessmentTool not implemented")
+}
diff --git a/cli/session_test.go b/cli/session_test.go
index 1c4fcdfb..0b5cdd69 100644
--- a/cli/session_test.go
+++ b/cli/session_test.go
@@ -49,7 +49,7 @@ var server *grpc.Server
 func TestMain(m *testing.M) {
 	var err error
 
-	sock, server, err = service_auth.StartStandaloneAuthServer(":0")
+	sock, server, err = service_auth.StartDedicatedAuthServer(":0")
 
 	if err != nil {
 		panic(err)