Skip to content

Commit a0cda6f

Browse files
authored
Deprecate containertest and mysqltest officially (#277)
containertest now aliases the external package.
1 parent b18b01e commit a0cda6f

14 files changed

+109
-793
lines changed

containertest/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Mysqltest library
22

3+
> [!CAUTION]
4+
>
5+
> This library is deprecated and will be removed in a future version. Use the
6+
> standalone version at https://github.com/abcxyz/containertest instead.
7+
38
## Introduction
49

510
This is a Go library for starting an ephemeral Docker container. It is used to test

containertest/config.go

+32-64
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,43 @@
1515
package containertest
1616

1717
import (
18-
"log"
18+
"github.com/abcxyz/containertest"
1919
)
2020

2121
// This file implements the "functional options" pattern.
2222

23-
type config struct {
24-
killAfterSec int // This is in integer seconds because that's what Docker takes.
25-
service Service
26-
progressLogger TestLogger
27-
}
28-
29-
// TestLogger allows the caller to optionally provide a custom logger for printing status updates about
30-
// service startup progress. The default is to use the go "log" package.
31-
// testing.TB satisfies TestLogger, and is usually what you want to put here.
32-
type TestLogger interface {
33-
Log(args ...any)
34-
Logf(format string, args ...any)
35-
}
36-
37-
func makeDefaultConfig(service Service) *config {
38-
return &config{
39-
killAfterSec: 10 * 60,
40-
service: service,
41-
progressLogger: &stdlibLogger{},
42-
}
43-
}
44-
45-
func buildConfig(service Service, opts ...Option) *config {
46-
config := makeDefaultConfig(service)
47-
for _, opt := range opts {
48-
config = opt(config)
49-
}
50-
return config
51-
}
52-
53-
// Option sets a configuration option for this package. Users should not implement these functions,
54-
// they should use one of the With* functions.
55-
type Option func(*config) *config
56-
57-
// WithKillAfterSeconds is an option that overrides the default time period after which the docker
58-
// container will kill itself.
23+
// TestLogger allows the caller to optionally provide a custom logger for
24+
// printing status updates about service startup progress. The default is to use
25+
// the go "log" package. testing.TB satisfies TestLogger, and is usually what
26+
// you want to put here.
5927
//
60-
// Containers might bypass the normal clean shutdown logic if the test terminates abnormally, such
61-
// as when ctrl-C is pressed during a test. Therefore we instruct the container to kill itself after
62-
// a while. The duration must be longer than longest test that uses the container. There's no harm in
63-
// leaving lots of extra time.
64-
func WithKillAfterSeconds(seconds int) Option {
65-
return func(c *config) *config {
66-
c.killAfterSec = seconds
67-
return c
68-
}
69-
}
28+
// Deprecated: This has moved to a new package. Use
29+
// [github.com/abcxyz/containertest.TestLogger] instead.
30+
type TestLogger = containertest.TestLogger
7031

71-
// WithLogger overrides the default logger. This logger will receive messages about service startup
72-
// progress. The default is to use the go "log" package.
73-
func WithLogger(l TestLogger) Option {
74-
return func(c *config) *config {
75-
c.progressLogger = l
76-
return c
77-
}
78-
}
79-
80-
// stdlibLogger is the default implementation of the TestLogger interface that calls log.Logf.
81-
type stdlibLogger struct{}
32+
// Option sets a configuration option for this package. Users should not
33+
// implement these functions, they should use one of the With* functions.
34+
//
35+
// Deprecated: This has moved to a new package. Use
36+
// [github.com/abcxyz/containertest.Option] instead.
37+
type Option = containertest.Option
8238

83-
func (s *stdlibLogger) Logf(fmtStr string, args ...any) {
84-
log.Printf(fmtStr, args...)
85-
}
39+
// WithKillAfterSeconds is an option that overrides the default time period
40+
// after which the docker container will kill itself.
41+
//
42+
// Containers might bypass the normal clean shutdown logic if the test
43+
// terminates abnormally, such as when ctrl-C is pressed during a test.
44+
// Therefore we instruct the container to kill itself after a while. The
45+
// duration must be longer than longest test that uses the container. There's no
46+
// harm in leaving lots of extra time.
47+
//
48+
// Deprecated: This has moved to a new package. Use
49+
// [github.com/abcxyz/containertest.WithKillAfterSeconds] instead.
50+
var WithKillAfterSeconds = containertest.WithKillAfterSeconds
8651

87-
func (s *stdlibLogger) Log(args ...any) {
88-
log.Print(args...)
89-
}
52+
// WithLogger overrides the default logger. This logger will receive messages
53+
// about service startup progress. The default is to use the go "log" package.
54+
//
55+
// Deprecated: This has moved to a new package. Use
56+
// [github.com/abcxyz/containertest.WithLogger] instead.
57+
var WithLogger = containertest.WithLogger

containertest/containertest.go

+21-49
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,34 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Package containertest provides an ephemeral container (such as a database) for integration testing.
16-
// It's designed to be used in code that needs to work inside and outside google.
15+
// Package containertest provides an ephemeral container (such as a database)
16+
// for integration testing. It's designed to be used in code that needs to work
17+
// inside and outside google.
18+
//
19+
// Deprecated: Use the standalone [github.com/abcxyz/containertest] package
20+
// instead.
1721
package containertest
1822

1923
import (
20-
"fmt"
21-
"io"
24+
"github.com/abcxyz/containertest"
2225
)
2326

2427
// ConnInfo specifies how connect to the created container.
25-
type ConnInfo struct {
26-
Host string
27-
28-
// PortMapper maps from container port to host port. Do not use after container is closed.
29-
PortMapper func(containerPort string) (hostPort string)
30-
31-
// io.Closer for closing the connection. Should always be initialized, either
32-
// with an actual closer or io.NoopCloser.
33-
closer io.Closer
34-
}
35-
36-
// Close implements io.Closer by passing to internal closer field.
37-
func (c ConnInfo) Close() error {
38-
return fmt.Errorf("error closing container: %w", c.closer.Close())
39-
}
28+
//
29+
// Deprecated: This has moved to a new package. Use
30+
// [github.com/abcxyz/containertest.ConnInfo] instead.
31+
type ConnInfo = containertest.ConnInfo
4032

4133
// Service provides information about what container image should be started and
4234
// how to know when it has finished stating up.
43-
type Service interface {
44-
// ImageRepository returns the repository for docker image (ex: mysql).
45-
ImageRepository() string
46-
47-
// ImageTag returns the tag for docker image (ex: 5.3).
48-
ImageTag() string
49-
50-
// Environment returns variables to be set in container. Each element is in format of "KEY=VALUE".
51-
Environment() []string
52-
53-
// StartupPorts is the list of ports that must be exposed by container before TestConn is run.
54-
StartupPorts() []string
55-
56-
// TestConn takes a logger and a struct with connection info, and returns nil if app has started.
57-
TestConn(progressLogger TestLogger, info *ConnInfo) error
58-
}
59-
60-
// Start starts a container, or returns an error. On err ConnInfo will be automatically
61-
// closed and nil will be returned.
62-
func Start(service Service, opts ...Option) (*ConnInfo, error) {
63-
conf := buildConfig(service, opts...)
64-
ci, err := start(conf)
65-
if err != nil {
66-
// The Closer must be called even if there's an error, to clean up the docker container that may
67-
// exist.
68-
_ = ci.Close()
69-
return nil, err
70-
}
35+
//
36+
// Deprecated: This has moved to a new package. Use
37+
// [github.com/abcxyz/containertest.Service] instead.
38+
type Service = containertest.Service
7139

72-
return ci, nil
73-
}
40+
// Start starts a container, or returns an error. On err ConnInfo will be
41+
// automatically closed and nil will be returned.
42+
//
43+
// Deprecated: This has moved to a new package. Use
44+
// [github.com/abcxyz/containertest.Start] instead.
45+
var Start = containertest.Start

containertest/containertest_test.go

-73
This file was deleted.

0 commit comments

Comments
 (0)