Skip to content
Snippets Groups Projects
integrationTestUtils.go 2.01 KiB
Newer Older
  • Learn to ignore specific revisions
  • package integration_test_utils
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"time"
    
    	configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement"
    	ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
    
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/credentials/insecure"
    )
    
    func CreateConnection() (*grpc.ClientConn, error) {
    	controller_url := "localhost:55055"
    	controller_env := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
    	if controller_env != "" {
    		controller_url = controller_env
    	}
    	dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
    	conn, err := grpc.Dial(controller_url, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
    	if err != nil {
    		return nil, err
    	}
    
    	return conn, nil
    }
    
    func PrepareEvironment(conn *grpc.ClientConn) (string, error) {
    	ctx := context.Background()
    	pndService := ppb.NewPndServiceClient(conn)
    	pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
    	if err != nil {
    		return "", err
    	}
    	pndID := pndRes.Pnd[0].Id
    
    	configMgmtService := configMgmtPb.NewConfigurationManagementServiceClient(conn)
    
    	sdnConfigResponse, err := configMgmtService.ExportSDNConfig(ctx, &configMgmtPb.ExportSDNConfigRequest{Timestamp: getTimestamp(), Pid: pndID})
    	if err != nil {
    		return "", err
    	}
    
    	return sdnConfigResponse.SdnConfigData, nil
    }
    
    func RestoreEnvironment(conn *grpc.ClientConn, sdnConfig string) {
    	ctx := context.Background()
    
    	pndService := ppb.NewPndServiceClient(conn)
    	pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
    	if err != nil {
    		fmt.Println(err)
    	}
    
    	// currently only support for default PND
    	pndID := pndRes.Pnd[0].Id
    
    	configMgmtService := configMgmtPb.NewConfigurationManagementServiceClient(conn)
    
    	_, err = configMgmtService.ImportSDNConfig(ctx, &configMgmtPb.ImportSDNConfigRequest{Timestamp: getTimestamp(), Pid: pndID, SdnConfigData: sdnConfig})
    	if err != nil {
    		fmt.Println(err)
    	}
    }
    
    func getTimestamp() int64 {
    	return int64(time.Now().Nanosecond())
    }