Skip to content

Commit

Permalink
Wrote and applied helper function for tests
Browse files Browse the repository at this point in the history
replaced with new function

removed log/slog

readded log/slog when needed

fixed last testwriter problems
  • Loading branch information
miampf committed Jan 22, 2024
1 parent 2ce5533 commit aaa9713
Show file tree
Hide file tree
Showing 55 changed files with 133 additions and 178 deletions.
6 changes: 3 additions & 3 deletions bootstrapper/internal/initserver/initserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"context"
"errors"
"io"
"log/slog"
"net"
"strings"
"sync"
"testing"
"time"
"log/slog"

"github.com/edgelesssys/constellation/v2/bootstrapper/initproto"
"github.com/edgelesssys/constellation/v2/internal/atls"
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestNew(t *testing.T) {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)

server, err := New(context.TODO(), newFakeLock(), &stubClusterInitializer{}, atls.NewFakeIssuer(variant.Dummy{}), fh, &tc.metadata, slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)))
server, err := New(context.TODO(), newFakeLock(), &stubClusterInitializer{}, atls.NewFakeIssuer(variant.Dummy{}), fh, &tc.metadata, logger.NewTest(t))
if tc.wantErr {
assert.Error(err)
return
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestInit(t *testing.T) {
initializer: tc.initializer,
disk: tc.disk,
fileHandler: tc.fileHandler,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
grpcServer: serveStopper,
cleaner: &fakeCleaner{serveStopper: serveStopper},
initSecretHash: tc.initSecretHash,
Expand Down
6 changes: 3 additions & 3 deletions bootstrapper/internal/joinclient/joinclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ package joinclient
import (
"context"
"errors"
"log/slog"
"net"
"strconv"
"sync"
"testing"
"time"
"log/slog"

"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
"github.com/edgelesssys/constellation/v2/internal/constants"
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestClient(t *testing.T) {
fileHandler: fileHandler,
metadataAPI: metadataAPI,
clock: clock,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
}

serverCreds := atlscredentials.New(nil, nil)
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestClientConcurrentStartStop(t *testing.T) {
fileHandler: file.NewHandler(afero.NewMemMapFs()),
metadataAPI: &stubRepeaterMetadataAPI{},
clock: testclock.NewFakeClock(time.Now()),
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
}

wg := sync.WaitGroup{}
Expand Down
6 changes: 3 additions & 3 deletions bootstrapper/internal/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package kubernetes
import (
"context"
"errors"
"log/slog"
"net"
"strconv"
"testing"
"log/slog"

"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/k8sapi"
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/kubewaiter"
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestInitCluster(t *testing.T) {

_, err := kube.InitCluster(
context.Background(), string(tc.k8sVersion), "kubernetes",
false, nil, nil, "", slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
false, nil, nil, "", logger.NewTest(t),
)

if tc.wantErr {
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestJoinCluster(t *testing.T) {
getIPAddr: func() (string, error) { return privateIP, nil },
}

err := kube.JoinCluster(context.Background(), joinCommand, tc.role, tc.k8sComponents, slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)))
err := kube.JoinCluster(context.Background(), joinCommand, tc.role, tc.k8sComponents, logger.NewTest(t))
if tc.wantErr {
assert.Error(err)
return
Expand Down
5 changes: 2 additions & 3 deletions cli/internal/cmd/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -197,7 +196,7 @@ func TestBackupHelmCharts(t *testing.T) {
applier: &stubConstellApplier{
stubKubernetesUpgrader: tc.backupClient,
},
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
}

err := a.backupHelmCharts(context.Background(), tc.helmApplier, tc.includesUpgrades, "")
Expand Down Expand Up @@ -443,7 +442,7 @@ func TestValidateInputs(t *testing.T) {
cmd.SetIn(bytes.NewBufferString(tc.stdin))

a := applyCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
fileHandler: fileHandler,
flags: tc.flags,
}
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/configfetchmeasurements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd

import (
"context"
"log/slog"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -181,7 +180,7 @@ func TestConfigFetchMeasurements(t *testing.T) {
err := fileHandler.WriteYAML(constants.ConfigFilename, gcpConfig, file.OptMkdirAll)
require.NoError(err)
fetcher := stubVerifyFetcher{err: tc.err}
cfm := &configFetchMeasurementsCmd{canFetchMeasurements: true, log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), verifyFetcher: fetcher}
cfm := &configFetchMeasurementsCmd{canFetchMeasurements: true, log: logger.NewTest(t), verifyFetcher: fetcher}
cfm.flags.insecure = tc.insecureFlag
cfm.flags.force = true

Expand Down
7 changes: 3 additions & 4 deletions cli/internal/cmd/configgenerate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd

import (
"fmt"
"log/slog"
"strings"
"testing"

Expand Down Expand Up @@ -93,7 +92,7 @@ func TestConfigGenerateDefault(t *testing.T) {
cmd := newConfigGenerateCmd()

cg := &configGenerateCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
flags: generateFlags{
attestationVariant: variant.Dummy{},
k8sVersion: versions.Default,
Expand Down Expand Up @@ -145,7 +144,7 @@ func TestConfigGenerateDefaultProviderSpecific(t *testing.T) {
wantConf.RemoveProviderAndAttestationExcept(tc.provider)

cg := &configGenerateCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
flags: generateFlags{
attestationVariant: variant.Dummy{},
k8sVersion: versions.Default,
Expand Down Expand Up @@ -178,7 +177,7 @@ func TestConfigGenerateDefaultExists(t *testing.T) {
cmd := newConfigGenerateCmd()

cg := &configGenerateCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
flags: generateFlags{attestationVariant: variant.Dummy{}},
}
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, ""))
Expand Down
5 changes: 2 additions & 3 deletions cli/internal/cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package cmd
import (
"bytes"
"context"
"log/slog"
"testing"

"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
Expand Down Expand Up @@ -228,7 +227,7 @@ func TestCreate(t *testing.T) {
skipPhases: newPhases(skipInitPhase, skipAttestationConfigPhase, skipCertSANsPhase, skipHelmPhase, skipImagePhase, skipK8sPhase),
},

log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},

newInfraApplier: func(_ context.Context) (cloudApplier, func(), error) {
Expand Down Expand Up @@ -296,7 +295,7 @@ func TestCheckDirClean(t *testing.T) {
for _, f := range tc.existingFiles {
require.NoError(fh.Write(f, []byte{1, 2, 3}, file.OptNone))
}
a := &applyCmd{log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), fileHandler: fh}
a := &applyCmd{log: logger.NewTest(t), fileHandler: fh}
err := a.checkInitFilesClean()

if tc.wantErr {
Expand Down
7 changes: 3 additions & 4 deletions cli/internal/cmd/iamcreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd
import (
"bytes"
"encoding/base64"
"log/slog"
"strings"
"testing"

Expand Down Expand Up @@ -210,7 +209,7 @@ func TestIAMCreateAWS(t *testing.T) {

iamCreator := &iamCreator{
cmd: cmd,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},
creator: tc.creator,
fileHandler: fileHandler,
Expand Down Expand Up @@ -386,7 +385,7 @@ func TestIAMCreateAzure(t *testing.T) {

iamCreator := &iamCreator{
cmd: cmd,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},
creator: tc.creator,
fileHandler: fileHandler,
Expand Down Expand Up @@ -577,7 +576,7 @@ func TestIAMCreateGCP(t *testing.T) {

iamCreator := &iamCreator{
cmd: cmd,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},
creator: tc.creator,
fileHandler: fileHandler,
Expand Down
5 changes: 2 additions & 3 deletions cli/internal/cmd/iamdestroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"errors"
"log/slog"
"testing"

"github.com/edgelesssys/constellation/v2/internal/cloud/gcpshared"
Expand Down Expand Up @@ -107,7 +106,7 @@ func TestIAMDestroy(t *testing.T) {
cmd.SetErr(&bytes.Buffer{})
cmd.SetIn(bytes.NewBufferString(tc.stdin))

c := &destroyCmd{log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), flags: iamDestroyFlags{
c := &destroyCmd{log: logger.NewTest(t), flags: iamDestroyFlags{
yes: tc.yesFlag,
}}

Expand Down Expand Up @@ -197,7 +196,7 @@ func TestDeleteGCPServiceAccountKeyFile(t *testing.T) {
cmd.SetErr(&bytes.Buffer{})
cmd.SetIn(bytes.NewBufferString(tc.stdin))

c := &destroyCmd{log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil))}
c := &destroyCmd{log: logger.NewTest(t)}

proceed, err := c.deleteGCPServiceAccountKeyFile(cmd, tc.destroyer, tc.fsHandler)
if tc.wantErr {
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/iamupgradeapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd
import (
"context"
"io"
"log/slog"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -132,7 +131,7 @@ func TestIamUpgradeApply(t *testing.T) {

iamUpgradeApplyCmd := &iamUpgradeApplyCmd{
fileHandler: tc.fh,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
configFetcher: tc.configFetcher,
flags: iamUpgradeApplyFlags{
yes: tc.yesFlag,
Expand Down
11 changes: 5 additions & 6 deletions cli/internal/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"context"
"fmt"
"log/slog"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -229,7 +228,7 @@ func TestInitialize(t *testing.T) {
rootFlags: rootFlags{force: true},
skipPhases: newPhases(skipInfrastructurePhase),
},
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},
merger: &stubMerger{},
applier: &stubConstellApplier{
Expand Down Expand Up @@ -369,8 +368,8 @@ func TestWriteOutput(t *testing.T) {
fileHandler: fileHandler,
spinner: &nopSpinner{},
merger: &stubMerger{},
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
applier: constellation.NewApplier(slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), &nopSpinner{}, constellation.ApplyContextCLI, nil),
log: logger.NewTest(t),
applier: constellation.NewApplier(logger.NewTest(t), &nopSpinner{}, constellation.ApplyContextCLI, nil),
}
err = i.writeInitOutput(stateFile, initOutput, false, &out, measurementSalt)
require.NoError(err)
Expand Down Expand Up @@ -461,8 +460,8 @@ func TestGenerateMasterSecret(t *testing.T) {
var out bytes.Buffer
i := &applyCmd{
fileHandler: fileHandler,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
applier: constellation.NewApplier(slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), &nopSpinner{}, constellation.ApplyContextCLI, nil),
log: logger.NewTest(t),
applier: constellation.NewApplier(logger.NewTest(t), &nopSpinner{}, constellation.ApplyContextCLI, nil),
}
secret, err := i.generateAndPersistMasterSecret(&out)

Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/maapatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd

import (
"context"
"log/slog"
"testing"

"github.com/edgelesssys/constellation/v2/internal/logger"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestMAAPatch(t *testing.T) {
t.Run(name, func(t *testing.T) {
require := require.New(t)

c := &maaPatchCmd{log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)), patcher: tc.patcher}
c := &maaPatchCmd{log: logger.NewTest(t), patcher: tc.patcher}
err := c.patchMAA(&cobra.Command{}, tc.attestationURL)
if tc.wantErr {
require.Error(err)
Expand Down
5 changes: 2 additions & 3 deletions cli/internal/cmd/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"context"
"errors"
"log/slog"
"net"
"strconv"
"testing"
Expand Down Expand Up @@ -165,7 +164,7 @@ func TestRecover(t *testing.T) {

newDialer := func(atls.Validator) *dialer.Dialer { return nil }
r := &recoverCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
configFetcher: stubAttestationFetcher{},
flags: recoverFlags{
rootFlags: rootFlags{force: true},
Expand Down Expand Up @@ -219,7 +218,7 @@ func TestDoRecovery(t *testing.T) {
go recoverServer.Serve(listener)
defer recoverServer.GracefulStop()

r := &recoverCmd{log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil))}
r := &recoverCmd{log: logger.NewTest(t)}
recoverDoer := &recoverDoer{
dialer: dialer.New(nil, nil, netDialer),
endpoint: addr,
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/terminate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package cmd
import (
"bytes"
"errors"
"log/slog"
"testing"

"github.com/edgelesssys/constellation/v2/internal/constants"
Expand Down Expand Up @@ -140,7 +139,7 @@ func TestTerminate(t *testing.T) {
fileHandler := file.NewHandler(tc.setupFs(require, tc.stateFile))

tCmd := &terminateCmd{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
fileHandler: fileHandler,
flags: terminateFlags{
yes: tc.yesFlag,
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/cmd/upgradeapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package cmd
import (
"bytes"
"context"
"log/slog"
"testing"

"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
Expand Down Expand Up @@ -252,7 +251,7 @@ func TestUpgradeApply(t *testing.T) {
upgrader := &applyCmd{
fileHandler: fh,
flags: tc.flags,
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
log: logger.NewTest(t),
spinner: &nopSpinner{},
merger: &stubMerger{},
newInfraApplier: func(ctx context.Context) (cloudApplier, func(), error) {
Expand Down
Loading

0 comments on commit aaa9713

Please sign in to comment.