From 86c8f4190f9d0283465a265de40c3507ea1758c4 Mon Sep 17 00:00:00 2001 From: svteb Date: Wed, 11 Sep 2024 08:08:17 +0000 Subject: [PATCH] Feat: Limit cnf_setup to only one CNF Refs: #2095 - In attempt to set up a different CNF the user gets a warning and is blocked from proceeding. - There were some changes necessary in the ran_spec.cr tests to forego using two cnf_setups (auxiliary function was created to deploy 5g through helm instead). This should be changed in the future when cnf_manager.cr is rehauled. - This change also removes two spec tests that were considered unnecessary/non-conformant with the new approach to only having a single CNF set up at any given time. - Spec test to verify functionality was added. --- spec/5g/ran_spec.cr | 35 +++++++++++++++++++++----- spec/setup_spec.cr | 12 +++++++++ spec/utils/utils_spec.cr | 54 ---------------------------------------- src/tasks/cnf_setup.cr | 9 +++++++ 4 files changed, 50 insertions(+), 60 deletions(-) diff --git a/spec/5g/ran_spec.cr b/spec/5g/ran_spec.cr index a0fcdb062..67b495cfa 100644 --- a/spec/5g/ran_spec.cr +++ b/spec/5g/ran_spec.cr @@ -5,22 +5,44 @@ require "../../src/tasks/kind_setup.cr" require "file_utils" require "sam" -describe "5g" do +def setup_5g_network + deployment_names = [ + "open5gs-amf", "open5gs-ausf", "open5gs-bsf", "open5gs-mongodb", "open5gs-nrf", "open5gs-nssf", + "open5gs-pcf", "open5gs-populate", "open5gs-smf", "open5gs-udm", "open5gs-udr", "open5gs-upf" + ] + + # Run Helm install command for the 5G network + helm_chart_path = "sample-cnfs/sample_srsran_ueauth_open5gs/open5gs" + Helm.install("open5gs #{helm_chart_path} -n oran --create-namespace") + + deployment_names.each do |deployment_name| + # Wait for each deployment to be ready + ready = KubectlClient::Get.resource_wait_for_install("deployment", deployment_name, namespace: "oran") + if !ready + stdout_failure "Could not set up the 5g network" + return false + end + end + stdout_success "Successfully setup open5gs" + return true +end + +describe "5g" do before_all do result = ShellCmd.run_testsuite("setup") result[:status].success?.should be_true end - it "'oran_e2_connection' should pass if the ORAN enabled RAN connects to the RIC using the e2 standard", tags: ["oran"] do begin - ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample_srsran_ueauth_open5gs/cnf-testsuite.yml") + setup_success = setup_5g_network + setup_success.should be_true ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample-oran-ric/cnf-testsuite.yml") result = ShellCmd.run_testsuite("oran_e2_connection verbose") (/(PASSED).*(RAN connects to a RIC using the e2 standard interface)/ =~ result[:output]).should_not be_nil ensure - result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample_srsran_ueauth_open5gs/cnf-testsuite.yml") + result = Helm.delete("open5gs -n oran") result[:status].success?.should be_true result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample-oran-ric/cnf-testsuite.yml") result[:status].success?.should be_true @@ -29,12 +51,13 @@ describe "5g" do it "'oran_e2_connection' should fail if the ORAN enabled RAN does not connect to the RIC using the e2 standard", tags: ["oran"] do begin - ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample_srsran_ueauth_open5gs/cnf-testsuite.yml") + setup_success = setup_5g_network + setup_success.should be_true ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample-oran-noric/cnf-testsuite.yml") result = ShellCmd.run_testsuite("oran_e2_connection verbose") (/(FAILED).*(RAN does not connect to a RIC using the e2 standard interface)/ =~ result[:output]).should_not be_nil ensure - result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample_srsran_ueauth_open5gs/cnf-testsuite.yml") + result = Helm.delete("open5gs -n oran") result[:status].success?.should be_true result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample-oran-noric/cnf-testsuite.yml") result[:status].success?.should be_true diff --git a/spec/setup_spec.cr b/spec/setup_spec.cr index 4fbed6c66..d31242f29 100644 --- a/spec/setup_spec.cr +++ b/spec/setup_spec.cr @@ -74,6 +74,18 @@ describe "Setup" do result = ShellCmd.run_testsuite("cnf_cleanup cnf-path=spec/fixtures/cnf-testsuite-v1-example.yml") result[:status].success?.should be_true (/Successfully cleaned up/ =~ result[:output]).should_not be_nil + + it "'cnf_setup' should fail if another CNF is already installed", tags: ["setup"] do + begin + result = ShellCmd.cnf_setup("cnf-path=sample-cnfs/sample_coredns/cnf-testsuite.yml") + (/Successfully setup coredns/ =~ result[:output]).should_not be_nil + result = ShellCmd.cnf_setup("cnf-path=sample-cnfs/sample-minimal-cnf/cnf-testsuite.yml") + (/A CNF is already set up. Setting up multiple CNFs is not allowed./ =~ result[:output]).should_not be_nil + ensure + result = ShellCmd.run_testsuite("cnf_cleanup cnf-path=sample-cnfs/sample_coredns/cnf-testsuite.yml") + result[:status].success?.should be_true + (/Successfully cleaned up/ =~ result[:output]).should_not be_nil + result = ShellCmd.run_testsuite("cnf_cleanup cnf-path=sample-cnfs/sample-minimal-cnf/cnf-testsuite.yml") end end end diff --git a/spec/utils/utils_spec.cr b/spec/utils/utils_spec.cr index a7e09c90f..0a8dacb47 100644 --- a/spec/utils/utils_spec.cr +++ b/spec/utils/utils_spec.cr @@ -58,7 +58,6 @@ describe "Utils" do #TODO make CNFManager.sample_setup_args accept the full path to the config yml instead of the directory (check_cnf_config(args)).should eq("./sample-cnfs/sample-generic-cnf") end - it "'upsert_skipped_task' should put a 0 in the results file", tags: ["task_runner"] do CNFManager::Points.clean_results_yml @@ -150,59 +149,6 @@ describe "Utils" do (yaml["exit_code"]).should eq(2) end - it "'all_cnfs_task_runner' should run a test against all cnfs in the cnfs directory if there is not cnf-config argument passed to it", tags: ["task_runner"] do - my_args = Sam::Args.new - ShellCmd.cnf_setup("cnf-path=sample-cnfs/sample-generic-cnf") - ShellCmd.cnf_setup("cnf-path=sample-cnfs/sample_privileged_cnf") - task_response = CNFManager::Task.all_cnfs_task_runner(my_args) do |args, config| - Log.info { "all_cnfs_task_runner spec args #{args.inspect}" } - Log.for("verbose").info { "privileged_containers" } if check_verbose(args) - white_list_container_names = config.common.white_list_container_names - Log.for("verbose").info { "white_list_container_names #{white_list_container_names.inspect}" } if check_verbose(args) - violation_list = [] of String - resource_response = CNFManager.workload_resource_test(args, config) do |resource, container, initialized| - - privileged_list = KubectlClient::Get.privileged_containers - resource_containers = KubectlClient::Get.resource_containers(resource["kind"],resource["name"],resource["namespace"]) - resource_containers_list = (JSON.parse(resource_containers.to_json).as_a).map { |element| element["name"] } - # Only check the containers that are in the deployed helm chart or manifest - (privileged_list & (resource_containers_list - white_list_container_names)).each do |x| - violation_list << x - end - if violation_list.size > 0 - false - else - true - end - end - Log.debug { "violator list: #{violation_list.flatten}" } - emoji_security="" - if resource_response - resp = upsert_passed_task("privileged_containers", "✔️ PASSED: No privileged containers", Time.utc) - else - resp = upsert_failed_task("privileged_containers", "✖️ FAILED: Found #{violation_list.size} privileged containers: #{violation_list.inspect}", Time.utc) - end - resp - end - (task_response).should eq(["✔️ PASSED: No privileged containers", - "✖️ FAILED: Found 1 privileged containers: [\"privileged-coredns\"]"]) - ensure - CNFManager.sample_cleanup(config_file: "sample-cnfs/sample-generic-cnf", verbose: true) - CNFManager.sample_cleanup(config_file: "sample-cnfs/sample_privileged_cnf", verbose: true) - end - - it "'task_runner' should run a test against a single cnf if passed a cnf-config argument even if there are multiple cnfs installed", tags: ["task_runner"] do - ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample-generic-cnf/cnf-testsuite.yml") - ShellCmd.cnf_setup("cnf-config=sample-cnfs/sample_privileged_cnf/cnf-testsuite.yml") - result = ShellCmd.run_testsuite("privileged_containers") - (/(FAILED).*(Found 1 privileged containers)/ =~ result[:output]).should_not be_nil - ensure - result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample-generic-cnf/cnf-testsuite.yml") - result[:status].success?.should be_true - result = ShellCmd.run_testsuite("cnf_cleanup cnf-config=sample-cnfs/sample_privileged_cnf/cnf-testsuite.yml") - result[:status].success?.should be_true - end - it "'logger' command line logger level setting via config.yml", tags: ["logger"] do # NOTE: the config.yml file is in the root of the repo directory. # as written this test depends on they key loglevel being set to 'info' in that config.yml diff --git a/src/tasks/cnf_setup.cr b/src/tasks/cnf_setup.cr index 94005edfc..b04b71941 100644 --- a/src/tasks/cnf_setup.cr +++ b/src/tasks/cnf_setup.cr @@ -10,12 +10,21 @@ task "cnf_setup", ["helm_local_install", "create_namespace"] do |_, args| Log.for("verbose").debug { "args = #{args.inspect}" } if check_verbose(args) cli_hash = CNFManager.sample_setup_cli_args(args) config_file = cli_hash[:config_file] + + # To avoid undefined behavior, only one CNF can be set up at any time. + if CNFManager.cnf_installed? + stdout_warning "A CNF is already set up. Setting up multiple CNFs is not allowed." + stdout_warning "To set up a new CNF, clean up the existing one by running: cnf_cleanup cnf-path=#{CNFManager.cnf_config_list.first}" + exit 0 + end + if ClusterTools.install stdout_success "ClusterTools installed" else stdout_failure "The ClusterTools installation timed out. Please check the status of the cluster-tools pods." exit 1 end + stdout_success "cnf setup start" CNFManager.sample_setup(cli_hash) stdout_success "cnf setup complete"