Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Nov 4, 2024
1 parent d16a49f commit 3ff73ec
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 20 deletions.
128 changes: 126 additions & 2 deletions internal/applications/remoter/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,131 @@
package handler

import (
"testing"
"context"
"net/http"
"os"
"time"

"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/app/config"
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/settings"
"github.com/number571/hidden-lake/internal/service/pkg/app"
hls_config "github.com/number571/hidden-lake/internal/service/pkg/app/config"
)

var (
tgPrivKey1 = asymmetric.NewPrivKey()
tgPrivKey2 = asymmetric.NewPrivKey()
)

const (
tcPassword = "test-password"
tcPathConfigTemplate = "config_test_%d.yml"
tcPathDatabaseTemplate = "database_test_%d.yml"
)

func TestNothing(_ *testing.T) {}
func testRunService(addr string) (config.IConfig, *http.Server) {
mux := http.NewServeMux()

cfg := &config.SConfig{
FSettings: &config.SConfigSettings{
FPassword: tcPassword,
FExecTimeoutMS: 5_000,
},
FAddress: &config.SAddress{
FIncoming: addr,
},
}

logger := logger.NewLogger(
logger.NewSettings(&logger.SSettings{}),
func(_ logger.ILogArg) string { return "" },
)

ctx := context.Background()

mux.HandleFunc(settings.CExecPath, HandleIncomingExecHTTP(ctx, cfg, logger))

srv := &http.Server{
Addr: addr,
Handler: http.TimeoutHandler(mux, time.Minute/2, "timeout"),
ReadTimeout: time.Second,
}

go func() { _ = srv.ListenAndServe() }()
return cfg, srv
}

func testRunNewNodes(ctx context.Context, httpAddrNode1, tcpAddrNode2, addrService string) {
runner1 := app.NewApp(
initConfig(
"./testdata/node1/hls.yml",
"",
httpAddrNode1,
"",
tcpAddrNode2,
tgPrivKey2.GetPubKey(),
),
tgPrivKey1,
"./testdata/node1",
1,
)

runner2 := app.NewApp(
initConfig(
"./testdata/node2/hls.yml",
tcpAddrNode2,
"",
addrService,
"",
tgPrivKey1.GetPubKey(),
),
tgPrivKey2,
"./testdata/node2",
1,
)

go func() { _ = runner1.Run(ctx) }()
go func() { _ = runner2.Run(ctx) }()
}

func initConfig(
cfgPath, tcpAddrNode, httpAddrNode, addrService, conn string,
pubKey asymmetric.IPubKey,
) hls_config.IConfig {
os.Remove(cfgPath)

services := map[string]string{}
if addrService != "" {
services[settings.CServiceFullName] = addrService
}

connections := []string{}
if conn != "" {
connections = []string{conn}
}

cfg, err := hls_config.BuildConfig(cfgPath, &hls_config.SConfig{
FSettings: &hls_config.SConfigSettings{
FMessageSizeBytes: (8 << 10),
FWorkSizeBits: 1,
FFetchTimeoutMS: 10_000,
FQueuePeriodMS: 500,
},
FLogging: []string{},
FAddress: &hls_config.SAddress{
FTCP: tcpAddrNode,
FHTTP: httpAddrNode,
},
FServices: services,
FConnections: connections,
FFriends: map[string]string{
"test_recv": pubKey.ToString(),
},
})
if err != nil {
panic(err)
}
return cfg
}
58 changes: 58 additions & 0 deletions internal/applications/remoter/internal/handler/incoming_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package handler

import (
"context"
"net/http"
"strings"
"testing"
"time"

"github.com/number571/hidden-lake/internal/applications/remoter/pkg/client"
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/settings"
hls_client "github.com/number571/hidden-lake/internal/service/pkg/client"
testutils "github.com/number571/hidden-lake/test/utils"
)

func TestIncomingExecHTTP(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, service := testRunService(testutils.TgAddrs[40])
defer service.Close()

testRunNewNodes(
ctx,
testutils.TgAddrs[41],
testutils.TgAddrs[42],
testutils.TgAddrs[40],
)

time.Sleep(100 * time.Millisecond)

hlsClient := hls_client.NewClient(
hls_client.NewBuilder(),
hls_client.NewRequester(
"http://"+testutils.TgAddrs[41],
&http.Client{Timeout: time.Minute},
),
)

hlrClient := client.NewClient(
client.NewBuilder(tcPassword),
client.NewRequester(hlsClient),
)

msg := "hello, world!"
rsp, err := hlrClient.Exec(ctx, "test_recv", "echo"+settings.CExecSeparator+msg)
if err != nil {
t.Error(err)
return
}

if strings.TrimSpace(string(rsp)) != msg {
t.Error("get invalid response")
return
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.yml
*.db
Empty file.
Empty file.
Empty file.
13 changes: 10 additions & 3 deletions internal/applications/remoter/pkg/client/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ var (
)

type sBuilder struct {
fPassword string
}

func NewBuilder() IBuilder {
return &sBuilder{}
func NewBuilder(pPassword string) IBuilder {
return &sBuilder{
fPassword: pPassword,
}
}

func (p *sBuilder) Exec(pCmd ...string) hls_request.IRequest {
return hls_request.NewRequest(
http.MethodPost,
hlr_settings.CServiceFullName,
hlr_settings.CExecPath,
).WithBody([]byte(strings.Join(pCmd, " ")))
).
WithHead(map[string]string{
hlr_settings.CHeaderPassword: p.fPassword,
}).
WithBody([]byte(strings.Join(pCmd, " ")))
}
2 changes: 1 addition & 1 deletion test/result/badge_codelines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/result/badge_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions test/result/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion test/utils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
)

var (
TgAddrs = [40]string{
TgAddrs = [50]string{
"localhost:8000",
"localhost:8001",
"localhost:8002",
Expand Down Expand Up @@ -49,5 +49,16 @@ var (
"localhost:8037",
"localhost:8038",
"localhost:8039",

"localhost:8040",
"localhost:8041",
"localhost:8042",
"localhost:8043",
"localhost:8044",
"localhost:8045",
"localhost:8046",
"localhost:8047",
"localhost:8048",
"localhost:8049",
}
)

0 comments on commit 3ff73ec

Please sign in to comment.