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

Add device owner's approval unit tests #61

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
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
80 changes: 80 additions & 0 deletions config/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"fmt"
"os"
"reflect"
"slices"
"testing"

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

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -129,6 +131,10 @@ func TestSetupFlags(t *testing.T) {
flag: "phase-timeout",
expectedType: reflect.String.String(),
},
"test_flags_owner_consent_timeout": {
flag: "owner-consent-timeout",
expectedType: reflect.String.String(),
},
}
for testName, testCase := range tests {
t.Run(testName, func(t *testing.T) {
Expand Down Expand Up @@ -231,6 +237,47 @@ func TestParseDomainsFlag(t *testing.T) {
})
}

func TestParseOwnerConsentCommandsFlag(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
testCommand := types.CommandDownload

t.Run("test_parse_consent_commands_flag_1", func(t *testing.T) {
actualConsentCommands := parseOwnerConsentCommandsFlag("download,update")
if len(actualConsentCommands) != 1 && !slices.Contains(actualConsentCommands, testCommand) {
t.Error("consent phase not set")
}
})

t.Run("test_parse_consent_commands_flag_2", func(t *testing.T) {
actualConsentPhases := parseOwnerConsentCommandsFlag("update,download")
if len(actualConsentPhases) != 1 && !slices.Contains(actualConsentPhases, testCommand) {
t.Error("consent phase not set")
}
})

t.Run("test_parse_consent_commands_flag_3", func(t *testing.T) {
actualConsentPhases := parseOwnerConsentCommandsFlag("DoWnLoAd")
if len(actualConsentPhases) != 1 && !slices.Contains(actualConsentPhases, testCommand) {
t.Error("consent phase not set")
}
})

t.Run("test_parse_consent_commands_flag_4", func(t *testing.T) {
actualConsentPhases := parseOwnerConsentCommandsFlag("UPDATE,DOWNLOAD")
if len(actualConsentPhases) != 1 && !slices.Contains(actualConsentPhases, testCommand) {
t.Error("consent phase not set")
}
})

t.Run("test_parse_consent_commands_flag_err_1", func(t *testing.T) {
actualConsentPhases := parseOwnerConsentCommandsFlag("")
if len(actualConsentPhases) != 0 {
t.Errorf("\"incorrect value: %v , expecting: empty \"", actualConsentPhases)
}
})
}

func TestParseFlags(t *testing.T) {
testVersion := "testVersion"
t.Run("test_config_agent_not_configured_with_domain_specific_flags", func(t *testing.T) {
Expand Down Expand Up @@ -372,4 +419,37 @@ func TestParseFlags(t *testing.T) {
parseFlags(cfg, testVersion)
assert.Equal(t, expectedAgents, cfg.Agents)
})
t.Run("test_owner_consent_commands", func(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

testConfigPath := "../config/testdata/config.json"
expectedCommands := []types.CommandType{types.CommandDownload}

os.Args = []string{oldArgs[0], fmt.Sprintf("--%s=%s", configFileFlagID, testConfigPath)}
cfg := newDefaultConfig()
configFilePath := ParseConfigFilePath()
if configFilePath != "" {
assert.NoError(t, LoadConfigFromFile(configFilePath, cfg))
}
parseFlags(cfg, testVersion)
assert.Equal(t, expectedCommands, cfg.OwnerConsentCommands)
})
t.Run("test_overwrite_owner_consent_commands", func(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

testConfigPath := "../config/testdata/config.json"
expectedCommands := []types.CommandType{types.CommandUpdate, types.CommandActivate}

os.Args = []string{oldArgs[0], fmt.Sprintf("--%s=%s", configFileFlagID, testConfigPath),
fmt.Sprintf("--%s=%s", ownerConsentCommandsFlagID, "update,activate")}
cfg := newDefaultConfig()
configFilePath := ParseConfigFilePath()
if configFilePath != "" {
assert.NoError(t, LoadConfigFromFile(configFilePath, cfg))
}
parseFlags(cfg, testVersion)
assert.Equal(t, expectedCommands, cfg.OwnerConsentCommands)
})
}
173 changes: 173 additions & 0 deletions mqtt/owner_consent_agent_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright (c) 2024 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 mqtt

import (
"errors"
"testing"

"github.com/eclipse-kanto/update-manager/api/types"
mqttmocks "github.com/eclipse-kanto/update-manager/mqtt/mocks"
"github.com/eclipse-kanto/update-manager/test/mocks"

pahomqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestOwnerConsentAgentClientStart(t *testing.T) {
tests := map[string]testCaseOutgoing{
"test_connect_ok": {domain: "testdomain", isTimedOut: false},
"test_connect_timeout": {domain: "mydomain", isTimedOut: true},
}

mockCtrl, mockPaho, mockToken := setupCommonMocks(t)
defer mockCtrl.Finish()

mockHandler := mocks.NewMockOwnerConsentAgentHandler(mockCtrl)

for name, test := range tests {
t.Run(name, func(t *testing.T) {
client := &ownerConsentAgentClient{
domain: test.domain,
mqttClient: newInternalClient(test.domain, mqttTestConfig, mockPaho),
}

mockPaho.EXPECT().Connect().Return(mockToken)
setupMockToken(mockToken, mqttTestConfig.ConnectTimeout, test.isTimedOut)

assertOutgoingResult(t, test.isTimedOut, client.Start(mockHandler))
assert.Equal(t, mockHandler, client.handler)
})
}
}

func TestOwnerConsentAgentClientStop(t *testing.T) {
tests := map[string]testCaseOutgoing{
//"test_disconnect_ok": {domain: "testdomain", isTimedOut: false},
"test_disconnect_timeout": {domain: "mydomain", isTimedOut: true},
}

mockCtrl, mockPaho, mockToken := setupCommonMocks(t)
defer mockCtrl.Finish()

mockHandler := mocks.NewMockOwnerConsentAgentHandler(mockCtrl)

for name, test := range tests {
t.Run(name, func(t *testing.T) {
client := &ownerConsentAgentClient{
domain: test.domain,
mqttClient: newInternalClient(test.domain, mqttTestConfig, mockPaho),
handler: mockHandler,
}

mockPaho.EXPECT().Unsubscribe(test.domain + "update/ownerconsent").Return(mockToken)
mockPaho.EXPECT().Disconnect(disconnectQuiesce)
setupMockToken(mockToken, mqttTestConfig.UnsubscribeTimeout, test.isTimedOut)

assert.NoError(t, client.Stop())
assert.Nil(t, client.handler)
})
}
}

func TestSendOwnerConsentFeedback(t *testing.T) {
tests := map[string]testCaseOutgoing{
"test_send_owner_consent_feedback_ok": {domain: "testdomain", isTimedOut: false},
"test_send_owner_consent_feedback_error": {domain: "mydomain", isTimedOut: true},
}

mockCtrl, mockPaho, mockToken := setupCommonMocks(t)
defer mockCtrl.Finish()

testConsentFeedback := &types.OwnerConsentFeedback{
Status: types.StatusApproved,
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
client := &ownerConsentAgentClient{
domain: test.domain,
mqttClient: newInternalClient(test.domain, mqttTestConfig, mockPaho),
}
mockPaho.EXPECT().Publish(test.domain+"update/ownerconsentfeedback", uint8(1), false, gomock.Any()).DoAndReturn(
func(topic string, qos byte, retained bool, payload interface{}) pahomqtt.Token {
consentFeedback := &types.OwnerConsentFeedback{}
envelope, err := types.FromEnvelope(payload.([]byte), consentFeedback)
assert.NoError(t, err)
assert.Equal(t, name, envelope.ActivityID)
assert.True(t, envelope.Timestamp > 0)
assert.Equal(t, testConsentFeedback, consentFeedback)
return mockToken
})
setupMockToken(mockToken, mqttTestConfig.AcknowledgeTimeout, false)

assert.NoError(t, client.SendOwnerConsentFeedback(name, testConsentFeedback))
})
}
}

func TestOwnerConsentOnConnect(t *testing.T) {
mockCtrl, mockPaho, mockToken := setupCommonMocks(t)
defer mockCtrl.Finish()

client := newInternalClient("test", mqttTestConfig, mockPaho)

t.Run("test_onConnect", func(t *testing.T) {
mockHandler := mocks.NewMockOwnerConsentAgentHandler(mockCtrl)
client := &ownerConsentAgentClient{
mqttClient: client,
domain: "test",
handler: mockHandler,
}
mockPaho.EXPECT().Subscribe("testupdate/ownerconsent", uint8(1), gomock.Any()).Return(mockToken)
setupMockToken(mockToken, mqttTestConfig.SubscribeTimeout, false)

client.onConnect(nil)
})
}

func TestHandleOwnerConsentMessage(t *testing.T) {
tests := map[string]testCaseIncoming{
"test_handle_owner_consent_ok": {domain: "testdomain", handlerError: nil, expectedJSONErr: false},
"test_handle_owner_consent_error": {domain: "mydomain", handlerError: errors.New("handler error"), expectedJSONErr: false},
"test_handle_owner_consent_json_error": {domain: "testdomain", handlerError: nil, expectedJSONErr: true},
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockMessage := mqttmocks.NewMockMessage(mockCtrl)

for name, test := range tests {
t.Run(name, func(t *testing.T) {
testConsent := &types.OwnerConsent{
Command: types.CommandDownload,
}
testBytes, expectedCalls := testBytesToEnvelope(t, name, testConsent, test.expectedJSONErr)

mockHandler := mocks.NewMockOwnerConsentAgentHandler(mockCtrl)
mockHandler.EXPECT().HandleOwnerConsent(name, gomock.Any(), testConsent).Times(expectedCalls).Return(test.handlerError)

client := &ownerConsentAgentClient{
mqttClient: newInternalClient(test.domain, mqttTestConfig, nil),
domain: test.domain,
handler: mockHandler,
}
mockMessage.EXPECT().Topic().Return(test.domain + "update/ownerconsent")
mockMessage.EXPECT().Payload().Return(testBytes)

client.handleMessage(nil, mockMessage)
})
}
}
Loading
Loading