首页 > 解决方案 > 无法使用 CredentialInterceptor 测试 BaselinePipelineTest

问题描述

我正在尝试在我的 Jenkins 脚本管道中测试一个函数。

void doSomeDockerThings(){
   withCredentials([[$class: 'UsernamePasswordMultiBinding', creditialsId: 'my_creds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
      sh "docker login -u $USERNAME -p '$PASSWORD' $DTR"
   }
}

从扩展 BasePipelineTest 的 Groovy 单元测试中,我有以下测试方法。

@Test
void testDoSomeDockerThings(){
   helper.registerAllowedMethod( "withCredentials", [LinkedHasMap.class, Closure.class], null );
   helper.registerAllowedMethod( "sh", [String.class], {c -> return c; })
   binding.setVariable( "USERNAME", "user" );
   binding.setVariable( "PASSWORD", "password" );
   binding.setVariable( "DTR", "Docker_Trusted_Registry" );
}

我现在要做的是编写一个断言,该命令按照我的预期执行。既然那是在 withCredentials 的 Closer 中,我该如何验证呢?

sh "docker login -u $USERNAME -p '$PASSWORD' $DTR"

我也尝试过使用 withCredentialsInterceptor ,但我似乎无法获得符合预期的语法。如果有人有这样的例子,我将不胜感激。

helper.registerAllowedMethod( "withCredentials", [LinkedHasMap.class, Closure.class], withCredentialsInterceptor );

我真的很难找到文档或示例是如何做到这一点的。

标签: groovyjenkins-pipelinejenkins-pipeline-unit

解决方案


推荐阅读