forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-add ocb version command and unit tests
- Loading branch information
1 parent
df3c9e3
commit f88230e
Showing
4 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: cmd/builder | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: re-adds function to properly set and view version number of OpenTelemetry Collector Builder (ocb) binaries | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [11208] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"runtime/debug" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Mock debug.ReadBuildInfo function | ||
var readBuildInfo = debug.ReadBuildInfo | ||
|
||
func TestBinVersion(t *testing.T) { | ||
// Test case: version is set | ||
version = "v1.0.0" | ||
v, err := binVersion(readBuildInfo) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if v != "v1.0.0" { | ||
t.Fatalf("expected version 'v1.0.0', got %v", v) | ||
} | ||
|
||
// // Test case: version is not set, ReadBuildInfo returns valid info | ||
version = "" | ||
readBuildInfo = func() (*debug.BuildInfo, bool) { | ||
return &debug.BuildInfo{ | ||
Main: debug.Module{ | ||
Version: "v2.0.0", | ||
}, | ||
}, true | ||
} | ||
v, err = binVersion(readBuildInfo) | ||
fmt.Printf("v: %v, err: %v", v, err) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if v != "v2.0.0" { | ||
t.Fatalf("expected version 'v2.0.0', got %v", v) | ||
} | ||
|
||
// Test case: version is not set, ReadBuildInfo fails | ||
readBuildInfo = func() (*debug.BuildInfo, bool) { | ||
return nil, false | ||
} | ||
v, err = binVersion(readBuildInfo) | ||
if err == nil { | ||
t.Fatalf("expected error, got nil") | ||
} | ||
if v != "" { | ||
t.Fatalf("expected empty version, got %v", v) | ||
} | ||
} | ||
|
||
var validBinVersionFunc binVersionFunc = func(_ debugReadBuildInfoFunc) (string, error) { | ||
return "v1.0.0", nil | ||
} | ||
|
||
var invalidBinVersionFunc binVersionFunc = func(_ debugReadBuildInfoFunc) (string, error) { | ||
return "", fmt.Errorf("failed to get version") | ||
} | ||
|
||
func TestVersionCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
binVersion binVersionFunc | ||
expectedOutput string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "valid version", | ||
binVersion: validBinVersionFunc, | ||
expectedOutput: "ocb version v1.0.0\n", | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "error in binVersion", | ||
binVersion: invalidBinVersionFunc, | ||
expectedOutput: "", | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Set a mock parent command name | ||
parentCmd := &cobra.Command{ | ||
Use: "ocb <command>", | ||
} | ||
// Create the command | ||
var cmd = versionCommand(tt.binVersion) | ||
parentCmd.AddCommand(cmd) | ||
// Capture the output | ||
output := bytes.NewBufferString("") | ||
errOutput := bytes.NewBufferString("") | ||
cmd.SetOut(output) | ||
cmd.SetErr(errOutput) | ||
// Create a new context with a fake value | ||
type contextKey string | ||
ctx := context.WithValue(context.Background(), contextKey("key"), "value") | ||
// Set fake CLI arguments | ||
fakeArgs := []string{"cmd", "version"} | ||
os.Args = fakeArgs | ||
// Execute the command | ||
err := cmd.ExecuteContext(ctx) | ||
// Check for expected error | ||
if (err != nil) != tt.expectedError { | ||
t.Fatalf("expected error: %v, got: %v", tt.expectedError, err) | ||
} | ||
// Check for expected output | ||
if output.String() != tt.expectedOutput { | ||
t.Fatalf("expected output: %v, got: %v", tt.expectedOutput, output.String()) | ||
} | ||
}) | ||
} | ||
} |