cleanup integration test helpers
This commit is contained in:
parent
82d885c213
commit
3fcc2edce6
|
|
@ -48,10 +48,8 @@ var knownFailingFiles = sets.NewString(
|
||||||
"k8s.io/registry.k8s.io/cmd/geranos/s3uploader.go",
|
"k8s.io/registry.k8s.io/cmd/geranos/s3uploader.go",
|
||||||
"k8s.io/registry.k8s.io/cmd/geranos/schemav1.go",
|
"k8s.io/registry.k8s.io/cmd/geranos/schemav1.go",
|
||||||
"k8s.io/registry.k8s.io/cmd/geranos/walkimages.go",
|
"k8s.io/registry.k8s.io/cmd/geranos/walkimages.go",
|
||||||
// integration test utilites
|
// We cover this with integration tests and including integration coverage
|
||||||
"k8s.io/registry.k8s.io/internal/integration/paths.go",
|
// here would mask a lack of unit test coverage.
|
||||||
"k8s.io/registry.k8s.io/internal/integration/bins.go",
|
|
||||||
// TODO: we can cover this
|
|
||||||
"k8s.io/registry.k8s.io/cmd/archeio/main.go",
|
"k8s.io/registry.k8s.io/cmd/archeio/main.go",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2022 The Kubernetes 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 integration
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func EnsureBinsInPath(binDir string) error {
|
|
||||||
path := os.Getenv("PATH")
|
|
||||||
// if bins are already at front of PATH, do nothing
|
|
||||||
if strings.HasPrefix(path, binDir+string(os.PathSeparator)) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// otherwise prepend and set
|
|
||||||
newPath := binDir + string(os.PathListSeparator) + path
|
|
||||||
return os.Setenv("PATH", newPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnsureCrane ensures crane is available in PATH for testing
|
|
||||||
// under rootPath/bin
|
|
||||||
// See also: EnsureBinsInPath
|
|
||||||
func EnsureCrane(rootPath string) error {
|
|
||||||
// ensure $REPO_ROOT/bin is in the front of $PATH
|
|
||||||
root, err := ModuleRootDir()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to detect path to project root: %w", err)
|
|
||||||
}
|
|
||||||
binDir := rootToBinDir(root)
|
|
||||||
if err := EnsureBinsInPath(binDir); err != nil {
|
|
||||||
return fmt.Errorf("failed to ensure PATH: %w", err)
|
|
||||||
}
|
|
||||||
// install crane
|
|
||||||
// nolint:gosec // we *want* user supplied command arguments ...
|
|
||||||
cmd := exec.Command(
|
|
||||||
"go", "build",
|
|
||||||
"-o", filepath.Join(binDir, "crane"),
|
|
||||||
"github.com/google/go-containerregistry/cmd/crane",
|
|
||||||
)
|
|
||||||
cmd.Dir = rootToToolsDir(root)
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to install crane: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
@ -23,8 +23,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ModuleRootDir() (string, error) {
|
func ModuleRootDir() (string, error) {
|
||||||
|
return moduleRootDir(os.Getwd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func moduleRootDir(getWD func() (string, error)) (string, error) {
|
||||||
// in a test, the working directory will be the test package source dir
|
// in a test, the working directory will be the test package source dir
|
||||||
wd, err := os.Getwd()
|
wd, err := getWD()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -35,8 +39,6 @@ func ModuleRootDir() (string, error) {
|
||||||
_, err := os.Stat(filepath.Join(currDir, "go.mod"))
|
_, err := os.Stat(filepath.Join(currDir, "go.mod"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return currDir, nil
|
return currDir, nil
|
||||||
} else if !os.IsNotExist(err) {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
// if we get back the same path, we've hit the disk / volume root
|
// if we get back the same path, we've hit the disk / volume root
|
||||||
nextDir := filepath.Dir(currDir)
|
nextDir := filepath.Dir(currDir)
|
||||||
|
|
@ -46,11 +48,3 @@ func ModuleRootDir() (string, error) {
|
||||||
currDir = nextDir
|
currDir = nextDir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func rootToBinDir(root string) string {
|
|
||||||
return filepath.Join(root, "bin")
|
|
||||||
}
|
|
||||||
|
|
||||||
func rootToToolsDir(root string) string {
|
|
||||||
return filepath.Join(root, "hack", "tools")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,35 @@ limitations under the License.
|
||||||
|
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestModuleRootDir(t *testing.T) {
|
func TestModuleRootDir(t *testing.T) {
|
||||||
root, err := ModuleRootDir()
|
root, err := ModuleRootDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error getting root dir: %v", err)
|
t.Fatalf("unexpected error getting root dir: %v", err)
|
||||||
|
} else if root == "" {
|
||||||
|
t.Fatal("expected root dir to be non-empty string")
|
||||||
}
|
}
|
||||||
if root == "" {
|
|
||||||
t.Fatalf("expected root dir to be non-empty string")
|
// we reasonably assume the filesystem root is not a module
|
||||||
|
wdAlwaysRoot := func() (string, error) { return "/", nil }
|
||||||
|
root, err = moduleRootDir(wdAlwaysRoot)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error getting moduleRootDir for /")
|
||||||
|
} else if root != "" {
|
||||||
|
t.Fatal("did not expect non-empty string getting moduleRootDir for /")
|
||||||
|
}
|
||||||
|
|
||||||
|
// test error handling for os.Getwd
|
||||||
|
expectErr := errors.New("err")
|
||||||
|
wdAlwaysError := func() (string, error) { return "", expectErr }
|
||||||
|
root, err = moduleRootDir(wdAlwaysError)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error getting moduleRootDir with erroring getWD")
|
||||||
|
} else if root != "" {
|
||||||
|
t.Fatal("did not expect non-empty string getting moduleRootDir for erroring getWD")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue