/*
Copyright 2023 Stefan Prodan
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 oci
import (
“context”
“fmt”
“io”
“math/rand”
“os”
“testing”
“time”
"github.com/distribution/distribution/v3/configuration"
"github.com/distribution/distribution/v3/registry"
_ "github.com/distribution/distribution/v3/registry/auth/htpasswd"
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/phayes/freeport"
"github.com/sirupsen/logrus"
)
var dockerRegistry string
func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := setupRegistryServer(ctx)
if err != nil {
panic(fmt.Sprintf("failed to start docker registry: %s", err))
}
code := m.Run()
os.Exit(code)
}
func setupRegistryServer(ctx context.Context) error {
config := &configuration.Configuration{}
config.Log.AccessLog.Disabled = true
config.Log.Level = “error”
port, err := freeport.GetFreePort()
if err != nil {
return fmt.Errorf(“failed to get free port: %s”, err)
}
logrus.SetOutput(io.Discard)
dockerRegistry = fmt.Sprintf(“localhost:%d”, port)
config.HTTP.Addr = fmt.Sprintf(“127.0.0.1:%d”, port)
config.HTTP.DrainTimeout = time.Duration(10) * time.Second
config.Storage = map[string]configuration.Parameters{“inmemory”: map[string]interface{}{}}
dockerRegistry, err := registry.NewRegistry(ctx, config)
if err != nil {
return fmt.Errorf(“failed to create docker registry: %w”, err)
}
go dockerRegistry.ListenAndServe()
return nil
}
func rnd(prefix string, n int) string {
runes := []rune(“abcdefghijklmnopqrstuvwxyz1234567890”)
b := make([]rune, n)
for i := range b {
b[i] = runes[rand.Intn(len(runes))]
}
return prefix + “-” + string(b)
}