Skip to content

Commit

Permalink
chore(CLI): e2e build test (#152)
Browse files Browse the repository at this point in the history
* feat: e2e build test

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* chore: Update

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Print in test

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fixL Fix logic

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Install sudo with root user

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed May 17, 2022
1 parent a7735f1 commit 0ca21d2
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 87 deletions.
28 changes: 0 additions & 28 deletions .github/labeler.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jobs:
${{ runner.OS }}-build-${{ env.cache-name }}-
${{ runner.OS }}-build-
${{ runner.OS }}-
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Verify mockgen
run: |
make generate && git add pkg
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ addlicense:
addlicense -c "The envd Authors" **/*.go **/**/*.go **/**/**/*.go

test: generate
@go test -race -coverprofile=coverage.out ./...
@go test -v -race -coverprofile=coverage.out ./...
@go tool cover -func coverage.out | tail -n 1 | awk '{ print "Total coverage: " $$3 }'

clean:
Expand Down
15 changes: 10 additions & 5 deletions cmd/envd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/builder"
"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/util/fileutil"
)
Expand Down Expand Up @@ -76,12 +78,15 @@ func build(clicontext *cli.Context) error {
}

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
flag.FlagBuildkitdContainer: viper.GetString(flag.FlagBuildkitdContainer),
flag.FlagSSHImage: viper.GetString(flag.FlagSSHImage),
})
logger.Debug("starting build")
logger.Debug("starting build command")

builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions cmd/envd/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 The envd Authors
//
// 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 main

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/tensorchord/envd/pkg/docker"
)

var _ = Describe("build command", func() {
buildContext := "testdata"
args := []string{
"envd.test", "--debug", "build", "--path", buildContext,
}
BeforeEach(func() {
cli, err := docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_ = cli.Destroy(context.TODO(), buildContext)
})
When("given the right arguments", func() {
It("should build successfully", func() {
_, err := run(args)
Expect(err).NotTo(HaveOccurred())
})
})
})
9 changes: 7 additions & 2 deletions cmd/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/tensorchord/envd/pkg/version"
)

func main() {
func run(args []string) (bool, error) {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func main() {
viper.Set(flag.FlagSSHImage, context.String(flag.FlagSSHImage))
return nil
}
handleErr(debugEnabled, app.Run(os.Args))
return debugEnabled, app.Run(args)
}

func handleErr(debug bool, err error) {
Expand All @@ -110,3 +110,8 @@ func handleErr(debug bool, err error) {
}
os.Exit(1)
}

func main() {
debug, err := run(os.Args)
handleErr(debug, err)
}
27 changes: 27 additions & 0 deletions cmd/envd/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 The envd Authors
//
// 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 main

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestMain(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "envd Suite")
}
4 changes: 4 additions & 0 deletions cmd/envd/testdata/build.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
base(os="ubuntu20.04", language="python3")
pip_package(name = [
"ormb",
])
41 changes: 28 additions & 13 deletions cmd/envd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/builder"
"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/ssh"
Expand Down Expand Up @@ -75,6 +77,11 @@ var CommandUp = &cli.Command{
Usage: "Timeout of container creation",
Value: time.Second * 30,
},
&cli.BoolFlag{
Name: "detach",
Usage: "detach from the container",
Value: false,
},
},

Action: up,
Expand Down Expand Up @@ -103,14 +110,20 @@ func up(clicontext *cli.Context) error {
}
ctr := fileutil.Base(buildContext)

detach := clicontext.Bool("detach")

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"container-name": ctr,
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"container-name": ctr,
"detach": detach,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
flag.FlagBuildkitdContainer: viper.GetString(flag.FlagBuildkitdContainer),
flag.FlagSSHImage: viper.GetString(flag.FlagSSHImage),
})
logger.Debug("starting up")
logger.Debug("starting up command")

builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
Expand All @@ -134,13 +147,15 @@ func up(clicontext *cli.Context) error {
}
logrus.Debugf("container %s is running", containerID)

sshClient, err := ssh.NewClient(
containerIP, "envd", 2222, clicontext.Bool("auth"), clicontext.Path("private-key"), "")
if err != nil {
return err
}
if err := sshClient.Attach(); err != nil {
return err
if !detach {
sshClient, err := ssh.NewClient(
containerIP, "root", 2222, clicontext.Bool("auth"), clicontext.Path("private-key"), "")
if err != nil {
return err
}
if err := sshClient.Attach(); err != nil {
return err
}
}

return nil
Expand Down
47 changes: 47 additions & 0 deletions cmd/envd/up_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 The envd Authors
//
// 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 main

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/tensorchord/envd/pkg/docker"
)

var _ = Describe("up command", func() {
buildContext := "testdata"
args := []string{
"envd.test", "--debug", "up", "--path", buildContext, "--detach",
}
BeforeEach(func() {
cli, err := docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_ = cli.Destroy(context.TODO(), buildContext)
})
When("given the right arguments", func() {
It("should up and destroy successfully", func() {
_, err := run(args)
Expect(err).NotTo(HaveOccurred())
destroyArgs := []string{
"envd.test", "--debug", "destroy", "--path", buildContext,
}
_, err = run(destroyArgs)
Expect(err).NotTo(HaveOccurred())
})
})
})
18 changes: 10 additions & 8 deletions pkg/lang/ir/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ func (g *Graph) compileBase() llb.State {
var base llb.State
if g.CUDA == nil && g.CUDNN == nil {
base = llb.Image("docker.io/library/python:3.8")
} else {
base = g.compileCUDAPackages()
}
base = g.compileCUDAPackages()
// TODO(gaocegege): Refactor user to a seperate stage.
run := base.
Run(llb.Shlex("groupadd -g 1000 envd")).
Run(llb.Shlex("useradd -p \"\" -u 1000 -g envd -s /bin/sh -m envd")).
Run(llb.Shlex("adduser envd sudo"))
return llb.User("envd")(run.Root())
return base
}

func (g *Graph) compileCUDAPackages() llb.State {
Expand Down Expand Up @@ -201,7 +197,13 @@ func (g Graph) compileBuiltinSystemPackages(root llb.State) llb.State {
llb.AsPersistentCacheDir("/"+cacheDir, llb.CacheMountShared))
run.AddMount(cacheLibDir, llb.Scratch(),
llb.AsPersistentCacheDir("/"+cacheLibDir, llb.CacheMountShared))
return run.Root()

// TODO(gaocegege): Refactor user to a seperate stage.
run = run.
Run(llb.Shlex("groupadd -g 1000 envd"), llb.WithCustomName("create user group envd")).
Run(llb.Shlex("useradd -p \"\" -u 1000 -g envd -s /bin/sh -m envd"), llb.WithCustomName("create user envd")).
Run(llb.Shlex("adduser envd sudo"), llb.WithCustomName("add user envd to sudoers"))
return llb.User("envd")(run.Root())
}

func (g Graph) compileSystemPackages(root llb.State) llb.State {
Expand Down
Loading

0 comments on commit 0ca21d2

Please sign in to comment.