Skip to content

Commit

Permalink
Satisfy linter (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Nov 14, 2023
1 parent 34d00cc commit ed8caa2
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 91 deletions.
6 changes: 5 additions & 1 deletion auditing/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ func New(c Config) (Auditing, error) {
}

func (a *meiliAuditing) Flush() error {
taskResult, err := a.client.GetTasks(&meilisearch.TasksQuery{Statuses: []string{"enqueued", "processing"}, Limit: 100})
taskStatuses := []meilisearch.TaskStatus{
meilisearch.TaskStatusEnqueued,
meilisearch.TaskStatusProcessing,
}
taskResult, err := a.client.GetTasks(&meilisearch.TasksQuery{Statuses: taskStatuses, Limit: 100})
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

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

const testCloudContextName123 = "ctx123"
Expand Down Expand Up @@ -65,13 +66,13 @@ func Test_NewUpdateKubeConfigHandler(t *testing.T) {
var b bytes.Buffer
thf := NewUpdateKubeConfigHandler(file.Name(), &b)
err = thf(tokenInfo)
assert.NoError(t, err)
require.NoError(t, err)

_, err = GetAuthContext(file.Name(), "xyz")
assert.EqualError(t, err, "no contexts, name=xyz found")
require.EqualError(t, err, "no contexts, name=xyz found")

authCtx, err := GetAuthContext(file.Name(), cloudContext)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "123", authCtx.IDToken)
}

Expand All @@ -92,12 +93,12 @@ func Test_NewUpdateKubeConfigHandlerWithContext(t *testing.T) {
var b bytes.Buffer
thf := NewUpdateKubeConfigHandler(file.Name(), &b, WithContextName("ctx123"))
err = thf(tokenInfo)
assert.NoError(t, err)
require.NoError(t, err)

_, err = GetAuthContext(file.Name(), "cloudctl-xyz")
assert.EqualError(t, err, "no contexts, name=cloudctl-xyz found")
require.EqualError(t, err, "no contexts, name=cloudctl-xyz found")

authCtx, err := GetAuthContext(file.Name(), testCloudContextName123)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "123", authCtx.IDToken)
}
69 changes: 32 additions & 37 deletions auth/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,8 @@ var demoToken2 = TokenInfo{

func TestUpdateUserNewFile(t *testing.T) {

asserter := require.New(t)

tmpFile, err := os.CreateTemp("", "this_file_must_not_exist_*")
assert.NoError(t, err)
require.NoError(t, err)
tmpfileName := tmpFile.Name()

// delete file, just to be sure
Expand All @@ -253,36 +251,33 @@ func TestUpdateUserNewFile(t *testing.T) {
// "Update" -> create new file
ti := demoToken
_, err = UpdateKubeConfig(tmpfileName, ti, ExtractEMail)
assert.NoError(t, err)
require.NoError(t, err)

defer os.Remove(tmpfileName)

// check it is written
asserter.FileExists(tmpfileName, "expected file to exist")
require.FileExists(t, tmpfileName, "expected file to exist")

// check contents
diffFiles(t, "./testdata/createdDemoConfig", tmpfileName)

authContext, err := CurrentAuthContext(tmpfileName)
assert.NoError(t, err)
require.NoError(t, err)

asserter.Equal(authContext.User, demoToken.TokenClaims.EMail, "User")
asserter.Equal(authContext.IDToken, demoToken.IDToken, "IDToken")
asserter.Equal(authContext.AuthProviderName, "oidc", "AuthProvider")
asserter.Equal(authContext.Ctx, testCloudContextName, "Context")
asserter.Equal(authContext.ClientID, demoToken.ClientID, "ClientID")
asserter.Equal(authContext.ClientSecret, demoToken.ClientSecret, "ClientSecret")
asserter.Equal(authContext.IssuerURL, demoToken.IssuerURL, "Issuer")
asserter.Equal(authContext.IssuerCA, demoToken.IssuerCA, "IssuerCA")
require.Equal(t, authContext.User, demoToken.TokenClaims.EMail, "User")
require.Equal(t, authContext.IDToken, demoToken.IDToken, "IDToken")
require.Equal(t, "oidc", authContext.AuthProviderName, "AuthProvider")
require.Equal(t, testCloudContextName, authContext.Ctx, "Context")
require.Equal(t, authContext.ClientID, demoToken.ClientID, "ClientID")
require.Equal(t, authContext.ClientSecret, demoToken.ClientSecret, "ClientSecret")
require.Equal(t, authContext.IssuerURL, demoToken.IssuerURL, "Issuer")
require.Equal(t, authContext.IssuerCA, demoToken.IssuerCA, "IssuerCA")

}

func TestUpdateUserWithNameExtractorNewFile(t *testing.T) {

asserter := require.New(t)

tmpFile, err := os.CreateTemp("", "this_file_must_not_exist_*")
assert.NoError(t, err)
require.NoError(t, err)
tmpfileName := tmpFile.Name()

// delete file, just to be sure
Expand All @@ -291,27 +286,27 @@ func TestUpdateUserWithNameExtractorNewFile(t *testing.T) {
// "Update" -> create new file
ti := demoToken
_, err = UpdateKubeConfig(tmpfileName, ti, ExtractName)
assert.NoError(t, err)
require.NoError(t, err)

defer os.Remove(tmpfileName)

// check it is written
asserter.FileExists(tmpfileName, "expected file to ")
require.FileExists(t, tmpfileName, "expected file to ")

// check contents
diffFiles(t, "./testdata/createdDemoConfigName", tmpfileName)

authContext, err := CurrentAuthContext(tmpfileName)
assert.NoError(t, err)

asserter.Equal(authContext.User, demoToken.TokenClaims.Username(), "User")
asserter.Equal(authContext.IDToken, demoToken.IDToken, "IDToken")
asserter.Equal(authContext.ClientID, demoToken.ClientID, "ClientID")
asserter.Equal(authContext.ClientSecret, demoToken.ClientSecret, "ClientSecret")
asserter.Equal(authContext.IssuerURL, demoToken.IssuerURL, "Issuer")
asserter.Equal(authContext.IssuerCA, demoToken.IssuerCA, "IssuerCA")
asserter.Equal(authContext.AuthProviderName, "oidc", "AuthProvider")
asserter.Equal(authContext.Ctx, testCloudContextName, "Context")
require.NoError(t, err)

require.Equal(t, authContext.User, demoToken.TokenClaims.Username(), "User")
require.Equal(t, authContext.IDToken, demoToken.IDToken, "IDToken")
require.Equal(t, authContext.ClientID, demoToken.ClientID, "ClientID")
require.Equal(t, authContext.ClientSecret, demoToken.ClientSecret, "ClientSecret")
require.Equal(t, authContext.IssuerURL, demoToken.IssuerURL, "Issuer")
require.Equal(t, demoToken.IssuerCA, authContext.IssuerCA, "IssuerCA")
require.Equal(t, "oidc", authContext.AuthProviderName, "AuthProvider")
require.Equal(t, testCloudContextName, authContext.Ctx, "Context")
}

func TestLoadExistingConfigWithOIDC(t *testing.T) {
Expand All @@ -326,8 +321,8 @@ func TestLoadExistingConfigWithOIDC(t *testing.T) {
require.Equal(t, authContext.ClientSecret, demoToken.ClientSecret, "ClientSecret")
require.Equal(t, authContext.IssuerURL, demoToken.IssuerURL, "Issuer")
require.Equal(t, authContext.IssuerCA, demoToken.IssuerCA, "IssuerCA")
require.Equal(t, authContext.AuthProviderName, "oidc", "AuthProvider")
require.Equal(t, authContext.Ctx, testCloudContextName, "Context")
require.Equal(t, "oidc", authContext.AuthProviderName, "AuthProvider")
require.Equal(t, testCloudContextName, authContext.Ctx, "Context")
}

func TestUpdateUserExistingConfig(t *testing.T) {
Expand Down Expand Up @@ -420,7 +415,7 @@ func TestManipulateEncodeKubeconfig(t *testing.T) {

clusters, err := GetClusterNames(cfg)
require.NoError(t, err)
require.Equal(t, 1, len(clusters))
require.Len(t, clusters, 1)

err = AddContext(cfg, "myContext", clusters[0], "username")
require.NoError(t, err)
Expand Down Expand Up @@ -469,7 +464,7 @@ func TestKubeconfigFromEnv(t *testing.T) {
defer os.Setenv(RecommendedConfigPathEnvVar, "")

_, filename, isDefault, err := LoadKubeConfig("")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, tmpfile.Name(), filename)
require.False(t, isDefault)
}
Expand All @@ -483,7 +478,7 @@ func TestAuthContextFromEnv(t *testing.T) {
defer os.Setenv(RecommendedConfigPathEnvVar, "")

authCtx, err := GetAuthContext("", testCloudContextName)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, testCloudContextName, authCtx.Ctx)
require.Equal(t, "[email protected]", authCtx.User)
}
Expand All @@ -502,7 +497,7 @@ func TestKubeconfigFromEnvDoesNotExist(t *testing.T) {
defer os.Setenv(RecommendedConfigPathEnvVar, "")

authCtx, filename, isDefault, err := LoadKubeConfig("")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, "/tmp/path/to/kubeconfig", filename)
require.NotNil(t, authCtx)
require.False(t, isDefault)
Expand All @@ -517,7 +512,7 @@ func TestAuthContextFromEnvDoesNotExist(t *testing.T) {
defer os.Setenv(RecommendedConfigPathEnvVar, "")

_, err := CurrentAuthContext("")
require.Nil(t, err)
require.NoError(t, err)
}

func TestKubeconfigFromEnvMultiplePaths(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions auth/kubedefaults_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package auth

import (
"github.com/stretchr/testify/assert"
"os"
"testing"

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

func TestFromEnvMultiplePath(t *testing.T) {
Expand Down Expand Up @@ -31,5 +32,5 @@ func TestFromEnvEmpty(t *testing.T) {
os.Setenv(RecommendedConfigPathEnvVar, "")

s := fromEnv()
assert.Len(t, s, 0)
assert.Empty(t, s)
}
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/metal-stack/metal-lib
go 1.21

require (
connectrpc.com/connect v1.11.1
connectrpc.com/connect v1.12.0
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.2.3
github.com/avast/retry-go/v4 v4.5.0
github.com/coreos/go-oidc/v3 v3.6.0
github.com/coreos/go-oidc/v3 v3.7.0
github.com/emicklei/go-restful-openapi/v2 v2.9.1
github.com/emicklei/go-restful/v3 v3.10.2
github.com/fatih/color v1.15.0
Expand All @@ -19,7 +19,7 @@ require (
github.com/icza/dyno v0.0.0-20230330125955-09f820a8d9c0
github.com/jszwec/csvutil v1.8.0
github.com/mattn/go-isatty v0.0.19
github.com/meilisearch/meilisearch-go v0.25.0
github.com/meilisearch/meilisearch-go v0.26.0
github.com/metal-stack/security v0.6.6
github.com/metal-stack/v v1.0.3
github.com/nsqio/go-nsq v1.1.0
Expand All @@ -31,7 +31,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.23.0
go.uber.org/zap v1.25.0
golang.org/x/oauth2 v0.11.0
golang.org/x/oauth2 v0.13.0
golang.org/x/sync v0.3.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -133,7 +133,7 @@ require (
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down Expand Up @@ -173,19 +173,19 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
go4.org/netipx v0.0.0-20230728180743-ad4cb58a6516 // indirect
golang.org/x/crypto v0.12.0
golang.org/x/crypto v0.14.0
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0
golang.org/x/text v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit ed8caa2

Please sign in to comment.