Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add dependency support
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 25, 2022
1 parent 1e8825c commit e90f652
Show file tree
Hide file tree
Showing 25 changed files with 508 additions and 200 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ github.com/acorn-io/apiserver v0.24.1-ot-1 h1:HPyswxxeEMq2gywsyQo+/fknhhl50SErvq
github.com/acorn-io/apiserver v0.24.1-ot-1/go.mod h1:J41BYwfMMj7Mm6OfFX3mup3myklBebjEE5mfL6zm/Jg=
github.com/acorn-io/apiserver-1 v0.0.0-20220608053213-0ffc3be57697 h1:zEzrL1ewSmEJSYdHamsxBO9JAf/z+xBC9mSq0oH6jkA=
github.com/acorn-io/apiserver-1 v0.0.0-20220608053213-0ffc3be57697/go.mod h1:sG6OmZ4yEWeQ9JmGjnp8WgQAk9D9z4hivMFsUUh9QF8=
github.com/acorn-io/baaah v0.0.0-20220621163714-34375f25ecdb h1:AK/vua0bYfO6+E0jlVI+XhWNrgEzs+dfRBunpaoAotI=
github.com/acorn-io/baaah v0.0.0-20220621163714-34375f25ecdb/go.mod h1:9FvNcgPq6o0x8Z8Nfq9EvG6tTvw7yVkagoNcMjvcm10=
github.com/acorn-io/baaah v0.0.0-20220624052343-a3d0714dcd95 h1:ltqQq25iSE5//wiO6ohFKhp4fVxv6H7JyCzxIff/OcI=
github.com/acorn-io/baaah v0.0.0-20220624052343-a3d0714dcd95/go.mod h1:CSj9RfR9Ab5LsLmdAcJlrMtyz0tD1kfqAKiLnbkXDg0=
github.com/acorn-io/component-base v0.24.1-ot-1 h1:GRbiCcCxZdKovA1L8eLFIvI9pJp3kJFOtKTOW7LSsQI=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package client
package apps

import (
"strings"
"testing"

client2 "github.com/acorn-io/acorn/integration/client"
"github.com/acorn-io/acorn/integration/helper"
apiv1 "github.com/acorn-io/acorn/pkg/apis/api.acorn.io/v1"
v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
Expand All @@ -22,7 +23,7 @@ func TestAppStartStop(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestAppDelete(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -105,8 +106,8 @@ func TestAppUpdate(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID2 := newImage2(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
imageID2 := client2.NewImage2(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -332,7 +333,7 @@ func TestAppGet(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -361,7 +362,7 @@ func TestAppList(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -392,7 +393,7 @@ func TestAppLog(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down Expand Up @@ -437,7 +438,7 @@ func TestAppRun(t *testing.T) {
kclient := helper.MustReturn(kclient.Default)
ns := helper.TempNamespace(t, kclient)

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

c, err := client.New(restConfig, ns.Name)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions integration/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"testing"

"github.com/acorn-io/acorn/integration/helper"
"github.com/acorn-io/acorn/pkg/build"
)

func NewImage2(t *testing.T, namespace string) string {
image, err := build.Build(helper.GetCTX(t), "../testdata/nginx2/acorn.cue", &build.Options{
Client: helper.BuilderClient(t, namespace),
Cwd: "../testdata/nginx2",
})
if err != nil {
t.Fatal(err)
}
return image.ID
}

func NewImage(t *testing.T, namespace string) string {
image, err := build.Build(helper.GetCTX(t), "../testdata/nginx/acorn.cue", &build.Options{
Client: helper.BuilderClient(t, namespace),
Cwd: "../testdata/nginx",
})
if err != nil {
t.Fatal(err)
}
return image.ID
}
22 changes: 0 additions & 22 deletions integration/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func newImage2(t *testing.T, namespace string) string {
image, err := build.Build(helper.GetCTX(t), "./testdata/nginx2/acorn.cue", &build.Options{
Client: helper.BuilderClient(t, namespace),
Cwd: "./testdata/nginx2",
})
if err != nil {
t.Fatal(err)
}
return image.ID
}

func newImage(t *testing.T, namespace string) string {
image, err := build.Build(helper.GetCTX(t), "./testdata/nginx/acorn.cue", &build.Options{
Client: helper.BuilderClient(t, namespace),
Cwd: "./testdata/nginx",
})
if err != nil {
t.Fatal(err)
}
return image.ID
}

func TestFriendlyNameInContainer(t *testing.T) {
helper.StartController(t)
cfg := helper.StartAPI(t)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package client
package containers

import (
"io/ioutil"
"strings"
"testing"

client2 "github.com/acorn-io/acorn/integration/client"
"github.com/acorn-io/acorn/integration/helper"
apiv1 "github.com/acorn-io/acorn/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/acorn/pkg/client"
Expand All @@ -30,7 +31,7 @@ func TestContainerList(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
app, err := c.AppRun(ctx, imageID, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestContainerDelete(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
app, err := c.AppRun(ctx, imageID, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestContainerGet(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
app, err := c.AppRun(ctx, imageID, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestContainerExec(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
app, err := c.AppRun(ctx, imageID, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestContainerDebugExec(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
app, err := c.AppRun(ctx, imageID, nil)
if err != nil {
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package credentials

import (
"sort"
Expand Down
101 changes: 101 additions & 0 deletions integration/client/depends_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package client

import (
"context"
"strconv"
"testing"

"github.com/acorn-io/acorn/integration/helper"
v1 "github.com/acorn-io/acorn/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/acorn/pkg/build"
"github.com/acorn-io/acorn/pkg/client"
"github.com/acorn-io/acorn/pkg/k8sclient"
"github.com/acorn-io/acorn/pkg/labels"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func depImage(t *testing.T, c client.Client) string {
image, err := build.Build(helper.GetCTX(t), "./testdata/dependson/acorn.cue", &build.Options{
Client: c,
Cwd: "./testdata/dependson",
})
if err != nil {
t.Fatal(err)
}
return image.ID
}

func toRevision(t *testing.T, obj kclient.Object) int {
i, err := strconv.Atoi(obj.GetResourceVersion())
if err != nil {
t.Fatalf("Invalid resource version %s on %s/%s", obj.GetResourceVersion(), obj.GetNamespace(), obj.GetName())
}
return i
}

func TestDependsOn(t *testing.T) {
ctx := context.Background()
c, _ := helper.ClientAndNamespace(t)
k8sclient := helper.MustReturn(k8sclient.Default)
image := depImage(t, c)

app, err := c.AppRun(ctx, image, nil)
if err != nil {
t.Fatal(err)
}

jobs := map[string]int{}
deployments := map[string]int{}

app = helper.WaitForObject(t, c.GetClient().Watch, &v1.AppList{}, app, func(app *v1.App) bool {
return app.Status.Namespace != ""
})

eg := errgroup.Group{}

eg.Go(func() error {
helper.Wait(t, k8sclient.Watch, &batchv1.JobList{}, func(job *batchv1.Job) bool {
if job.Namespace != app.Status.Namespace {
return false
}
name := job.Labels[labels.AcornJobName]
if _, ok := jobs[name]; !ok {
jobs[name] = toRevision(t, job)
if len(jobs) == 2 {
return true
}
}
return false
})
return nil
})

eg.Go(func() error {
helper.Wait(t, k8sclient.Watch, &appsv1.DeploymentList{}, func(dep *appsv1.Deployment) bool {
if dep.Namespace != app.Status.Namespace {
return false
}
name := dep.Labels[labels.AcornContainerName]
if _, ok := deployments[name]; !ok {
deployments[name] = toRevision(t, dep)
if len(deployments) == 3 {
return true
}
}
return false
})
return nil
})

_ = eg.Wait()

assert.Less(t, jobs["job2"], jobs["job1"])
assert.Less(t, jobs["job1"], deployments["one"])
assert.Less(t, jobs["job2"], deployments["one"])
assert.Less(t, deployments["one"], deployments["two"])
assert.Less(t, deployments["two"], deployments["three"])
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package client
package images

import (
"strings"
"testing"

client2 "github.com/acorn-io/acorn/integration/client"
"github.com/acorn-io/acorn/integration/helper"
"github.com/acorn-io/acorn/pkg/build"
"github.com/acorn-io/acorn/pkg/client"
Expand All @@ -25,7 +26,7 @@ func TestImageListGetDelete(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)
images, err := c.ImageList(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -70,10 +71,10 @@ func TestImageTagMove(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
image2, err := build.Build(helper.GetCTX(t), "./testdata/nginx2/acorn.cue", &build.Options{
imageID := client2.NewImage(t, ns.Name)
image2, err := build.Build(helper.GetCTX(t), "../testdata/nginx2/acorn.cue", &build.Options{
Client: helper.BuilderClient(t, ns.Name),
Cwd: "./testdata/nginx2",
Cwd: "../testdata/nginx2",
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -121,7 +122,7 @@ func TestImageTag(t *testing.T) {
t.Fatal(err)
}

_ = newImage(t, ns.Name)
_ = client2.NewImage(t, ns.Name)
images, err := c.ImageList(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestImagePush(t *testing.T) {
t.Fatal(err)
}

_ = newImage(t, ns.Name)
_ = client2.NewImage(t, ns.Name)
images, err := c.ImageList(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -220,7 +221,7 @@ func TestImagePull(t *testing.T) {
t.Fatal(err)
}

id := newImage(t, ns.Name)
id := client2.NewImage(t, ns.Name)
tagName := registry + "/test:ci"

err = c.ImageTag(ctx, id, tagName)
Expand Down Expand Up @@ -283,7 +284,7 @@ func TestImageDetails(t *testing.T) {
t.Fatal(err)
}

id := newImage(t, ns.Name)
id := client2.NewImage(t, ns.Name)
remoteTagName := registry + "/test:ci"

err = c.ImageTag(ctx, id, remoteTagName)
Expand All @@ -309,7 +310,7 @@ func TestImageDetails(t *testing.T) {
t.Fatal(err)
}

imageID := newImage(t, ns.Name)
imageID := client2.NewImage(t, ns.Name)

err = c.ImageTag(ctx, imageID, "foo")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package secrets

import (
"testing"
Expand Down
Loading

0 comments on commit e90f652

Please sign in to comment.