Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdutka-dell committed Apr 9, 2024
1 parent 6a7b306 commit 19b7b56
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
10 changes: 8 additions & 2 deletions service/utils/emcutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,19 @@ func GetHostIP() ([]string, error) {

// GetNFSClientIP is used to fetch IP address from networks on which NFS traffic is allowed
func GetNFSClientIP(allowedNetworks []string) ([]string, error) {
var nodeIPs []string
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
nodeIPs, err := GetAddresses(allowedNetworks, addrs)
if err != nil {
return nil, err
}
return nodeIPs, nil
}

func GetAddresses(allowedNetworks []string, addrs []net.Addr) ([]string, error) {
var nodeIPs []string
networks := make(map[string]bool)
for _, cnet := range allowedNetworks {
networks[cnet] = false
Expand All @@ -213,7 +220,6 @@ func GetNFSClientIP(allowedNetworks []string) ([]string, error) {
if len(nodeIPs) == 0 {
return nil, fmt.Errorf("no valid IP address found matching against allowedNetworks %v", allowedNetworks)
}

return nodeIPs, nil
}

Expand Down
62 changes: 62 additions & 0 deletions service/utils/emcutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright © 2019 Dell Inc. or its subsidiaries. All Rights Reserved.
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 utils

import (
"fmt"
"net"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetAddresses(t *testing.T) {

Check failure on line 26 in service/utils/emcutils_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
type errorTestCases struct {
description string
addrs []net.Addr
networkAddresses []string
expectedAddresses []string
expectedError string
}

for _, scenario := range []errorTestCases{
{
description: "invalid",
addrs: []net.Addr{&net.IPAddr{IP: []byte("192.168.2.1")}},
networkAddresses: []string{"192.168.1.0/24"},
expectedAddresses: []string{},
expectedError: fmt.Sprintf("no valid IP address found matching against allowedNetworks %v", []string{"192.168.1.0/24"}),
},
{
description: "ok",
addrs: []net.Addr{&net.IPAddr{IP: []byte("192.168.1.1")}},
networkAddresses: []string{"192.168.1.0/24"},
expectedAddresses: []string{"192.168.1.1"},
expectedError: "",
},
} {
t.Run(scenario.description, func(t *testing.T) {
addresses, err := GetAddresses(scenario.networkAddresses, scenario.addrs)
if err != nil {
assert.EqualError(t, err, scenario.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, scenario.expectedAddresses, addresses)
}

Check failure on line 59 in service/utils/emcutils_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
})
}
}

0 comments on commit 19b7b56

Please sign in to comment.