Skip to content

Commit d2f13df

Browse files
committed
test/e2e/memcached_test.go: fix dep ensure solve issue
1 parent c9c0cdb commit d2f13df

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

test/e2e/memcached_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,55 @@ func TestMemcached(t *testing.T) {
6464
}
6565

6666
t.Log("Creating new operator project")
67+
var solveFailed bool
6768
cmdOut, err := exec.Command("operator-sdk",
6869
"new",
6970
"memcached-operator").CombinedOutput()
7071
if err != nil {
71-
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
72+
// HACK: dep cannot resolve non-master branches as the base branch for PR's,
73+
// so running `dep ensure` will fail when first running
74+
// `operator-sdk new ...`. For now we can ignore the first solve failure,
75+
// and run `dep ensure` again.
76+
// A permanent solution can be implemented once the following is merged:
77+
// https://github.com/golang/dep/pull/1658
78+
solveFailRe := regexp.MustCompile(`(?m)^[ \t]*Solving failure:.+github\.com/operator-framework/operator-sdk.+:$`)
79+
if solveFailRe.Match(cmdOut) {
80+
prSlug, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SLUG")
81+
if ok && prSlug != "" {
82+
prSha, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SHA")
83+
if ok && prSha != "" {
84+
os.Chdir("memcached-operator")
85+
gopkg, err := ioutil.ReadFile("Gopkg.toml")
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
// Match against the '#osdk_branch_annotation' used for version substitution
90+
// and comment out the current branch.
91+
branchRe := regexp.MustCompile("([ ]+)(.+#osdk_branch_annotation)")
92+
gopkg = branchRe.ReplaceAll(gopkg, []byte("$1# $2"))
93+
// Plug in the fork to test against so `dep ensure` can resolve dependencies
94+
// correctly.
95+
gopkgString := string(gopkg)
96+
gopkgLoc := strings.LastIndex(gopkgString, "\n name = \"github.com/operator-framework/operator-sdk\"\n")
97+
gopkgString = gopkgString[:gopkgLoc] + "\n source = \"https://github.com/" + prSlug + "\"\n revision = \"" + prSha + "\"\n" + gopkgString[gopkgLoc+1:]
98+
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), filemode)
99+
if err != nil {
100+
t.Fatalf("failed to write updated Gopkg.toml: %v", err)
101+
}
102+
103+
solveFailed = true
104+
t.Logf("Gopkg.toml: %v", gopkgString)
105+
} else {
106+
t.Fatal("could not find sha of PR")
107+
}
108+
}
109+
cmdOut, err = exec.Command("dep", "ensure").CombinedOutput()
110+
if err != nil {
111+
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
112+
}
113+
} else {
114+
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
115+
}
72116
}
73117
ctx.AddFinalizerFn(func() error { return os.RemoveAll(absProjectPath) })
74118

@@ -143,9 +187,10 @@ func TestMemcached(t *testing.T) {
143187
if err != nil {
144188
t.Fatalf("could not rename test/e2e/memcached_test.go.tmpl: %v\nCommand Output:\n%v", err, string(cmdOut))
145189
}
190+
146191
t.Log("Pulling new dependencies with dep ensure")
147192
prSlug, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SLUG")
148-
if ok && prSlug != "" {
193+
if !solveFailed && ok && prSlug != "" {
149194
prSha, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SHA")
150195
if ok && prSha != "" {
151196
gopkg, err := ioutil.ReadFile("Gopkg.toml")

0 commit comments

Comments
 (0)