forked from kubeedge/kubeedge
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubeedge#5970 from 1Shubham7/device-controller-tests
UT coverage for `cloud/pkg/devicecontroller/controller` pkg
- Loading branch information
Showing
2 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
cloud/pkg/devicecontroller/controller/downstream_test.go
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,122 @@ | ||
/* | ||
Copyright 2024 The KubeEdge Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kubeedge/api/apis/devices/v1beta1" | ||
) | ||
|
||
func TestRemoveTwinWithNameChanged(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
device *v1beta1.Device | ||
expected []v1beta1.Twin | ||
}{ | ||
{ | ||
name: "Remove twin with changed property name", | ||
device: &v1beta1.Device{ | ||
Spec: v1beta1.DeviceSpec{ | ||
Properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "temp", | ||
}, | ||
{ | ||
Name: "humidity", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temp", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
{ | ||
PropertyName: "pressure", // This will be be removed | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "1000", | ||
}, | ||
}, | ||
{ | ||
PropertyName: "humidity", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temp", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
{ | ||
PropertyName: "humidity", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "No twins to remove", | ||
device: &v1beta1.Device{ | ||
Spec: v1beta1.DeviceSpec{ | ||
Properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "temp", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temp", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temp", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
removeTwinWithNameChanged(tt.device) | ||
assert.Equal(t, tt.expected, tt.device.Status.Twins) | ||
}) | ||
} | ||
} |
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,241 @@ | ||
/* | ||
Copyright 2024 The KubeEdge Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kubeedge/api/apis/devices/v1beta1" | ||
) | ||
|
||
func TestNewUpstreamController(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
dc := &DownstreamController{} | ||
uc, err := NewUpstreamController(dc) | ||
assert.NoError(err) | ||
assert.NotNil(uc) | ||
|
||
assert.NotNil(uc.messageLayer) | ||
assert.NotNil(uc.dc) | ||
assert.Equal(dc, uc.dc) | ||
|
||
// Channels are not initialized (they should be initialized in Start()) | ||
assert.Nil(uc.deviceTwinsChan) | ||
assert.Nil(uc.deviceStatesChan) | ||
} | ||
|
||
func TestFindOrCreateTwinByName(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
tests := []struct { | ||
name string | ||
twinName string | ||
properties []v1beta1.DeviceProperty | ||
status *DeviceStatus | ||
expected *v1beta1.Twin | ||
}{ | ||
{ | ||
name: "finding existing twin", | ||
twinName: "temperature", | ||
properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "temperature", | ||
}, | ||
}, | ||
status: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temperature", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &v1beta1.Twin{ | ||
PropertyName: "temperature", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "creating new twin", | ||
twinName: "humidity", | ||
properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "humidity", | ||
}, | ||
}, | ||
status: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{}, | ||
}, | ||
}, | ||
expected: &v1beta1.Twin{ | ||
PropertyName: "humidity", | ||
}, | ||
}, | ||
{ | ||
name: "property not found", | ||
twinName: "nonexistent", | ||
properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "temperature", | ||
}, | ||
}, | ||
status: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "multiple properties", | ||
twinName: "temperature", | ||
properties: []v1beta1.DeviceProperty{ | ||
{ | ||
Name: "humidity", | ||
}, | ||
{ | ||
Name: "temperature", | ||
}, | ||
}, | ||
status: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "humidity", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &v1beta1.Twin{ | ||
PropertyName: "temperature", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := findOrCreateTwinByName(tt.twinName, tt.properties, tt.status) | ||
if tt.expected == nil { | ||
assert.Nil(result) | ||
} else { | ||
assert.Equal(tt.expected.PropertyName, result.PropertyName) | ||
if tt.expected.Reported.Value != "" { | ||
assert.Equal(tt.expected.Reported, result.Reported) | ||
} | ||
// Verify twin was added to DeviceStatus if created | ||
if len(tt.status.Status.Twins) > 0 { | ||
found := false | ||
for _, twin := range tt.status.Status.Twins { | ||
if twin.PropertyName == tt.twinName { | ||
found = true | ||
break | ||
} | ||
} | ||
assert.True(found) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFindTwinByName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
twinName string | ||
deviceStatus *DeviceStatus | ||
expected *v1beta1.Twin | ||
}{ | ||
{ | ||
name: "twin exists", | ||
twinName: "temperature", | ||
deviceStatus: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temperature", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
{ | ||
PropertyName: "humidity", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &v1beta1.Twin{ | ||
PropertyName: "temperature", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "twin doesn't exist", | ||
twinName: "pressure", | ||
deviceStatus: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{ | ||
Twins: []v1beta1.Twin{ | ||
{ | ||
PropertyName: "temperature", | ||
Reported: v1beta1.TwinProperty{ | ||
Value: "25", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "device status is nil", | ||
twinName: "temperature", | ||
deviceStatus: &DeviceStatus{ | ||
Status: v1beta1.DeviceStatus{}, | ||
}, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := findTwinByName(tt.twinName, tt.deviceStatus) | ||
if tt.expected == nil { | ||
assert.Nil(t, result) | ||
} else { | ||
assert.Equal(t, tt.expected.PropertyName, result.PropertyName) | ||
assert.Equal(t, tt.expected.Reported, result.Reported) | ||
} | ||
}) | ||
} | ||
} |