Skip to content

Commit

Permalink
Use a small helper for tests instead of pulling images
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumerose committed Jan 6, 2022
1 parent e3d5299 commit 3598532
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ jobs:

- name: Test
run: make test

- uses: actions/upload-artifact@v2
if: always()
with:
name: qcon
path: test/qcon.log
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ cross:
GOOS=darwin go build $(LDFLAGS) -o bin/gvproxy-darwin ./cmd/gvproxy
GOOS=linux go build $(LDFLAGS) -o bin/gvproxy-linux ./cmd/gvproxy

.PHONY: testee
testee:
GOOS=linux go build $(LDFLAGS) -o bin/testee ./cmd/testee

.PHONY: test
test: gvproxy
test: gvproxy testee
go test -v ./test
45 changes: 45 additions & 0 deletions cmd/testee/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"net"
"net/http"

"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

func main() {
go func() {
mux := dns.NewServeMux()
mux.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.RecursionAvailable = true
for _, q := range m.Question {
if q.Qtype == dns.TypeA {
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: net.ParseIP("1.2.3.4"),
})
}
}
if err := w.WriteMsg(m); err != nil {
log.Error(err)
}
})
if err := dns.ListenAndServe(":53", "udp", mux); err != nil {
log.Fatal(err)
}
}()

mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte(`Hello world!`))
})
log.Fatal(http.ListenAndServe(":8080", mux))
}
45 changes: 2 additions & 43 deletions test/port_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package e2e

import (
"context"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -47,14 +45,7 @@ var _ = Describe("port forwarding", func() {
})

It("should reach a http server in the VM using dynamic port forwarding", func() {
_, err := sshExec("sudo podman run --rm --name http-test -d -p 8080:80 -t docker.io/library/nginx:alpine")
Expect(err).ShouldNot(HaveOccurred())
defer func() {
_, err := sshExec("sudo podman stop http-test")
Expect(err).ShouldNot(HaveOccurred())
}()

_, err = net.Dial("tcp", "127.0.0.1:9090")
_, err := net.Dial("tcp", "127.0.0.1:9090")
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(HaveSuffix("connection refused"))

Expand All @@ -81,24 +72,6 @@ var _ = Describe("port forwarding", func() {
})

It("should reach a dns server in the VM using dynamic port forwarding", func() {
const dnsmasqConfiguration = `user=root
port=53
bind-interfaces
address=/foobar/1.2.3.4
`
base64Data := base64.StdEncoding.EncodeToString([]byte(dnsmasqConfiguration))
_, err := sshExec(fmt.Sprintf("sudo install -m 0%o /dev/null %s && cat <<EOF | base64 --decode | sudo tee %s\n%s\nEOF", 0644, "/tmp/cfg", "/tmp/cfg", base64Data))
Expect(err).ShouldNot(HaveOccurred())

_, _ = sshExec("sudo podman pull quay.io/crcont/dnsmasq")

_, err = sshExec("sudo podman run --rm --name dns-test -v /tmp/cfg:/etc/dnsmasq.conf:z -d -p 53:53/udp -t quay.io/crcont/dnsmasq")
Expect(err).ShouldNot(HaveOccurred())
defer func() {
_, err := sshExec("sudo podman stop dns-test")
Expect(err).ShouldNot(HaveOccurred())
}()

Expect(client.Expose(&types.ExposeRequest{
Local: ":1053",
Remote: "192.168.127.2:53",
Expand All @@ -119,13 +92,6 @@ address=/foobar/1.2.3.4
})

It("should reach a http server in the VM using the tunneling of the daemon", func() {
_, err := sshExec("sudo podman run --rm --name http-test -d -p 8080:80 -t docker.io/library/nginx:alpine")
Expect(err).ShouldNot(HaveOccurred())
defer func() {
_, err := sshExec("sudo podman stop http-test")
Expect(err).ShouldNot(HaveOccurred())
}()

httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand All @@ -146,14 +112,7 @@ address=/foobar/1.2.3.4
})

It("should reach a http server in the VM using dynamic port forwarding configured within the VM", func() {
_, err := sshExec("sudo podman run --rm --name http-test -d -p 8080:80 -t docker.io/library/nginx:alpine")
Expect(err).ShouldNot(HaveOccurred())
defer func() {
_, err := sshExec("sudo podman stop http-test")
Expect(err).ShouldNot(HaveOccurred())
}()

_, err = net.Dial("tcp", "127.0.0.1:9090")
_, err := net.Dial("tcp", "127.0.0.1:9090")
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(HaveSuffix("connection refused"))

Expand Down
66 changes: 30 additions & 36 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -17,7 +18,6 @@ import (
. "github.com/onsi/gomega"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)

func TestSuite(t *testing.T) {
Expand Down Expand Up @@ -108,6 +108,8 @@ outer:
template := `%s -m 2048 -nographic -serial file:%s -snapshot -drive if=virtio,file=%s -fw_cfg name=opt/com.coreos/config,file=%s -netdev socket,id=vlan,connect=127.0.0.1:%d -device virtio-net-pci,netdev=vlan,mac=5a:94:ef:e4:0c:ee`
// #nosec
client = exec.Command(qemuExecutable(), strings.Split(fmt.Sprintf(template, qemuArgs(), qconLog, qemuImage, ignFile, qemuPort), " ")...)
client.Stderr = os.Stderr
client.Stdout = os.Stdout
Expect(client.Start()).Should(Succeed())
go func() {
if err := client.Wait(); err != nil {
Expand Down Expand Up @@ -138,6 +140,12 @@ outer:
time.Sleep(time.Second)
}
}

err = scp("../bin/testee", "/tmp/testee")
Expect(err).ShouldNot(HaveOccurred())

cmd := sshCommand("sudo chmod +x /tmp/testee && sudo /tmp/testee")
Expect(cmd.Start()).ShouldNot(HaveOccurred())
})

func qemuExecutable() string {
Expand Down Expand Up @@ -174,45 +182,31 @@ func readPublicKey() (string, error) {
return strings.TrimSpace(string(publicKey)), nil
}

func parsePrivateKey(path string) (ssh.Signer, error) {
key, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return signer, nil
func scp(src, dst string) error {
sshCmd := exec.Command("scp",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-i", privateKeyFile,
"-P", strconv.Itoa(sshPort),
src,
fmt.Sprintf("%s@127.0.0.1:%s", ignitionUser, dst)) // #nosec G204
sshCmd.Stderr = os.Stderr
sshCmd.Stdout = os.Stdout
return sshCmd.Run()
}

func sshExec(cmd ...string) ([]byte, error) {
key, err := parsePrivateKey(privateKeyFile)
if err != nil {
return nil, err
}

client, err := ssh.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", sshPort), &ssh.ClientConfig{
User: ignitionUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(key),
},
// #nosec
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 5 * time.Second,
})
if err != nil {
return nil, err
}
defer client.Close()

sess, err := client.NewSession()
if err != nil {
return nil, err
}
defer sess.Close()
return sshCommand(cmd...).Output()
}

return sess.CombinedOutput(strings.Join(cmd, " "))
func sshCommand(cmd ...string) *exec.Cmd {
sshCmd := exec.Command("ssh",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-i", privateKeyFile,
"-p", strconv.Itoa(sshPort),
fmt.Sprintf("%s@127.0.0.1", ignitionUser), "--", strings.Join(cmd, " ")) // #nosec G204
return sshCmd
}

func panicCheck(con string) (bool, error) {
Expand Down

0 comments on commit 3598532

Please sign in to comment.