Skip to content

Commit

Permalink
mock: Add support for setting job variables
Browse files Browse the repository at this point in the history
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 <ryan.gonzalez@collabora.com>
  • Loading branch information
refi64 authored and sjoerdsimons committed Mar 16, 2022
1 parent 9ccbe6a commit 177762a
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 79 deletions.
79 changes: 1 addition & 78 deletions gitlab-runner-mock/src/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <test@example.com>", "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()
Expand Down Expand Up @@ -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": [],
Expand Down
34 changes: 34 additions & 0 deletions gitlab-runner-mock/src/job.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -84,6 +95,7 @@ pub struct MockJob {
name: String,
id: u64,
token: String,
variables: Vec<MockJobVariable>,
steps: Vec<MockJobStep>,
dependencies: Vec<MockJob>,
artifacts: Vec<MockJobArtifact>,
Expand Down Expand Up @@ -124,6 +136,10 @@ impl MockJob {
&self.artifacts
}

pub fn variables(&self) -> &[MockJobVariable] {
&self.variables
}

pub fn steps(&self) -> &[MockJobStep] {
&self.steps
}
Expand Down Expand Up @@ -202,6 +218,7 @@ impl MockJob {
pub struct MockJobBuilder {
name: String,
id: u64,
variables: HashMap<String, MockJobVariable>,
steps: Vec<MockJobStep>,
dependencies: Vec<MockJob>,
artifacts: Vec<MockJobArtifact>,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions gitlab-runner-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
Loading

0 comments on commit 177762a

Please sign in to comment.