Skip to content

Commit

Permalink
chore: add cni plugins test (Mellanox#785)
Browse files Browse the repository at this point in the history
Add unit-test to CNI plugins state.

The test is a different package so that only public APIs are used.
  • Loading branch information
adrianchiris authored Feb 5, 2024
2 parents 4a98cec + 1502af5 commit 8826838
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 3 deletions.
67 changes: 67 additions & 0 deletions pkg/state/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
2024 NVIDIA CORPORATION & AFFILIATES
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 state_test

import (
"github.com/Mellanox/network-operator/pkg/clustertype"
"github.com/Mellanox/network-operator/pkg/nodeinfo"
"github.com/Mellanox/network-operator/pkg/state"
"github.com/Mellanox/network-operator/pkg/staticconfig"
)

type testProvider struct {
isOpenshift bool
cniBinDir string
}

func (tp *testProvider) GetClusterType() clustertype.Type {
if tp.isOpenshift {
return clustertype.Openshift
}
return clustertype.Kubernetes
}

func (tp *testProvider) IsKubernetes() bool {
return !tp.isOpenshift
}

func (tp *testProvider) IsOpenshift() bool {
return tp.isOpenshift
}

func (tp *testProvider) GetStaticConfig() staticconfig.StaticConfig {
return staticconfig.StaticConfig{CniBinDirectory: tp.cniBinDir}
}

func (tp *testProvider) GetNodesAttributes(...nodeinfo.Filter) []nodeinfo.NodeAttributes {
nodeAttr := make(map[nodeinfo.AttributeType]string)
nodeAttr[nodeinfo.AttrTypeCPUArch] = "amd64"
nodeAttr[nodeinfo.AttrTypeOSName] = "ubuntu"
nodeAttr[nodeinfo.AttrTypeOSVer] = "20.04"

return []nodeinfo.NodeAttributes{{Attributes: nodeAttr}}
}

func getTestCatalog() state.InfoCatalog {
catalog := state.NewInfoCatalog()
tp := &testProvider{isOpenshift: false, cniBinDir: ""}
catalog.Add(state.InfoTypeNodeInfo, tp)
catalog.Add(state.InfoTypeStaticConfig, tp)
catalog.Add(state.InfoTypeClusterType, tp)

return catalog
}
6 changes: 5 additions & 1 deletion pkg/state/state_cni_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ func (s *stateCNIPlugins) GetManifestObjects(
if staticConfig == nil {
return nil, errors.New("staticConfig provider required")
}
clusterInfo := catalog.GetClusterTypeProvider()
if clusterInfo == nil {
return nil, errors.New("clusterInfo provider required")
}
renderData := &CNIPluginsManifestRenderData{
CrSpec: cr.Spec.SecondaryNetwork.CniPlugins,
Tolerations: cr.Spec.Tolerations,
NodeAffinity: cr.Spec.NodeAffinity,
RuntimeSpec: &cniRuntimeSpec{
runtimeSpec: runtimeSpec{config.FromEnv().State.NetworkOperatorResourceNamespace},
CniBinDirectory: utils.GetCniBinDirectory(staticConfig, nil),
CniBinDirectory: utils.GetCniBinDirectory(staticConfig, clusterInfo),
ContainerResources: createContainerResourcesMap(cr.Spec.SecondaryNetwork.CniPlugins.ContainerResources),
},
}
Expand Down
Loading

0 comments on commit 8826838

Please sign in to comment.