|
| 1 | +package job_distributor |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/rs/zerolog" |
| 11 | + "github.com/rs/zerolog/log" |
| 12 | + tc "github.com/testcontainers/testcontainers-go" |
| 13 | + tcwait "github.com/testcontainers/testcontainers-go/wait" |
| 14 | + |
| 15 | + "github.com/goplugin/plugin-testing-framework/lib/docker" |
| 16 | + "github.com/goplugin/plugin-testing-framework/lib/docker/test_env" |
| 17 | + "github.com/goplugin/plugin-testing-framework/lib/logging" |
| 18 | + "github.com/goplugin/plugin-testing-framework/lib/utils/testcontext" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + JDContainerName string = "job-distributor" |
| 23 | + DEAFULTJDContainerPort string = "42242" |
| 24 | + DEFAULTCSAKeyEncryptionKey string = "!PASsword000!" |
| 25 | + DEAFULTWSRPCContainerPort string = "8080" |
| 26 | +) |
| 27 | + |
| 28 | +type Option = func(j *Component) |
| 29 | + |
| 30 | +type Component struct { |
| 31 | + test_env.EnvComponent |
| 32 | + Grpc string |
| 33 | + Wsrpc string |
| 34 | + InternalGRPC string |
| 35 | + InternalWSRPC string |
| 36 | + l zerolog.Logger |
| 37 | + t *testing.T |
| 38 | + dbConnection string |
| 39 | + containerPort string |
| 40 | + wsrpcPort string |
| 41 | + csaKeyEncryptionKey string |
| 42 | +} |
| 43 | + |
| 44 | +func (j *Component) startOrRestartContainer(withReuse bool) error { |
| 45 | + req := j.getContainerRequest() |
| 46 | + l := logging.GetTestContainersGoTestLogger(j.t) |
| 47 | + c, err := docker.StartContainerWithRetry(j.l, tc.GenericContainerRequest{ |
| 48 | + ContainerRequest: *req, |
| 49 | + Started: true, |
| 50 | + Reuse: withReuse, |
| 51 | + Logger: l, |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + j.Container = c |
| 57 | + ctx := testcontext.Get(j.t) |
| 58 | + host, err := test_env.GetHost(ctx, c) |
| 59 | + if err != nil { |
| 60 | + return errors.Wrapf(err, "cannot get host for container %s", j.ContainerName) |
| 61 | + } |
| 62 | + |
| 63 | + p, err := c.MappedPort(ctx, test_env.NatPort(j.containerPort)) |
| 64 | + if err != nil { |
| 65 | + return errors.Wrapf(err, "cannot get container mapped port for container %s", j.ContainerName) |
| 66 | + } |
| 67 | + j.Grpc = fmt.Sprintf("%s:%s", host, p.Port()) |
| 68 | + |
| 69 | + p, err = c.MappedPort(ctx, test_env.NatPort(j.wsrpcPort)) |
| 70 | + if err != nil { |
| 71 | + return errors.Wrapf(err, "cannot get wsrpc mapped port for container %s", j.ContainerName) |
| 72 | + } |
| 73 | + j.Wsrpc = fmt.Sprintf("%s:%s", host, p.Port()) |
| 74 | + j.InternalGRPC = fmt.Sprintf("%s:%s", j.ContainerName, j.containerPort) |
| 75 | + |
| 76 | + j.InternalWSRPC = fmt.Sprintf("%s:%s", j.ContainerName, j.wsrpcPort) |
| 77 | + j.l.Info(). |
| 78 | + Str("containerName", j.ContainerName). |
| 79 | + Str("grpcURI", j.Grpc). |
| 80 | + Str("wsrpcURI", j.Wsrpc). |
| 81 | + Str("internalGRPC", j.InternalGRPC). |
| 82 | + Str("internalWSRPC", j.InternalWSRPC). |
| 83 | + Msg("Started Job Distributor container") |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (j *Component) getContainerRequest() *tc.ContainerRequest { |
| 89 | + return &tc.ContainerRequest{ |
| 90 | + Name: j.ContainerName, |
| 91 | + Image: fmt.Sprintf("%s:%s", j.ContainerImage, j.ContainerVersion), |
| 92 | + ExposedPorts: []string{ |
| 93 | + test_env.NatPortFormat(j.containerPort), |
| 94 | + test_env.NatPortFormat(j.wsrpcPort), |
| 95 | + }, |
| 96 | + Env: map[string]string{ |
| 97 | + "DATABASE_URL": j.dbConnection, |
| 98 | + "PORT": j.containerPort, |
| 99 | + "NODE_RPC_PORT": j.wsrpcPort, |
| 100 | + "CSA_KEY_ENCRYPTION_SECRET": j.csaKeyEncryptionKey, |
| 101 | + }, |
| 102 | + Networks: j.Networks, |
| 103 | + WaitingFor: tcwait.ForAll( |
| 104 | + tcwait.ForListeningPort(test_env.NatPort(j.containerPort)), |
| 105 | + tcwait.ForListeningPort(test_env.NatPort(j.wsrpcPort)), |
| 106 | + ), |
| 107 | + LifecycleHooks: []tc.ContainerLifecycleHooks{ |
| 108 | + { |
| 109 | + PostStarts: j.PostStartsHooks, |
| 110 | + PostStops: j.PostStopsHooks, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (j *Component) StartContainer() error { |
| 117 | + return j.startOrRestartContainer(false) |
| 118 | +} |
| 119 | + |
| 120 | +func (j *Component) RestartContainer() error { |
| 121 | + return j.startOrRestartContainer(true) |
| 122 | +} |
| 123 | + |
| 124 | +func New(networks []string, opts ...Option) *Component { |
| 125 | + id, _ := uuid.NewRandom() |
| 126 | + j := &Component{ |
| 127 | + EnvComponent: test_env.EnvComponent{ |
| 128 | + ContainerName: fmt.Sprintf("%s-%s", JDContainerName, id.String()[0:8]), |
| 129 | + Networks: networks, |
| 130 | + StartupTimeout: 2 * time.Minute, |
| 131 | + }, |
| 132 | + containerPort: DEAFULTJDContainerPort, |
| 133 | + wsrpcPort: DEAFULTWSRPCContainerPort, |
| 134 | + csaKeyEncryptionKey: DEFAULTCSAKeyEncryptionKey, |
| 135 | + l: log.Logger, |
| 136 | + } |
| 137 | + j.SetDefaultHooks() |
| 138 | + for _, opt := range opts { |
| 139 | + opt(j) |
| 140 | + } |
| 141 | + return j |
| 142 | +} |
| 143 | + |
| 144 | +func WithTestInstance(t *testing.T) Option { |
| 145 | + return func(j *Component) { |
| 146 | + j.l = logging.GetTestLogger(t) |
| 147 | + j.t = t |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func WithContainerPort(port string) Option { |
| 152 | + return func(j *Component) { |
| 153 | + j.containerPort = port |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func WithWSRPCContainerPort(port string) Option { |
| 158 | + return func(j *Component) { |
| 159 | + j.wsrpcPort = port |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func WithDBURL(db string) Option { |
| 164 | + return func(j *Component) { |
| 165 | + if db != "" { |
| 166 | + j.dbConnection = db |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func WithContainerName(name string) Option { |
| 172 | + return func(j *Component) { |
| 173 | + j.ContainerName = name |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func WithImage(image string) Option { |
| 178 | + return func(j *Component) { |
| 179 | + j.ContainerImage = image |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func WithVersion(version string) Option { |
| 184 | + return func(j *Component) { |
| 185 | + j.ContainerVersion = version |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func WithCSAKeyEncryptionKey(key string) Option { |
| 190 | + return func(j *Component) { |
| 191 | + j.csaKeyEncryptionKey = key |
| 192 | + } |
| 193 | +} |
0 commit comments