Skip to content

Commit

Permalink
Add mocking and test for functions
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham <[email protected]>
  • Loading branch information
shubham14bajpai committed May 6, 2022
1 parent 74be2a2 commit 69a0897
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
12 changes: 6 additions & 6 deletions agent/registration/host_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ func (hr *HostRegistrar) GetNetworkStatus() []infrastructurev1beta1.NetworkStatu
return Network
}

// getHostInfo gets the host platform details...
// getHostInfo gets the host platform details.
func (hr *HostRegistrar) getHostInfo() (infrastructurev1beta1.HostInfo, error) {
hostInfo := infrastructurev1beta1.HostInfo{}

hostInfo.Architecture = runtime.GOARCH
hostInfo.OSName = runtime.GOOS

if distribution, err := getOperatingSystem(); err != nil {
if distribution, err := getOperatingSystem(ioutil.ReadFile); err != nil {
return hostInfo, errors.Wrap(err, "failed to get host operating system image")
} else {
hostInfo.OSImage = distribution
Expand All @@ -158,13 +158,13 @@ func (hr *HostRegistrar) getHostInfo() (infrastructurev1beta1.HostInfo, error) {
}

// getOperatingSystem gets the name of the current operating system image.
func getOperatingSystem() (string, error) {
func getOperatingSystem(f func(string) ([]byte, error)) (string, error) {
rex := regexp.MustCompile("(PRETTY_NAME)=(.*)")

bytes, err := ioutil.ReadFile("/etc/os-release")
bytes, err := f("/etc/os-release")
if err != nil && os.IsNotExist(err) {
// /usr/lib/os-release in stateless systems like Clear Linux
bytes, err = ioutil.ReadFile("/usr/lib/os-release")
bytes, err = f("/usr/lib/os-release")
}
if err != nil {
return "", fmt.Errorf("error opening file : %v", err)
Expand All @@ -173,5 +173,5 @@ func getOperatingSystem() (string, error) {
if len(line) > 0 {
return strings.Trim(line[0][2], "\""), nil
}
return "Linux", nil
return "Unknown", nil
}
78 changes: 78 additions & 0 deletions agent/registration/host_registrar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// nolint: testpackage
package registration

import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"
)

func getMockFile(targetOs string) ([]byte, error) {
out := fmt.Sprintf(`NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=%s
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal`, targetOs)
return []byte(out), nil
}

var _ = Describe("Host Registrar Tests", func() {

var (
targetOs = "\"Ubuntu 20.04.4 LTS\""
)

Context("When the OS is detected", func() {
It("Should return the operating system for os following /etc/os-release", func() {
detectedOS, err := getOperatingSystem(func(string) ([]byte, error) { return getMockFile(targetOs) })
Expect(err).ShouldNot(HaveOccurred())
Expect(detectedOS).To(Equal("Ubuntu 20.04.4 LTS"))
})

It("Should return the operating system for os following /usr/lib/os-release", func() {
targetOs = "\"Clear Linux Initramfs\""
detectedOS, err := getOperatingSystem(func(releaseFile string) ([]byte, error) {
if releaseFile == "/etc/os-release" {
return nil, os.ErrNotExist
}
return getMockFile(targetOs)
})
Expect(err).ShouldNot(HaveOccurred())
Expect(detectedOS).To(Equal("Clear Linux Initramfs"))
})

It("Should not error with real hostnamectl", func() {
_, err := getOperatingSystem(ioutil.ReadFile)
Expect(err).ShouldNot(HaveOccurred())
})
})

Context("When the os-release file is missing", func() {
It("Should return error", func() {
_, err := getOperatingSystem(func(string) ([]byte, error) {
return nil, os.ErrNotExist
})
Expect(err.Error()).To(Equal("error opening file : file does not exist"))
})
})

Context("When the os-release does not contain PRETTY_NAME", func() {
It("Should return Unknown as operating system", func() {
detectedOS, err := getOperatingSystem(func(string) ([]byte, error) { return []byte("some_file_without_PRETTY_NAME"), nil })
Expect(err).ShouldNot(HaveOccurred())
Expect(detectedOS).To(Equal("Unknown"))
})
})
})

0 comments on commit 69a0897

Please sign in to comment.