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 5, 2022
1 parent 74be2a2 commit e3308b1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
8 changes: 4 additions & 4 deletions agent/registration/host_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (hr *HostRegistrar) getHostInfo() (infrastructurev1beta1.HostInfo, error) {
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 Down
61 changes: 61 additions & 0 deletions agent/registration/host_registrar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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())
})
})
})

0 comments on commit e3308b1

Please sign in to comment.