@@ -17,7 +17,6 @@ package e2e
17
17
import (
18
18
"bytes"
19
19
"context"
20
- "errors"
21
20
"fmt"
22
21
"io/ioutil"
23
22
"os"
@@ -35,8 +34,8 @@ import (
35
34
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
36
35
37
36
"github.com/ghodss/yaml"
37
+ "github.com/pkg/errors"
38
38
"github.com/prometheus/prometheus/util/promlint"
39
- "github.com/rogpeppe/go-internal/modfile"
40
39
v1 "k8s.io/api/core/v1"
41
40
apierrors "k8s.io/apimachinery/pkg/api/errors"
42
41
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -114,18 +113,24 @@ func TestMemcached(t *testing.T) {
114
113
// stub go.mod into the local SDK repo referred to in
115
114
// memcached-operator's go.mod, which allows go to recognize
116
115
// 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 )
125
121
}
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 )
127
132
}
128
- writeGoModReplace ( t , sdkRepo , replace . repo , replace . ref )
133
+ t . Logf ( "go.mod: %v" , string ( modBytes ) )
129
134
}
130
135
131
136
cmdOut , err = exec .Command ("go" , "mod" , "vendor" ).CombinedOutput ()
@@ -306,30 +311,21 @@ func getGoModReplace(t *testing.T, localSDKPath string) goModReplace {
306
311
}
307
312
}
308
313
309
- func writeGoModReplace (t * testing.T , repo , path , sha string ) {
314
+ func insertGoModReplace (t * testing.T , repo , path , sha string ) ([] byte , error ) {
310
315
modBytes , err := ioutil .ReadFile ("go.mod" )
311
316
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" )
324
318
}
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 )
327
322
}
323
+ modBytes = append (modBytes , []byte ("\n " + sdkReplace )... )
328
324
err = ioutil .WriteFile ("go.mod" , modBytes , fileutil .DefaultFileMode )
329
325
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" )
331
327
}
332
- t . Logf ( "go.mod: %v" , string ( modBytes ))
328
+ return modBytes , nil
333
329
}
334
330
335
331
func memcachedLeaderTest (t * testing.T , f * framework.Framework , ctx * framework.TestCtx ) error {
0 commit comments