Skip to content

Commit

Permalink
Transform test into e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Litvinov <[email protected]>
  • Loading branch information
Zensey committed Sep 9, 2024
1 parent f916db2 commit a8a5d2b
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests-and-linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
- name: E2E basic test
run: go run mage.go -v TestE2EBasic

- name: E2E basic test2
run: go run mage.go -v TestE2EShaper

e2e-nat:
runs-on: ubuntu-latest

Expand Down
31 changes: 31 additions & 0 deletions ci/test/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func BuildE2eTestBinary() error {
if err != nil {
return err
}
err = sh.RunWith(crossCompileFlags, "go", "test", "-c", "./e2e/bug6022")
if err != nil {
return err
}
err = sh.RunWith(crossCompileFlags, "go", "build", "-o", "bug6022.aux", "./e2e/bug6022")
if err != nil {
return err
}
_ = os.Mkdir("./build/e2e/", os.ModeDir)
os.Rename("./bug6022.test", "./build/e2e/bug6022.test")
os.Rename("./bug6022.aux", "./build/e2e/bug6022.aux")

_ = os.Mkdir("./build/e2e/", os.ModeDir)
return os.Rename("./e2e.test", "./build/e2e/test")
Expand Down Expand Up @@ -75,6 +86,26 @@ func TestE2EBasic() error {
return runner.Test("myst-provider")
}

// TestE2EShaper runs end-to-end tests
func TestE2EShaper() error {
logconfig.Bootstrap()

mg.Deps(BuildMystBinaryForE2eDocker, BuildE2eDeployerBinary)

// not running this in parallel as it does some package switching magic
mg.Deps(BuildE2eTestBinary)

composeFiles := []string{
"./docker-compose.e2e-basic.yml",
}
runner, cleanup := e2e.NewRunner(composeFiles, "node_e2e_basic_test", "")
defer cleanup()
if err := runner.Init_(); err != nil {
return err
}
return runner.Test_()
}

// TestE2ENAT runs end-to-end tests in NAT environment
func TestE2ENAT() error {
logconfig.Bootstrap()
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.e2e-basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,20 @@ services:
--observer.address=""
daemon
bug6022-aux:
build:
context: .
dockerfile: ./e2e/bug6022/Dockerfile
cap_add:
- NET_ADMIN
working_dir: /node
expose:
- 8083

#go runner to run go programs inside localnet (usefull for contract deployment or e2e test running)
go-runner:
depends_on:
- bug6022-aux
build:
context: .
dockerfile: ./e2e/gorunner/Dockerfile.precompiled
Expand Down
6 changes: 6 additions & 0 deletions e2e/bug6022/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM alpine:3.12

#COPY ./build/e2e/bug6022.test /usr/local/bin/bug6022.test
COPY ./build/e2e/bug6022.aux /usr/local/bin/bug6022.aux

ENTRYPOINT ["/usr/local/bin/bug6022.aux"]
33 changes: 33 additions & 0 deletions e2e/bug6022/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 The "MysteriumNetwork/node" 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 main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8083", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, r.RemoteAddr)
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package netstack_provider
package main

import (
"bytes"
"encoding/hex"
"io"
"log"
"net"
"net/http"
"net/netip"
"net/url"
"strings"
"testing"

Expand All @@ -33,6 +35,7 @@ import (

"github.com/mysteriumnetwork/node/config"
netstack "github.com/mysteriumnetwork/node/services/wireguard/endpoint/netstack"
netstack_provider "github.com/mysteriumnetwork/node/services/wireguard/endpoint/netstack-provider"
)

func startClient(t *testing.T, priv, pubServ wgtypes.Key) {
Expand Down Expand Up @@ -68,15 +71,28 @@ func startClient(t *testing.T, priv, pubServ wgtypes.Key) {
DialContext: tnet.DialContext,
},
}
url_ := "http://bug6022-aux:8083/test"
u, _ := url.Parse(url_)
// log.Println(u.Host)

addr, err := net.LookupHost("bug6022-aux")
log.Println(addr, err)
if len(addr) > 0 {
u.Host = addr[0] + ":" + u.Port()
}
log.Println(">>>> ", u.String())

resp, err := client.Get("http://107.173.23.19:8080/test")
resp, err := client.Get(u.String())
if err != nil {
t.Error(err)
log.Println(err)
return
}
// _ = resp
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
log.Println(err)
return
}
log.Println("Reply:", string(body))
Expand All @@ -87,12 +103,10 @@ func startClient(t *testing.T, priv, pubServ wgtypes.Key) {
ok = "failed"
}
log.Println("Test result:", ok)
// dev.Down()
// tun.Close()
}

func startServer(t *testing.T, privKey, pubClinet wgtypes.Key) {
tun, _, _, err := CreateNetTUNWithStack(
tun, _, _, err := netstack_provider.CreateNetTUNWithStack(
[]netip.Addr{netip.MustParseAddr("192.168.4.1")},
53,
device.DefaultMTU,
Expand Down Expand Up @@ -124,7 +138,9 @@ func TestShaperEnabled(t *testing.T) {

config.Current.SetDefault(config.FlagShaperBandwidth.Name, "6250")
config.Current.SetDefault(config.FlagShaperEnabled.Name, "true")
InitUserspaceShaper(nil)
config.FlagFirewallProtectedNetworks.Value = "10.0.0.0/8,192.168.0.0/16,127.0.0.0/8"

netstack_provider.InitUserspaceShaper(nil)

privKey1, err := wgtypes.GeneratePrivateKey()
if err != nil {
Expand All @@ -136,6 +152,50 @@ func TestShaperEnabled(t *testing.T) {
t.Error(err)
return
}
_, _ = privKey1, privKey2

startServer(t, privKey1, privKey2.PublicKey())
startClient(t, privKey2, privKey1.PublicKey())
}

// func TestMain(t *testing.T) {
// // var (
// // dnsResolverIP = "8.8.8.8:53" // Google DNS resolver.
// // dnsResolverProto = "udp" // Protocol to use for the DNS resolver
// // dnsResolverTimeoutMs = 5000 // Timeout (ms) for the DNS resolver (optional)
// // )

// // dialer := &net.Dialer{
// // Resolver: &net.Resolver{
// // PreferGo: true,
// // Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
// // d := net.Dialer{
// // Timeout: time.Duration(dnsResolverTimeoutMs) * time.Millisecond,
// // }
// // return d.DialContext(ctx, dnsResolverProto, dnsResolverIP)
// // },
// // },
// // }

// // dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
// // return dialer.DialContext(ctx, network, addr)
// // }
// // _=dialContext

// //http.DefaultTransport.(*http.Transport).DialContext = dialContext
// httpClient := &http.Client{}

// // Testing the new HTTP client with the custom DNS resolver.
// resp, err := httpClient.Get("https://www.violetnorth.com")
// if err != nil {
// log.Fatalln(err)
// }
// defer resp.Body.Close()

// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Fatalln(err)
// }

// log.Println(string(body))
// }
2 changes: 2 additions & 0 deletions e2e/gorunner/Dockerfile.precompiled
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ RUN ln -s /sbin/iptables /usr/sbin/iptables

COPY ./build/e2e/test /usr/local/bin/test
COPY ./build/e2e/deployer /usr/local/bin/deployer
COPY ./build/e2e/bug6022.test /usr/local/bin/bug6022.test
#COPY ./build/e2e/bug6022.aux /usr/local/bin/bug6022.aux
52 changes: 52 additions & 0 deletions e2e/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ func (r *Runner) Test(providerHost string) (retErr error) {
return
}

// Test_ starts given provider and consumer nodes and runs e2e tests.
func (r *Runner) Test_() (retErr error) {
// services := strings.Split(r.services, ",")
// if err := r.startProviderConsumerNodes(providerHost, services); err != nil {
// retErr = errors.Wrap(err, "tests failed!")
// return
// }

// defer func() {
// if err := r.stopProviderConsumerNodes(providerHost, services); err != nil {
// log.Err(err).Msg("Could not stop provider consumer nodes")
// }
//
// if retErr == nil { // check public IPs in logs only if all the tests succeeded
// if err := r.checkPublicIPInLogs("myst-provider", "myst-consumer-wireguard"); err != nil {
// retErr = errors.Wrap(err, "tests failed!")
// return
// }
// }
// }()

log.Info().Msg("Running tests for env: " + r.testEnv)
log.Info().Msg("Running >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ")

err := r.compose("run", "go-runner",
// "wget", "http://bug6022-aux:8083/test",
"/usr/local/bin/bug6022.test",
)

retErr = errors.Wrap(err, "tests failed!")
return
}

func (r *Runner) checkPublicIPInLogs(containers ...string) error {
regExps := []*regexp.Regexp{
regexp.MustCompile(`(^|[^0-9])(172\.30\.0\.2)($|[^0-9])`),
Expand Down Expand Up @@ -204,6 +237,25 @@ func (r *Runner) Init() error {
return nil
}

// Init_ starts bug6022.test dependency
func (r *Runner) Init_() error {
log.Info().Msg("Starting other services >>")
if err := r.compose("pull"); err != nil {
return errors.Wrap(err, "could not pull images")
}

if err := r.compose("up", "-d", "bug6022-aux"); err != nil {
return errors.Wrap(err, "starting other services failed!")
}

log.Info().Msg("Building app images")
if err := r.compose("build"); err != nil {
return errors.Wrap(err, "building app images failed!")
}

return nil
}

func (r *Runner) startProviderConsumerNodes(providerHost string, services []string) error {
log.Info().Msg("Starting provider consumer containers")

Expand Down

0 comments on commit a8a5d2b

Please sign in to comment.