Newer
Older
Fabian Seidl
committed
"io/fs"
"io/ioutil"
Fabian Seidl
committed
"path/filepath"
Fabian Seidl
committed
"time"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/device"
"code.fbi.h-da.de/danet/gosdn/controller/store"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
"code.fbi.h-da.de/danet/gosdn/controller/mocks"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/util/proto"
"code.fbi.h-da.de/danet/gosdn/controller/test"
"code.fbi.h-da.de/danet/gosdn/forks/goarista/gnmi"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
pb "google.golang.org/protobuf/proto"
)
// UUIDs for test cases
var did uuid.UUID
var mdid uuid.UUID
var defaultSbiID uuid.UUID
var defaultPndID uuid.UUID
var ocUUID uuid.UUID
var iid uuid.UUID
var altIid uuid.UUID
var gnmiMessages map[string]pb.Message
var gnmiConfig *gnmi.Config
var startGnmiTarget chan string
var stopGnmiTarget chan bool
var mockContext = mock.MatchedBy(func(ctx context.Context) bool { return true })
// TestMain bootstraps all tests. Humongous beast
// TODO: Move somewhere more sensible
func TestMain(m *testing.M) {
log.SetReportCaller(true)
if os.Getenv("GOSDN_LOG") == "nolog" {
log.SetLevel(log.PanicLevel)
}
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
gnmiMessages = map[string]pb.Message{
"../test/proto/cap-resp-arista-ceos": &gpb.CapabilityResponse{},
"../test/proto/req-full-node": &gpb.GetRequest{},
"../test/proto/req-full-node-arista-ceos": &gpb.GetRequest{},
"../test/proto/req-interfaces-arista-ceos": &gpb.GetRequest{},
"../test/proto/req-interfaces-interface-arista-ceos": &gpb.GetRequest{},
"../test/proto/req-interfaces-wildcard": &gpb.GetRequest{},
"../test/proto/resp-full-node": &gpb.GetResponse{},
"../test/proto/resp-full-node-arista-ceos": &gpb.GetResponse{},
"../test/proto/resp-interfaces-arista-ceos": &gpb.GetResponse{},
"../test/proto/resp-interfaces-interface-arista-ceos": &gpb.GetResponse{},
"../test/proto/resp-interfaces-wildcard": &gpb.GetResponse{},
"../test/proto/resp-set-system-config-hostname": &gpb.SetResponse{},
}
for k, v := range gnmiMessages {
if err := proto.Read(k, v); err != nil {
log.Fatalf("error parsing %v: %v", k, err)
}
}
readTestUUIDs()
testSetupGnmi()
os.Exit(m.Run())
}
func targetRunner() {
for {
addr := <-startGnmiTarget
if err := test.GnmiTarget(stopGnmiTarget, addr); err != nil {
log.Fatal(err)
}
}
}
func mockTransport() Gnmi {
return Gnmi{
SetNode: nil,
RespChan: make(chan *gpb.SubscribeResponse),
Options: newGnmiTransportOptions(),
client: &mocks.GNMIClient{},
func newGnmiTransportOptions() *tpb.TransportOption {
return &tpb.TransportOption{
Address: "localhost:13371",
Username: "test",
Password: "test",
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
}
}
func readTestUUIDs() {
var err error
did, err = uuid.Parse("4d8246f8-e884-41d6-87f5-c2c784df9e44")
mdid, err = uuid.Parse("688a264e-5f85-40f8-bd13-afc42fcd5c7a")
defaultSbiID, err = uuid.Parse("b70c8425-68c7-4d4b-bb5e-5586572bd64b")
defaultPndID, err = uuid.Parse("b4016412-eec5-45a1-aa29-f59915357bad")
ocUUID, err = uuid.Parse("5e252b70-38f2-4c99-a0bf-1b16af4d7e67")
iid, err = uuid.Parse("8495a8ac-a1e8-418e-b787-10f5878b2690")
altIid, err = uuid.Parse("edc5de93-2d15-4586-b2a7-fb1bc770986b")
cuid, err = uuid.Parse("3e8219b0-e926-400d-8660-217f2a25a7c6")
if err != nil {
log.Fatal(err)
}
}
}
}
func newPnd() pndImplementation {
sbiStore := NewMemorySbiStore()
deviceStore := NewMemoryDeviceStore()
sbiService := NewSbiService(sbiStore)
deviceService := NewDeviceService(
deviceStore,
sbiService,
)
Name: "default",
Description: "default test pnd",
southboundService: sbiService,
deviceService: deviceService,
changes: store.NewChangeStore(),
Id: defaultPndID,
Fabian Seidl
committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// removeTestPlugins removes the plugins created during running the test in the current directory based on their name consisting of a uuid
// and the time since they`ve been created
func removeTestPlugins() {
currentDirectory := "./"
// pattern to match plugin uuid used as dir name
pattern := "*-*-*-*-*"
// max time passed since creation in seconds
var seconds int64 = 10
// get all available files
dirs, err := ioutil.ReadDir(currentDirectory)
if err != nil {
log.Info(err)
}
for _, dir := range dirs {
if !dir.IsDir() {
continue
} else if found, _ := filepath.Match(pattern, dir.Name()); found && isDirYoungerThanSeconds(dir.Name(), seconds) {
log.Infof("removing: %v", dir.Name())
err = os.RemoveAll(dir.Name())
if err != nil {
log.Info(err)
}
}
}
}
// isDirYoungerThanSeconds returns true if the provided dir is younger than the given amount in seconds
// and therefore was created as part of the test suite
func isDirYoungerThanSeconds(dirName string, seconds int64) bool {
fileInfo, err := os.Stat(dirName)
if err != nil {
log.Info(err)
}
return fs.FileInfo.ModTime(fileInfo).Unix() > (time.Now().Unix() - seconds)
}