Skip to content

Commit 709fb1d

Browse files
authored
e2e: better method of adding SDK repo replace directive to oper… (#1718)
1 parent ee081f9 commit 709fb1d

File tree

4 files changed

+61
-49
lines changed

4 files changed

+61
-49
lines changed

hack/lib/test_lib.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,37 @@ function trap_add() {
4848
fatal "unable to add to trap ${trap_add_name}"
4949
done
5050
}
51+
52+
# add_go_mod_replace adds a "replace" directive from $1 to $2 with an
53+
# optional version version $3 to the current working directory's go.mod file.
54+
function add_go_mod_replace() {
55+
local from_path="${1:?first path in replace statement is required}"
56+
local to_path="${2:?second path in replace statement is required}"
57+
local version="${3:-}"
58+
59+
if [[ ! -d "$to_path" && -z "$version" ]]; then
60+
echo "second replace path $to_path requires a version be set because it is not a directory"
61+
exit 1
62+
fi
63+
if [[ ! -e go.mod ]]; then
64+
echo "go.mod file not found in $(pwd)"
65+
exit 1
66+
fi
67+
68+
# If $to_path is a directory, it needs a `go.mod` file that specifies the
69+
# module name to make the go toolchain happy.
70+
#
71+
# TODO: remove the below if statement once
72+
# https://github.com/operator-framework/operator-sdk/pull/1566 is merged,
73+
# which updates the SDK to use go modules.
74+
if [[ -d "${to_path}" && ! -e "${to_path}/go.mod" ]]; then
75+
echo "module ${from_path}" > "${to_path}/go.mod"
76+
trap_add "rm ${to_path}/go.mod" EXIT
77+
fi
78+
# Do not use "go mod edit" so formatting stays the same.
79+
local replace="replace ${from_path} => ${to_path}"
80+
if [[ -n "$version" ]]; then
81+
replace="$replace $version"
82+
fi
83+
echo "$replace" >> go.mod
84+
}

hack/tests/e2e-ansible.sh

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,9 @@ then
133133
exit 1
134134
fi
135135

136-
# Right now, SDK projects still need a vendor directory, so run `go mod vendor`
137-
# to pull down the deps specified by the scaffolded `go.mod` file.
136+
add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR"
138137
go mod vendor
139138

140-
# Use the local operator-sdk directory as the repo. To make the go toolchain
141-
# happy, the directory needs a `go.mod` file that specifies the module name,
142-
# so we need this temporary hack until we update the SDK repo itself to use
143-
# go modules.
144-
echo "module github.com/operator-framework/operator-sdk" > $ROOTDIR/go.mod
145-
trap_add 'rm $ROOTDIR/go.mod' EXIT
146-
go mod edit -replace=github.com/operator-framework/operator-sdk=$ROOTDIR
147-
148139
operator-sdk build "$DEST_IMAGE"
149140

150141
deploy_operator

hack/tests/e2e-helm.sh

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,9 @@ then
128128
exit 1
129129
fi
130130

131-
# Right now, SDK projects still need a vendor directory, so run `go mod vendor`
132-
# to pull down the deps specified by the scaffolded `go.mod` file.
131+
add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR"
133132
go mod vendor
134133

135-
# Use the local operator-sdk directory as the repo. To make the go toolchain
136-
# happy, the directory needs a `go.mod` file that specifies the module name,
137-
# so we need this temporary hack until we update the SDK repo itself to use
138-
# go modules.
139-
echo "module github.com/operator-framework/operator-sdk" > $ROOTDIR/go.mod
140-
trap_add 'rm $ROOTDIR/go.mod' EXIT
141-
go mod edit -replace=github.com/operator-framework/operator-sdk=$ROOTDIR
142-
143134
operator-sdk build "$DEST_IMAGE"
144135

145136
deploy_operator

test/e2e/memcached_test.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package e2e
1717
import (
1818
"bytes"
1919
"context"
20-
"errors"
2120
"fmt"
2221
"io/ioutil"
2322
"os"
@@ -35,8 +34,8 @@ import (
3534
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
3635

3736
"github.com/ghodss/yaml"
37+
"github.com/pkg/errors"
3838
"github.com/prometheus/prometheus/util/promlint"
39-
"github.com/rogpeppe/go-internal/modfile"
4039
v1 "k8s.io/api/core/v1"
4140
apierrors "k8s.io/apimachinery/pkg/api/errors"
4241
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -114,18 +113,24 @@ func TestMemcached(t *testing.T) {
114113
// stub go.mod into the local SDK repo referred to in
115114
// memcached-operator's go.mod, which allows go to recognize
116115
// the local SDK repo as a module.
117-
sdkModPath := filepath.Join(replace.repo, "go.mod")
118-
err = ioutil.WriteFile(sdkModPath, []byte("module "+sdkRepo), fileutil.DefaultFileMode)
119-
if err != nil {
120-
t.Fatalf("Failed to write main repo go.mod file: %v", err)
121-
}
122-
defer func() {
123-
if err = os.RemoveAll(sdkModPath); err != nil {
124-
t.Fatalf("Failed to remove %s: %v", sdkModPath, err)
116+
sdkModPath := filepath.Join(filepath.FromSlash(replace.repo), "go.mod")
117+
if _, err = os.Stat(sdkModPath); err != nil && os.IsNotExist(err) {
118+
err = ioutil.WriteFile(sdkModPath, []byte("module "+sdkRepo), fileutil.DefaultFileMode)
119+
if err != nil {
120+
t.Fatalf("Failed to write main repo go.mod file: %v", err)
125121
}
126-
}()
122+
defer func() {
123+
if err = os.RemoveAll(sdkModPath); err != nil {
124+
t.Fatalf("Failed to remove %s: %v", sdkModPath, err)
125+
}
126+
}()
127+
}
128+
}
129+
modBytes, err := insertGoModReplace(t, sdkRepo, replace.repo, replace.ref)
130+
if err != nil {
131+
t.Fatalf("Failed to insert go.mod replace: %v", err)
127132
}
128-
writeGoModReplace(t, sdkRepo, replace.repo, replace.ref)
133+
t.Logf("go.mod: %v", string(modBytes))
129134
}
130135

131136
cmdOut, err = exec.Command("go", "mod", "vendor").CombinedOutput()
@@ -306,30 +311,21 @@ func getGoModReplace(t *testing.T, localSDKPath string) goModReplace {
306311
}
307312
}
308313

309-
func writeGoModReplace(t *testing.T, repo, path, sha string) {
314+
func insertGoModReplace(t *testing.T, repo, path, sha string) ([]byte, error) {
310315
modBytes, err := ioutil.ReadFile("go.mod")
311316
if err != nil {
312-
t.Fatalf("Failed to read go.mod: %v", err)
313-
}
314-
modFile, err := modfile.Parse("go.mod", modBytes, nil)
315-
if err != nil {
316-
t.Fatalf("Failed to parse go.mod: %v", err)
317-
}
318-
if err = modFile.AddReplace(repo, "", path, sha); err != nil {
319-
s := ""
320-
if sha != "" {
321-
s = " " + sha
322-
}
323-
t.Fatalf(`Failed to add "replace %s => %s%s: %v"`, repo, path, s, err)
317+
return nil, errors.Wrap(err, "failed to read go.mod")
324318
}
325-
if modBytes, err = modFile.Format(); err != nil {
326-
t.Fatalf("Failed to format go.mod: %v", err)
319+
sdkReplace := fmt.Sprintf("replace %s => %s", repo, path)
320+
if sha != "" {
321+
sdkReplace = fmt.Sprintf("%s %s", sdkReplace, sha)
327322
}
323+
modBytes = append(modBytes, []byte("\n"+sdkReplace)...)
328324
err = ioutil.WriteFile("go.mod", modBytes, fileutil.DefaultFileMode)
329325
if err != nil {
330-
t.Fatalf("Failed to write updated go.mod: %v", err)
326+
return nil, errors.Wrap(err, "failed to write go.mod before replacing SDK repo")
331327
}
332-
t.Logf("go.mod: %v", string(modBytes))
328+
return modBytes, nil
333329
}
334330

335331
func memcachedLeaderTest(t *testing.T, f *framework.Framework, ctx *framework.TestCtx) error {

0 commit comments

Comments
 (0)