Skip to content
Snippets Groups Projects
initialise_test.go 5.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    Andre Sterba's avatar
    Andre Sterba committed
    	"context"
    
    Andre Sterba's avatar
    Andre Sterba committed
    	"os"
    
    Andre Sterba's avatar
    Andre Sterba committed
    	"testing"
    
    	"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 cuid 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)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    	if os.Getenv("GOSDN_LOG") == "nolog" {
    		log.SetLevel(log.PanicLevel)
    	}
    
    
    	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{},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		config:   gnmiConfig,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    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")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	mdid, err = uuid.Parse("688a264e-5f85-40f8-bd13-afc42fcd5c7a")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	defaultSbiID, err = uuid.Parse("b70c8425-68c7-4d4b-bb5e-5586572bd64b")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	defaultPndID, err = uuid.Parse("b4016412-eec5-45a1-aa29-f59915357bad")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	ocUUID, err = uuid.Parse("5e252b70-38f2-4c99-a0bf-1b16af4d7e67")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	iid, err = uuid.Parse("8495a8ac-a1e8-418e-b787-10f5878b2690")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	altIid, err = uuid.Parse("edc5de93-2d15-4586-b2a7-fb1bc770986b")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	cuid, err = uuid.Parse("3e8219b0-e926-400d-8660-217f2a25a7c6")
    
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    
    func mockDevice() device.Device {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	sbi := &OpenConfig{}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return &CommonDevice{
    
    		UUID:      mdid,
    
    Andre Sterba's avatar
    Andre Sterba committed
    		Model:     sbi.Schema().Root,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		sbi:       sbi,
    		transport: &mocks.Transport{},
    
    	}
    }
    
    func newPnd() pndImplementation {
    
    	sbiStore := NewMemorySbiStore()
    	deviceStore := NewMemoryDeviceStore()
    	sbiService := NewSbiService(sbiStore)
    	deviceService := NewDeviceService(
    		deviceStore,
    		sbiService,
    	)
    
    	return pndImplementation{
    
    		Name:              "default",
    		Description:       "default test pnd",
    		southboundService: sbiService,
    		deviceService:     deviceService,
    		changes:           store.NewChangeStore(),
    		Id:                defaultPndID,
    
    
    // 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)
    }