Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
networking service for linux without mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsaturyan committed May 23, 2024
1 parent e161901 commit 41cb45e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 176 deletions.
226 changes: 109 additions & 117 deletions backend/cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
package main

import (
"backend/internal/conv"
"backend/internal/dbmodel"
mock_network "backend/internal/mocks/mock_networking"
"backend/internal/network"
"backend/internal/utils"
"context"
"database/sql"
"errors"
"fmt"
"github.com/golang/mock/gomock"
"log"
"math/rand"
"net"
"time"

_ "backend/docs"
"backend/internal/app/sledilka"
"backend/internal/db"
"backend/internal/network"
"backend/internal/tokener"
"context"
"fmt"
"github.com/compose-spec/compose-go/v2/cli"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
)

// @title Sledilka API
Expand Down Expand Up @@ -88,8 +77,8 @@ func main() {

conn, err := grpc.Dial(
fmt.Sprintf("%s:%v",
//"0.0.0.0",
tokenerInfo.Name,
"0.0.0.0",
//tokenerInfo.Name,
tokenerInfo.Ports[0].Published),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
Expand All @@ -98,114 +87,117 @@ func main() {
}
tokenerClient := tokener.NewTokenerClient(conn)

//conn, err = grpc.Dial(
// fmt.Sprintf("%s:%v", "0.0.0.0", 8282),
// grpc.WithTransportCredentials(insecure.NewCredentials()),
//)
conn, err = grpc.Dial(
fmt.Sprintf("%s:%v", "0.0.0.0", 8282),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)

networkerClient := network.NewNetworkClient(conn)
//ctrl := gomock.NewController(MockLogger{})
//subscribeClient := mock_network.NewMockNetwork_SubscribeClient(ctrl)
//subscribeClient.EXPECT().Recv().DoAndReturn(
// func() (*network.DeviceStatus, error) {
// time.Sleep(10 * time.Second)
// log.Println("taking random device from db")
// dev, err := dbmodel.New(DBConnection).GetRandomDevice(ctx)
// if err != nil {
// return nil, sql.ErrNoRows
// }
// return &network.DeviceStatus{
// MacAddress: dev.MacAddress,
// IsActive: utils.RandBool(),
// IpAddress: dev.IpAddress,
// }, nil
// },
//).AnyTimes()
//networkerClient := mock_network.NewMockNetworkClient(ctrl)
//networkerClient.EXPECT().Subscribe(gomock.Any(), gomock.Any()).Return(subscribeClient, nil).AnyTimes()
//networkerClient.EXPECT().GetAllMacAddresses(gomock.Any(), gomock.Any()).DoAndReturn(
// func(ctx context.Context, in *network.Empty, opts ...grpc.CallOption) (*network.GetAllMacAddressesResponse, error) {
// macs, err := dbmodel.New(DBConnection).GetAllDevicesMacs(ctx)
// if err != nil {
// return nil, errors.New("db error")
// }
// resp := &network.GetAllMacAddressesResponse{
// Devices: make([]*network.DeviceStatus, 0, len(macs)),
// }
// for _, mac := range macs {
// resp.Devices = append(resp.Devices, &network.DeviceStatus{
// MacAddress: mac,
// IsActive: utils.RandBool(),
// IpAddress: conv.StringToStringp("127.0.0.1"),
// })
// }
// resp.Devices = append(resp.Devices, &network.DeviceStatus{
// MacAddress: GenerateMac().String(),
// IsActive: true,
// IpAddress: conv.StringToStringp("127.0.0.1"),
// })
// resp.Devices = append(resp.Devices, &network.DeviceStatus{
// MacAddress: GenerateMac().String(),
// IsActive: true,
// IpAddress: conv.StringToStringp("127.0.0.1"),
// })
// resp.Devices = append(resp.Devices, &network.DeviceStatus{
// MacAddress: GenerateMac().String(),
// IsActive: true,
// IpAddress: conv.StringToStringp("127.0.0.1"),
// })
// return resp, nil
// },
//).AnyTimes()
//
//networkerClient := network.NewNetworkClient(conn)
ctrl := gomock.NewController(MockLogger{})
subscribeClient := mock_network.NewMockNetwork_SubscribeClient(ctrl)
subscribeClient.EXPECT().Recv().DoAndReturn(
func() (*network.DeviceStatus, error) {
time.Sleep(10 * time.Second)
log.Println("taking random device from db")
dev, err := dbmodel.New(DBConnection).GetRandomDevice(ctx)
if err != nil {
return nil, sql.ErrNoRows
}
return &network.DeviceStatus{
MacAddress: dev.MacAddress,
IsActive: utils.RandBool(),
IpAddress: dev.IpAddress,
}, nil
},
).AnyTimes()
networkerClient := mock_network.NewMockNetworkClient(ctrl)
networkerClient.EXPECT().Subscribe(gomock.Any(), gomock.Any()).Return(subscribeClient, nil).AnyTimes()
networkerClient.EXPECT().GetAllMacAddresses(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, in *network.Empty, opts ...grpc.CallOption) (*network.GetAllMacAddressesResponse, error) {
macs, err := dbmodel.New(DBConnection).GetAllDevicesMacs(ctx)
if err != nil {
return nil, errors.New("db error")
}
resp := &network.GetAllMacAddressesResponse{
Devices: make([]*network.DeviceStatus, 0, len(macs)),
}
for _, mac := range macs {
resp.Devices = append(resp.Devices, &network.DeviceStatus{
MacAddress: mac,
IsActive: utils.RandBool(),
IpAddress: conv.StringToStringp("127.0.0.1"),
})
}
resp.Devices = append(resp.Devices, &network.DeviceStatus{
MacAddress: GenerateMac().String(),
IsActive: true,
IpAddress: conv.StringToStringp("127.0.0.1"),
})
resp.Devices = append(resp.Devices, &network.DeviceStatus{
MacAddress: GenerateMac().String(),
IsActive: true,
IpAddress: conv.StringToStringp("127.0.0.1"),
})
resp.Devices = append(resp.Devices, &network.DeviceStatus{
MacAddress: GenerateMac().String(),
IsActive: true,
IpAddress: conv.StringToStringp("127.0.0.1"),
})
return resp, nil
},
).AnyTimes()

networkerClient.EXPECT().IsActiveListDevice(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, req *network.IsActiveListDeviceRequest, _ ...grpc.CallOption) (*network.IsActiveListDeviceResponse, error) {
resp := &network.IsActiveListDeviceResponse{
Devices: make([]*network.DeviceStatus, 0, len(req.GetDevices())),
}
for _, dev := range req.GetDevices() {
resp.Devices = append(resp.Devices, &network.DeviceStatus{
MacAddress: dev.GetMacAddress(),
IsActive: utils.RandBool(),
IpAddress: conv.StringToStringp("127.0.0.1"),
})
}
return resp, nil
},
).AnyTimes()
//networkerClient.EXPECT().IsActiveListDevice(gomock.Any(), gomock.Any()).DoAndReturn(
// func(_ context.Context, req *network.IsActiveListDeviceRequest, _ ...grpc.CallOption) (*network.IsActiveListDeviceResponse, error) {
// resp := &network.IsActiveListDeviceResponse{
// Devices: make([]*network.DeviceStatus, 0, len(req.GetDevices())),
// }
// for _, dev := range req.GetDevices() {
// resp.Devices = append(resp.Devices, &network.DeviceStatus{
// MacAddress: dev.GetMacAddress(),
// IsActive: utils.RandBool(),
// IpAddress: conv.StringToStringp("127.0.0.1"),
// })
// }
// return resp, nil
// },
//).AnyTimes()

app := sledilka.New(
DBConnection,
tokenerClient,
networkerClient,
//fmt.Sprintf("http://0.0.0.0:%v/", 8181),
fmt.Sprintf("http://%v:%v/", streamingInfo.Name, streamingInfo.Ports[0].Published),
fmt.Sprintf("http://%v:%v/",
//streamingInfo.Name,
"0.0.0.0",
streamingInfo.Ports[0].Published),
"0.0.0.0:8081")
log.Fatal(app.Run())
}

type MockLogger struct{}

func (m MockLogger) Errorf(format string, args ...interface{}) {
log.Printf(format, args...)
}

func (m MockLogger) Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}

func GenerateMac() net.HardwareAddr {
buf := make([]byte, 6)
var mac net.HardwareAddr

_, err := rand.Read(buf)
if err != nil {
}

// Set the local bit
buf[0] |= 2

mac = append(mac, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])

return mac
}
//type MockLogger struct{}
//
//func (m MockLogger) Errorf(format string, args ...interface{}) {
// log.Printf(format, args...)
//}
//
//func (m MockLogger) Fatalf(format string, args ...interface{}) {
// log.Fatalf(format, args...)
//}
//
//func GenerateMac() net.HardwareAddr {
// buf := make([]byte, 6)
// var mac net.HardwareAddr
//
// _, err := rand.Read(buf)
// if err != nil {
// }
//
// // Set the local bit
// buf[0] |= 2
//
// mac = append(mac, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
//
// return mac
//}
12 changes: 6 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
- "8082:8082"
backend:
build: backend/.
# network_mode: "host"
network_mode: "host"
ports:
- "8081:8081"
volumes:
Expand All @@ -44,10 +44,10 @@ services:
build: streaming-service/.
ports:
- "8181:8181"
# networking-service:
# build: networking-service/.
# network_mode: "host"
# volumes:
# - ./docker-compose.yaml:/app/docker-compose.yaml
networking-service:
build: networking-service/.
network_mode: "host"
volumes:
- ./docker-compose.yaml:/app/docker-compose.yaml
volumes:
postgres_data:
15 changes: 0 additions & 15 deletions networking-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,10 @@ require (
)

require (
github.com/compose-spec/compose-go/v2 v2.1.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc // indirect
github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966 // indirect
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
Expand Down
38 changes: 0 additions & 38 deletions networking-service/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
github.com/compose-spec/compose-go/v2 v2.1.0 h1:qdW2qISQlCQG8v1O2TChcdxgAWTUGgUX/CPSO+ES9+E=
github.com/compose-spec/compose-go/v2 v2.1.0/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand All @@ -16,46 +6,19 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/irai/arp v1.4.0 h1:NQfBv/S+7K6b+YaaucLHrUx979xeXPfDCTZpaLlAYPg=
github.com/irai/arp v1.4.0/go.mod h1:gqBFDhulRYAwFBB3IwqhsP8IsBLzbYrtv6BFhqlqPoU=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc h1:m7rJJJeXrYCFpsxXYapkDW53wJCDmf9bsIXUg0HoeQY=
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc/go.mod h1:eOj1DDj3NAZ6yv+WafaKzY37MFZ58TdfIhQ+8nQbiis=
github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966 h1:O3p5UmisBhl3V6lgs4Vdfg8HpjzbWJPyOfGLdwVJSmI=
github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966/go.mod h1:5s5p/sMJ6sNsFl6uCh85lkFGV8kLuIYJCRJLavVJwvg=
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d h1:rjAS0af7FIYCScTtEU5KjIldC6qVaEScUJhABHC+ccM=
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d/go.mod h1:r1fbeITl2xL/zLbVnNHFyOzQJTgr/3fpf1lJX/cjzR8=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -70,6 +33,5 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 41cb45e

Please sign in to comment.