Skip to content

Commit

Permalink
platform indepedendent errs, one windows-specific string check
Browse files Browse the repository at this point in the history
  • Loading branch information
swi-jared committed Jul 30, 2024
1 parent 705802f commit 1be17a0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -474,7 +475,13 @@ func TestInvalidConfigFile(t *testing.T) {
os.Setenv("SW_APM_SERVICE_KEY", "ae38315f6116585d64d82ec2455aa3ec61e02fee25d286f74ace9e4fea189217:go")
os.Setenv("SW_APM_CONFIG_FILE", "/tmp/file-not-exist.yaml")
_ = NewConfig()
assert.Contains(t, buf.String(), "no such file or directory")
var exp string
if runtime.GOOS == "windows" {
exp = "The system cannot find the path specified."
} else {
exp = "no such file or directory"
}
assert.Contains(t, buf.String(), exp)
}

func TestInvalidConfig(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions internal/host/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestRequestMetadataFromEnv(t *testing.T) {
md, err := requestMetadata()
require.Error(t, err)
require.Nil(t, md)
require.Equal(t, "open /run/secrets/kubernetes.io/serviceaccount/namespace: no such file or directory", err.Error())
require.True(t, os.IsNotExist(err))

defer testutils.Setenv(t, "SW_K8S_POD_NAMESPACE", "my env namespace")()
md, err = requestMetadata()
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestRequestMetadataNoNamespace(t *testing.T) {
md, err := requestMetadata()
require.Error(t, err)
require.Nil(t, md)
require.Equal(t, fmt.Sprintf("open %s: no such file or directory", determineNamspaceFileForOS()), err.Error())
require.True(t, os.IsNotExist(err))
}

func TestMetadata_ToPB(t *testing.T) {
Expand Down Expand Up @@ -159,9 +159,8 @@ func TestGetNamespaceNoneFound(t *testing.T) {
require.NoError(t, os.Unsetenv("SW_K8S_POD_NAMESPACE"))
ns, err := getNamespaceFromFile("this file does not exist and should not be opened")
require.Error(t, err)
require.Equal(t, "open this file does not exist and should not be opened: no such file or directory", err.Error())
require.True(t, os.IsNotExist(err))
require.Equal(t, "", ns)

}

// Test getPodName
Expand Down
4 changes: 3 additions & 1 deletion internal/uams/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package uams

import (
"errors"
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"io/fs"
"os"
"testing"
)
Expand All @@ -29,7 +31,7 @@ func TestReadFromFileNoExists(t *testing.T) {
uid, err := ReadFromFile(testfile)
require.Equal(t, uuid.Nil, uid)
require.Error(t, err)
require.Equal(t, "could not stat uams client file: stat /tmp/foobarbaz: no such file or directory", err.Error())
require.True(t, errors.Is(err, fs.ErrNotExist))
}

func TestReadFromFileDirectory(t *testing.T) {
Expand Down

0 comments on commit 1be17a0

Please sign in to comment.