Skip to content
Snippets Groups Projects
Commit f7099b42 authored by Neil-Jocelyn Schark's avatar Neil-Jocelyn Schark
Browse files

Integration test framework

See merge request !639
parent 96538acd
No related branches found
No related tags found
1 merge request!639Integration test framework
Pipeline #172949 passed
.test: &test .test: &test
stage: test stage: test
integration-test-controller: integration-test-gosdn:
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golang:$GOLANG_VERSION-bookworm image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golang:$GOLANG_VERSION-bookworm
needs: needs:
- ["build-integration-test-images"] - ["build-integration-test-images"]
#image: debian:latest
variables: variables:
MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example MONGO_INITDB_ROOT_PASSWORD: example
...@@ -30,10 +29,10 @@ integration-test-controller: ...@@ -30,10 +29,10 @@ integration-test-controller:
# alias: gosdn # alias: gosdn
# variables: # variables:
# GOSDN_ADMIN_PASSWORD: TestPassword # GOSDN_ADMIN_PASSWORD: TestPassword
# command: ["--config", "/app/configs/integration-test-gosdn.toml", "--security", "insecure"] # command: ["--config", "/app/configs/integration-test-gosdn.toml", "--security", "secure"]
script: script:
# Remove later when fixed. See: https://code.fbi.h-da.de/danet/gosdn/-/issues/335 # Remove start of goSDN later when fixed. See: https://code.fbi.h-da.de/danet/gosdn/-/issues/335
- make build-gosdn - make build-gosdn
- cd artifacts && GOSDN_ADMIN_PASSWORD=TestPassword ./gosdn --config ../controller/configs/integration-test-gosdn.toml --security insecure & - cd artifacts && GOSDN_ADMIN_PASSWORD=TestPassword ./gosdn --config ../controller/configs/integration-test-gosdn.toml --security secure &
- INTEGRATION_TEST_CONTROLLER_URL="localhost:55055" go test ./integration-tests/* - INTEGRATION_TEST_CONTROLLER_URL="localhost:55055" go test ./integration-tests/*
<<: *test <<: *test
...@@ -11,7 +11,7 @@ RUN --mount=type=cache,target=/root/go/pkg/mod \ ...@@ -11,7 +11,7 @@ RUN --mount=type=cache,target=/root/go/pkg/mod \
FROM ${GITLAB_PROXY}debian:bookworm-slim FROM ${GITLAB_PROXY}debian:bookworm-slim
WORKDIR /app/ WORKDIR /app/
RUN mkdir -p controller/configs/ && touch controller/configs/gNMISubscriptions.txt RUN mkdir -p controller/configs/
COPY --from=builder /gosdn/artifacts/gosdn ./gosdn COPY --from=builder /gosdn/artifacts/gosdn ./gosdn
......
...@@ -36,7 +36,7 @@ services: ...@@ -36,7 +36,7 @@ services:
rabbitmq: rabbitmq:
condition: service_healthy condition: service_healthy
command: command:
--config /app/configs/integration-test-gosdn.toml --security insecure --config /app/configs/integration-test-gosdn.toml --security secure
environment: environment:
GOSDN_ADMIN_PASSWORD: TestPassword GOSDN_ADMIN_PASSWORD: TestPassword
volumes: volumes:
......
package integration_test
import (
"testing"
integration_test_utils "code.fbi.h-da.de/danet/gosdn/integration-tests/integrationTestUtils"
)
func TestExample(t *testing.T) {
conn, err := integration_test_utils.CreateConnection()
if err != nil {
t.Errorf(err.Error())
}
sndConfig, err := integration_test_utils.PrepareEvironment(conn)
if err != nil {
t.Errorf(err.Error())
}
defer integration_test_utils.RestoreEnvironment(conn, sndConfig)
}
package integration_test_example
import (
"context"
"fmt"
"testing"
integration_test_utils "code.fbi.h-da.de/danet/gosdn/integration-tests/integrationTestUtils"
"google.golang.org/grpc"
)
// 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())
}
m.Run()
}
func TestExample(t *testing.T) {
defer integration_test_utils.ApplySDNConfig(conn, ctx, defaultSDNConfig)
// You can set the environment to a different sdn config like this:
//integration_test_utils.SetEnvironment(connection, ctx, customSDNConfig)
}
...@@ -8,28 +8,63 @@ import ( ...@@ -8,28 +8,63 @@ import (
configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement" configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement"
ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd" ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
"code.fbi.h-da.de/danet/gosdn/controller/api"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
) )
func CreateConnection() (*grpc.ClientConn, error) { func createContextWithAuthorization(loginResponse *rbac.LoginResponse) context.Context {
controller_url := "localhost:55055" md := metadata.Pairs("authorize", loginResponse.Token)
controller_env := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL") return metadata.NewOutgoingContext(context.Background(), md)
if controller_env != "" { }
controller_url = controller_env
func CreateSecureConnection() (*grpc.ClientConn, context.Context, error) {
username := "admin"
password := "TestPassword"
controllerUrl := "localhost:55055"
controllerEnv := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
if controllerEnv != "" {
controllerUrl = controllerEnv
}
loginResp, err := api.Login(context.Background(), controllerUrl, username, password)
if err != nil {
return nil, nil, err
}
sessionContext := createContextWithAuthorization(loginResp)
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
conn, err := grpc.Dial(controllerUrl, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
if err != nil {
return nil, nil, err
}
return conn, sessionContext, nil
}
func CreateConnection() (*grpc.ClientConn, context.Context, error) {
controllerUrl := "localhost:55055"
controllerEnv := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
if controllerEnv != "" {
controllerUrl = controllerEnv
} }
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials()) dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
conn, err := grpc.Dial(controller_url, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024))) conn, err := grpc.Dial(controllerUrl, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
return conn, nil sessionContext := context.Background()
return conn, sessionContext, nil
} }
func PrepareEvironment(conn *grpc.ClientConn) (string, error) { // ExportCurrentSDNConfig can be used to save the current SDN config as a string to use in an other test later on.
ctx := context.Background() func ExportCurrentSDNConfig(conn *grpc.ClientConn, ctx context.Context) (string, error) {
pndService := ppb.NewPndServiceClient(conn) pndService := ppb.NewPndServiceClient(conn)
pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()}) pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
if err != nil { if err != nil {
...@@ -47,9 +82,8 @@ func PrepareEvironment(conn *grpc.ClientConn) (string, error) { ...@@ -47,9 +82,8 @@ func PrepareEvironment(conn *grpc.ClientConn) (string, error) {
return sdnConfigResponse.SdnConfigData, nil return sdnConfigResponse.SdnConfigData, nil
} }
func RestoreEnvironment(conn *grpc.ClientConn, sdnConfig string) { // ApplySDNConfig can be used to apply a given SDN config as a string to set the testing environment as desired.
ctx := context.Background() func ApplySDNConfig(conn *grpc.ClientConn, ctx context.Context, sdnConfig string) {
pndService := ppb.NewPndServiceClient(conn) pndService := ppb.NewPndServiceClient(conn)
pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()}) pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment