Skip to content

Commit 46ed773

Browse files
authored
sriov-fec: add List method (#1054)
1 parent c46fc31 commit 46ed773

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

pkg/sriov-fec/nodeconfiglist.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package sriovfec
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/golang/glog"
8+
"github.com/openshift-kni/eco-goinfra/pkg/clients"
9+
sriovfectypes "github.com/openshift-kni/eco-goinfra/pkg/schemes/fec/fectypes"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
)
12+
13+
// List returns SriovFecNodeConfigList from given namespace.
14+
func List(apiClient *clients.Settings, nsname string, options ...client.ListOptions) ([]*NodeConfigBuilder, error) {
15+
if apiClient == nil {
16+
glog.V(100).Infof("SriovFecNodeConfigList 'apiClient' parameter can not be empty")
17+
18+
return nil, fmt.Errorf("failed to list SriovFecNodeConfig, 'apiClient' parameter is empty")
19+
}
20+
21+
err := apiClient.AttachScheme(sriovfectypes.AddToScheme)
22+
if err != nil {
23+
glog.V(100).Infof("Failed to add sriov-fec scheme to client schemes")
24+
25+
return nil, err
26+
}
27+
28+
if nsname == "" {
29+
glog.V(100).Infof("SriovFecNodeConfigList 'nsname' parameter can not be empty")
30+
31+
return nil, fmt.Errorf("failed to list SriovFecNodeConfig, 'nsname' parameter is empty")
32+
}
33+
34+
passedOptions := client.ListOptions{}
35+
logMessage := fmt.Sprintf("Listing SriovFecNodeConfig in the namespace %s", nsname)
36+
37+
if len(options) > 1 {
38+
glog.V(100).Infof("'options' parameter must be empty or single-valued")
39+
40+
return nil, fmt.Errorf("error: more than one ListOptions was passed")
41+
}
42+
43+
if len(options) == 1 {
44+
passedOptions = options[0]
45+
logMessage += fmt.Sprintf(" with the options %v", passedOptions)
46+
}
47+
48+
glog.V(100).Infof(logMessage)
49+
50+
sfncList := new(sriovfectypes.SriovFecNodeConfigList)
51+
err = apiClient.List(context.TODO(), sfncList, &passedOptions)
52+
53+
if err != nil {
54+
glog.V(100).Infof("Failed to list SriovFecNodeConfigs in the namespace %s due to %s", nsname, err.Error())
55+
56+
return nil, err
57+
}
58+
59+
var sfncBuilderList []*NodeConfigBuilder
60+
61+
for _, sfnc := range sfncList.Items {
62+
copiedObject := sfnc
63+
sfncBuilder := &NodeConfigBuilder{
64+
apiClient: apiClient.Client,
65+
Object: &copiedObject,
66+
Definition: &copiedObject,
67+
}
68+
69+
sfncBuilderList = append(sfncBuilderList, sfncBuilder)
70+
}
71+
72+
return sfncBuilderList, nil
73+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sriovfec
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/openshift-kni/eco-goinfra/pkg/clients"
8+
"github.com/stretchr/testify/assert"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
)
11+
12+
func TestNodeConfigList(t *testing.T) {
13+
testCases := []struct {
14+
apiclient bool
15+
nsname string
16+
expectedError error
17+
}{
18+
{
19+
apiclient: false,
20+
nsname: defaultNodeConfigNamespace,
21+
expectedError: fmt.Errorf("failed to list SriovFecNodeConfig, 'apiClient' parameter is empty"),
22+
},
23+
{
24+
apiclient: true,
25+
nsname: "",
26+
expectedError: fmt.Errorf("failed to list SriovFecNodeConfig, 'nsname' parameter is empty"),
27+
},
28+
{
29+
apiclient: true,
30+
nsname: defaultNodeConfigNamespace,
31+
expectedError: nil,
32+
},
33+
}
34+
35+
for _, testCase := range testCases {
36+
var testSettings *clients.Settings
37+
38+
if testCase.apiclient {
39+
testSettings = clients.GetTestClients(clients.TestClientParams{
40+
K8sMockObjects: buildDummyNodeConfigList(),
41+
SchemeAttachers: testSchemes,
42+
})
43+
}
44+
45+
testBuilderList, err := List(testSettings, testCase.nsname)
46+
assert.Equal(t, testCase.expectedError, err)
47+
48+
if testCase.expectedError == nil {
49+
assert.Equal(t, len(buildDummyNodeConfigList()), len(testBuilderList))
50+
}
51+
}
52+
}
53+
54+
func buildDummyNodeConfigList() []runtime.Object {
55+
return []runtime.Object{
56+
buildDummyNodeConfig("worker-0", "test-ns"),
57+
buildDummyNodeConfig("worker-1", "test-ns"),
58+
}
59+
}

0 commit comments

Comments
 (0)