2828import com .cloudbees .plugins .credentials .common .IdCredentials ;
2929import com .cloudbees .plugins .credentials .domains .Domain ;
3030import com .cloudbees .plugins .credentials .impl .UsernamePasswordCredentialsImpl ;
31- import java .util .Collections ;
32- import java .util .Map ;
33- import java .util .TreeMap ;
3431import org .jenkinsci .plugins .docker .commons .credentials .DockerRegistryEndpoint ;
32+ import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
33+ import org .jenkinsci .plugins .workflow .cps .SnippetizerTester ;
34+ import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
3535import org .jenkinsci .plugins .workflow .steps .StepConfigTester ;
36- import org .jenkinsci .plugins .workflow .structs .DescribableHelper ;
3736import static org .junit .Assert .assertEquals ;
3837import static org .junit .Assert .assertNotNull ;
39- import org .junit .ClassRule ;
4038import org .junit .Rule ;
4139import org .junit .Test ;
42- import org .junit .runners .model .Statement ;
43- import org .jvnet .hudson .test .BuildWatcher ;
44- import org .jvnet .hudson .test .RestartableJenkinsRule ;
40+ import org .jvnet .hudson .test .Issue ;
41+ import org .jvnet .hudson .test .JenkinsRule ;
4542
4643public class RegistryEndpointStepTest {
4744
48- @ ClassRule public static BuildWatcher buildWatcher = new BuildWatcher ();
49- @ Rule public RestartableJenkinsRule story = new RestartableJenkinsRule ();
45+ @ Rule public JenkinsRule r = new JenkinsRule ();
5046
51- @ Test public void configRoundTrip () {
52- story .addStep (new Statement () {
53- @ Override public void evaluate () throws Throwable {
54- IdCredentials registryCredentials = new UsernamePasswordCredentialsImpl (CredentialsScope .GLOBAL , "registryCreds" , null , "me" , "pass" );
55- CredentialsProvider .lookupStores (story .j .jenkins ).iterator ().next ().addCredentials (Domain .global (), registryCredentials );
56- StepConfigTester sct = new StepConfigTester (story .j );
57- Map <String ,Object > registryConfig = new TreeMap <String ,Object >();
58- registryConfig .put ("url" , "https://docker.my.com/" );
59- registryConfig .put ("credentialsId" , registryCredentials .getId ());
60- Map <String ,Object > config = Collections .<String ,Object >singletonMap ("registry" , registryConfig );
61- RegistryEndpointStep step = DescribableHelper .instantiate (RegistryEndpointStep .class , config );
62- step = sct .configRoundTrip (step );
63- DockerRegistryEndpoint registry = step .getRegistry ();
64- assertNotNull (registry );
65- assertEquals ("https://docker.my.com/" , registry .getUrl ());
66- assertEquals (registryCredentials .getId (), registry .getCredentialsId ());
67- assertEquals (config , DescribableHelper .uninstantiate (step ));
68- }
69- });
47+ @ Issue ("JENKINS-51395" )
48+ @ Test public void configRoundTrip () throws Exception {
49+ { // Recommended syntax.
50+ SnippetizerTester st = new SnippetizerTester (r );
51+ RegistryEndpointStep step = new RegistryEndpointStep (new DockerRegistryEndpoint ("https://myreg/" , null ));
52+ step .setToolName ("" );
53+ st .assertRoundTrip (step , "withDockerRegistry(url: 'https://myreg/') {\n // some block\n }" );
54+ step = new RegistryEndpointStep (new DockerRegistryEndpoint (null , "hubcreds" ));
55+ st .assertRoundTrip (step , "withDockerRegistry(credentialsId: 'hubcreds') {\n // some block\n }" );
56+ step = new RegistryEndpointStep (new DockerRegistryEndpoint ("https://myreg/" , "mycreds" ));
57+ step .setToolName ("ce" );
58+ st .assertRoundTrip (step , "withDockerRegistry(credentialsId: 'mycreds', toolName: 'ce', url: 'https://myreg/') {\n // some block\n }" );
59+ }
60+ { // Older syntax.
61+ WorkflowJob p = r .createProject (WorkflowJob .class , "p" );
62+ p .setDefinition (new CpsFlowDefinition ("node {withDockerRegistry(registry: [url: 'https://docker.my.com/'], toolName: 'irrelevant') {}}" , true ));
63+ r .buildAndAssertSuccess (p );
64+ p .setDefinition (new CpsFlowDefinition ("node {withDockerRegistry(registry: [url: 'https://docker.my.com/']) {}}" , true ));
65+ r .buildAndAssertSuccess (p );
66+ p .setDefinition (new CpsFlowDefinition ("node {withDockerRegistry([url: 'https://docker.my.com/']) {}}" , true ));
67+ r .buildAndAssertSuccess (p );
68+ // and new, just in case SnippetizerTester is faking it:
69+ p .setDefinition (new CpsFlowDefinition ("node {withDockerRegistry(url: 'https://docker.my.com/') {}}" , true ));
70+ r .buildAndAssertSuccess (p );
71+ }
72+ { // UI form.
73+ IdCredentials registryCredentials = new UsernamePasswordCredentialsImpl (CredentialsScope .GLOBAL , "registryCreds" , null , "me" , "pass" );
74+ CredentialsProvider .lookupStores (r .jenkins ).iterator ().next ().addCredentials (Domain .global (), registryCredentials );
75+ StepConfigTester sct = new StepConfigTester (r );
76+ RegistryEndpointStep step = new RegistryEndpointStep (new DockerRegistryEndpoint ("https://docker.my.com/" , "registryCreds" ));
77+ step = sct .configRoundTrip (step );
78+ DockerRegistryEndpoint registry = step .getRegistry ();
79+ assertNotNull (registry );
80+ assertEquals ("https://docker.my.com/" , registry .getUrl ());
81+ assertEquals ("registryCreds" , registry .getCredentialsId ());
82+ // TODO check toolName
83+ }
7084 }
7185
7286}
0 commit comments