|
| 1 | +#[cfg(test)] |
| 2 | +use pretty_assertions::assert_eq; |
| 3 | + |
| 4 | +use crate::init::{blank_init, example_init, interactive_init, templatize_init}; |
| 5 | + |
| 6 | +#[test] |
| 7 | +/// Config template renders correctly with blank init |
| 8 | +fn blank_init_rendering() { |
| 9 | + let blank = blank_init(); |
| 10 | + |
| 11 | + let rendered = templatize_init(&blank); |
| 12 | + assert!(rendered.is_ok(), "blank template failed to render"); |
| 13 | + assert_eq!( |
| 14 | + rendered.unwrap(), |
| 15 | + r#"# Used to check that all challenges' flags are in the correct format, |
| 16 | +# and by the scoreboard frontend as a first check for invalid submissions. |
| 17 | +flag_regex: "" |
| 18 | +
|
| 19 | +# Registry configuration for challenge images. |
| 20 | +registry: |
| 21 | + domain: "" |
| 22 | + # This is the default tag format; it will create a separate image for each |
| 23 | + # challenge pod. Most container registries (Docker, GHCR, Gitlab, Quay, ...) |
| 24 | + # are fine with this. If you are using a container registry that requires |
| 25 | + # every image within the repository to be created ahead-of-time (AWS ECR) |
| 26 | + # before it can be pushed, you can change this to use tags for each separate |
| 27 | + # challenge within one image in the registry. |
| 28 | + tag_format: "" |
| 29 | + # Build-time credentials used to push images during `beavercds deploy`. |
| 30 | + build: |
| 31 | + user: "" |
| 32 | + pass: "" |
| 33 | + # Used by the cluster to pull the built images. |
| 34 | + cluster: |
| 35 | + user: "" |
| 36 | + pass: "" |
| 37 | +
|
| 38 | +# Default difficulty class and resource requests used for challenges that did |
| 39 | +# not set their own. |
| 40 | +defaults: |
| 41 | + difficulty: "" |
| 42 | + resources: { cpu: 0, memory: "" } |
| 43 | +
|
| 44 | +# The list of different difficulties that challenges can be assigned, and how |
| 45 | +# many points challenges of that difficulty class should be worth. All |
| 46 | +# challenges use dynamic scoring; for static points set both min and max to the |
| 47 | +# same value. |
| 48 | +points: |
| 49 | + [] |
| 50 | +
|
| 51 | +# Control what challenges are deployed in each environment profile. |
| 52 | +deploy: |
| 53 | + {} |
| 54 | +
|
| 55 | +# Separate environment profiles to allow for multiple independent deployments |
| 56 | +# of challenges, e.g. staging and production to test challenges internally |
| 57 | +# before going live for all users. |
| 58 | +profiles: |
| 59 | + {} |
| 60 | +"# |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +/// Config template renders correctly with example values |
| 66 | +fn example_init_rendering() { |
| 67 | + let examples = example_init(); |
| 68 | + |
| 69 | + let rendered = templatize_init(&examples); |
| 70 | + assert!(rendered.is_ok(), "example template failed to render"); |
| 71 | + assert_eq!( |
| 72 | + rendered.unwrap(), |
| 73 | + r#"# Used to check that all challenges' flags are in the correct format, |
| 74 | +# and by the scoreboard frontend as a first check for invalid submissions. |
| 75 | +flag_regex: "ctf{.*}" |
| 76 | +
|
| 77 | +# Registry configuration for challenge images. |
| 78 | +registry: |
| 79 | + domain: "ghcr.io/youraccount" |
| 80 | + # This is the default tag format; it will create a separate image for each |
| 81 | + # challenge pod. Most container registries (Docker, GHCR, Gitlab, Quay, ...) |
| 82 | + # are fine with this. If you are using a container registry that requires |
| 83 | + # every image within the repository to be created ahead-of-time (AWS ECR) |
| 84 | + # before it can be pushed, you can change this to use tags for each separate |
| 85 | + # challenge within one image in the registry. |
| 86 | + tag_format: "{{domain}}/{{challenge}}-{{container}}:{{profile}}" |
| 87 | + # Build-time credentials used to push images during `beavercds deploy`. |
| 88 | + build: |
| 89 | + user: "build_user" |
| 90 | + pass: "notrealcreds" |
| 91 | + # Used by the cluster to pull the built images. |
| 92 | + cluster: |
| 93 | + user: "cluster_user" |
| 94 | + pass: "alsofake" |
| 95 | +
|
| 96 | +# Default difficulty class and resource requests used for challenges that did |
| 97 | +# not set their own. |
| 98 | +defaults: |
| 99 | + difficulty: "easy" |
| 100 | + resources: { cpu: 1, memory: "500M" } |
| 101 | +
|
| 102 | +# The list of different difficulties that challenges can be assigned, and how |
| 103 | +# many points challenges of that difficulty class should be worth. All |
| 104 | +# challenges use dynamic scoring; for static points set both min and max to the |
| 105 | +# same value. |
| 106 | +points: |
| 107 | + - difficulty: "easy" |
| 108 | + min: 200 |
| 109 | + max: 500 |
| 110 | + - difficulty: "hard" |
| 111 | + min: 300 |
| 112 | + max: 600 |
| 113 | +
|
| 114 | +# Control what challenges are deployed in each environment profile. |
| 115 | +deploy: |
| 116 | + default: {} |
| 117 | +
|
| 118 | +# Separate environment profiles to allow for multiple independent deployments |
| 119 | +# of challenges, e.g. staging and production to test challenges internally |
| 120 | +# before going live for all users. |
| 121 | +profiles: |
| 122 | + default: |
| 123 | + # Used to push challenge information into the frontend/scoreboard. |
| 124 | + frontend_url: "https://ctf.coolguy.invalid" |
| 125 | + frontend_token: "secretsecretsecret" |
| 126 | + # Root domain to expose all challenges under. |
| 127 | + challenges_domain: "chals.coolguy.invalid" |
| 128 | + # Kubernetes kubeconfig and context name of cluster for this profile. |
| 129 | + kubecontext: "ctf-cluster" |
| 130 | + # Credentials for the public challenge file asset bucket. |
| 131 | + s3: |
| 132 | + bucket_name: "ctf-bucket" |
| 133 | + endpoint: "s3.coolguy.invalid" |
| 134 | + region: "us-west-2" |
| 135 | + access_key: "accesskey" |
| 136 | + secret_key: "secretkey" |
| 137 | + # Config for the environment's external-dns deployment. |
| 138 | + dns: |
| 139 | + # Place external-dns configuration options here; |
| 140 | + # this yaml will be passed directly to external-dns without modification |
| 141 | + # Reference: https://github.com/bitnami/charts/tree/main/bitnami/external-dns |
| 142 | +"# |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +#[test] |
| 147 | +/// Config template renders correctly with faked interactive input |
| 148 | +fn interactive_init_rendering() { |
| 149 | + println!("TODO: interactive testing not implemented"); |
| 150 | + |
| 151 | + // TODO: how to test the prompts? inquire does not offer a test mode, see |
| 152 | + // https://github.com/mikaelmello/inquire/issues/71. |
| 153 | + // Possible solution: shell out and test output via https://github.com/rust-cli/rexpect |
| 154 | + |
| 155 | + // let interactive = interactive_init(); |
| 156 | + // assert!(interactive.is_ok(), "interactive prompts failed in testing"); |
| 157 | + |
| 158 | + // let rendered = templatize_init(&interactive.unwrap()); |
| 159 | + // assert!(rendered.is_ok(), "example template failed to render"); |
| 160 | + // assert_eq!(rendered.unwrap(), ""); |
| 161 | +} |
0 commit comments