Use OS-specific libs when computing client User-Agent.

**What this PR does / why we need it**:

The User-Agent reported by clients (e.g. kubectl) in request
headers should include the name of the client executable
but not the full path to that executable.

This PR changes how this name is determined by using the
operating-system specific package "path/filepath" (meant for
working with file system paths) instead of the "path" package
(meant for URL paths).

This fixes a problem on the Windows OS in the case where, if the
user has not set their PATH to point to the location of their
client executable, the User-Agent unnecessarily includes the
full path.

Fixes: #44419

Kubernetes-commit: 04f993250bc7a1aef0f2874d440ddb4bec1012c5
This commit is contained in:
Jeffrey Regan
2017-04-11 14:11:52 -07:00
committed by Kubernetes Publisher
parent bb2e2ad83c
commit 633aab4bea
3 changed files with 84 additions and 13 deletions

View File

@@ -19,6 +19,7 @@ package rest
import (
"io"
"net/http"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -32,6 +33,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/flowcontrol"
"github.com/stretchr/testify/assert"
_ "k8s.io/client-go/pkg/api/install"
)
@@ -100,6 +102,42 @@ func TestSetKubernetesDefaultsUserAgent(t *testing.T) {
}
}
func TestAdjustVersion(t *testing.T) {
assert := assert.New(t)
assert.Equal("1.2.3", adjustVersion("1.2.3-alpha4"))
assert.Equal("1.2.3", adjustVersion("1.2.3-alpha"))
assert.Equal("1.2.3", adjustVersion("1.2.3"))
assert.Equal("unknown", adjustVersion(""))
}
func TestAdjustCommit(t *testing.T) {
assert := assert.New(t)
assert.Equal("1234567", adjustCommit("1234567890"))
assert.Equal("123456", adjustCommit("123456"))
assert.Equal("unknown", adjustCommit(""))
}
func TestAdjustCommand(t *testing.T) {
assert := assert.New(t)
assert.Equal("beans", adjustCommand(filepath.Join("home", "bob", "Downloads", "beans")))
assert.Equal("beans", adjustCommand(filepath.Join(".", "beans")))
assert.Equal("beans", adjustCommand("beans"))
assert.Equal("unknown", adjustCommand(""))
}
func TestBuildUserAgent(t *testing.T) {
assert.New(t).Equal(
"lynx/nicest (beos/itanium) kubernetes/baaaaaaaaad",
buildUserAgent(
"lynx", "nicest",
"beos", "itanium", "baaaaaaaaad"))
}
// This function untestable since it doesn't accept arguments.
func TestDefaultKubernetesUserAgent(t *testing.T) {
assert.New(t).Contains(DefaultKubernetesUserAgent(), "kubernetes")
}
func TestRESTClientRequires(t *testing.T) {
if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{NegotiatedSerializer: api.Codecs}}); err == nil {
t.Errorf("unexpected non-error")