Skip to content

Commit

Permalink
fix(test): add way for non-linux runners to download a freshly compil…
Browse files Browse the repository at this point in the history
…ed devpod binary as agent

Signed-off-by: Luca Di Maio <[email protected]>
  • Loading branch information
89luca89 committed Mar 22, 2024
1 parent 979c546 commit ed0cf99
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ jobs:

- name: Build binary and copy to the E2E directory
run: |
go build -ldflags "-s -w" -o devpod-windows-amd64.exe
mkdir e2e\bin
cp devpod-windows-amd64.exe e2e\bin\
go build -ldflags "-s -w" -o e2e\bin\devpod-windows-amd64.exe
$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build -ldflags "-s -w" -o e2e\bin\devpod-linux-amd64
- name: E2E test
working-directory: .\e2e
run: |
go run github.com/onsi/ginkgo/v2/ginkgo -r -p --timeout=3600s --label-filter=${{ matrix.label }}
go run github.com/onsi/ginkgo/v2/ginkgo -r --timeout=3600s --label-filter=${{ matrix.label }}
- name: Container cleanup
if: ${{ always() }}
Expand Down
116 changes: 116 additions & 0 deletions e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package e2e

import (
"fmt"
"log"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"testing"
"time"

"github.com/onsi/ginkgo/v2"

Expand All @@ -26,6 +36,112 @@ import (
// generated in this directory, and cluster logs will also be saved.
// This function is called on each Ginkgo node in parallel mode.
func TestRunE2ETests(t *testing.T) {
if runtime.GOOS != "linux" {
go serveAgent()

// wait for http server to be up and running
for {
time.Sleep(time.Second)
if os.Getenv("DEVPOD_AGENT_URL") != "" {
break
}
}
}
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "DevPod e2e suite")
}

// serveAgent will be a simple http file server that will expose our
// freshly compiled devpod binaries to be downloaded as agents.
// useful for non-linux runners
func serveAgent() {
// Specify the directory containing the files you want to serve
dir := "bin"

wd, err := os.Getwd()
if err == nil {
dir = filepath.Join(wd, "bin")
}

// Create a file server handler for the specified directory
fileServer := http.FileServer(http.Dir(dir))

// Register the file server handler to serve files under the /files route
http.Handle("/files/", http.StripPrefix("/files", fileServer))

randomPort, err := findOpenPort(0)
if err != nil {
log.Fatal(err)
}

ip := getIP()
addr := fmt.Sprintf("%s:%d", ip, randomPort)

err = os.Setenv("DEVPOD_AGENT_URL", "http://"+addr+"/files/")
if err != nil {
log.Fatal(err)
}

// Start the HTTP server on port 8080
log.Printf("Server started on %s", addr)
err = http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}

func findOpenPort(retries int) (int, error) {
if retries > 100 {
return 0, fmt.Errorf("no open port available in the range")
}
// Create a new random number generator with a custom seed (e.g., current time)
source := rand.NewSource(time.Now().UnixNano())
rng := rand.New(source)

min := 10000
max := 40000
port := rng.Intn(max-min+1) + min

conn, err := net.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(port)))
if err == nil {
conn.Close()
return findOpenPort(retries + 1)
}

return port, nil
}

func getIP() string {
// Get a list of network interfaces
ifaces, err := net.Interfaces()
if err != nil {
return "0.0.0.0"
}

// Iterate over each network interface
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
return "0.0.0.0"
}

for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPAddr:
if v.IP.To4() != nil {
if v.IP.DefaultMask().String() == "ffffff00" || v.IP.DefaultMask().String() == "ff000000" {
return v.IP.String()
}
}
case *net.IPNet:
if v.IP.To4() != nil {
if v.IP.DefaultMask().String() == "ffffff00" || v.IP.DefaultMask().String() == "ff000000" {
return v.IP.String()
}
}
}
}
}

return "0.0.0.0"
}
4 changes: 4 additions & 0 deletions pkg/agent/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func InjectAgentAndExecute(
versionCheck := fmt.Sprintf(`[ "$(%s version 2>/dev/null || echo 'false')" != "%s" ]`, remoteAgentPath, version.GetVersion())
if version.GetVersion() == version.DevVersion {
preferDownload = false

if runtime.GOOS != "linux" {
preferDownload = true
}
}

// install devpod into the target
Expand Down

0 comments on commit ed0cf99

Please sign in to comment.