forked from eclipse-kanto/container-management
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide unit tests for container update agent service (eclipse-kanto#198
) [eclipse-kanto#197] Provide unit tests for container update agent service Added unit tests for handling update agent config options. Added unit tests for parsing incoming desired state in container update agent. Added MockUpdateOperation (auto-generated). Added unit tests for implementation of UpdateManager interface for containers. Made update agent report COMPLETED if no container actions are identified. Signed-off-by: Stoyan Zoubev <Stoyan.Zoubev@bosch.io>
- Loading branch information
1 parent
1e06303
commit 38f6633
Showing
8 changed files
with
745 additions
and
11 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.