Skip to content

Commit

Permalink
fix: adds testify back.
Browse files Browse the repository at this point in the history
Signed-off-by: ianhundere <138915+ianhundere@users.noreply.github.com>
  • Loading branch information
ianhundere committed Dec 18, 2024
1 parent ae6c735 commit 7cee823
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 45 deletions.
34 changes: 10 additions & 24 deletions cmd/certificate_maker/certificate_maker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package main
import (
"os"
"path/filepath"
"strings"
"testing"

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

func TestGetConfigValue(t *testing.T) {
Expand Down Expand Up @@ -83,25 +84,19 @@ func TestGetConfigValue(t *testing.T) {
defer os.Unsetenv(tt.envVar)
}
got := getConfigValue(tt.flagValue, tt.envVar)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got)
})
}
}

func TestInitLogger(t *testing.T) {
logger := initLogger()
if logger == nil {
t.Errorf("logger is nil")
}
require.NotNil(t, logger)
}

func TestRunCreate(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "cert-test-*")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

rootTemplate := `{
Expand Down Expand Up @@ -136,13 +131,9 @@ func TestRunCreate(t *testing.T) {
rootTmplPath := filepath.Join(tmpDir, "root-template.json")
leafTmplPath := filepath.Join(tmpDir, "leaf-template.json")
err = os.WriteFile(rootTmplPath, []byte(rootTemplate), 0600)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)
err = os.WriteFile(leafTmplPath, []byte(leafTemplate), 0600)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)

tests := []struct {
name string
Expand Down Expand Up @@ -260,15 +251,10 @@ func TestRunCreate(t *testing.T) {
err := cmd.Execute()

if tt.wantError {
if err == nil {
t.Errorf("expected error, but got nil")
} else if !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("error %q should contain %q", err.Error(), tt.errMsg)
}
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errMsg)
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
require.NoError(t, err)
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/urfave/negroni v1.0.0
go.step.sm/crypto v0.55.0
go.uber.org/zap v1.27.0
Expand Down Expand Up @@ -81,6 +82,7 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down Expand Up @@ -131,6 +133,7 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
13 changes: 6 additions & 7 deletions pkg/certmaker/certmaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.step.sm/crypto/kms/apiv1"
)

Expand Down Expand Up @@ -245,13 +247,10 @@ func TestValidateKMSConfig(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
err := ValidateKMSConfig(tt.config)
if tt.wantError != "" {
if err == nil {
t.Error("Expected error but got none")
} else if !strings.Contains(err.Error(), tt.wantError) {
t.Errorf("Expected error containing %q, got %q", tt.wantError, err.Error())
}
} else if err != nil {
t.Errorf("Unexpected error: %v", err)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantError)
} else {
require.NoError(t, err)
}
})
}
Expand Down
21 changes: 7 additions & 14 deletions pkg/certmaker/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"path/filepath"
"strings"
"testing"

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

func TestParseTemplate(t *testing.T) {
Expand Down Expand Up @@ -108,24 +111,14 @@ func TestParseTemplate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tmpFile := filepath.Join(tmpDir, "template.json")
err := os.WriteFile(tmpFile, []byte(tt.content), 0600)
if err != nil {
t.Fatalf("Failed to write template file: %v", err)
}
require.NoError(t, err)

cert, err := ParseTemplate(tmpFile, tt.parent)
if tt.wantError != "" {
if err == nil {
t.Error("Expected error but got none")
} else if !strings.Contains(err.Error(), tt.wantError) {
t.Errorf("Expected error containing %q, got %q", tt.wantError, err.Error())
}
if cert != nil {
t.Error("Expected nil certificate when error occurs")
}
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantError)
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
require.NoError(t, err)
if cert == nil {
t.Error("Expected non-nil certificate")
}
Expand Down

0 comments on commit 7cee823

Please sign in to comment.