func TestIsZeroWhenConfigHasValues(t *testing.T) {
	hasValues := map[string]bool{
		"CPUShares":   true,
		"Command":     true,
		"Hostname":    true,
		"Image":       true,
		"Links":       true,
		"MemLimit":    true,
		"Privileged":  true,
		"ReadOnly":    true,
		"SecurityOpt": true,
		"User":        true,
		"WorkingDir":  true,
	}

	serviceConfig := &config.ServiceConfig{
		CPUShares:   yaml.StringorInt(int64(10)),
		Command:     []string{"cmd"},
		Hostname:    "foobarbaz",
		Image:       "testimage",
		Links:       []string{"container1"},
		MemLimit:    yaml.StringorInt(int64(104857600)),
		Privileged:  true,
		ReadOnly:    true,
		SecurityOpt: []string{"label:type:test_virt"},
		User:        "******",
		WorkingDir:  "/var",
	}

	configValue := reflect.ValueOf(serviceConfig).Elem()
	configType := configValue.Type()

	for i := 0; i < configValue.NumField(); i++ {
		f := configValue.Field(i)
		ft := configType.Field(i)
		fieldName := ft.Name

		zeroValue := isZero(f)
		_, hasValue := hasValues[fieldName]
		if zeroValue == hasValue {
			t.Errorf("Expected field [%s]: hasValues[%t] but found[%t]", ft.Name, hasValues, !zeroValue)
		}
	}
}
func TestMemSwappiness(t *testing.T) {
	ctx := &ctx.Context{}
	sc := &config.ServiceConfig{
		MemSwappiness: yaml.StringorInt(10),
	}
	_, hostCfg, err := Convert(sc, ctx.Context, nil)
	assert.Nil(t, err)

	assert.Equal(t, int64(10), *hostCfg.MemorySwappiness)
}
Exemple #3
0
func TestParseWithMultipleComposeFiles(t *testing.T) {
	configOne := []byte(`
  multiple:
    image: tianon/true
    ports:
      - 8000`)

	configTwo := []byte(`
  multiple:
    image: busybox
    container_name: multi
    ports:
      - 9000`)

	configThree := []byte(`
  multiple:
    image: busybox
    mem_limit: 40000000
    ports:
      - 10000`)

	p := NewProject(&Context{
		ComposeBytes: [][]byte{configOne, configTwo},
	}, nil, nil)

	err := p.Parse()

	assert.Nil(t, err)

	multipleConfig, _ := p.ServiceConfigs.Get("multiple")
	assert.Equal(t, "busybox", multipleConfig.Image)
	assert.Equal(t, "multi", multipleConfig.ContainerName)
	assert.Equal(t, []string{"8000", "9000"}, multipleConfig.Ports)

	p = NewProject(&Context{
		ComposeBytes: [][]byte{configTwo, configOne},
	}, nil, nil)

	err = p.Parse()

	assert.Nil(t, err)

	multipleConfig, _ = p.ServiceConfigs.Get("multiple")
	assert.Equal(t, "tianon/true", multipleConfig.Image)
	assert.Equal(t, "multi", multipleConfig.ContainerName)
	assert.Equal(t, []string{"9000", "8000"}, multipleConfig.Ports)

	p = NewProject(&Context{
		ComposeBytes: [][]byte{configOne, configTwo, configThree},
	}, nil, nil)

	err = p.Parse()

	assert.Nil(t, err)

	multipleConfig, _ = p.ServiceConfigs.Get("multiple")
	assert.Equal(t, "busybox", multipleConfig.Image)
	assert.Equal(t, "multi", multipleConfig.ContainerName)
	assert.Equal(t, []string{"8000", "9000", "10000"}, multipleConfig.Ports)
	assert.Equal(t, yaml.StringorInt(40000000), multipleConfig.MemLimit)
}
func TestConvertToTaskDefinition(t *testing.T) {
	name := "mysql"
	cpu := int64(131072) // 128 * 1024
	command := "cmd"
	hostname := "foobarbaz"
	image := "testimage"
	links := []string{"container1"}
	memory := int64(131072) // 128 GiB = 131072 MiB
	privileged := true
	readOnly := true
	securityOpts := []string{"label:type:test_virt"}
	user := "******"
	workingDir := "/var"

	serviceConfig := &config.ServiceConfig{
		CPUShares:   yaml.StringorInt(cpu),
		Command:     []string{command},
		Hostname:    hostname,
		Image:       image,
		Links:       links,
		MemLimit:    yaml.StringorInt(int64(1048576) * memory), //1 MiB = 1048576B
		Privileged:  privileged,
		ReadOnly:    readOnly,
		SecurityOpt: securityOpts,
		User:        user,
		WorkingDir:  workingDir,
	}

	// convert
	taskDefinition := convertToTaskDefinitionInTest(t, name, serviceConfig)
	containerDef := *taskDefinition.ContainerDefinitions[0]

	// verify
	if name != aws.StringValue(containerDef.Name) {
		t.Errorf("Expected Name [%s] But was [%s]", name, aws.StringValue(containerDef.Name))
	}
	if cpu != aws.Int64Value(containerDef.Cpu) {
		t.Errorf("Expected cpu [%s] But was [%s]", cpu, aws.Int64Value(containerDef.Cpu))
	}
	if len(containerDef.Command) != 1 || command != aws.StringValue(containerDef.Command[0]) {
		t.Errorf("Expected command [%s] But was [%v]", command, containerDef.Command)
	}
	if !reflect.DeepEqual(securityOpts, aws.StringValueSlice(containerDef.DockerSecurityOptions)) {
		t.Errorf("Expected securityOpt [%v] But was [%v]", securityOpts, aws.StringValueSlice(containerDef.DockerSecurityOptions))
	}
	if hostname != aws.StringValue(containerDef.Hostname) {
		t.Errorf("Expected hostname [%s] But was [%s]", hostname, aws.StringValue(containerDef.Hostname))
	}
	if image != aws.StringValue(containerDef.Image) {
		t.Errorf("Expected Image [%s] But was [%s]", image, aws.StringValue(containerDef.Image))
	}
	if !reflect.DeepEqual(links, aws.StringValueSlice(containerDef.Links)) {
		t.Errorf("Expected links [%v] But was [%v]", links, aws.StringValueSlice(containerDef.Links))
	}
	if memory != aws.Int64Value(containerDef.Memory) {
		t.Errorf("Expected memory [%s] But was [%s]", memory, aws.Int64Value(containerDef.Memory))
	}
	if privileged != aws.BoolValue(containerDef.Privileged) {
		t.Errorf("Expected privileged [%s] But was [%s]", privileged, aws.BoolValue(containerDef.Privileged))
	}
	if readOnly != aws.BoolValue(containerDef.ReadonlyRootFilesystem) {
		t.Errorf("Expected ReadonlyRootFilesystem [%s] But was [%s]", readOnly, aws.BoolValue(containerDef.ReadonlyRootFilesystem))
	}
	if user != aws.StringValue(containerDef.User) {
		t.Errorf("Expected user [%s] But was [%s]", user, aws.StringValue(containerDef.User))
	}
	if workingDir != aws.StringValue(containerDef.WorkingDirectory) {
		t.Errorf("Expected WorkingDirectory [%s] But was [%s]", workingDir, aws.StringValue(containerDef.WorkingDirectory))
	}
}