8000 app/add: Use the image name as a default name for app by nhlfr · Pull Request #3802 · rkt/rkt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 24, 2020. It is now read-only.

app/add: Use the image name as a default name for app #3802

Merged
merged 1 commit into from
Sep 28, 2017
Merged
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
8000
14 changes: 14 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,17 @@ func GetOSArch() (os string, arch string) {
os, arch, _ = types.ToAppcOSArch(runtime.GOOS, arch, flavor)
return os, arch
}

// ImageNameToAppName converts the full name of image to an app name without special
// characters - we use it as a default app name when specyfing it is optional
func ImageNameToAppName(name types.ACIdentifier) (*types.ACName, error) {
parts := strings.Split(name.String(), "/")
last := parts[len(parts)-1]

sn, err := types.SanitizeACName(last)
if err != nil {
return nil, err
}

return types.MustACName(sn), nil
}
56 changes: 56 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2017 The rkt 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 common

import (
"testing"

"github.com/appc/spec/schema/types"
)

func TestImageNameToAppName(t *testing.T) {
for _, tt := range []struct {
in types.ACIdentifier
out types.ACName
err bool
}{
{
in: types.ACIdentifier("coreos.com/etcd:v2.0.0"),
out: types.ACName("etcd-v2-0-0"),
err: false,
},
{
in: types.ACIdentifier("coreos.com/etcd"),
out: types.ACName("etcd"),
err: false,
},
{
in: types.ACIdentifier("docker://registry.hub.docker.com/library/fedora"),
out: types.ACName("fedora"),
err: false,
},
} {
appName, err := ImageNameToAppName(tt.in)
if err != nil {
if !tt.err {
t.Fatal(err)
}
} else if appName == nil {
t.Errorf("got nil app name without any error")
} else if *appName != tt.out {
t.Errorf("got %s app name, expected %s app name", appName, tt.out)
}
}
}
3 changes: 2 additions & 1 deletion stage0/app_add.go
< 8000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ func AddApp(cfg AddConfig) error {
return err
}
} else {
appName, err = imageNameToAppName(am.Name)
appName, err = common.ImageNameToAppName(am.Name)
if err != nil {
return err
}
app.Name = appName.String()
}

pod, err := pkgPod.PodFromUUIDString(cfg.DataDir, cfg.UUID.String())
Expand Down
14 changes: 1 addition & 13 deletions stage0/run.go
10000
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,6 @@ func mergeEnvs(appEnv *types.Environment, env []string, override bool) {
}
}

func imageNameToAppName(name types.ACIdentifier) (*types.ACName, error) {
parts := strings.Split(name.String(), "/")
last := parts[len(parts)-1]

sn, err := types.SanitizeACName(last)
if err != nil {
return nil, err
}

return types.MustACName(sn), nil
}

// deduplicateMPs removes Mounts with duplicated paths. If there's more than
// one Mount with the same path, it keeps the first one encountered.
func deduplicateMPs(mounts []schema.Mount) []schema.Mount {
Expand Down Expand Up @@ -213,7 +201,7 @@ func generatePodManifest(cfg PrepareConfig, dir string) ([]byte, error) {
}

if app.Name == "" {
appName, err := imageNameToAppName(am.Name)
appName, err := common.ImageNameToAppName(am.Name)
if err != nil {
return errwrap.Wrap(errors.New("error converting image name to app name"), err)
}
Expand Down
25 changes: 25 additions & 0 deletions tests/rkt_app_sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ func TestAppSandboxAddStartRemove(t *testing.T) {
})
}

// TestAppSandboxAddDefaultAppName tests applying default names to apps.
// It starts the sandbox, adds one app and ensures that it has a name converted from
// the image name.
func TestAppSandboxAddDefaultAppName(t *testing.T) {
testSandbox(t, func(ctx *testutils.RktRunCtx, child *gexpect.ExpectSubprocess, podUUID string) {
imageName := "coreos.com/rkt-inspect/hello"
msg := "HelloFromAppInSandbox"

expectedAppName := "hello"

aciHello := patchTestACI("rkt-inspect-hello.aci", "--name="+imageName, "--exec=/inspect --print-msg="+msg)
defer os.Remove(aciHello)

combinedOutput(t, ctx.ExecCmd("fetch", "--insecure-options=image", aciHello))
combinedOutput(t, ctx.ExecCmd("app", "add", "--debug", podUUID, imageName))

podInfo := getPodInfo(t, ctx, podUUID)
appName := podInfo.manifest.Apps[0].Name

if appName.String() != expectedAppName {
t.Errorf("got %s app name, expected %s app name", appName, expectedAppName)
}
})
}

// TestAppSandboxMultipleApps tests multiple apps in a sandbox:
// one that exits successfully, one that exits with an error, and one that keeps running.
func TestAppSandboxMultipleApps(t *testing.T) {
Expand Down
0