pull/32469/merge
yugo kobayashi 1 month ago committed by GitHub
commit a00bf80f23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -21,7 +21,7 @@ import (
"runtime/debug"
"slices"
_ "k8s.io/client-go/kubernetes" // Force k8s.io/client-go to be included in the build
_ "k8s.io/client-go/pkg/version" // Force the k8s.io/client-go module version into build info
)
func K8sIOClientGoModVersion() (string, error) {

@ -17,14 +17,52 @@ limitations under the License.
package version
import (
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestK8sClientGoModVersion(t *testing.T) {
// Unfortunately, test builds don't include debug info / module info
// So we expect "K8sIOClientGoModVersion" to return error
// Generated test binaries omit dependency modules from embedded build info,
// even when the package under test imports them. The built-binary test below
// covers the normal executable path where dependency modules are present.
_, err := K8sIOClientGoModVersion()
require.ErrorContains(t, err, "k8s.io/client-go not found in build info")
}
func TestK8sClientGoModVersionFromBuiltBinary(t *testing.T) {
cmd := exec.CommandContext(t.Context(), "go", "list", "-m", "-f={{.Version}}", "k8s.io/client-go")
expectedVersion, err := cmd.CombinedOutput()
require.NoError(t, err, string(expectedVersion))
binary := filepath.Join(t.TempDir(), "clientgo-version")
if runtime.GOOS == "windows" {
binary += ".exe"
}
cmd = exec.CommandContext(t.Context(), "go", "build", "-o", binary, "./testdata/clientgo-version")
output, err := cmd.CombinedOutput()
require.NoError(t, err, string(output))
cmd = exec.CommandContext(t.Context(), binary)
output, err = cmd.CombinedOutput()
require.NoError(t, err, string(output))
require.Equal(t, strings.TrimSpace(string(expectedVersion)), strings.TrimSpace(string(output)))
}
func TestK8sClientGoDependencyIsLightweight(t *testing.T) {
cmd := exec.CommandContext(t.Context(), "go", "list", "-deps", ".")
output, err := cmd.CombinedOutput()
require.NoError(t, err, string(output))
dependencies := strings.Fields(string(output))
require.Contains(t, dependencies, "k8s.io/client-go/pkg/version")
for _, dependency := range dependencies {
require.NotEqual(t, "k8s.io/client-go/kubernetes", dependency)
require.NotContains(t, dependency, "k8s.io/client-go/kubernetes/")
}
}

@ -0,0 +1,34 @@
/*
Copyright The Helm 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 main
import (
"fmt"
"os"
"helm.sh/helm/v4/internal/version"
)
func main() {
clientGoVersion, err := version.K8sIOClientGoModVersion()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(clientGoVersion)
}
Loading…
Cancel
Save