-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
178 lines (164 loc) · 5.53 KB
/
session.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2024 Harald Albrecht.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package morbyd
import (
"context"
"fmt"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/thediveo/morbyd/moby"
"github.com/thediveo/morbyd/session"
)
// Session represents a Docker API client connection, together with additional
// configuration options that are inherited to newly created images, containers,
// and networks.
type Session struct {
opts session.Options
moby moby.Client
}
// NewSession creates a new Docker client and test session, returning a Session
// object on success, or an error otherwise.
//
// When [sess.WithAutoCleaning] has been specified, then NewSession will then
// forcefully remove all containers and then networks matching the specified
// auto-cleaning label. In this case, [Session.Close] will then run a
// post-session cleaning.
//
// Note: the Docker client is created using the options [client.FromEnv] and
// [client.WithAPIVersionNegotiation].
func NewSession(ctx context.Context, opts ...session.Opt) (*Session, error) {
s := &Session{}
for _, opt := range opts {
if err := opt(&s.opts); err != nil {
return nil, fmt.Errorf("cannot create new test session, reason: %w",
err)
}
}
clientOpts := append([]client.Opt{
client.FromEnv,
client.WithAPIVersionNegotiation(),
}, s.opts.DockerClientOpts...)
moby, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
return nil, err
}
s.moby = moby
if s.opts.Wrapper != nil {
s.moby = s.opts.Wrapper(s.moby)
}
s.AutoClean(ctx)
return s, nil
}
// Client returns the Docker client used in this test session.
func (s *Session) Client() moby.Client { return s.moby }
// Close removes left-over containers and networks if auto-cleaning has been
// enabled, and then closes idle HTTP connections to the Docker daemon.
func (s *Session) Close(ctx context.Context) {
s.AutoClean(ctx)
s.moby.Close()
}
// AutoClean forcefully removes all left-over containers and networks that are
// labelled with the auto-cleaning label specified when creating this session.
// If no auto-cleaning label was specified, AutoClean simply returns, doing
// nothing. (Well, it does something: it returns ... but that is now too meta).
func (s *Session) AutoClean(ctx context.Context) {
if s.opts.AutoCleaningLabel == "" {
return
}
s.autoClean(ctx, s.opts.AutoCleaningLabel)
}
func (s *Session) autoClean(ctx context.Context, aclabel string) {
// Assemble a filter based on the auto-cleaning label.
key, value, _ := strings.Cut(aclabel, "=")
if value == "" {
aclabel = key // just the key, no trailing "=" in case of a zero value.
}
f := filters.NewArgs(filters.Arg("label", aclabel))
// List all matching containers and then kill them.
cntrs, err := s.moby.ContainerList(ctx, container.ListOptions{
Filters: f,
})
if err != nil {
return
}
for _, cntr := range cntrs {
err := s.moby.ContainerRemove(ctx, cntr.ID, container.RemoveOptions{
Force: true,
RemoveVolumes: true,
})
if err != nil {
return
}
}
// List all matching networks (which by now should not have any test
// containers attached to them anymore) and then remove them.
nets, err := s.moby.NetworkList(ctx, network.ListOptions{
Filters: f,
})
if err != nil {
return
}
for _, net := range nets {
_ = s.moby.NetworkRemove(ctx, net.ID)
}
}
// Container returns a *Container object for the specified name or ID if it
// exists, otherwise it returns an error. Please note that multiple calls for
// the same name or ID will return different *Container objects, as there is no
// caching.
func (s *Session) Container(ctx context.Context, nameID string) (*Container, error) {
details, err := s.moby.ContainerInspect(ctx, nameID)
if err != nil {
return nil, err
}
cntr := &Container{
Name: details.Name[1:],
ID: details.ID,
Session: s,
Details: details,
}
return cntr, nil
}
// Network returns a *Network object for the specified name or ID if it exists,
// otherwise it returns an error. Please note that multiple calls for the same
// name or ID will return different *Network objects, as there is no caching.
func (s *Session) Network(ctx context.Context, nameID string) (*Network, error) {
details, err := s.moby.NetworkInspect(ctx, nameID, network.InspectOptions{
Verbose: true,
})
if err != nil {
return nil, err
}
netw := &Network{
Name: details.Name,
ID: details.ID,
Session: s,
Details: details,
}
return netw, nil
}
// IsDockerDesktop returns true if the Docker engine is the Docker Desktop
// engine, as opposed to a “plain” Docker engine. This differentiation is
// important in some situation, as containers managed by Docker Desktop cannot
// be directly reached from the host, but always require ports to be published.
func (s *Session) IsDockerDesktop(ctx context.Context) bool {
info, err := s.moby.ServerVersion(ctx)
if err != nil {
return false
}
return strings.Contains(info.Platform.Name, "Desktop")
}