Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide unit tests for container update agent service #198

Merged
101 changes: 101 additions & 0 deletions containerm/pkg/testutil/mocks/updateagent/mock_operation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions containerm/updateagent/internal_desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type internalDesiredState struct {
desiredState *types.DesiredState
systemContainers []string

containers map[string]*ctrtypes.Container
containers []*ctrtypes.Container
baselines map[string][]*ctrtypes.Container
}

Expand Down Expand Up @@ -98,12 +98,15 @@ func toInternalDesiredState(desiredState *types.DesiredState, domainName string)
for _, configPair := range desiredState.Domains[0].Config {
if configPair.Key == keySystemContainers {
systemContainers = strings.Split(configPair.Value, ",")
for index, name := range systemContainers {
systemContainers[index] = strings.TrimSpace(name)
}
}
}

return &internalDesiredState{
desiredState: desiredState,
containers: util.AsNamedMap(containers),
containers: containers,
baselines: baselines,
systemContainers: systemContainers,
}, nil
Expand Down
136 changes: 136 additions & 0 deletions containerm/updateagent/internal_desired_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0

package updateagent

import (
"testing"

"github.com/eclipse-kanto/container-management/containerm/pkg/testutil"

"github.com/eclipse-kanto/update-manager/api/types"
)

func TestFindComponent(t *testing.T) {
desiredState := &internalDesiredState{
desiredState: &types.DesiredState{
Domains: []*types.Domain{{
Components: []*types.ComponentWithConfig{
createSimpleDesiredComponent(testContainerName, testContainerVersion),
createSimpleDesiredComponent(testContainerName2, testContainerVersion2),
},
}},
},
}
testCases := map[string]struct {
target string
expected types.Component
}{
"test_present_1": {target: testContainerName, expected: types.Component{ID: testContainerName, Version: testContainerVersion}},
"test_present_2": {target: testContainerName2, expected: types.Component{ID: testContainerName2, Version: testContainerVersion2}},
"test_missing": {target: "missing", expected: types.Component{}},
"test_empty": {target: "", expected: types.Component{}},
}
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
testutil.AssertEqual(t, testCase.expected, desiredState.findComponent(testCase.target))
})
}
}

func TestToInternalDesiredStateDomainError(t *testing.T) {
testCases := map[string]([]string){
"test_no_domains": nil,
"test_empty_domains": {},
"test_no_containers_domain": {"containerized-apps"},
"test_too_many_domains": {"containers", "apps"},
}
for testName, testCaseDomains := range testCases {
t.Run(testName, func(t *testing.T) {
testDesiredState := &types.DesiredState{}
if testCaseDomains != nil {
domains := []*types.Domain{}
for _, domain := range testCaseDomains {
domains = append(domains, &types.Domain{ID: domain})
}
testDesiredState.Domains = domains
}
internalDesiredState, err := toInternalDesiredState(testDesiredState, "containers")
testutil.AssertNil(t, internalDesiredState)
testutil.AssertNotNil(t, err)

})
}
}

func TestToInternalDesiredContainersError(t *testing.T) {
testDesiredState := &types.DesiredState{Domains: []*types.Domain{{ID: "containers",
Components: []*types.ComponentWithConfig{{
Component: types.Component{ID: testContainerName, Version: testContainerVersion},
Config: []*types.KeyValuePair{{Key: "image", Value: ""}},
}},
}}}
internalDesiredState, err := toInternalDesiredState(testDesiredState, "containers")
testutil.AssertNil(t, internalDesiredState)
testutil.AssertNotNil(t, err)
}

func TestToInternalDesiredBaselinesError(t *testing.T) {
testDesiredState := &types.DesiredState{
Domains: []*types.Domain{{ID: "containers",
Components: []*types.ComponentWithConfig{
createSimpleDesiredComponent(testContainerName, testContainerVersion),
},
}},
Baselines: []*types.Baseline{
{Title: "test-baseline", Components: []string{"containers:" + testContainerName, "containers:" + testContainerName2}},
},
}
internalDesiredState, err := toInternalDesiredState(testDesiredState, "containers")
testutil.AssertNil(t, internalDesiredState)
testutil.AssertNotNil(t, err)
}

func TestToInternalDesiredStateSystemContainers(t *testing.T) {
testCases := map[string]struct {
key string
value string
expected []string
}{
"test_valid_system_containers": {key: "systemContainers", value: "sys-container-1, corelib", expected: []string{"sys-container-1", "corelib"}},
"test_no_system_containers": {key: "coreContainers", value: "some-container"},
"test_no_config": {},
}
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
testDesiredState := &types.DesiredState{
Domains: []*types.Domain{{ID: "containers",
Config: []*types.KeyValuePair{{Key: testCase.key, Value: testCase.value}},
Components: []*types.ComponentWithConfig{
createSimpleDesiredComponent(testContainerName, testContainerVersion),
createSimpleDesiredComponent(testContainerName2, testContainerVersion2),
},
}},
Baselines: []*types.Baseline{
{Title: "test-baseline", Components: []string{"containers:" + testContainerName}},
},
}
internalDesiredState, err := toInternalDesiredState(testDesiredState, "containers")
testutil.AssertNil(t, err)
testutil.AssertEqual(t, testDesiredState, internalDesiredState.desiredState)
testutil.AssertEqual(t, 2, len(internalDesiredState.containers))
testutil.AssertEqual(t, 2, len(internalDesiredState.baselines))
testutil.AssertEqual(t, testCase.expected, internalDesiredState.systemContainers)

})
}
}
107 changes: 107 additions & 0 deletions containerm/updateagent/to_containers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0

package updateagent

import (
"testing"

ctrtypes "github.com/eclipse-kanto/container-management/containerm/containers/types"
"github.com/eclipse-kanto/container-management/containerm/pkg/testutil"

"github.com/eclipse-kanto/update-manager/api/types"
)

func TestToContainerMinimalConfig(t *testing.T) {
containerConfig := createSimpleDesiredComponent(testContainerName, testContainerVersion)
container, err := toContainer(containerConfig)
testutil.AssertNil(t, err)
testutil.AssertNotEqual(t, "", container.ID)
testutil.AssertEqual(t, testContainerName, container.Name)
testutil.AssertEqual(t, testContainerName+":"+testContainerVersion, container.Image.Name)
testutil.AssertEqual(t, testContainerName+"-domain", container.DomainName)
testutil.AssertEqual(t, testContainerName+"-host", container.HostName)

testutil.AssertEqual(t, false, container.HostConfig.Privileged)
testutil.AssertEqual(t, ctrtypes.NetworkModeBridge, container.HostConfig.NetworkMode)
testutil.AssertEqual(t, ctrtypes.UnlessStopped, container.HostConfig.RestartPolicy.Type)
testutil.AssertEqual(t, 0, container.HostConfig.RestartPolicy.MaximumRetryCount)
testutil.AssertNil(t, container.HostConfig.Resources)
testutil.AssertNil(t, container.HostConfig.Devices)
testutil.AssertNil(t, container.HostConfig.PortMappings)
testutil.AssertNil(t, container.HostConfig.ExtraHosts)
testutil.AssertEqual(t, ctrtypes.LogConfigDriverJSONFile, container.HostConfig.LogConfig.DriverConfig.Type)
testutil.AssertEqual(t, 2, container.HostConfig.LogConfig.DriverConfig.MaxFiles)
testutil.AssertEqual(t, "100M", container.HostConfig.LogConfig.DriverConfig.MaxSize)
testutil.AssertEqual(t, ctrtypes.LogModeBlocking, container.HostConfig.LogConfig.ModeConfig.Mode)
testutil.AssertEqual(t, "", container.HostConfig.LogConfig.ModeConfig.MaxBufferSize)
testutil.AssertEqual(t, ctrtypes.IOConfig{}, *container.IOConfig)
testutil.AssertNil(t, container.Mounts)
}

func TestToContainer(t *testing.T) {
containerConfig := &types.ComponentWithConfig{
Component: types.Component{ID: testContainerName, Version: testContainerVersion},
Config: []*types.KeyValuePair{
// device mappings
{Key: "device", Value: "/dev/abc:/dev/ABC"}, // valid setting
{Key: "device", Value: "/dev/xyz"}, // invalid setting, shall be ignored
// port mappings
{Key: "port", Value: "80:8888/tcp"}, // valid setting
{Key: "port", Value: "123.456.789.000:80:80"}, // invalid setting, shall be ignored
// mounts
{Key: "mount", Value: "/tmp:/var/tmp"}, // valid setting
{Key: "mount", Value: "/TMP"}, // invalid setting, shall be ignored
// extra hosts
{Key: "host", Value: "ctr_host"},
{Key: "host", Value: "testhost"},
// env & cmd
{Key: "env", Value: "DEBUG=true"},
{Key: "cmd", Value: "arg1"},
{Key: "env", Value: "ENV1="},
{Key: "cmd", Value: "arg2"},
// restart policy
{Key: "restartPolicy", Value: "on-failure"},
{Key: "restartMaxRetries", Value: "5"},
{Key: "restartTimeout", Value: "X"}, // not valid, shall fallback to 0
// io config
{Key: "terminal", Value: "YES"},
{Key: "interactive", Value: "1"},
{Key: "memory", Value: "50M"},
},
}
container, err := toContainer(containerConfig)
testutil.AssertNil(t, err)

testutil.AssertEqual(t, 1, len(container.HostConfig.Devices))
testutil.AssertEqual(t, "/dev/abc", container.HostConfig.Devices[0].PathOnHost)
testutil.AssertEqual(t, "/dev/ABC", container.HostConfig.Devices[0].PathInContainer)

testutil.AssertEqual(t, 1, len(container.HostConfig.PortMappings))
testutil.AssertEqual(t, "0.0.0.0", container.HostConfig.PortMappings[0].HostIP)
testutil.AssertEqual(t, uint16(80), container.HostConfig.PortMappings[0].HostPort)
testutil.AssertEqual(t, uint16(80), container.HostConfig.PortMappings[0].HostPortEnd)
testutil.AssertEqual(t, uint16(8888), container.HostConfig.PortMappings[0].ContainerPort)
testutil.AssertEqual(t, "tcp", container.HostConfig.PortMappings[0].Proto)

testutil.AssertEqual(t, 1, len(container.Mounts))
testutil.AssertEqual(t, "/tmp", container.Mounts[0].Source)
testutil.AssertEqual(t, "/var/tmp", container.Mounts[0].Destination)

testutil.AssertEqual(t, []string{"ctr_host", "testhost"}, container.HostConfig.ExtraHosts)

testutil.AssertEqual(t, []string{"arg1", "arg2"}, container.Config.Cmd)
testutil.AssertEqual(t, []string{"DEBUG=true", "ENV1="}, container.Config.Env)
testutil.AssertEqual(t, &ctrtypes.RestartPolicy{Type: ctrtypes.OnFailure, MaximumRetryCount: 5}, container.HostConfig.RestartPolicy)
testutil.AssertEqual(t, &ctrtypes.IOConfig{Tty: false, OpenStdin: true}, container.IOConfig)
testutil.AssertEqual(t, &ctrtypes.Resources{Memory: "50M"}, container.HostConfig.Resources)
}
Loading
Loading