Skip to content

Commit

Permalink
refactor test utils into seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavDhulipala committed Aug 29, 2024
1 parent 6b59229 commit 7fcae89
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 75 deletions.
71 changes: 71 additions & 0 deletions exporter/mock_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: 2023 Rivos Inc.
//
// SPDX-License-Identifier: Apache-2.0

package exporter

import (
"bytes"
"errors"
"os"
"time"
)

type MockFetchErrored struct{}

func (f *MockFetchErrored) FetchRawBytes() ([]byte, error) {
return nil, errors.New("mock fetch error")
}

func (f *MockFetchErrored) Duration() time.Duration {
return 1
}

// implements SlurmByteScraper by pulling fixtures instead
// used exclusively for testing
type MockScraper struct {
fixture string
duration time.Duration
CallCount int
}

func (f *MockScraper) FetchRawBytes() ([]byte, error) {
defer func(t time.Time) {
f.duration = time.Since(t)
}(time.Now())
f.CallCount++
file, err := os.ReadFile(f.fixture)
if err != nil {
return nil, err
}
// allow commenting in text files
sep := []byte("\n")
lines := bytes.Split(file, sep)
filtered := make([][]byte, 0)
for _, line := range lines {
if !bytes.HasPrefix(line, []byte("#")) {
filtered = append(filtered, line)
}
}
return bytes.Join(filtered, sep), nil
}

func (f *MockScraper) Duration() time.Duration {
return f.duration
}

// implements SlurmByteScraper by emmiting string payload instead
// used exclusively for testing
type StringByteScraper struct {
msg string
Callcount int
}

func (es *StringByteScraper) FetchRawBytes() ([]byte, error) {
es.Callcount++
return []byte(es.msg), nil
}

func (es *StringByteScraper) Duration() time.Duration {
return time.Duration(1)
}
49 changes: 0 additions & 49 deletions exporter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,55 +133,6 @@ func NewCliScraper(args ...string) *CliScraper {
}
}

// implements SlurmByteScraper by pulling fixtures instead
// used exclusively for testing
type MockScraper struct {
fixture string
duration time.Duration
CallCount int
}

func (f *MockScraper) FetchRawBytes() ([]byte, error) {
defer func(t time.Time) {
f.duration = time.Since(t)
}(time.Now())
f.CallCount++
file, err := os.ReadFile(f.fixture)
if err != nil {
return nil, err
}
// allow commenting in text files
sep := []byte("\n")
lines := bytes.Split(file, sep)
filtered := make([][]byte, 0)
for _, line := range lines {
if !bytes.HasPrefix(line, []byte("#")) {
filtered = append(filtered, line)
}
}
return bytes.Join(filtered, sep), nil
}

func (f *MockScraper) Duration() time.Duration {
return f.duration
}

// implements SlurmByteScraper by emmiting string payload instead
// used exclusively for testing
type StringByteScraper struct {
msg string
Callcount int
}

func (es *StringByteScraper) FetchRawBytes() ([]byte, error) {
es.Callcount++
return []byte(es.msg), nil
}

func (es *StringByteScraper) Duration() time.Duration {
return time.Duration(1)
}

// convert slurm mem string to float64 bytes
func MemToFloat(mem string) (float64, error) {
if num, err := strconv.ParseFloat(mem, 64); err == nil {
Expand Down
26 changes: 0 additions & 26 deletions exporter/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package exporter

import (
"errors"
"fmt"
"math"
"math/rand"
Expand Down Expand Up @@ -34,31 +33,6 @@ func generateRandString(n int) string {
return string(randBytes)
}

// used to ensure the fetch function was called
type MockFetchTriggered struct {
msg []byte
called bool
}

func (f *MockFetchTriggered) Fetch() ([]byte, error) {
f.called = true
return f.msg, nil
}

func (f *MockFetchTriggered) Duration() time.Duration {
return 1
}

type MockFetchErrored struct{}

func (f *MockFetchErrored) FetchRawBytes() ([]byte, error) {
return nil, errors.New("mock fetch error")
}

func (f *MockFetchErrored) Duration() time.Duration {
return 1
}

func TestCliFetcher(t *testing.T) {
assert := assert.New(t)
cliFetcher := NewCliScraper("ls")
Expand Down

0 comments on commit 7fcae89

Please sign in to comment.