Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions hack/lib/test_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,37 @@ function trap_add() {
fatal "unable to add to trap ${trap_add_name}"
done
}

# add_go_mod_replace adds a "replace" directive from $1 to $2 with an
# optional version version $3 to the current working directory's go.mod file.
function add_go_mod_replace() {
local from_path="${1:?first path in replace statement is required}"
local to_path="${2:?second path in replace statement is required}"
local version="${3:-}"

if [[ ! -d "$to_path" && -z "$version" ]]; then
echo "second replace path $to_path requires a version be set because it is not a directory"
exit 1
fi
if [[ ! -e go.mod ]]; then
echo "go.mod file not found in $(pwd)"
exit 1
fi

# If $to_path is a directory, it needs a `go.mod` file that specifies the
# module name to make the go toolchain happy.
#
# TODO: remove the below if statement once
# https://github.com/operator-framework/operator-sdk/pull/1566 is merged,
# which updates the SDK to use go modules.
if [[ -d "${to_path}" && ! -e "${to_path}/go.mod" ]]; then
echo "module ${from_path}" > "${to_path}/go.mod"
trap_add "rm ${to_path}/go.mod" EXIT
fi
# Do not use "go mod edit" so formatting stays the same.
local replace="replace ${from_path} => ${to_path}"
if [[ -n "$version" ]]; then
replace="$replace $version"
fi
echo "$replace" >> go.mod
}
21 changes: 2 additions & 19 deletions hack/tests/e2e-ansible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,8 @@ then
exit 1
fi

# Remove any "replace" and "require" lines for the SDK repo before vendoring
# in case this is a release PR and the tag doesn't exist yet. This must be
# done without using "go mod edit", which first parses go.mod and will error
# if it doesn't find a tag/version/package.
# TODO: remove SDK repo references if PR/branch is not from the main SDK repo.
SDK_REPO="github.com/operator-framework/operator-sdk"
sed -E -i 's|^.*'"$SDK_REPO"'.*$||g' go.mod

# Run `go build ./...` to pull down the deps specified by the scaffolded
# `go.mod` file and verify dependencies build correctly.
go build ./...

# Use the local operator-sdk directory as the repo. To make the go toolchain
# happy, the directory needs a `go.mod` file that specifies the module name,
# so we need this temporary hack until we update the SDK repo itself to use
# Go modules.
echo "module ${SDK_REPO}" > "${ROOTDIR}/go.mod"
trap_add "rm ${ROOTDIR}/go.mod" EXIT
go mod edit -replace="${SDK_REPO}=$ROOTDIR"
add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR"
# Build the project to resolve dependency versions in the modfile.
go build ./...

operator-sdk build "$DEST_IMAGE"
Expand Down
21 changes: 2 additions & 19 deletions hack/tests/e2e-helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,8 @@ then
exit 1
fi

# Remove any "replace" and "require" lines for the SDK repo before vendoring
# in case this is a release PR and the tag doesn't exist yet. This must be
# done without using "go mod edit", which first parses go.mod and will error
# if it doesn't find a tag/version/package.
# TODO: remove SDK repo references if PR/branch is not from the main SDK repo.
SDK_REPO="github.com/operator-framework/operator-sdk"
sed -E -i 's|^.*'"$SDK_REPO"'.*$||g' go.mod

# Run `go build ./...` to pull down the deps specified by the scaffolded
# `go.mod` file and verify dependencies build correctly.
go build ./...

# Use the local operator-sdk directory as the repo. To make the go toolchain
# happy, the directory needs a `go.mod` file that specifies the module name,
# so we need this temporary hack until we update the SDK repo itself to use
# Go modules.
echo "module ${SDK_REPO}" > "${ROOTDIR}/go.mod"
trap_add "rm ${ROOTDIR}/go.mod" EXIT
go mod edit -replace="${SDK_REPO}=$ROOTDIR"
add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR"
# Build the project to resolve dependency versions in the modfile.
go build ./...

operator-sdk build "$DEST_IMAGE"
Expand Down
58 changes: 23 additions & 35 deletions test/e2e/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
Expand All @@ -35,10 +34,10 @@ import (
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/prometheus/util/promlint"
"github.com/rogpeppe/go-internal/modfile"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -119,30 +118,21 @@ func TestMemcached(t *testing.T) {
// memcached-operator's go.mod, which allows go to recognize
// the local SDK repo as a module.
sdkModPath := filepath.Join(filepath.FromSlash(replace.repo), "go.mod")
err = ioutil.WriteFile(sdkModPath, []byte("module "+sdkRepo), fileutil.DefaultFileMode)
if err != nil {
t.Fatalf("Failed to write main repo go.mod file: %v", err)
}
defer func() {
if err = os.RemoveAll(sdkModPath); err != nil {
t.Fatalf("Failed to remove %s: %v", sdkModPath, err)
if _, err = os.Stat(sdkModPath); err != nil && os.IsNotExist(err) {
err = ioutil.WriteFile(sdkModPath, []byte("module "+sdkRepo), fileutil.DefaultFileMode)
if err != nil {
t.Fatalf("Failed to write main repo go.mod file: %v", err)
}
}()
}
modBytes, err := ioutil.ReadFile("go.mod")
if err != nil {
t.Fatalf("Failed to read go.mod: %v", err)
}
// Remove SDK repo dependency lines so we can parse the modfile.
sdkRe := regexp.MustCompile(`.*github\.com/operator-framework/operator-sdk.*`)
modBytes = sdkRe.ReplaceAll(modBytes, nil)
modBytes, err = insertGoModReplace(t, modBytes, sdkRepo, replace.repo, replace.ref)
if err != nil {
t.Fatalf("Failed to insert replace: %v", err)
defer func() {
if err = os.RemoveAll(sdkModPath); err != nil {
t.Fatalf("Failed to remove %s: %v", sdkModPath, err)
}
}()
}
}
err = ioutil.WriteFile("go.mod", modBytes, fileutil.DefaultFileMode)
modBytes, err := insertGoModReplace(t, sdkRepo, replace.repo, replace.ref)
if err != nil {
t.Fatalf("Failed to write updated go.mod: %v", err)
t.Fatalf("Failed to insert go.mod replace: %v", err)
}
t.Logf("go.mod: %v", string(modBytes))
}
Expand Down Expand Up @@ -321,21 +311,19 @@ func getGoModReplace(t *testing.T, localSDKPath string) goModReplace {
}
}

func insertGoModReplace(t *testing.T, modBytes []byte, repo, path, sha string) ([]byte, error) {
modFile, err := modfile.Parse("go.mod", modBytes, nil)
func insertGoModReplace(t *testing.T, repo, path, sha string) ([]byte, error) {
modBytes, err := ioutil.ReadFile("go.mod")
if err != nil {
return nil, fmt.Errorf("failed to parse go.mod: %v", err)
return nil, errors.Wrap(err, "failed to read go.mod")
}
if err = modFile.AddReplace(repo, "", path, sha); err != nil {
s := ""
if sha != "" {
s = " " + sha
}
return nil, fmt.Errorf(`failed to add "replace %s => %s%s: %v"`, repo, path, s, err)
sdkReplace := fmt.Sprintf("replace %s => %s", repo, path)
if sha != "" {
sdkReplace = fmt.Sprintf("%s %s", sdkReplace, sha)
}
modFile.Cleanup()
if modBytes, err = modFile.Format(); err != nil {
return nil, fmt.Errorf("failed to format go.mod: %v", err)
modBytes = append(modBytes, []byte("\n"+sdkReplace)...)
err = ioutil.WriteFile("go.mod", modBytes, fileutil.DefaultFileMode)
if err != nil {
return nil, errors.Wrap(err, "failed to write go.mod before replacing SDK repo")
}
return modBytes, nil
}
Expand Down