-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
auth_test.go
134 lines (111 loc) · 4.2 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package bindings_test
import (
"os"
"time"
"github.com/containers/common/pkg/auth"
"github.com/containers/image/v5/types"
podmanRegistry "github.com/containers/podman/v5/hack/podman-registry-go"
"github.com/containers/podman/v5/pkg/bindings/images"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Podman images", func() {
var (
registry *podmanRegistry.Registry
bt *bindingTest
s *gexec.Session
err error
)
BeforeEach(func() {
registryOptions := &podmanRegistry.Options{
PodmanPath: getPodmanBinary(),
}
// Note: we need to start the registry **before** setting up
// the test. Otherwise, the registry is not reachable for
// currently unknown reasons.
registry, err = podmanRegistry.StartWithOptions(registryOptions)
Expect(err).ToNot(HaveOccurred())
bt = newBindingTest()
bt.RestoreImagesFromCache()
s = bt.startAPIService()
time.Sleep(1 * time.Second)
err := bt.NewConnection()
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
s.Kill()
bt.cleanup()
err := registry.Stop()
Expect(err).ToNot(HaveOccurred())
})
// Test using credentials.
It("tag + push + pull + search (with credentials)", func() {
imageRep := "localhost:" + registry.Port + "/test"
imageTag := "latest"
imageRef := imageRep + ":" + imageTag
// Tag the alpine image and verify it has worked.
err = images.Tag(bt.conn, alpine.shortName, imageTag, imageRep, nil)
Expect(err).ToNot(HaveOccurred())
_, err = images.GetImage(bt.conn, imageRef, nil)
Expect(err).ToNot(HaveOccurred())
// Now push the image.
pushOpts := new(images.PushOptions)
err = images.Push(bt.conn, imageRef, imageRef, pushOpts.WithUsername(registry.User).WithPassword(registry.Password).WithSkipTLSVerify(true))
Expect(err).ToNot(HaveOccurred())
// Now pull the image.
pullOpts := new(images.PullOptions)
_, err = images.Pull(bt.conn, imageRef, pullOpts.WithSkipTLSVerify(true).WithPassword(registry.Password).WithUsername(registry.User))
Expect(err).ToNot(HaveOccurred())
// Last, but not least, exercise search.
searchOptions := new(images.SearchOptions)
_, err = images.Search(bt.conn, imageRef, searchOptions.WithSkipTLSVerify(true).WithPassword(registry.Password).WithUsername(registry.User))
Expect(err).ToNot(HaveOccurred())
})
// Test using authfile.
It("tag + push + pull + search (with authfile)", func() {
imageRep := "localhost:" + registry.Port + "/test"
imageTag := "latest"
imageRef := imageRep + ":" + imageTag
// Create a temporary authentication file.
tmpFile, err := os.CreateTemp("", "auth.json.")
Expect(err).ToNot(HaveOccurred())
_, err = tmpFile.Write([]byte{'{', '}'})
Expect(err).ToNot(HaveOccurred())
err = tmpFile.Close()
Expect(err).ToNot(HaveOccurred())
authFilePath := tmpFile.Name()
// Now login to a) test the credentials and to b) store them in
// the authfile for later use.
sys := types.SystemContext{
AuthFilePath: authFilePath,
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
}
loginOptions := auth.LoginOptions{
Username: registry.User,
Password: registry.Password,
AuthFile: authFilePath,
Stdin: os.Stdin,
Stdout: os.Stdout,
}
err = auth.Login(bt.conn, &sys, &loginOptions, []string{imageRep})
Expect(err).ToNot(HaveOccurred())
// Tag the alpine image and verify it has worked.
err = images.Tag(bt.conn, alpine.shortName, imageTag, imageRep, nil)
Expect(err).ToNot(HaveOccurred())
_, err = images.GetImage(bt.conn, imageRef, nil)
Expect(err).ToNot(HaveOccurred())
// Now push the image.
pushOpts := new(images.PushOptions)
err = images.Push(bt.conn, imageRef, imageRef, pushOpts.WithAuthfile(authFilePath).WithSkipTLSVerify(true))
Expect(err).ToNot(HaveOccurred())
// Now pull the image.
pullOpts := new(images.PullOptions)
_, err = images.Pull(bt.conn, imageRef, pullOpts.WithAuthfile(authFilePath).WithSkipTLSVerify(true))
Expect(err).ToNot(HaveOccurred())
// Last, but not least, exercise search.
searchOptions := new(images.SearchOptions)
_, err = images.Search(bt.conn, imageRef, searchOptions.WithSkipTLSVerify(true).WithAuthfile(authFilePath))
Expect(err).ToNot(HaveOccurred())
})
})