From 177762af44a5a470376b8cca24af0b92fa787c6e Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 14 Mar 2022 12:24:07 -0500 Subject: [PATCH] mock: Add support for setting job variables This refactors job variables to be built on top of a new MockJobVariable struct, allowing them to now be added by the API user. Signed-off-by: Ryan Gonzalez --- gitlab-runner-mock/src/api/request.rs | 79 +---- gitlab-runner-mock/src/job.rs | 34 ++ gitlab-runner-mock/src/lib.rs | 1 + gitlab-runner-mock/src/variables.rs | 463 ++++++++++++++++++++++++++ gitlab-runner/tests/integration.rs | 23 +- 5 files changed, 521 insertions(+), 79 deletions(-) create mode 100644 gitlab-runner-mock/src/variables.rs diff --git a/gitlab-runner-mock/src/api/request.rs b/gitlab-runner-mock/src/api/request.rs index 0bfec35..a131584 100644 --- a/gitlab-runner-mock/src/api/request.rs +++ b/gitlab-runner-mock/src/api/request.rs @@ -61,83 +61,6 @@ impl Respond for JobRequestResponder { if let Some(job) = self.mock.grab_pending_job() { job.update_state(MockJobState::Running); - let variables = json!([ - { "key": "CI_PIPELINE_ID", "value": "120", "public": true, "masked": false }, - { "key": "CI_PIPELINE_URL", "value": "https://gitlab.example.com/test/gitlab-test/-/pipelines/120", "public": true, "masked": false }, - { "key": "CI_JOB_ID", "value": job.id().to_string(), "public": true, "masked": false }, - { "key": "CI_JOB_URL", "value": "https://gitlab.example.com/test/gitlab-test/-/jobs/265", "public": true, "masked": false }, - { "key": "CI_JOB_TOKEN", "value": "tokn", "public": false, "masked": true }, - { "key": "CI_JOB_STARTED_AT", "value": "2021-05-05T17:16:32Z", "public": true, "masked": false }, - { "key": "CI_BUILD_ID", "value": "265", "public": true, "masked": false }, - { "key": "CI_BUILD_TOKEN", "value": "tokn", "public": false, "masked": true }, - { "key": "CI_REGISTRY_USER", "value": "gitlab-ci-token", "public": true, "masked": false }, - { "key": "CI_REGISTRY_PASSWORD", "value": "token", "public": false, "masked": true }, - { "key": "CI_REPOSITORY_URL", "value": "https://gitlab-ci-token:token@gitlab.example.com/test/gitlab-test.git", "public": false, "masked": false }, - { "key": "CI_DEPENDENCY_PROXY_USER", "value": "gitlab-ci-token", "public": true, "masked": false }, - { "key": "CI_DEPENDENCY_PROXY_PASSWORD", "value": "token", "public": false, "masked": true }, - { "key": "CI_JOB_NAME", "value": "custom", "public": true, "masked": false }, - { "key": "CI_JOB_STAGE", "value": "build", "public": true, "masked": false }, - { "key": "CI_NODE_TOTAL", "value": "1", "public": true, "masked": false }, - { "key": "CI_BUILD_NAME", "value": "custom", "public": true, "masked": false }, - { "key": "CI_BUILD_STAGE", "value": "build", "public": true, "masked": false }, - { "key": "GITLAB_CI", "value": "true", "public": true, "masked": false }, - { "key": "CI_SERVER_URL", "value": "https://gitlab.example.com", "public": true, "masked": false }, - { "key": "CI_SERVER_HOST", "value": "gitlab.example.com", "public": true, "masked": false }, - { "key": "CI_SERVER_PORT", "value": "443", "public": true, "masked": false }, - { "key": "CI_SERVER_PROTOCOL", "value": "https", "public": true, "masked": false }, - { "key": "CI_SERVER_NAME", "value": "GitLab", "public": true, "masked": false }, - { "key": "CI_SERVER_VERSION", "value": "13.11.3", "public": true, "masked": false }, - { "key": "CI_SERVER_VERSION_MAJOR", "value": "13", "public": true, "masked": false }, - { "key": "CI_SERVER_VERSION_MINOR", "value": "11", "public": true, "masked": false }, - { "key": "CI_SERVER_VERSION_PATCH", "value": "3", "public": true, "masked": false }, - { "key": "CI_SERVER_REVISION", "value": "b321336e443", "public": true, "masked": false }, - { "key": "GITLAB_FEATURES", "value": "", "public": true, "masked": false }, - { "key": "CI_PROJECT_ID", "value": "4", "public": true, "masked": false }, - { "key": "CI_PROJECT_NAME", "value": "gitlab-test", "public": true, "masked": false }, - { "key": "CI_PROJECT_TITLE", "value": "gitlab-test", "public": true, "masked": false }, - { "key": "CI_PROJECT_PATH", "value": "test/gitlab-test", "public": true, "masked": false }, - { "key": "CI_PROJECT_PATH_SLUG", "value": "test-gitlab-test", "public": true, "masked": false }, - { "key": "CI_PROJECT_NAMESPACE", "value": "test", "public": true, "masked": false }, - { "key": "CI_PROJECT_ROOT_NAMESPACE", "value": "test", "public": true, "masked": false }, - { "key": "CI_PROJECT_URL", "value": "https://gitlab.example.com/test/gitlab-test", "public": true, "masked": false }, - { "key": "CI_PROJECT_VISIBILITY", "value": "private", "public": true, "masked": false }, - { "key": "CI_PROJECT_REPOSITORY_LANGUAGES", "value": "dockerfile,ocaml", "public": true, "masked": false }, - { "key": "CI_DEFAULT_BRANCH", "value": "master", "public": true, "masked": false }, - { "key": "CI_PROJECT_CONFIG_PATH", "value": ".gitlab-ci.yml", "public": true, "masked": false }, - { "key": "CI_CONFIG_PATH", "value": ".gitlab-ci.yml", "public": true, "masked": false }, - { "key": "CI_PAGES_DOMAIN", "value": "example.com", "public": true, "masked": false }, - { "key": "CI_PAGES_URL", "value": "http://test.example.com/gitlab-test", "public": true, "masked": false }, { "key": "CI_REGISTRY", "value": "registry.example.com", "public": true, "masked": false }, - { "key": "CI_REGISTRY_IMAGE", "value": "registry.example.com/test/gitlab-test", "public": true, "masked": false }, - { "key": "CI_DEPENDENCY_PROXY_SERVER", "value": "gitlab.example.com:443", "public": true, "masked": false }, - { "key": "CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX", "value": "gitlab.example.com:443/test/dependency_proxy/containers", "public": true, "masked": false }, - { "key": "CI_API_V4_URL", "value": "https://gitlab.example.com/api/v4", "public": true, "masked": false }, - { "key": "CI_PIPELINE_IID", "value": "33", "public": true, "masked": false }, - { "key": "CI_PIPELINE_SOURCE", "value": "push", "public": true, "masked": false }, - { "key": "CI_PIPELINE_CREATED_AT", "value": "2021-05-05T10:20:00Z", "public": true, "masked": false }, - { "key": "CI_COMMIT_SHA", "value": "265c14cf140a66cfc61e40e4ab45c95ba8df5ed1", "public": true, "masked": false }, - { "key": "CI_COMMIT_SHORT_SHA", "value": "265c14cf", "public": true, "masked": false }, - { "key": "CI_COMMIT_BEFORE_SHA", "value": "fc40ad32cdd36b814f07a540605110edc209a38c", "public": true, "masked": false }, - { "key": "CI_COMMIT_REF_NAME", "value": "custom", "public": true, "masked": false }, - { "key": "CI_COMMIT_REF_SLUG", "value": "custom", "public": true, "masked": false }, - { "key": "CI_COMMIT_BRANCH", "value": "custom", "public": true, "masked": false }, - { "key": "CI_COMMIT_MESSAGE", "value": "test rust runner\n", "public": true, "masked": false }, - { "key": "CI_COMMIT_TITLE", "value": "test rust runner", "public": true, "masked": false }, - { "key": "CI_COMMIT_DESCRIPTION", "value": "", "public": true, "masked": false }, - { "key": "CI_COMMIT_REF_PROTECTED", "value": "false", "public": true, "masked": false }, - { "key": "CI_COMMIT_TIMESTAMP", "value": "2021-05-05T12:19:53+02:00", "public": true, "masked": false }, - { "key": "CI_COMMIT_AUTHOR", "value": "Test User ", "public": true, "masked": false }, - { "key": "CI_BUILD_REF", "value": "265c14cf140a66cfc61e40e4ab45c95ba8df5ed1", "public": true, "masked": false }, - { "key": "CI_BUILD_BEFORE_SHA", "value": "fc40ad32cdd36b814f07a540605110edc209a38c", "public": true, "masked": false }, - { "key": "CI_BUILD_REF_NAME", "value": "custom", "public": true, "masked": false }, - { "key": "CI_BUILD_REF_SLUG", "value": "custom", "public": true, "masked": false }, - { "key": "CI_RUNNER_ID", "value": "8", "public": true, "masked": false }, - { "key": "CI_RUNNER_DESCRIPTION", "value": "Rust runner test", "public": true, "masked": false }, - { "key": "CI_RUNNER_TAGS", "value": "rust-runner", "public": true, "masked": false }, - { "key": "GITLAB_USER_ID", "value": "2", "public": true, "masked": false }, - { "key": "GITLAB_USER_EMAIL", "value": "test@example.com", "public": true, "masked": false }, - { "key": "GITLAB_USER_LOGIN", "value": "test", "public": true, "masked": false }, - { "key": "GITLAB_USER_NAME", "value": "Test User", "public": true, "masked": false } - ]); let dependencies: Vec<_> = job .dependencies() .iter() @@ -201,7 +124,7 @@ impl Respond for JobRequestResponder { "timeout": 3600, "runner_session_url": null }, - "variables": variables, + "variables": job.variables(), "steps": job.steps(), "image": null, "services": [], diff --git a/gitlab-runner-mock/src/job.rs b/gitlab-runner-mock/src/job.rs index 73f4a08..def2042 100644 --- a/gitlab-runner-mock/src/job.rs +++ b/gitlab-runner-mock/src/job.rs @@ -1,9 +1,12 @@ use serde::Deserialize; use serde::Serialize; +use std::collections::HashMap; use std::sync::Arc; use std::sync::Mutex; use thiserror::Error; +use crate::variables::default_job_variables; + #[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "lowercase")] pub enum MockJobState { @@ -36,6 +39,14 @@ pub(crate) struct MockJobInner { log_patches: u32, } +#[derive(Clone, Serialize, Debug)] +pub struct MockJobVariable { + pub key: String, + pub value: String, + pub public: bool, + pub masked: bool, +} + #[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)] #[serde(rename_all = "snake_case")] pub enum MockJobStepWhen { @@ -84,6 +95,7 @@ pub struct MockJob { name: String, id: u64, token: String, + variables: Vec, steps: Vec, dependencies: Vec, artifacts: Vec, @@ -124,6 +136,10 @@ impl MockJob { &self.artifacts } + pub fn variables(&self) -> &[MockJobVariable] { + &self.variables + } + pub fn steps(&self) -> &[MockJobStep] { &self.steps } @@ -202,6 +218,7 @@ impl MockJob { pub struct MockJobBuilder { name: String, id: u64, + variables: HashMap, steps: Vec, dependencies: Vec, artifacts: Vec, @@ -212,10 +229,26 @@ impl MockJobBuilder { Self { name, id, + variables: default_job_variables(id) + .into_iter() + .map(|v| (v.key.clone(), v)) + .collect(), ..Default::default() } } + pub fn add_variable(&mut self, key: String, value: String, public: bool, masked: bool) { + self.variables.insert( + key.clone(), + MockJobVariable { + key, + value, + public, + masked, + }, + ); + } + pub fn add_step( &mut self, name: MockJobStepName, @@ -293,6 +326,7 @@ impl MockJobBuilder { id: self.id, token: format!("job-token-{}", self.id), steps: self.steps, + variables: self.variables.into_values().collect(), dependencies: self.dependencies, artifacts: self.artifacts, inner, diff --git a/gitlab-runner-mock/src/lib.rs b/gitlab-runner-mock/src/lib.rs index aeec508..8824f39 100644 --- a/gitlab-runner-mock/src/lib.rs +++ b/gitlab-runner-mock/src/lib.rs @@ -8,6 +8,7 @@ use wiremock::matchers::{body_json_schema, method, path, path_regex}; use wiremock::{Mock, MockServer}; mod job; +mod variables; pub use job::{ MockJob, MockJobArtifactWhen, MockJobBuilder, MockJobState, MockJobStepName, MockJobStepWhen, }; diff --git a/gitlab-runner-mock/src/variables.rs b/gitlab-runner-mock/src/variables.rs new file mode 100644 index 0000000..659cdcc --- /dev/null +++ b/gitlab-runner-mock/src/variables.rs @@ -0,0 +1,463 @@ +use crate::job::MockJobVariable; + +pub fn default_job_variables(job_id: u64) -> Vec { + vec![ + MockJobVariable { + key: "CI_PIPELINE_ID".to_owned(), + value: "120".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PIPELINE_URL".to_owned(), + value: "https://gitlab.example.com/test/gitlab-test/-/pipelines/120".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_JOB_ID".to_owned(), + value: job_id.to_string(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_JOB_URL".to_owned(), + value: "https://gitlab.example.com/test/gitlab-test/-/jobs/265".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_JOB_TOKEN".to_owned(), + value: "tokn".to_owned(), + public: false, + masked: true, + }, + MockJobVariable { + key: "CI_JOB_STARTED_AT".to_owned(), + value: "2021-05-05T17:16:32Z".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_ID".to_owned(), + value: "265".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_TOKEN".to_owned(), + value: "tokn".to_owned(), + public: false, + masked: true, + }, + MockJobVariable { + key: "CI_REGISTRY_USER".to_owned(), + value: "gitlab-ci-token".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_REGISTRY_PASSWORD".to_owned(), + value: "token".to_owned(), + public: false, + masked: true, + }, + MockJobVariable { + key: "CI_REPOSITORY_URL".to_owned(), + value: "https://gitlab-ci-token:token@gitlab.example.com/test/gitlab-test.git" + .to_owned(), + public: false, + masked: false, + }, + MockJobVariable { + key: "CI_DEPENDENCY_PROXY_USER".to_owned(), + value: "gitlab-ci-token".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_DEPENDENCY_PROXY_PASSWORD".to_owned(), + value: "token".to_owned(), + public: false, + masked: true, + }, + MockJobVariable { + key: "CI_JOB_NAME".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_JOB_STAGE".to_owned(), + value: "build".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_NODE_TOTAL".to_owned(), + value: "1".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_NAME".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_STAGE".to_owned(), + value: "build".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_CI".to_owned(), + value: "true".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_URL".to_owned(), + value: "https://gitlab.example.com".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_HOST".to_owned(), + value: "gitlab.example.com".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_PORT".to_owned(), + value: "443".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_PROTOCOL".to_owned(), + value: "https".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_NAME".to_owned(), + value: "GitLab".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_VERSION".to_owned(), + value: "13.11.3".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_VERSION_MAJOR".to_owned(), + value: "13".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_VERSION_MINOR".to_owned(), + value: "11".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_VERSION_PATCH".to_owned(), + value: "3".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_SERVER_REVISION".to_owned(), + value: "b321336e443".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_FEATURES".to_owned(), + value: "".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_ID".to_owned(), + value: "4".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_NAME".to_owned(), + value: "gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_TITLE".to_owned(), + value: "gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_PATH".to_owned(), + value: "test/gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_PATH_SLUG".to_owned(), + value: "test-gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_NAMESPACE".to_owned(), + value: "test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_ROOT_NAMESPACE".to_owned(), + value: "test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_URL".to_owned(), + value: "https://gitlab.example.com/test/gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_VISIBILITY".to_owned(), + value: "private".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_REPOSITORY_LANGUAGES".to_owned(), + value: "dockerfile,ocaml".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_DEFAULT_BRANCH".to_owned(), + value: "master".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PROJECT_CONFIG_PATH".to_owned(), + value: ".gitlab-ci.yml".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_CONFIG_PATH".to_owned(), + value: ".gitlab-ci.yml".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PAGES_DOMAIN".to_owned(), + value: "example.com".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PAGES_URL".to_owned(), + value: "http://test.example.com/gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_REGISTRY".to_owned(), + value: "registry.example.com".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_REGISTRY_IMAGE".to_owned(), + value: "registry.example.com/test/gitlab-test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_DEPENDENCY_PROXY_SERVER".to_owned(), + value: "gitlab.example.com:443".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX".to_owned(), + value: "gitlab.example.com:443/test/dependency_proxy/containers".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_API_V4_URL".to_owned(), + value: "https://gitlab.example.com/api/v4".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PIPELINE_IID".to_owned(), + value: "33".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PIPELINE_SOURCE".to_owned(), + value: "push".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_PIPELINE_CREATED_AT".to_owned(), + value: "2021-05-05T10:20:00Z".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_SHA".to_owned(), + value: "265c14cf140a66cfc61e40e4ab45c95ba8df5ed1".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_SHORT_SHA".to_owned(), + value: "265c14cf".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_BEFORE_SHA".to_owned(), + value: "fc40ad32cdd36b814f07a540605110edc209a38c".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_REF_NAME".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_REF_SLUG".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_BRANCH".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_MESSAGE".to_owned(), + value: "test rust runner\n".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_TITLE".to_owned(), + value: "test rust runner".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_DESCRIPTION".to_owned(), + value: "".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_REF_PROTECTED".to_owned(), + value: "false".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_TIMESTAMP".to_owned(), + value: "2021-05-05T12:19:53+02:00".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_COMMIT_AUTHOR".to_owned(), + value: "Test User ".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_REF".to_owned(), + value: "265c14cf140a66cfc61e40e4ab45c95ba8df5ed1".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_BEFORE_SHA".to_owned(), + value: "fc40ad32cdd36b814f07a540605110edc209a38c".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_REF_NAME".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_BUILD_REF_SLUG".to_owned(), + value: "custom".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_RUNNER_ID".to_owned(), + value: "8".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_RUNNER_DESCRIPTION".to_owned(), + value: "Rust runner test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "CI_RUNNER_TAGS".to_owned(), + value: "rust-runner".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_USER_ID".to_owned(), + value: "2".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_USER_EMAIL".to_owned(), + value: "test@example.com".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_USER_LOGIN".to_owned(), + value: "test".to_owned(), + public: true, + masked: false, + }, + MockJobVariable { + key: "GITLAB_USER_NAME".to_owned(), + value: "Test User".to_owned(), + public: true, + masked: false, + }, + ] +} diff --git a/gitlab-runner/tests/integration.rs b/gitlab-runner/tests/integration.rs index a66f9ad..c54d49a 100644 --- a/gitlab-runner/tests/integration.rs +++ b/gitlab-runner/tests/integration.rs @@ -587,8 +587,23 @@ async fn runner_delay() { #[tokio::test] async fn job_variables() { + const TEST_VARIABLE: &str = "TEST_VARIABLE"; + const TEST_VALUE: &str = "a testing value"; + let mock = GitlabRunnerMock::start().await; - let job = mock.add_dummy_job("variables".to_string()); + let mut builder = mock.job_builder("variables".to_string()); + + builder.add_step( + MockJobStepName::Script, + vec!["dummy".to_owned()], + 3600, + MockJobStepWhen::OnSuccess, + false, + ); + builder.add_variable(TEST_VARIABLE.to_owned(), TEST_VALUE.to_owned(), false, true); + + let job = builder.build(); + mock.enqueue_job(job.clone()); let dir = tempfile::tempdir().unwrap(); let (mut runner, layer) = Runner::new_with_layer( @@ -605,6 +620,12 @@ async fn job_variables() { assert_eq!(job.id(), id.value().parse::().unwrap()); assert!(id.public()); assert!(!id.masked()); + + let test = job.variable(TEST_VARIABLE).unwrap(); + assert_eq!(test.value(), TEST_VALUE); + assert!(!test.public()); + assert!(test.masked()); + SimpleRun::dummy(Ok(())).await }) .await