Skip to content
Closed
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
55 changes: 55 additions & 0 deletions cmd/nerdctl/restart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main
Copy link
Member

@AkihiroSuda AkihiroSuda Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs copyright header

/*
Copyright The containerd 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 (
"fmt"
"io/ioutil"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecated ( https://golang.org/doc/go1.16#ioutil) plz use os package

"strings"
"testing"
"time"

"github.com/containerd/nerdctl/pkg/testutil"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
)

func TestRestart(t *testing.T) {
const (
hostPort = 8080
testContainerName = "nerdctl-test-stop-start-nginx"
sleepTime = 3 * time.Second
)
base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainerName).Run()

base.Cmd("run", "-d",
"--restart=no",
"--name", testContainerName,
"-p", fmt.Sprintf("127.0.0.1:%d:80", hostPort),
testutil.NginxAlpineImage).AssertOK()

check := func(httpGetRetry int) error {
resp, err := httpGet(fmt.Sprintf("http://127.0.0.1:%d", hostPort), httpGetRetry)
if err != nil {
return err
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if !strings.Contains(string(respBody), testutil.NginxAlpineIndexHTMLSnippet) {
return errors.Errorf("expected contain %q, got %q",
testutil.NginxAlpineIndexHTMLSnippet, string(respBody))
}
return nil
}

assert.NilError(t, check(30))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check the container output Before excuting restart ?


base.Cmd("restart", testContainerName).AssertOK()
base.Cmd("exec", testContainerName, "ps").AssertOK()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exec here ? Check not enough ?

time.Sleep(sleepTime)
if check(3) != nil {
t.Fatal("restart faild,expected to get an error")
}
assert.NilError(t, check(30))
}