From a66d0ff8c73a27aa68f90bf898da08f647d6fd01 Mon Sep 17 00:00:00 2001 From: Andrey Pozolotin Date: Tue, 23 Mar 2021 00:40:19 +0300 Subject: [PATCH] Added test to job rendering logic --- .github/workflows/test.yml | 2 +- internal/pkg/config/input_test.go | 96 +++++++++++++++++++++++++++++++ internal/pkg/output/job_test.go | 19 ++++-- 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 internal/pkg/config/input_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2fe0864..8ab8241 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/internal/pkg/config/input_test.go b/internal/pkg/config/input_test.go new file mode 100644 index 0000000..3905598 --- /dev/null +++ b/internal/pkg/config/input_test.go @@ -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", "")) +} diff --git a/internal/pkg/output/job_test.go b/internal/pkg/output/job_test.go index 6379a3e..e21b06f 100644 --- a/internal/pkg/output/job_test.go +++ b/internal/pkg/output/job_test.go @@ -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 @@ -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":""}} `, }, { @@ -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, @@ -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 @@ -91,6 +101,7 @@ result: Status: "success", FinishedAt: timeToCheck, ClientID: "cl123", + ClientName: "some cl name", Command: "ls", Shell: "cmd", Pid: 123, @@ -107,7 +118,7 @@ result: jr := &JobRenderer{ Writer: buf, Format: testCase.Format, - IsFullOutput: true, + IsFullOutput: testCase.IsFullOutput, } err = jr.RenderJob(tunnel)