github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/build/golang/golang_test.go (about) 1 package golang 2 3 import ( 4 "archive/tar" 5 "archive/zip" 6 "bytes" 7 "fmt" 8 "io" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "strings" 13 "testing" 14 15 "github.com/tickoalcantara12/micro/v3/service/build" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 var ( 20 testMainGo = "package main; import \"fmt\"; func main() { fmt.Println(\"HelloWorld\") }" 21 testSecondGo = "package main; import \"fmt\"; func init() { fmt.Println(\"Init\") }" 22 ) 23 24 func TestGolangBuilder(t *testing.T) { 25 t.Run("NoArchive", func(t *testing.T) { 26 buf := bytes.NewBuffer([]byte(testMainGo)) 27 err := testBuilder(t, buf) 28 assert.Nil(t, err, "No error should be returned") 29 }) 30 31 t.Run("InvalidArchive", func(t *testing.T) { 32 buf := bytes.NewBuffer([]byte(testMainGo)) 33 err := testBuilder(t, buf, build.Archive("foo")) 34 assert.Error(t, err, "An error should be returned") 35 }) 36 37 t.Run("TarArchive", func(t *testing.T) { 38 // Create a tar writer 39 tf := bytes.NewBuffer(nil) 40 tw := tar.NewWriter(tf) 41 42 // Add some files to the archive. 43 var files = []struct { 44 Name, Body string 45 }{ 46 {"main.go", testMainGo}, 47 {"second.go", testSecondGo}, 48 } 49 for _, file := range files { 50 hdr := &tar.Header{ 51 Name: file.Name, 52 Mode: 0600, 53 Size: int64(len(file.Body)), 54 } 55 if err := tw.WriteHeader(hdr); err != nil { 56 t.Fatal(err) 57 } 58 if _, err := tw.Write([]byte(file.Body)); err != nil { 59 t.Fatal(err) 60 } 61 } 62 if err := tw.Close(); err != nil { 63 t.Fatal(err) 64 } 65 66 err := testBuilder(t, tf, build.Archive("tar")) 67 assert.Nil(t, err, "No error should be returned") 68 }) 69 70 t.Run("ZipArchive", func(t *testing.T) { 71 // Create a buffer to write our archive to. 72 buf := new(bytes.Buffer) 73 74 // Create a new zip archive. 75 w := zip.NewWriter(buf) 76 defer w.Close() 77 78 // Add some files to the archive. 79 var files = []struct { 80 Name, Body string 81 }{ 82 {"main.go", testMainGo}, 83 {"second.go", testSecondGo}, 84 } 85 for _, file := range files { 86 f, err := w.Create(file.Name) 87 if err != nil { 88 t.Fatal(err) 89 } 90 _, err = f.Write([]byte(file.Body)) 91 if err != nil { 92 t.Fatal(err) 93 } 94 } 95 if err := w.Close(); err != nil { 96 t.Fatal(err) 97 } 98 99 err := testBuilder(t, buf, build.Archive("zip")) 100 assert.Nil(t, err, "No error should be returned") 101 }) 102 } 103 104 func testBuilder(t *testing.T, buf io.Reader, opts ...build.Option) error { 105 // setup the build 106 build, err := NewBuilder() 107 if err != nil { 108 return fmt.Errorf("Error creating the build: %v", err) 109 } 110 111 // build the source 112 res, err := build.Build(buf, opts...) 113 if err != nil { 114 return fmt.Errorf("Error building source: %v", err) 115 } 116 117 // write the binary to a tmp file and make it executable 118 file, err := ioutil.TempFile(os.TempDir(), "res") 119 if err != nil { 120 return fmt.Errorf("Error creating tmp output file: %v", err) 121 } 122 if _, err := io.Copy(file, res); err != nil { 123 return fmt.Errorf("Error copying binary to tmp file: %v", err) 124 } 125 if err := file.Close(); err != nil { 126 return err 127 } 128 if err := os.Chmod(file.Name(), 0111); err != nil { 129 return err 130 } 131 defer os.Remove(file.Name()) 132 133 // execute the binary 134 cmd := exec.Command(file.Name()) 135 outp, err := cmd.Output() 136 if err != nil { 137 return fmt.Errorf("Error executing binary: %v", err) 138 } 139 if !strings.Contains(string(outp), "HelloWorld") { 140 return fmt.Errorf("Output does not contain HelloWorld") 141 } 142 // when an archive is used we also check for the second file to be loaded 143 if len(opts) > 0 && !strings.Contains(string(outp), "Init") { 144 return fmt.Errorf("Output does not contain Init") 145 } 146 147 return nil 148 }