Skip to content

Commit

Permalink
Added unit tests for /pkg/version (#5074)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Part of #5068 

## Description of the changes
- added some missing unit tests in /pkg/version

## How was this change tested?
- go test

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Pushkar Mishra <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
Pushkarm029 and yurishkuro authored Jan 3, 2024
1 parent 154d662 commit 2c7947c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 5 deletions.
2 changes: 0 additions & 2 deletions pkg/version/.nocover

This file was deleted.

46 changes: 46 additions & 0 deletions pkg/version/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"testing"

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

func TestGet(t *testing.T) {
commitSHA = "foobar"
latestVersion = "v1.2.3"
date = "2024-01-04"

info := Get()

assert.Equal(t, commitSHA, info.GitCommit)
assert.Equal(t, latestVersion, info.GitVersion)
assert.Equal(t, date, info.BuildDate)
}

func TestString(t *testing.T) {
commitSHA = "foobar"
latestVersion = "v1.2.3"
date = "2024-01-04"
test := Info{
GitCommit: commitSHA,
GitVersion: latestVersion,
BuildDate: date,
}
expectedOutput := "git-commit=foobar, git-version=v1.2.3, build-date=2024-01-04"
assert.Equal(t, expectedOutput, test.String())
}
5 changes: 2 additions & 3 deletions pkg/version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// Command creates version command
func Command() *cobra.Command {
info := Get()
log.Println("applicatio version:", info)
log.Println("application version:", info)
return &cobra.Command{
Use: "version",
Short: "Print the version.",
Expand All @@ -35,8 +35,7 @@ func Command() *cobra.Command {
if err != nil {
return err
}
fmt.Println(string(json))

fmt.Fprint(cmd.OutOrStdout(), string(json))
return nil
},
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/version/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2024 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCommand(t *testing.T) {
commitSHA = "foobar"
latestVersion = "v1.2.3"
date = "2024-01-04"
cmd := Command()

var b bytes.Buffer
cmd.SetOut(&b)
err := cmd.Execute()
require.NoError(t, err)
out, err := io.ReadAll(&b)
require.NoError(t, err)
expectedCommandOutput := `{"gitCommit":"foobar","gitVersion":"v1.2.3","buildDate":"2024-01-04"}`
assert.Equal(t, expectedCommandOutput, string(out))
}
48 changes: 48 additions & 0 deletions pkg/version/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestRegisterHandler(t *testing.T) {
commitSHA = "foobar"
latestVersion = "v1.2.3"
date = "2024-01-04"
expectedJSON := []byte(`{"gitCommit":"foobar","gitVersion":"v1.2.3","buildDate":"2024-01-04"}`)

mockLogger := zap.NewNop()
mux := http.NewServeMux()
RegisterHandler(mux, mockLogger)
server := httptest.NewServer(mux)

defer server.Close()

resp, err := http.Get(server.URL + "/version")
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, expectedJSON, body)
}

0 comments on commit 2c7947c

Please sign in to comment.