diff --git a/.gitlab/ci/.test.yml b/.gitlab/ci/.test.yml index 9a20eb0c03c80a40b6a25160ee0dbabd7b7fd1e7..30a13ba0b8460a62f0a542b1fb7b9059cd0adcf7 100644 --- a/.gitlab/ci/.test.yml +++ b/.gitlab/ci/.test.yml @@ -1,11 +1,10 @@ .test: &test stage: test -integration-test-controller: +integration-test-gosdn: image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golang:$GOLANG_VERSION-bookworm needs: - ["build-integration-test-images"] - #image: debian:latest variables: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: example @@ -30,10 +29,10 @@ integration-test-controller: # alias: gosdn # variables: # 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: - # 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 - - 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/* <<: *test diff --git a/controller/controller.Dockerfile b/controller/controller.Dockerfile index 56a9bbecc72a802387748615dc6c9decb586ad73..873e576ed1b969200c8dc140b8582c1c303c6aac 100644 --- a/controller/controller.Dockerfile +++ b/controller/controller.Dockerfile @@ -11,7 +11,7 @@ RUN --mount=type=cache,target=/root/go/pkg/mod \ FROM ${GITLAB_PROXY}debian:bookworm-slim WORKDIR /app/ -RUN mkdir -p controller/configs/ && touch controller/configs/gNMISubscriptions.txt +RUN mkdir -p controller/configs/ COPY --from=builder /gosdn/artifacts/gosdn ./gosdn diff --git a/dev_env_data/docker-compose/integration-test_docker-compose.yml b/dev_env_data/docker-compose/integration-test_docker-compose.yml index 2d2058dadb43a8542930e406ad8aab17efd9b88e..6bb99815b1a9785718f787f3dc8c4b8b6b797c32 100644 --- a/dev_env_data/docker-compose/integration-test_docker-compose.yml +++ b/dev_env_data/docker-compose/integration-test_docker-compose.yml @@ -36,7 +36,7 @@ services: rabbitmq: condition: service_healthy command: - --config /app/configs/integration-test-gosdn.toml --security insecure + --config /app/configs/integration-test-gosdn.toml --security secure environment: GOSDN_ADMIN_PASSWORD: TestPassword volumes: diff --git a/integration-tests/example_tests/controller_test.go b/integration-tests/example_tests/controller_test.go deleted file mode 100644 index 14d3833c2c9abb8462d36ccde12f08de68a420dd..0000000000000000000000000000000000000000 --- a/integration-tests/example_tests/controller_test.go +++ /dev/null @@ -1,19 +0,0 @@ -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) -} diff --git a/integration-tests/example_tests/example_test.go b/integration-tests/example_tests/example_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f88f8be2877577749f40b6b87731a9c4d6a9747f --- /dev/null +++ b/integration-tests/example_tests/example_test.go @@ -0,0 +1,41 @@ +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) +} diff --git a/integration-tests/integrationTestUtils/integrationTestUtils.go b/integration-tests/integrationTestUtils/integrationTestUtils.go index 2afa6eb835035a1378ee1185dd6e36e53971534a..2d88c1a13a4cd9900e417fb7b567bffc842cf1e4 100644 --- a/integration-tests/integrationTestUtils/integrationTestUtils.go +++ b/integration-tests/integrationTestUtils/integrationTestUtils.go @@ -8,28 +8,63 @@ import ( configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement" 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/credentials/insecure" + "google.golang.org/grpc/metadata" ) -func CreateConnection() (*grpc.ClientConn, error) { - controller_url := "localhost:55055" - controller_env := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL") - if controller_env != "" { - controller_url = controller_env +func createContextWithAuthorization(loginResponse *rbac.LoginResponse) context.Context { + md := metadata.Pairs("authorize", loginResponse.Token) + return metadata.NewOutgoingContext(context.Background(), md) +} + +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()) - 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 { - return nil, err + return nil, nil, err } - return conn, nil + sessionContext := context.Background() + + return conn, sessionContext, nil } -func PrepareEvironment(conn *grpc.ClientConn) (string, error) { - ctx := context.Background() +// ExportCurrentSDNConfig can be used to save the current SDN config as a string to use in an other test later on. +func ExportCurrentSDNConfig(conn *grpc.ClientConn, ctx context.Context) (string, error) { pndService := ppb.NewPndServiceClient(conn) pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()}) if err != nil { @@ -47,9 +82,8 @@ func PrepareEvironment(conn *grpc.ClientConn) (string, error) { return sdnConfigResponse.SdnConfigData, nil } -func RestoreEnvironment(conn *grpc.ClientConn, sdnConfig string) { - ctx := context.Background() - +// ApplySDNConfig can be used to apply a given SDN config as a string to set the testing environment as desired. +func ApplySDNConfig(conn *grpc.ClientConn, ctx context.Context, sdnConfig string) { pndService := ppb.NewPndServiceClient(conn) pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()}) if err != nil {