Skip to content
Snippets Groups Projects
lab00_test.go 4.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • package integration_test_labs
    
    import (
    	"context"
    	"fmt"
    	"testing"
    
    	mnepb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/networkelement"
    	integration_test_utils "code.fbi.h-da.de/danet/gosdn/integration-tests/integrationTestUtils"
    	"github.com/stretchr/testify/assert"
    	"google.golang.org/grpc"
    )
    
    const pndID = "5f20f34b-cbd0-4511-9ddc-c50cf6a3b49d"
    
    const (
    	targetAName = "gnmi-targetA"
    	targetAUUID = "e3cc56ad-2a6c-4b76-8315-5f17a6dc25bf"
    	targetBName = "gnmi-targetB"
    	targetBUUID = "87c8bcc1-d95c-401e-b447-f02c56abd8b1"
    )
    
    // The connection to the controller to use in each test.
    var conn *grpc.ClientConn
    
    // The context containing the credentials when authenticated.
    var ctx context.Context
    
    // A defaultSDN config with default/empty values.
    var defaultSDNConfig string
    
    func TestMain(m *testing.M) {
    	localConn, localCtx, err := integration_test_utils.CreateSecureConnection()
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	conn = localConn
    	ctx = localCtx
    
    	sndConfig, err := integration_test_utils.ExportCurrentSDNConfig(conn, ctx)
    	defaultSDNConfig = sndConfig
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    
    	integration_test_utils.ApplySDNConfig(conn, ctx, defaultSDNConfig)
    
    	m.Run()
    }
    
    // The following test describes a chain of commands (see
    // https://code.fbi.h-da.de/danet/gosdn/-/wikis/Labs/Lab00-education).
    
    func TestLab00(t *testing.T) {
    	defer integration_test_utils.ApplySDNConfig(conn, ctx, defaultSDNConfig)
    
    	managedNetworkElements := []struct {
    		name        string
    		newHostname string
    		id          string
    		addr        string
    		username    string
    		password    string
    	}{
    		{
    			name:        targetAName,
    			newHostname: "newHostnameForTargetA",
    			id:          targetAUUID,
    			addr:        integration_test_utils.DefaultTargetAAdress,
    		},
    		{
    			name:        targetBName,
    			newHostname: "newHostnameForTargetB",
    			id:          targetBUUID,
    			addr:        integration_test_utils.DefaultTargetBAdress,
    		},
    	}
    
    	mneService := mnepb.NewNetworkElementServiceClient(conn)
    
    	// Create the two managed network elements.
    	for i, mne := range managedNetworkElements {
    		alr := createAddListRequestSingleMne(mne.addr, mne.name, mne.id, integration_test_utils.DefaultTargetUsername, integration_test_utils.DefaultTargetPassword)
    
    		_, err := mneService.AddList(ctx, alr)
    		if err != nil {
    			t.Error(err)
    		}
    
    		getAllMneRequest := &mnepb.GetAllRequest{
    			Timestamp: integration_test_utils.GetTimestamp(),
    			Pid:       pndID,
    		}
    
    		getAllResponse, err := mneService.GetAll(ctx, getAllMneRequest)
    		if err != nil {
    			t.Error(err)
    		}
    
    		assert.Equal(t, i+1, len(getAllResponse.GetMne()), "Amount of registered managed network elements is not correct.")
    	}
    
    	// Check the initial hostname of both managed network elements.
    	for _, mne := range managedNetworkElements {
    		hostnameRequest := getHostnameSingleMne(mne.id)
    		resp, err := mneService.GetPath(ctx, hostnameRequest)
    		if err != nil {
    			t.Error(err)
    		}
    
    		assert.Equal(t, 1, len(resp.GetMneNotification()), "The hostname for the managed network element has been requested. Therefore exactly one response is expected.")
    		assert.NotEmpty(t, resp.MneNotification[0].GetUpdate()[0].GetVal().GetStringVal(), "It is expected, that the hostname of the managed network element is not empty.")
    	}
    
    	// Change the hostname of both managed network elements.
    
    	for _, mne := range managedNetworkElements {
    		hostnamePathListRequest, err := setHostnamePathListRequestSingleMne(mne.id, mne.newHostname, mnepb.ApiOperation_API_OPERATION_UPDATE)
    		if err != nil {
    			t.Error(err)
    		}
    
    		setResp, err := mneService.SetPathList(ctx, hostnamePathListRequest)
    		if err != nil {
    			t.Error(err)
    		}
    
    		assert.Equal(t, 1, len(setResp.GetResponses()), "A single path change for the hostname has been requested. Therefore exactly one response is expected.")
    
    		// Commit the change.
    		commitChange := commitOrConfirmChange(setResp.GetResponses()[0].GetId(), mnepb.Operation_OPERATION_COMMIT)
    		_, err = mneService.SetChangeList(ctx, commitChange)
    		if err != nil {
    			t.Error(err)
    		}
    
    		// Confirm the change.
    		confirmChange := commitOrConfirmChange(setResp.GetResponses()[0].GetId(), mnepb.Operation_OPERATION_CONFIRM)
    		_, err = mneService.SetChangeList(ctx, confirmChange)
    		if err != nil {
    			t.Error(err)
    		}
    	}
    
    	// Check if the hostname has been changed and is accessible through a
    	// GetPath request.
    	for _, mne := range managedNetworkElements {
    		hostnameRequest := getHostnameSingleMne(mne.id)
    		resp, err := mneService.GetPath(ctx, hostnameRequest)
    		if err != nil {
    			t.Error(err)
    		}
    
    		assert.Equal(t, 1, len(resp.GetMneNotification()), "The hostname for the managed network element has been requested. Therefore exactly one response is expected.")
    		assert.Equal(t, mne.newHostname, resp.MneNotification[0].GetUpdate()[0].GetVal().GetStringVal(), "Exptected the new changed hostname.")
    	}
    }