Skip to content

Commit

Permalink
TEMPORARY: use a different repository for source catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
sherine-k committed Jun 7, 2023
1 parent 13f851c commit f101e83
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 61 deletions.
4 changes: 2 additions & 2 deletions test/e2e/e2e-simple.sh
Expand Up @@ -22,8 +22,8 @@ MIRROR_OCI_DIR="${DATA_TMP}/mirror_oci"
OCI_CTLG_PATH="oc-mirror-dev.tgz"
WORKSPACE="oc-mirror-workspace"
CATALOGREGISTRY="quay.io"
CATALOGORG="redhatgov"
CATALOGNAMESPACE="redhatgov/oc-mirror-dev"
CATALOGORG="skhoury"
CATALOGNAMESPACE="skhoury/test-catalog"
REGISTRY_CONN_PORT=5000
REGISTRY_DISCONN_PORT=5001
METADATA_REGISTRY="localhost.localdomain:$REGISTRY_CONN_PORT"
Expand Down
155 changes: 155 additions & 0 deletions test/e2e/lib/catalog-extract/main.go
@@ -0,0 +1,155 @@
package main

import (
"archive/tar"
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
)

const (
opmCachePrefix = "/tmp/cache"
opmBinarySuffix = "opm"
opmBinaryDir = "usr/bin/registry"
configsDir = "/configs" //taking a shortcut, this should come from a label normally
)

func createRT(insecure bool) http.RoundTripper {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
// By default, we wrap the transport in retries, so reduce the
// default dial timeout to 5s to avoid 5x 30s of connection
// timeouts when doing the "ping" on certain http registries.
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
MinVersion: tls.VersionTLS12,
},
}
}

func getCraneOpts(ctx context.Context, insecure bool) []crane.Option {
opts := []crane.Option{
crane.WithAuthFromKeychain(authn.DefaultKeychain),
crane.WithTransport(createRT(insecure)),
crane.WithContext(ctx),
}
if insecure {
opts = append(opts, crane.Insecure)
}
return opts
}

func extractResource(img v1.Image, resource string, outDir string) error {
tr := tar.NewReader(mutate.Extract(img))

resourceExtracted := ""
for {
header, err := tr.Next()

// break the infinite loop when EOF
if errors.Is(err, io.EOF) {
break
}

// skip the file if it is a directory or doesn't correspond to the resource
if resource == opmBinarySuffix {
if !strings.HasSuffix(header.Name, resource) || header.FileInfo().IsDir() {
continue
}
} else {
if !strings.HasPrefix(header.Name, resource) || header.FileInfo().IsDir() {
continue
}
}

var buf bytes.Buffer
_, err = buf.ReadFrom(tr)
if err != nil {
return err
}

targetFileName := filepath.Join(outDir, header.Name)
bytes := buf.Bytes()

baseDir := filepath.Dir(targetFileName)
err = os.MkdirAll(baseDir, 0755)
if err != nil {
return err
}

f, err := os.Create(targetFileName)
if err == nil {
defer f.Close()
} else {
return err
}

_, err = f.Write(bytes)
if err != nil {
return err
}
resourceExtracted = targetFileName
}
// check for the folder (it should exist if we found something)
_, err := os.Stat(resourceExtracted)
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unable to extract %q", resourceExtracted)
}
return nil
}

func main() {
argsWithoutProg := os.Args[1:]
var img v1.Image
var err error

remoteOpts := getCraneOpts(context.TODO(), true)
img, err = crane.Pull(argsWithoutProg[0], remoteOpts...)
if err != nil {
fmt.Printf("unable to pull image from %s: %v", argsWithoutProg[0], err)
}

// if we get here and no image was found bail out
if img == nil {
fmt.Printf("unable to obtain image for %v", argsWithoutProg[0])
}
err = extractResource(img, opmBinarySuffix, argsWithoutProg[1])
if err != nil {
fmt.Printf("%v", err)
}
err = extractResource(img, opmCachePrefix, argsWithoutProg[1])
if err != nil {
fmt.Printf("%v", err)
}
err = extractResource(img, configsDir, argsWithoutProg[1])
if err != nil {
fmt.Printf("%v", err)
}
err = extractResource(img, opmCachePrefix, argsWithoutProg[1])
if err != nil {
fmt.Printf("%v", err)
}
}
28 changes: 16 additions & 12 deletions test/e2e/lib/check.sh
Expand Up @@ -3,26 +3,30 @@
# check_bundles ensures the number and names of bundles in catalog_image's index.json
# matches that of exp_bundles_list, and that all bundle images are pullable.
function check_bundles() {
local catalog_image="${1:?catalog image required}"
local catalog_image="${1:?catalog image required}"
local exp_bundles_list="${2:?expected bundles list must be set}"
local disconn_registry="${3:?disconnected registry host name must be set}"
local ns="${4:-""}"

crane export --insecure $catalog_image temp.tar
local index_dir="${DATA_TMP}/unpacked"
local tmp_cache_dir="${DATA_TMP}/unpacked_cache"
local index_dir="${DATA_TMP}/configs"
mkdir -p "$index_dir"
mkdir -p "$tmp_cache_dir"
local extraction_dir="${DATA_TMP}/extracted"
mkdir -p "$extraction_dir"
go run test/e2e/lib/catalog-extract/main.go $catalog_image $extraction_dir
local index_path="${index_dir}/index.json"
tar xvf temp.tar /configs/index.json --strip-components=1
tar xvf temp.tar /tmp/cache
mv index.json $index_dir
mv /tmp/cache $tmp_cache_dir
local cache_path="${extraction_dir}/tmp/cache"
local opm_path="${extraction_dir}/bin/opm"

cp $extraction_dir/configs/index.json $index_path

rm -f temp.tar
chmod +x $opm_path


opm validate $index_dir
opm serve $index_dir --cache-dir=$tmp_cache_dir --cache-only --cache-enforce-integrity
$opm_path validate $index_dir
echo $opm_path serve $index_dir --cache-dir=$cache_path --cache-only --cache-enforce-integrity
sleep 300
$opm_path serve $index_dir --cache-dir=$cache_path --cache-only --cache-enforce-integrity
rm -f "$extraction_dir"

declare -A exp_bundles_set
for bundle in $exp_bundles_list; do
Expand Down

0 comments on commit f101e83

Please sign in to comment.