From 26d9ccda4a2aa3a8fa8bfd0c3ce9712ba1f2e0df Mon Sep 17 00:00:00 2001 From: Maksim Sokolnikov Date: Fri, 27 Sep 2024 10:56:02 +0300 Subject: [PATCH 1/4] feat: add docker login function --- modules/docker/login.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 modules/docker/login.go diff --git a/modules/docker/login.go b/modules/docker/login.go new file mode 100644 index 000000000..60217c176 --- /dev/null +++ b/modules/docker/login.go @@ -0,0 +1,30 @@ +package docker + +import ( + "github.com/gruntwork-io/terratest/modules/logger" + "github.com/gruntwork-io/terratest/modules/shell" + "github.com/gruntwork-io/terratest/modules/testing" + "github.com/stretchr/testify/require" +) + +type LoginOptions struct { + Registry string + Login string + Password string +} + +// Login runs the 'docker login' command to login the given registry. This will fail the test if there are any errors. +// Do not use production password, only for testing purpose. +func Login(t testing.TestingT, opt LoginOptions) { + require.NoError(t, login(t, opt)) +} + +// login runs the 'docker login' command to login the given registry. +func login(t testing.TestingT, opt LoginOptions) error { + logger.Logf(t, "Running 'docker login' for user %s", opt.Login) + cmd := shell.Command{ + Command: "docker", + Args: []string{"login", opt.Registry, "-u", opt.Login, "-p", opt.Password}, + } + return shell.RunCommandE(t, cmd) +} From bbe53e824fd4a621b88b8c5419aadc13cf323df0 Mon Sep 17 00:00:00 2001 From: Maksim Sokolnikov Date: Thu, 21 Nov 2024 17:41:14 +0300 Subject: [PATCH 2/4] Fix MR --- modules/docker/login.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/docker/login.go b/modules/docker/login.go index 60217c176..4b0976133 100644 --- a/modules/docker/login.go +++ b/modules/docker/login.go @@ -16,12 +16,13 @@ type LoginOptions struct { // Login runs the 'docker login' command to login the given registry. This will fail the test if there are any errors. // Do not use production password, only for testing purpose. func Login(t testing.TestingT, opt LoginOptions) { - require.NoError(t, login(t, opt)) + require.NoError(t, LoginE(t, opt)) } // login runs the 'docker login' command to login the given registry. -func login(t testing.TestingT, opt LoginOptions) error { +func LoginE(t testing.TestingT, opt LoginOptions) error { logger.Logf(t, "Running 'docker login' for user %s", opt.Login) + cmd := shell.Command{ Command: "docker", Args: []string{"login", opt.Registry, "-u", opt.Login, "-p", opt.Password}, From bda1dec0c235e6d9cddf05d3bd2cc7e74e510b64 Mon Sep 17 00:00:00 2001 From: Maksim Sokolnikov Date: Thu, 21 Nov 2024 17:43:04 +0300 Subject: [PATCH 3/4] Fix comment --- modules/docker/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/docker/login.go b/modules/docker/login.go index 4b0976133..8d9cf2b7b 100644 --- a/modules/docker/login.go +++ b/modules/docker/login.go @@ -19,7 +19,7 @@ func Login(t testing.TestingT, opt LoginOptions) { require.NoError(t, LoginE(t, opt)) } -// login runs the 'docker login' command to login the given registry. +// LoginE runs the 'docker login' command to login the given registry. func LoginE(t testing.TestingT, opt LoginOptions) error { logger.Logf(t, "Running 'docker login' for user %s", opt.Login) From f3f1328f895c853b9509e19b125f302876de4d47 Mon Sep 17 00:00:00 2001 From: Maksim Sokolnikov Date: Thu, 28 Nov 2024 16:47:07 +0300 Subject: [PATCH 4/4] feat: add test for docker LoginE --- modules/docker/login_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 modules/docker/login_test.go diff --git a/modules/docker/login_test.go b/modules/docker/login_test.go new file mode 100644 index 000000000..0606c4f7f --- /dev/null +++ b/modules/docker/login_test.go @@ -0,0 +1,14 @@ +package docker + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Login(t *testing.T) { + + err := LoginE(t, LoginOptions{Registry: "registry-1.docker.io", Login: "1", Password: "1"}) + require.Error(t, err) + require.Contains(t, err.Error(), "incorrect username or password") +}