Skip to content

Commit 418f266

Browse files
committed
[FAB-9751] address flakes in dockercontroller_test
Three issues resolved. - The command executed in the container started by the test completes very quickly but the test expected the container to remain running until stopped. - The container and image names created in the integration were always the same. This resulted in test pollution across tests. - TestGetDockerHostConfig set environment variables at the process scope. When running tests with the `-count n` flag, this impacted future test execution. Change-Id: I58feec423a8c2540923779013a6f6cc4b767e9b3 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 466e61b commit 418f266

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

core/container/dockercontroller/dockercontroller_test.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
"errors"
1616
"fmt"
1717
"io"
18-
"os"
1918
"testing"
2019
"time"
2120

22-
"github.com/fsouza/go-dockerclient"
21+
docker "github.com/fsouza/go-dockerclient"
2322
"github.com/spf13/viper"
2423
"github.com/stretchr/testify/assert"
2524
"github.com/stretchr/testify/require"
@@ -36,7 +35,7 @@ import (
3635
func TestIntegrationPath(t *testing.T) {
3736
coreutil.SetupTestConfig()
3837
ctxt := context.Background()
39-
dc := NewDockerVM("", "")
38+
dc := NewDockerVM("", util.GenerateUUID())
4039
ccid := ccintf.CCID{Name: "simple"}
4140

4241
err := dc.Start(ctxt, ccid, nil, nil, nil, InMemBuilder{})
@@ -67,18 +66,16 @@ func TestHostConfig(t *testing.T) {
6766
}
6867

6968
func TestGetDockerHostConfig(t *testing.T) {
70-
os.Setenv("CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE", "overlay")
71-
os.Setenv("CORE_VM_DOCKER_HOSTCONFIG_CPUSHARES", fmt.Sprint(1024*1024*1024*2))
7269
coreutil.SetupTestConfig()
7370
hostConfig = nil // There is a cached global singleton for docker host config, the other tests can collide with
7471
hostConfig := getDockerHostConfig()
7572
testutil.AssertNotNil(t, hostConfig)
76-
testutil.AssertEquals(t, hostConfig.NetworkMode, "overlay")
73+
testutil.AssertEquals(t, hostConfig.NetworkMode, "host")
7774
testutil.AssertEquals(t, hostConfig.LogConfig.Type, "json-file")
7875
testutil.AssertEquals(t, hostConfig.LogConfig.Config["max-size"], "50m")
7976
testutil.AssertEquals(t, hostConfig.LogConfig.Config["max-file"], "5")
8077
testutil.AssertEquals(t, hostConfig.Memory, int64(1024*1024*1024*2))
81-
testutil.AssertEquals(t, hostConfig.CPUShares, int64(1024*1024*1024*2))
78+
testutil.AssertEquals(t, hostConfig.CPUShares, int64(0))
8279
}
8380

8481
func Test_Start(t *testing.T) {
@@ -117,9 +114,11 @@ func Test_Start(t *testing.T) {
117114
testerr(t, err, false)
118115

119116
chaincodePath := "github.com/hyperledger/fabric/examples/chaincode/go/example01/cmd"
120-
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
117+
spec := &pb.ChaincodeSpec{
118+
Type: pb.ChaincodeSpec_GOLANG,
121119
ChaincodeId: &pb.ChaincodeID{Name: "ex01", Path: chaincodePath},
122-
Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}}
120+
Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")},
121+
}
123122
codePackage, err := platforms.GetDeploymentPayload(spec)
124123
if err != nil {
125124
t.Fatal()
@@ -261,16 +260,22 @@ func TestGetVMName(t *testing.T) {
261260
type InMemBuilder struct{}
262261

263262
func (imb InMemBuilder) Build() (io.Reader, error) {
263+
buf := &bytes.Buffer{}
264+
fmt.Fprintln(buf, "FROM busybox:latest")
265+
fmt.Fprintln(buf, `CMD ["tail", "-f", "/dev/null"]`)
266+
264267
startTime := time.Now()
265268
inputbuf := bytes.NewBuffer(nil)
266269
gw := gzip.NewWriter(inputbuf)
267270
tr := tar.NewWriter(gw)
268-
dockerFileContents := []byte("FROM busybox:latest\n\nCMD echo hello")
269-
dockerFileSize := int64(len([]byte(dockerFileContents)))
270-
271-
tr.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize,
272-
ModTime: startTime, AccessTime: startTime, ChangeTime: startTime})
273-
tr.Write([]byte(dockerFileContents))
271+
tr.WriteHeader(&tar.Header{
272+
Name: "Dockerfile",
273+
Size: int64(buf.Len()),
274+
ModTime: startTime,
275+
AccessTime: startTime,
276+
ChangeTime: startTime,
277+
})
278+
tr.Write(buf.Bytes())
274279
tr.Close()
275280
gw.Close()
276281
return inputbuf, nil
@@ -309,7 +314,8 @@ var getClientErr, createErr, uploadErr, noSuchImgErr, buildErr, removeImgErr,
309314
func (c *mockClient) CreateContainer(options docker.CreateContainerOptions) (*docker.Container, error) {
310315
if createErr {
311316
return nil, errors.New("Error creating the container")
312-
} else if noSuchImgErr && !c.noSuchImgErrReturned {
317+
}
318+
if noSuchImgErr && !c.noSuchImgErrReturned {
313319
c.noSuchImgErrReturned = true
314320
return nil, docker.ErrNoSuchImage
315321
}

0 commit comments

Comments
 (0)