feat: enabling terraform unit test

This commit is contained in:
Pierre Mavro
2021-08-30 09:17:07 +02:00
parent f5c8011da1
commit f6b6bd606b
2 changed files with 19 additions and 11 deletions

View File

@@ -21,12 +21,15 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.14.10
- name: build
run: |
export PATH=$GITHUB_WORKSPACE/bin:$PATH
export RUSTC_WRAPPER=$GITHUB_WORKSPACE/bin/sccache
export SCCACHE_REDIS=${{ secrets.SCCACHE_REDIS }}
mkdir -p $GITHUB_WORKSPACE/bin
mkdir -p $GITHUB_WORKSPACE/bin $GITHUB_WORKSPACE/.terraform.d/plugin-cache
sccache_release=$(curl --silent "https://github.com/Qovery/sccache-bin/releases/latest" | sed -r 's/^.+tag\/(.+)">.+/\1/')
curl -sLo $GITHUB_WORKSPACE/bin/sccache https://github.com/Qovery/sccache-bin/releases/download/${sccache_release}/sccache
chmod 755 $GITHUB_WORKSPACE/bin/sccache

View File

@@ -8,7 +8,7 @@ use crate::error::{SimpleError, SimpleErrorKind};
use chrono::Duration;
use rand::Rng;
use retry::Error::Operation;
use std::{fs, thread, time};
use std::{env, fs, thread, time};
fn terraform_init_validate(root_dir: &str) -> Result<(), SimpleError> {
let terraform_provider_lock = format!("{}/.terraform.lock.hcl", &root_dir);
@@ -184,15 +184,21 @@ pub fn terraform_init_validate_state_list(root_dir: &str) -> Result<Vec<String>,
}
pub fn terraform_exec(root_dir: &str, args: Vec<&str>) -> Result<Vec<String>, SimpleError> {
let home_dir = home_dir().expect("Could not find $HOME");
let tf_plugin_cache_dir = format!("{}/.terraform.d/plugin-cache", home_dir.to_str().unwrap());
// override if environment variable is set
let tf_plugin_cache_dir_value = match env::var_os(TF_PLUGIN_CACHE_DIR) {
Some(val) => format!("{:?}", val),
None => {
let home_dir = home_dir().expect("Could not find $HOME");
format!("{}/.terraform.d/plugin-cache", home_dir.to_str().unwrap())
}
};
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let result = exec_with_envs_and_output(
format!("{} terraform", root_dir).as_str(),
args,
vec![(TF_PLUGIN_CACHE_DIR, tf_plugin_cache_dir.as_str())],
vec![(TF_PLUGIN_CACHE_DIR, tf_plugin_cache_dir_value.as_str())],
|line: Result<String, std::io::Error>| {
let output = line.unwrap();
stdout.push(output.clone());
@@ -221,16 +227,15 @@ pub fn terraform_exec(root_dir: &str, args: Vec<&str>) -> Result<Vec<String>, Si
mod tests {
use crate::cmd::terraform::terraform_init_validate;
use std::fs;
//use tracing::{span, Level};
//use tracing_test::traced_test;
use tracing::{span, Level};
use tracing_test::traced_test;
#[test]
#[ignore]
//#[traced_test]
#[traced_test]
// https://github.com/hashicorp/terraform/issues/28041
fn test_terraform_init_lock_issue() {
//let span = span!(Level::TRACE, "terraform_test");
//let _enter = span.enter();
let span = span!(Level::TRACE, "terraform_test");
let _enter = span.enter();
// those 2 files are a voluntary broken config, it should detect it and auto repair
let terraform_lock_file = r#"