Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,46 @@ This repository runs the OCM integration tests. It installs a local OCI registry

This project uses Python 3.9+ to run the tests against the [OCM CLI](https://github.com/open-component-model/ocm). It is targeted to be executed in a Github action workflow. You can also run the tests locally:

* Install Python 3.9+
* Install Python 3.9+ `sudo apt install -y python3 python3.10-venv`
* Create a virtual environment, e.g `python -m venv <path-to-your-env>/ocmtest`
* Install pip: `python -m pip install --upgrade pip`
* Activate environment: `. <path-to-your-env>/ocmtest/bin/activate`
* Activate environment: `. <path-to-your-env>/ocmtest/bin/activate` (on Windows: `. <path-to-your-env>\ocmtest\Scripts\Activate.ps1`) -- . D:\pip\ocm-integrationtest\Scripts\Activate.ps1
* Install requirements: `pip install -r requirements.txt`
* Install docker
* Install htpasswd
* Install [crane](https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane.md)
* Install htpasswd - `sudo apt -y install apache2-utils`
* Install [crane](https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane.md) - `go install github.com/google/go-containerregistry/cmd/crane@latest`
* Create SSL certificates and store them in `./certs` directory (see `create-cert.sh` for instructions [Link](create-cert.sh) (be sure to have a hostname with a fully qualified domain name)
* Create a user for the OCI registry and passwd file: `htpasswd -b -v certs/htpasswd ocmuser <my-secret-password>`
* Set environment variables: `export FDQN_NAME=<Your fully qualified host-name>:4430; export USER_NAME=ocmuser; export PASSWD=<my-secret-password>`
* If you user alternative container runtimes to docker (like e.g. colima) you may need to set DOCKER_HOST env var. e.g.: `export DOCKER_HOST=unix:///Users/<my-user>/.colima/default/docker.sock`
* If you use alternative container runtimes to docker (like e.g. colima) you may need to set DOCKER_HOST env var. e.g.: `export DOCKER_HOST=unix:///Users/<my-user>/.colima/default/docker.sock`
* Run local OCI registry in docker: `./start_docker.sh`
* Build local test binaries: `./build.sh`
* Run tests: `pytest tests`
* Stop and remove container: `./stop_docker.sh`

```bash
sudo apt -y install apache2-utils python3 python3.10-venv
go install github.com/google/go-containerregistry/cmd/crane@latest
python3 -m venv /mnt/d/pip/ocm-integrationtest/
python3 -m pip install --upgrade pip
. /mnt/d/pip/ocm-integrationtest/bin/activate
pip install -r requirements.txt
# start here
cd /mnt/d/git/ocm/ocm-integrationtest/
. /mnt/d/pip/ocm-integrationtest/bin/activate
mkdir -p
export FDQN_NAME=`hostname --fqdn`:4430

htpasswd -b -c certs/htpasswd ocmuser ocmpass
htpasswd -b -v certs/htpasswd ocmuser ocmpass
```

```powershell
cd D:\git\ocm\ocm-integrationtest\
. D:\pip\ocm-integrationtest\Scripts\Activate.ps1
pip install -r requirements.txt
```

## Support, Feedback, Contributing

This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/open-component-model/ocm-integrationtest/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
Expand Down
4 changes: 4 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go build -o local/hello.exe -ldflags '-extldflags "-static"' main.go
#env GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o local/hello.amd64 -ldflags '-extldflags "-static"' main.go
docker build -f Dockerfile.arm64 --tag hello-arm64:0.1.0 --tag eu.gcr.io/sap-cp-k8s-ocm-gcp-eu30-dev/dev/d058463/images/hello-arm64:0.1.0 .
docker build -f Dockerfile.amd64 --tag hello-amd64:0.1.0 --tag eu.gcr.io/sap-cp-k8s-ocm-gcp-eu30-dev/dev/d058463/images/hello-amd64:0.1.0 .
1 change: 1 addition & 0 deletions cases/multiple-arguments.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocm version 0.17.0-dev
348 changes: 348 additions & 0 deletions cases/no-args.golden

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cases/one-argument.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Major":"0", "Minor":"17", "Patch":"0", "PreRelease":"dev", "Meta":"", "GitVersion":"0.17.0-dev", "GitCommit":"", "GitTreeState":"", "BuildDate":"1970-01-01T00:00:00Z", "GoVersion":"go1.23.2", "Compiler":"gc", "Platform":"windows/amd64"}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/open-component-model/ocm-integrationtest

go 1.23.2

require github.com/sergi/go-diff v1.3.1 // indirect
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
141 changes: 141 additions & 0 deletions ocm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"text/template"

"github.com/sergi/go-diff/diffmatchpatch"
)

var binaryName = "ocm"

var binaryPath = "D:\\git\\ocm\\ocm\\cmds\\ocm\\"

var update = flag.Bool("update", false, "update golden case files")

func TestMain(m *testing.M) {
err := os.Chdir(binaryPath)
if err != nil {
fmt.Printf("could not change dir: %v", err)
os.Exit(1)
}

dir, err := os.Getwd()
if err != nil {
fmt.Printf("could not get current dir: %v", err)
}

binaryPath = filepath.Join(dir, binaryName)

os.Exit(m.Run())
}

func runBinary(args []string) ([]byte, error) {
cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")
return cmd.CombinedOutput()
}

func fixturePath(t *testing.T, fixture string) string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("problems recovering caller information")
}
return filepath.Join(filepath.Dir(filename), "cases", fixture)
}

func writeFixture(t *testing.T, fixture string, content []byte) {
err := os.WriteFile(fixturePath(t, fixture), content, 0o644)
if err != nil {
t.Fatal(err)
}
}

func loadFixture(t *testing.T, fixture string) string {
tmpl, err := template.ParseFiles(fixturePath(t, fixture))
if err != nil {
t.Fatal(err)
}
var tmplBytes bytes.Buffer
err = tmpl.Execute(&tmplBytes, envToMap())
if err != nil {
t.Fatal(err)
}
return tmplBytes.String()
}

func envToMap() map[string]string {
evpMap := map[string]string{}

for _, v := range os.Environ() {
split := strings.Split(v, "=")
if len(split) == 2 {
evpMap[split[0]] = split[1]
}
}
return evpMap
}

func TestCliArgs(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
}{
{"no arguments", []string{}, "no-args.golden"},
{"one argument", []string{"version"}, "one-argument.golden"},
{"multiple arguments", []string{"--version"}, "multiple-arguments.golden"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := runBinary(tt.args)

if err != nil {
t.Fatal(err)
}

if *update {
writeFixture(t, tt.fixture, output)
}

actual := string(output)

expected := loadFixture(t, tt.fixture)

if !reflect.DeepEqual(actual, expected) {
// t.Fatalf("actual = %s, expected = %s", actual, expected)
//diff.Diff("actual", []byte(actual), "expected", []byte(expected))
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(actual, expected, true)
//fmt.Println(dmp.DiffPrettyText(diff))
var newDiffs []diffmatchpatch.Diff
for i, item := range diffs {
if item.Type != diffmatchpatch.DiffEqual {
if i-1 >= 0 && diffs[i-1].Type == diffmatchpatch.DiffEqual {
newDiffs = append(newDiffs, diffs[i-1])
}
newDiffs = append(newDiffs, item)
if i+1 <= len(diffs) && diffs[i+1].Type == diffmatchpatch.DiffEqual {
newDiffs = append(newDiffs, diffs[i+1])
}
}
}

fmt.Printf("len(diff) = %v", len(diffs))
fmt.Printf("len(newDiffs) = %v", len(newDiffs))
//t.Errorf("diff = %s", dmp.DiffToDelta(diffs))
t.Errorf("diff = %s", dmp.DiffPrettyText(newDiffs))
}
})
}
}