Skip to content

Commit

Permalink
Merge pull request #22 from mysteriumnetwork/feature/e2e-test
Browse files Browse the repository at this point in the history
Feature/e2e test
  • Loading branch information
Waldz authored Nov 18, 2022
2 parents df2db35 + 3580522 commit 7b25396
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 60 deletions.
79 changes: 22 additions & 57 deletions ci/commands/command_test_e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,40 @@
package commands

import (
"errors"
"fmt"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

var (
dockerCompose = sh.RunCmd("docker-compose", "-f", "docker-compose.yml")
dockerComposeOut = sh.OutCmd("docker-compose", "-f", "docker-compose.yml")
ipTablesAppend = sh.RunCmd("docker-compose", "exec", "forwarder", "iptables", "-t", "nat", "-A")
ipTablesDelete = sh.RunCmd("docker-compose", "exec", "forwarder", "iptables", "-t", "nat", "-D")
)
var dockerCompose = sh.RunCmd("docker-compose", "-f", "docker-compose.yml")

// TestE2E runs end-to-end test
func TestE2E() {
dockerCompose("up", "-d")
defer dockerCompose("down")

mg.Deps(TestE2EHTTP, TestE2EHTTPS)
}

// TestE2EHTTPS runs end-to-end test for HTTPS traffic forwarding
func TestE2EHTTPS() error {
originalIP := checkIP("https://api.ipify.org/?format=text")
fmt.Println("Original IP:", originalIP)

redirectRule := []string{"OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "443", "-j", "REDIRECT", "--to-ports", "8443"}
if err := ipTablesAppend(redirectRule...); err != nil {
func TestE2E() (err error) {
fmt.Println("Starting E2E containers")
if err = runCompose("up", "-d"); err != nil {
return err
}
defer ipTablesDelete(redirectRule...)

currentIP := checkIP("https://api.ipify.org/?format=text")
fmt.Println("Current IP:", currentIP)

if currentIP == originalIP {
return errors.New("request proxying failed")
}
fmt.Printf("Request successfuly proxied: %s -> %s", originalIP, currentIP)
return nil
}
defer func() {
fmt.Println("Forwarder logs:")
_ = runCompose("logs", "forwarder")
fmt.Println()

// TestE2EHTTP runs end-to-end test for HTTP traffic forwarding
func TestE2EHTTP() error {
originalIP := checkIP("http://api.ipify.org/?format=text")
fmt.Println("Original IP:", originalIP)
err = runCompose("down")
}()

redirectRule := []string{"OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", "REDIRECT", "--to-ports", "8443"}
if err := ipTablesAppend(redirectRule...); err != nil {
return err
}
defer ipTablesDelete(redirectRule...)

currentIP := checkIP("http://api.ipify.org/?format=text")
fmt.Println("Current IP:", currentIP)

if currentIP == originalIP {
return errors.New("request proxying failed")
}
fmt.Printf("Request successfuly proxied: %s -> %s", originalIP, currentIP)
return nil
fmt.Println("Starting E2E tests")
return runCompose("run", "-T", "machine", "go", "test", "-v", "-tags=e2e", "./e2e/...")
}

func checkIP(apiURL string) string {
ip, err := dockerComposeOut("exec", "forwarder", "wget", "-q", "-O", "-", apiURL)
if err != nil {
panic(err)
}

return ip
func runCompose(args ...string) error {
return sh.RunV(
"docker-compose",
append(
[]string{
"-f", "docker-compose.yml",
},
args...,
)...,
)
}
13 changes: 10 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ services:
--proxy.pass=
--filter.zones=api.ipify.org
--exclude.hostnames=ipify.org
ports:
- "8443:8443"

machine:
build:
context: .
dockerfile: e2e/Dockerfile
volumes:
- .:/go/src/github.com/mysteriumnetwork/openvpn-forwarder
cap_add:
- NET_ADMIN
- NET_RAW
ports:
- "8443:8443"
- NET_RAW
8 changes: 8 additions & 0 deletions e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.18-alpine

# Install packages
RUN apk add --update --no-cache bash git gcc musl-dev make iptables bind-tools

# Install application
WORKDIR /go/src/github.com/mysteriumnetwork/openvpn-forwarder
ADD . .
89 changes: 89 additions & 0 deletions e2e/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build e2e
// +build e2e

/*
* Copyright (C) 2019 The "MysteriumNetwork/openvpn-forwarder" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package e2e

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/magefile/mage/sh"
)

var (
ipTablesAppend = sh.RunCmd("iptables", "-t", "nat", "-A")
ipTablesDelete = sh.RunCmd("iptables", "-t", "nat", "-D")
)

// TestHTTPS runs end-to-end test for HTTPS traffic forwarding
func TestHTTPS(t *testing.T) {
// given
originalIP, err := checkIP("https://api.ipify.org/?format=text")
t.Log("Original IP:", originalIP)
assert.NoError(t, err)

forwarderIP, err := getForwarderIP()
assert.NoError(t, err)

// when
redirectRule := []string{"OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "443", "-j", "DNAT", "--to-destination", forwarderIP + ":8443"}
err = ipTablesAppend(redirectRule...)
defer ipTablesDelete(redirectRule...)
assert.NoError(t, err)

// then
currentIP, err := checkIP("https://api.ipify.org/?format=text")
t.Log("Current IP:", currentIP)
assert.NoError(t, err)

assert.NotEqualf(t, originalIP, currentIP, "Request proxying failed: %s -> %s", originalIP, currentIP)
}

// TestHTTP runs end-to-end test for HTTP traffic forwarding
func TestHTTP(t *testing.T) {
// given
originalIP, err := checkIP("http://api.ipify.org/?format=text")
t.Log("Original IP:", originalIP)
assert.NoError(t, err)

forwarderIP, err := getForwarderIP()
assert.NoError(t, err)

// when
redirectRule := []string{"OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", "DNAT", "--to-destination", forwarderIP + ":8443"}
err = ipTablesAppend(redirectRule...)
defer ipTablesDelete(redirectRule...)
assert.NoError(t, err)

currentIP, err := checkIP("http://api.ipify.org/?format=text")
t.Log("Current IP:", currentIP)
assert.NoError(t, err)

assert.NotEqualf(t, originalIP, currentIP, "Request proxying failed: %s -> %s", originalIP, currentIP)
}

func getForwarderIP() (string, error) {
return sh.Output("dig", "forwarder", "+short")
}

func checkIP(apiURL string) (string, error) {
return sh.Output("wget", "-q", "-O", "-", apiURL)
}

0 comments on commit 7b25396

Please sign in to comment.