Skip to content

Commit

Permalink
config: Switch to usage of new config
Browse files Browse the repository at this point in the history
Adapt all usages of config to the new format.
Remove code that was used by old config.
Update all sample and example configs.

Refs: cnti-testcatalog#2135
Signed-off-by: Konstantin Yarovoy <[email protected]>
  • Loading branch information
kosstennbl committed Aug 29, 2024
1 parent 1f28854 commit 5e93cd3
Show file tree
Hide file tree
Showing 30 changed files with 289 additions and 1,172 deletions.
45 changes: 0 additions & 45 deletions spec/setup_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,6 @@ describe "Setup" do
(/Setup complete/ =~ result[:output]).should_not be_nil
end

it "'generate_config' should generate a cnf-testsuite.yml for a helm chart", tags: ["setup-generate"] do
result = ShellCmd.run_testsuite("setup")
result = ShellCmd.run_testsuite("generate_config config-src=stable/coredns output-file=./cnf-testsuite-test.yml")
result[:status].success?.should be_true

yaml = File.open("./cnf-testsuite-test.yml") do |file|
YAML.parse(file)
end
Log.debug { "test yaml: #{yaml}" }
result = ShellCmd.run("cat ./cnf-testsuite-test.yml", force_output: true)
(yaml["helm_chart"] == "stable/coredns").should be_true
ensure
result = ShellCmd.run("rm ./cnf-testsuite-test.yml", force_output: true)
end

it "'generate_config' should generate a cnf-testsuite.yml for a helm directory", tags: ["setup-generate"] do
result = ShellCmd.run_testsuite("setup")
result = ShellCmd.run_testsuite("generate_config config-src=sample-cnfs/k8s-sidecar-container-pattern/chart output-file=./cnf-testsuite-test.yml")
result[:status].success?.should be_true

yaml = File.open("./cnf-testsuite-test.yml") do |file|
YAML.parse(file)
end
Log.debug { "test yaml: #{yaml}" }
result = ShellCmd.run("cat ./cnf-testsuite-test.yml", force_output: true)
(yaml["helm_directory"] == "sample-cnfs/k8s-sidecar-container-pattern/chart").should be_true
ensure
result = ShellCmd.run("rm ./cnf-testsuite-test.yml", force_output: true)
end

it "'generate_config' should generate a cnf-testsuite.yml for a manifest directory", tags: ["setup-generate"] do
result = ShellCmd.run_testsuite("setup")
result = ShellCmd.run_testsuite("generate_config config-src=sample-cnfs/k8s-non-helm/manifests output-file=./cnf-testsuite-test.yml")
result[:status].success?.should be_true

yaml = File.open("./cnf-testsuite-test.yml") do |file|
YAML.parse(file)
end
Log.debug { "test yaml: #{yaml}" }
result = ShellCmd.run("cat ./cnf-testsuite-test.yml", force_output: true)
(yaml["manifest_directory"] == "sample-cnfs/k8s-non-helm/manifests").should be_true
ensure
result = ShellCmd.run("rm ./cnf-testsuite-test.yml", force_output: true)
end

it "'cnf_setup/cnf_cleanup' should install/cleanup with cnf-path arg as alias for cnf-config", tags: ["setup"] do
begin
result = ShellCmd.cnf_setup("cnf-path=example-cnfs/coredns/cnf-testsuite.yml")
Expand Down
7 changes: 0 additions & 7 deletions spec/utils/cnf_install/install_common_spec.cr

This file was deleted.

105 changes: 30 additions & 75 deletions spec/utils/cnf_manager_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ describe "SampleUtils" do
result[:status].success?.should be_true
end

it "'images_from_config_src' should return a list of containers for a cnf", tags: ["cnf-setup"] do
(CNFManager::GenerateConfig.images_from_config_src("stable/coredns").find {|x| x[:image_name] =="coredns/coredns" &&
x[:container_name] =="coredns"}).should be_truthy
end

it "'cnf_setup' should pass with a minimal cnf-testsuite.yml", tags: ["cnf-setup"] do
ShellCmd.cnf_setup("cnf-path=./sample-cnfs/sample-minimal-cnf/ skip_wait_for_install")
ensure
Expand Down Expand Up @@ -161,8 +156,8 @@ describe "SampleUtils" do
args = Sam::Args.new(["cnf-config=./sample-cnfs/sample-generic-cnf/cnf-testsuite.yml", "verbose"])
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(cli_hash[:config_file]))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(cli_hash[:config_file]))
release_name = config.deployments.get_deployment_param(:name)

(Dir.exists? "cnfs/#{release_name}").should be_true
(File.exists?("cnfs/#{release_name}/cnf-testsuite.yml")).should be_true
Expand All @@ -177,8 +172,8 @@ describe "SampleUtils" do
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
# check if directory exists
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.deployments.get_deployment_param(:name)

(Dir.exists? "cnfs/#{release_name}").should be_true
(File.exists?("cnfs/#{release_name}/cnf-testsuite.yml")).should be_true
Expand All @@ -193,8 +188,8 @@ describe "SampleUtils" do
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
# check if directory exists
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.deployments.get_deployment_param(:name)
(Dir.exists? "cnfs/#{release_name}").should be_true
(File.exists?("cnfs/#{release_name}/cnf-testsuite.yml")).should be_true
CNFManager.sample_cleanup(config_file: "sample-cnfs/sample-generic-cnf", verbose: true)
Expand All @@ -207,8 +202,8 @@ describe "SampleUtils" do
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
# check if directory exists
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.deployments.get_deployment_param(:name)
(Dir.exists? "sample-cnfs/sample-generic-cnf").should be_true
(File.exists?("cnfs/#{release_name}/cnf-testsuite.yml")).should be_true
CNFManager.sample_cleanup(config_file: "sample-cnfs/sample-generic-cnf", verbose: true)
Expand All @@ -231,8 +226,8 @@ describe "SampleUtils" do
args = Sam::Args.new(["cnf-config=./#{config_file}/cnf-testsuite.yml", "verbose", "skip_wait_for_install"])
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.deployments.get_deployment_param(:name)
(Dir.exists? "cnfs/#{release_name}").should be_true
# should not clone
(Dir.exists? "cnfs/#{release_name}/privileged-coredns").should be_false
Expand All @@ -249,8 +244,8 @@ describe "SampleUtils" do
Log.info { "Running Setup" }
CNFManager.sample_setup(cli_hash)
Log.info { "Parse Config" }
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.deployments.get_deployment_param(:name)
(Dir.exists? "cnfs/#{release_name}").should be_true
(Dir.exists? "cnfs/#{release_name}/manifests").should be_true
(File.exists? "cnfs/#{release_name}/cnf-testsuite.yml").should be_true
Expand All @@ -261,11 +256,6 @@ describe "SampleUtils" do
(Dir.exists? "cnfs/#{release_name}").should be_false
end

it "'cnf_destination_dir' should return the full path of the potential destination cnf directory based on the deployment name", tags: "WIP" do
args = Sam::Args.new
CNFManager.cnf_destination_dir("spec/fixtures/cnf-testsuite.yml").should contain("/cnfs/coredns")
end

it "'CNFManager.cnf_config_list' should return a list of all of the config files from the cnf directory", tags: ["cnf-setup"] do
config_file = "sample-cnfs/sample-generic-cnf"
args = Sam::Args.new(["cnf-config=./#{config_file}/cnf-testsuite.yml", "verbose", "skip_wait_for_install"])
Expand All @@ -274,8 +264,8 @@ describe "SampleUtils" do
args = Sam::Args.new(["cnf-config=./sample-cnfs/sample_privileged_cnf/cnf-testsuite.yml", "verbose"])
cli_hash = CNFManager.sample_setup_cli_args(args)
CNFManager.sample_setup(cli_hash)
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = config.cnf_config[:release_name]
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
release_name = CNFInstall::Config.parse_cnf_config_from_file
CNFManager.cnf_config_list()[0].should contain("#{release_name}/#{CONFIG_FILE}")
end

Expand All @@ -291,80 +281,45 @@ describe "SampleUtils" do
CNFManager.helm_repo_add("invalid", "invalid").should eq(false)
end

it "'CNFManager.validate_cnf_testsuite_yml' (function) should pass, when a cnf has a valid config file yml", tags: ["validate_config"] do
args = Sam::Args.new(["cnf-config=sample-cnfs/sample-coredns-cnf/cnf-testsuite.yml"])

yml = CNFManager.parsed_config_file(CNFManager.ensure_cnf_testsuite_yml_path(args.named["cnf-config"].as(String)))
Log.info { yml.inspect }
("#{yml.get("release_name").as_s?}").should eq("coredns")

valid, command_output = CNFManager.validate_cnf_testsuite_yml(yml)

(valid).should eq(true)
(command_output).should eq (nil)
end

it "'CNFManager.validate_cnf_testsuite_yml' (command) should pass, when a cnf has a valid config file yml", tags: ["validate_config"] do
result = ShellCmd.run_testsuite("validate_config cnf-config=sample-cnfs/sample-coredns-cnf/cnf-testsuite.yml")
result[:status].success?.should be_true
(/CNF configuration validated/ =~ result[:output]).should_not be_nil
end


it "'CNFManager.validate_cnf_testsuite_yml' (function) should warn, but be valid when a cnf config file yml has fields that are not a part of the validation type", tags: ["validate_config"] do
args = Sam::Args.new(["cnf-config=./spec/fixtures/cnf-testsuite-unmapped-keys-and-subkeys.yml"])

yml = CNFManager.parsed_config_file(CNFManager.ensure_cnf_testsuite_yml_path(args.named["cnf-config"].as(String)))
Log.info { yml.inspect }
("#{yml.get("release_name").as_s?}").should eq("coredns")

status, warning_output = CNFManager.validate_cnf_testsuite_yml(yml)

Log.warn { "WARNING: #{warning_output}" }

(status).should eq(true)
(warning_output).should_not be_nil
end


it "'CNFManager.validate_cnf_testsuite_yml' (command) should warn, but be valid when a cnf config file yml has fields that are not a part of the validation type", tags: ["validate_config"] do
result = ShellCmd.run_testsuite("validate_config cnf-config=spec/fixtures/cnf-testsuite-unmapped-keys-and-subkeys.yml")
it "'validate_config' should pass, when a cnf has a valid config file yml", tags: ["validate_config"] do
result = ShellCmd.run_testsuite("validate_config cnf-config=embedded_files/cnf-testsuite-v2-example.yml")
result[:status].success?.should be_true
Log.debug { "validate_config resp: #{result[:output]}" }
(/CNF configuration validated/ =~ result[:output]).should_not be_nil
(/Successfully validated CNF config/ =~ result[:output]).should_not be_nil
end


it "'CNFManager.validate_cnf_testsuite_yml' (command) should pass, for all sample-cnfs", tags: ["validate_config"] do
it "'validate_config' should pass, for all sample-cnfs", tags: ["validate_config"] do

get_dirs = Dir.entries("sample-cnfs")
dir_list = get_dirs - [".", ".."]
dir_list.each do |dir|
testsuite_yml = "sample-cnfs/#{dir}/cnf-testsuite.yml"
result = ShellCmd.run_testsuite("validate_config cnf-config=#{testsuite_yml}")
(/CNF configuration validated/ =~ result[:output]).should_not be_nil
unless result[:status].success?
Log.info {"Could not validate config: #{testsuite_yml}"}
end
(/Successfully validated CNF config/ =~ result[:output]).should_not be_nil
end
end

it "'CNFManager.validate_cnf_testsuite_yml' (command) should pass, for all example-cnfs", tags: ["validate_config"] do
it "'validate_config' should pass, for all example-cnfs", tags: ["validate_config"] do

get_dirs = Dir.entries("example-cnfs")
dir_list = get_dirs - [".", ".."]
dir_list.each do |dir|
testsuite_yml = "example-cnfs/#{dir}/cnf-testsuite.yml"
result = ShellCmd.run_testsuite("validate_config cnf-config=#{testsuite_yml}")
if (/Critical Error with CNF Configuration. Please review USAGE.md for steps to set up a valid CNF configuration file/ =~ result[:output])
Log.info { "\n #{testsuite_yml}: #{result[:output]}" }
unless result[:status].success?
Log.info {"Could not validate config: #{testsuite_yml}"}
end
(/CNF configuration validated/ =~ result[:output]).should_not be_nil
(/Successfully validated CNF config/ =~ result[:output]).should_not be_nil
end
end


it "'CNFManager::Config#parse_config_yml' should return a populated CNFManager::Config.cnf_config", tags: ["cnf-config"] do
it "'CNFInstall::Config.parse_cnf_config_from_file' should return a populated CNFInstall::Config::Config", tags: ["cnf-config"] do
begin
yaml = CNFManager::Config.parse_config_yml("spec/fixtures/cnf-testsuite.yml")
(yaml.cnf_config[:release_name]).should eq("coredns")
config = CNFInstall::Config.parse_cnf_config_from_file("spec/fixtures/cnf-testsuite.yml")
(config.deployments.get_deployment_param(:name)).should eq("coredns")
ensure
end
end
Expand All @@ -373,7 +328,7 @@ describe "SampleUtils" do
args = Sam::Args.new(["cnf-config=./sample-cnfs/sample-generic-cnf/cnf-testsuite.yml"])
cli_hash = CNFManager.sample_setup_cli_args(args, false)
CNFManager.sample_setup(cli_hash) if cli_hash["config_file"]
config = CNFManager::Config.parse_config_yml("./sample-cnfs/sample-generic-cnf/cnf-testsuite.yml")
config = CNFInstall::Config.parse_cnf_config_from_file("./sample-cnfs/sample-generic-cnf/cnf-testsuite.yml")
task_response = CNFManager.workload_resource_test(args, config) do |resource, container, initialized|
test_passed = true
begin
Expand Down
8 changes: 4 additions & 4 deletions spec/utils/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe "Utils" do

Log.info { "single_task_runner spec args #{args.inspect}" }

white_list_container_names = config.cnf_config[:white_list_container_names]
white_list_container_names = config.common.white_list_container_names
Log.info { "white_list_container_names #{white_list_container_names.inspect}" }
violation_list = [] of String
resource_response = CNFManager.workload_resource_test(args, config) do |resource, container, initialized|
Expand Down Expand Up @@ -123,8 +123,8 @@ describe "Utils" do
task_response = CNFManager::Task.single_task_runner(args) do
cdir = FileUtils.pwd()
response = String::Builder.new
config = CNFManager.parsed_config_file(CNFManager.ensure_cnf_testsuite_yml_path(args.named["cnf-config"].as(String)))
helm_directory = "#{config.get("helm_directory").as_s?}"
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(args.named["cnf-config"].as(String)))
helm_directory = config.deployments.get_deployment_param(:helm_directory)
if File.directory?(CNFManager.ensure_cnf_testsuite_dir(args.named["cnf-config"].as(String)) + helm_directory)
Dir.cd(CNFManager.ensure_cnf_testsuite_dir(args.named["cnf-config"].as(String)) + helm_directory)
Process.run("grep -r -P '^(?!.+0\.0\.0\.0)(?![[:space:]]*0\.0\.0\.0)(?!#)(?![[:space:]]*#)(?!\/\/)(?![[:space:]]*\/\/)(?!\/\\*)(?![[:space:]]*\/\\*)(.+([0-9]{1,3}[\.]){3}[0-9]{1,3})'", shell: true) do |proc|
Expand Down Expand Up @@ -157,7 +157,7 @@ describe "Utils" do
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.cnf_config[:white_list_container_names]
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|
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/cleanup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ task "samples_cleanup" do |_, args|
cnf_config_file = task_args["cnf-config"].as(String)
cnf_config_file = CNFManager.ensure_cnf_testsuite_yml_path(cnf_config_file)

config = CNFManager.parsed_config_file(cnf_config_file)
install_method = CNFInstall.cnf_installation_method(config)
config = CNFInstall::Config.parse_cnf_config_from_file(cnf_config_file)
install_method = config.dynamic.install_method
if install_method[0] == CNFInstall::InstallMethod::ManifestDirectory
installed_from_manifest = true
else
Expand Down
22 changes: 2 additions & 20 deletions src/tasks/cnf_setup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ task "cnf_cleanup" do |_, args|
else
force = false
end
config = CNFManager.parsed_config_file(CNFManager.ensure_cnf_testsuite_yml_path(cnf))
install_method = CNFInstall.cnf_installation_method(config)
config = CNFInstall::Config.parse_cnf_config_from_file(CNFManager.ensure_cnf_testsuite_yml_path(cnf))
install_method = config.dynamic.install_method
if install_method[0] == CNFInstall::InstallMethod::ManifestDirectory
installed_from_manifest = true
else
Expand All @@ -58,24 +58,6 @@ task "CNFManager.helm_repo_add" do |_, args|

end

task "generate_config" do |_, args|
Log.for("verbose").info { "CNFManager.generate_config" } if check_verbose(args)
Log.for("verbose").debug { "args = #{args.inspect}" } if check_verbose(args)
if args.named["config-src"]?
config_src = args.named["config-src"].as(String)
output_file = args.named["output-file"].as(String) if args.named["output-file"]?
output_file = args.named["of"].as(String) if args.named["of"]?
if output_file && !output_file.empty?
Log.info { "generating config with an output file" }
CNFManager::GenerateConfig.generate_config(config_src, output_file)
else
Log.info { "generating config without an output file" }
CNFManager::GenerateConfig.generate_config(config_src)
end
end

end

#TODO force all cleanups to use generic cleanup
task "bad_helm_cnf_cleanup" do |_, args|
CNFManager.sample_cleanup(config_file: "sample-cnfs/sample-bad_helm_coredns-cnf", verbose: true)
Expand Down
1 change: 1 addition & 0 deletions src/tasks/constants.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ IGNORED_SECRET_TYPES = ["kubernetes.io/service-account-token", "kubernetes.io/do
EMPTY_JSON = JSON.parse(%({}))
EMPTY_JSON_ARRAY = JSON.parse(%([]))
SPECIALIZED_INIT_SYSTEMS = ["tini", "dumb-init", "s6-svscan"]
ROLLING_VERSION_CHANGE_TEST_NAMES = ["rolling_update", "rolling_downgrade", "rolling_version_change"]

TESTSUITE_NAMESPACE = "cnf-testsuite"
DEFAULT_CNF_NAMESPACE = "cnf-default"
Expand Down
10 changes: 6 additions & 4 deletions src/tasks/setup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ task "configuration_file_setup" do |_, args|
CNFManager::Points.create_points_yml
end

task "test_config" do |_, args|
if args.named["cfg"]?
puts CNFInstall::Config.parse_cnf_config_from_file(args.named["cfg"].to_s).inspect
task "validate_config" do |_, args|
if args.named["cnf-config"]?
config = CNFInstall::Config.parse_cnf_config_from_file(args.named["cnf-config"].to_s)
puts "Successfully validated CNF config"
Log.info {"Config: #{config.inspect}"}
else
stdout_failure "cfg parameter needed"
stdout_failure "cnf-config parameter needed"
exit 1
end
end
Loading

0 comments on commit 5e93cd3

Please sign in to comment.