@@ -17,7 +17,6 @@ package e2e
1717import (
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
335331func memcachedLeaderTest (t * testing.T , f * framework.Framework , ctx * framework.TestCtx ) error {
0 commit comments