test: add automated Pale Moon tests (#903)
* test: start work on Pale Moon tests Signed-off-by: Xe Iaso <me@xeiaso.net> * test(palemoon): rewrite to use ci-images Signed-off-by: Xe Iaso <me@xeiaso.net> * ci: enable palemoon tests Signed-off-by: Xe Iaso <me@xeiaso.net> * test(palemoon): add some variables Signed-off-by: Xe Iaso <me@xeiaso.net> * fix: disable tmate Signed-off-by: Xe Iaso <me@xeiaso.net> * test(palemoon): disable i386 for now Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: spelling Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
0ef3461816
commit
bf42014ac3
18 changed files with 646 additions and 9 deletions
33
test/cmd/cipra/internal/containerip.go
Normal file
33
test/cmd/cipra/internal/containerip.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// GetContainerIPAddress returns the first non-empty IP address of the container with the given name.
|
||||
// It returns the IP address as a string or an error.
|
||||
func GetContainerIPAddress(containerName string) (string, error) {
|
||||
ctx := context.Background()
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Get container details
|
||||
containerJSON, err := cli.ContainerInspect(ctx, containerName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Loop through all networks and return the first IP address found
|
||||
for _, net := range containerJSON.NetworkSettings.Networks {
|
||||
if net.IPAddress != "" {
|
||||
return net.IPAddress, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("no IP address found for container %q", containerName)
|
||||
}
|
||||
50
test/cmd/cipra/internal/getlanip.go
Normal file
50
test/cmd/cipra/internal/getlanip.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
// GetLANIP returns the first non-loopback IPv4 LAN IP address.
|
||||
func GetLANIP() (net.IP, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
// Skip down or loopback interfaces
|
||||
if iface.Flags&(net.FlagUp|net.FlagLoopback) != net.FlagUp {
|
||||
continue
|
||||
}
|
||||
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue // skip interfaces we can't query
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
|
||||
if ip == nil || ip.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
ip = ip.To4()
|
||||
if ip == nil {
|
||||
continue // not an IPv4 address
|
||||
}
|
||||
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no connected LAN IPv4 address found")
|
||||
}
|
||||
34
test/cmd/cipra/internal/unbreakdocker.go
Normal file
34
test/cmd/cipra/internal/unbreakdocker.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// UnbreakDocker connects the container named after the current hostname
|
||||
// to the specified Docker network.
|
||||
func UnbreakDocker(networkName string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cli.NetworkConnect(ctx, networkName, hostname, &network.EndpointSettings{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Connected container %q to network %q\n", hostname, networkName)
|
||||
return nil
|
||||
}
|
||||
114
test/cmd/cipra/main.go
Normal file
114
test/cmd/cipra/main.go
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/TecharoHQ/anubis/test/cmd/cipra/internal"
|
||||
"github.com/facebookgo/flagenv"
|
||||
)
|
||||
|
||||
var (
|
||||
bind = flag.String("bind", ":9090", "TCP host:port to bind HTTP on")
|
||||
browserBin = flag.String("browser-bin", "palemoon", "browser binary name")
|
||||
browserContainerName = flag.String("browser-container-name", "palemoon", "browser container name")
|
||||
composeName = flag.String("compose-name", "", "docker compose base name for resources")
|
||||
vncServerContainer = flag.String("vnc-container-name", "display", "VNC host:port (NOT a display number)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flagenv.Parse()
|
||||
flag.Parse()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
lanip, err := internal.GetLANIP()
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
os.Setenv("TARGET", fmt.Sprintf("%s%s", lanip.String(), *bind))
|
||||
|
||||
http.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "OK", http.StatusOK)
|
||||
log.Println("got termination signal", r.RequestURI)
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
cancel()
|
||||
}()
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: http.DefaultServeMux,
|
||||
Addr: *bind,
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := RunScript(ctx, "docker", "compose", "up", "-d"); err != nil {
|
||||
log.Fatalf("can't start project: %v", err)
|
||||
}
|
||||
|
||||
defer RunScript(ctx, "docker", "compose", "down", "-t", "1")
|
||||
defer RunScript(ctx, "docker", "compose", "rm", "-f")
|
||||
|
||||
internal.UnbreakDocker(*composeName + "_default")
|
||||
|
||||
if err := RunScript(ctx, "docker", "exec", fmt.Sprintf("%s-%s-1", *composeName, *browserContainerName), "bash", "/hack/scripts/install-cert.sh"); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
if err := RunScript(ctx, "docker", "exec", fmt.Sprintf("%s-%s-1", *composeName, *browserContainerName), *browserBin, "https://relayd"); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
srv.Close()
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
func RunScript(ctx context.Context, args ...string) error {
|
||||
var err error
|
||||
backoff := 250 * time.Millisecond
|
||||
|
||||
for attempt := 0; attempt < 5; attempt++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
log.Printf("Running command: %s", strings.Join(args, " "))
|
||||
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err = cmd.Run()
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
log.Printf("attempt=%d code=%d", attempt, exitErr.ExitCode())
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("Attempt %d failed: %v %T", attempt+1, err, err)
|
||||
log.Printf("Retrying in %v...", backoff)
|
||||
time.Sleep(backoff)
|
||||
backoff *= 2
|
||||
}
|
||||
|
||||
return fmt.Errorf("script failed after 5 attempts: %w", err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue