Skip to content

Commit

Permalink
Added test to job rendering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
breathbath committed Mar 22, 2021
1 parent b2468f8 commit a66d0ff
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
version: v1.30
args: -c .golangci.yml
test:
name: Test
Expand Down
96 changes: 96 additions & 0 deletions internal/pkg/config/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package config

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestCommandPopulation(t *testing.T) {
reqs := []ParameterRequirement{
{
Field: "color",
ShortName: "c",
Help: "shows color",
Default: "red",
Description: "shows color me",
IsSecure: false,
IsRequired: false,
Type: StringRequirementType,
},
{
Field: "verbose",
ShortName: "v",
Help: "shows verbose output",
Default: "1",
Description: "shows verbose output",
IsSecure: false,
IsRequired: false,
Type: BoolRequirementType,
},
}

cmd := &cobra.Command{}
DefineCommandInputs(cmd, reqs)

_, err1 := cmd.Flags().GetBool("verbose")
assert.NoError(t, err1)

_, err2 := cmd.Flags().GetString("color")
assert.NoError(t, err2)
}

func TestCollectParams(t *testing.T) {
prm := &PromptReaderMock{
Inputs: []string{},
ReadOutputs: []string{
"127.1.1.1",
},
PasswordReadOutputs: []string{
"123",
},
}

reqs := []ParameterRequirement{
{
Field: "host",
ShortName: "h",
IsSecure: false,
IsRequired: true,
Type: StringRequirementType,
Validate: RequiredValidate,
},
{
Field: "https",
ShortName: "s",
Help: "uses https protocol",
Default: "0",
IsSecure: false,
IsRequired: false,
Type: BoolRequirementType,
},
{
Field: "password",
ShortName: "p",
Help: "provides password",
IsSecure: true,
IsRequired: true,
Type: StringRequirementType,
Validate: RequiredValidate,
},
}

cmd := &cobra.Command{}
DefineCommandInputs(cmd, reqs)

params, err := CollectParams(cmd, reqs, prm)
assert.NoError(t, err)
if err != nil {
return
}

assert.Equal(t, "127.1.1.1", params.ReadString("host", ""))
assert.False(t, params.ReadBool("https", true))
assert.Equal(t, "123", params.ReadString("password", ""))
}
19 changes: 15 additions & 4 deletions internal/pkg/output/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestRenderJob(t *testing.T) {
testCases := []struct {
Format string
ExpectedOutput string
IsFullOutput bool
}{
{
Format: FormatHuman,
ExpectedOutput: `Client ID: cl123
Client Name:
Client Name: some cl name
Command Execution Result
Job ID: 123
Status: success
Expand All @@ -36,10 +37,18 @@ Client Name:
Created By: me
Multi Job ID:
`,
IsFullOutput: true,
},
{
Format: FormatHuman,
ExpectedOutput: `some cl name
some std
`,
IsFullOutput: false,
},
{
Format: FormatJSON,
ExpectedOutput: `{"jid":"123","status":"success","finished_at":"2021-01-01T00:00:01Z","client_id":"cl123","command":"ls","shell":"cmd","pid":123,"started_at":"2021-01-01T00:00:01Z","created_by":"me","multi_job_id":"","timeout_sec":10,"error":"","result":{"stdout":"some std","stderr":""}}
ExpectedOutput: `{"jid":"123","status":"success","finished_at":"2021-01-01T00:00:01Z","client_id":"cl123","client_name":"some cl name","command":"ls","shell":"cmd","pid":123,"started_at":"2021-01-01T00:00:01Z","created_by":"me","multi_job_id":"","timeout_sec":10,"error":"","result":{"stdout":"some std","stderr":""}}
`,
},
{
Expand All @@ -49,6 +58,7 @@ Client Name:
"status": "success",
"finished_at": "2021-01-01T00:00:01Z",
"client_id": "cl123",
"client_name": "some cl name",
"command": "ls",
"shell": "cmd",
"pid": 123,
Expand All @@ -70,7 +80,7 @@ Client Name:
status: success
finishedat: 2021-01-01T00:00:01Z
clientid: cl123
clientname: ""
clientname: some cl name
command: ls
shell: cmd
pid: 123
Expand All @@ -91,6 +101,7 @@ result:
Status: "success",
FinishedAt: timeToCheck,
ClientID: "cl123",
ClientName: "some cl name",
Command: "ls",
Shell: "cmd",
Pid: 123,
Expand All @@ -107,7 +118,7 @@ result:
jr := &JobRenderer{
Writer: buf,
Format: testCase.Format,
IsFullOutput: true,
IsFullOutput: testCase.IsFullOutput,
}

err = jr.RenderJob(tunnel)
Expand Down

0 comments on commit a66d0ff

Please sign in to comment.